server1和server4组成高可用和负载均衡集群,server2和server3作为后端真实服务器提供httpd和nginx服务
1、关于haproxy+nginx+httpd在上个博客已有介绍,这个实验是在上个实验完成的基础上进行的。
2、添加server4和server1的连接,互相认证。
3、keepalived部分
[root@server1 keepalived]# cat install.sls
kp-install:
pkg.installed:
- pkgs:
- openssl-devel
- gcc
- mailx #所部必须的依赖安装
file.managed:
- name: /mnt/keepalived-2.0.6.tar.gz
- source: salt://keepalived/files/keepalived-2.0.6.tar.gz
cmd.run:
- name: cd /mnt && tar zxf keepalived-2.0.6.tar.gz && cd keepalived-2.0.6 && ./configure --prefix=/usr/local/keepalived --with-init=SYSV &> /dev/null && make &> /dev/null && make install &> /dev/null
- creates: /usr/local/keepalived #编译安装
/etc/keepalived:
file.directory:
- mode: 755
/etc/sysconfig/keepalived:
file.symlink:
- target: /usr/local/keepalived/sbin/keepalived
/sbin/keepalived:
file.symlink:
- target: /usr/local/keepalived/sbin/keepalived
[root@server1 keepalived]# cat service.sls
include:
- keepalived.install
/etc/keepalived/keepalived.conf:
file.managed:
- source: salt://keepalived/files/keepalived.conf
- template: jinja
- context:
STATE: {{ pillar['state'] }}
vrid: {{ pillar['vrid'] }}
priority: {{ pillar['priority'] }}
kp-service:
file.managed:
- name: /etc/init.d/keepalived
- source: salt://keepalived/files/keepalived
- mode: 755
service.running:
- name: keepalived
- reload: True
- watch:
- file: /etc/keepalived/keepalived.conf
4、jinjia部分
[root@server1 pillar]# cd keepalived/
[root@server1 keepalived]# cat install.sls
{% if grains['fqdn'] == 'server1' %}
state: MASTER
vrid: 24
priority: 100
{% elif grains['fqdn'] == 'server4' %}
state: BACKUP
vrid: 24
priority: 50
{% endif %}
[root@server1 web]# pwd
/srv/pillar/web
[root@server1 web]# cat install.sls
{% if grains['fqdn'] == 'server2' %}
webserver: apache
port: 80
bind: 172.25.4.2
{% elif grains['fqdn'] == 'server3' %}
webserver: nginx
port: 80
bind: 172.25.4.3
{% endif %}
[root@server1 pillar]# pwd
/srv/pillar
[root@server1 pillar]# cat
keepalived/ top.sls web/
[root@server1 pillar]# cat top.sls
base:
'server2':
- web.install
'server3':
- web.install
'server1':
- keepalived.install
'server4':
- keepalived.install
5、高级推送
[root@server1 keepalived]# salt '*' state.highstate
显示推送成功!
5、测试
实验成功,负载均衡和高可用都已经实现!
网友评论