1.创建推送目录并修改keepalived推送配置文件
[root@server1 salt]# mkdir keepalived
[root@server1 salt]# cd keepalived
[root@server1 keepalived]# mkdir files
[root@server1 keepalived]# cp /etc/keepalived/keepalived.conf ##添加配置文件
[root@server1 keepalived]# vim files/keepalived.conf
[root@server1 keepalived]# cat files/keepalived.conf
! Configuration File for keepalived
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 }}##虚拟id
priority {{ PRIORITY }}##优先级高的为master
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.32.100
}
}
2.编辑推送文件
[root@server1 keepalived]# vim install.sls ##编辑推送文件
[root@server1 keepalived]# cat install.sls
keepalived-install:
pkg.installed:
- pkgs:
- keepalived
file.managed:
- name: /etc/keepalived/keepalived.conf
- source: salt://keepalived/files/keepalived.conf
- user: root
- group: root
- mode: 644
- template: jinja
- context:
STATE: {{ pillar['state'] }}##STATE选择变量为state
VRID: {{ pillar['vrid'] }}##虚拟ID选择变量为vrid
PRIORITY: {{ pillar['priority'] }}##优先级选择变量为priority
service.running:
- name: keepalived
- enable: true
- reload: true
- watch:
- file: keepalived-install
3.编辑相应的pillar文件(变量文件)
[root@server1 keepalived]# vim /srv/pillar/web/vars.sls
[root@server1 keepalived]# cat /srv/pillar/web/vars.sls
{% if grains['fqdn'] == 'server2' %}
webserver: httpd
state: MASTER
ip: 172.25.32.2
vrid: 32
priority: 100
port: 80
{% elif grains['fqdn'] == 'server3' %}
webserver: nginx
state: BACKUP
vrid: 32
priority: 99
ip: 172.25.32.3
port: 80
{% endif %}
4.编辑顶层推送文件
[root@server1 keepalived]# cd ..
[root@server1 salt]# vim top.sls
[root@server1 salt]# cat top.sls
base:
'server2':
- apache.install
- keepalived.install
'server3':
- nginx.service
- keepalived.install
5.清除httpd推送文件与配置文件中的host选项(不然只监听本机的默认发布文件)
[root@server1 salt]# vim apache/install.sls
[root@server1 salt]# cat apache/install.sls
httpd:
pkg.installed:
- pkgs:
- httpd
- php
- httpd-tools
service.running:
- name: httpd
- enable: true
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/files/httpd.conf
- user: root
- group: root
- mode: 644
- template: jinja
- context:
port: {{ pillar['port'] }}
[root@server1 salt]# vim apache/files/httpd.conf
- 推送
[root@server1 salt]# salt ‘*’ state.highstate
- 我们设定server2的优先级较高,设定的虚拟ip为172.25.32.100,首先在server2上查看vip是否存在并进行测试
查看vip(可以看到server2为vip)
在网页访问
- 关闭server2的keepalived服务,vip漂移到server3
[root@server2 ~]# systemctl stop keepalived
在网页进行访问
- 开启server1的keepalived服务再次测试
网页测试