本博文在Saltstack推送httpd服务 与 nginx服务基础上:
环境
server1:salt-master | 172.25.60.1 |
---|---|
server2:salt-minion httpd keepalived | 172.25.60.2 |
server3:salt-minion nginx keepalived | 172.25.60.3 |
实验准备:
恢复httpd端口
vim /srv/salt/apache/files/httpd.conf
Listen 80
1.在base目录下面建立keepalived目录,并建立install.sls文件
mkdir /srv/salt/keepalived
cd keepalived/
cp ../apache/install.sls .
vim install.sls
install-keepalived:
pkg.installed:
- pkgs:
- keepalived
file.managed:
- name: /etc/keepalived/keepalived.conf
- source: salt://keepalived/files/keepalived.conf
- template: jinja ##使用jinja模板
- context:
STATE: {{ pillar['state'] }}
VRID: {{ pillar['vrid'] }}
PRIORITY: {{ pillar['priority'] }}
service.running:
- name: keepalived
- watch:
- file: install-keepalived
2.创建files目录,拷贝keepalived.conf模板文件到files目录
[root@server1 keepalived]# mkdir files
[root@server1 keepalived]# yum install -y keepalived
[root@server1 keepalived]# cd files
[root@server1 files]# cp /etc/keepalived/keepalived.conf .
[root@server1 files]# vim keepalived.conf
[root@server1 files]# cat keepalived.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state {{ STATE }}
interface eth0
virtual_router_id {{ VRID }}
priority {{ PRIORITY }}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.60.100
}
}
3.在pillar/web目录下,设置变量的取值
[root@server1 files]# cd /srv/pillar/
[root@server1 pillar]# vim web/vars.sls
[root@server1 pillar]# cat web/vars.sls
{% if grains['fqdn'] == 'server2' %}
state: MASTER
vrid: 60
priority: 100
{% elif grains['fqdn'] == 'server3' %}
state: BACKUP
vrid: 60
priority: 50
{% endif %}
4.设置主推送文件top.sls
[root@server1 pillar]# vim /srv/salt/top.sls
[root@server1 pillar]# cat /srv/salt/top.sls
base:
'roles:apache':
- match: grain
- apache.install
- keepalived.install
'roles:nginx':
- match: grain
- nginx.service
- keepalived.install
向server2和server3推送keepalived
salt ‘*’ state.highstate
查看高可用集群是否搭建完毕
server2为master,在server2中查看虚拟ip
server2中编写httpd默认发布文件
echo hello westos > /var/www/html/index.html
浏览器中访问http://172.25.60.100,默认访问的页面是server2中的内容
systemctl stop keepalived
将server2中的keepalived关闭,我们会发现虚拟ip漂移到server3中,且访问172.25.60.100时,显示的页面内容是server3中nginx的欢迎页面
再次开启server2中的keepalived服务,由于优先级的原因,虚拟ip会回到server2中