配置结构
- 1、install.sls
[root@server1 keepalived]# cat install.sls
install-keepalived:
pkg.installed:
- pkgs:
- keepalived
file.managed:
- name: /etc/keepalived/keepalived.conf
- source: salt://keepalived/files/keepalived.conf
- template: jinja
- context:
STATE: {{ pillar['state'] }}
VRID: {{ pillar['vrid'] }}
PRIORITY: {{ pillar['priority'] }}
service.running:
- name: keepalived
- watch:
- file: install-keepalived
- 2、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 }}
priority {{ PRIORITY }}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.14.100
}
}
- 3、top.sls
[root@server1 salt]# cat top.sls
base:
'roles:apache':
- match: grain
- apache.install
- keepalived.install
'roles:nginx':
- match: grain
- nginx.service
- keepalived.install
- 4、pillar
[root@server1 salt]# cat ../pillar/web/vars.sls
{% if grains['fqdn'] == 'server2' %}
ip: 172.25.14.2
state: MASTER
vrid: 43
priority: 100
{% elif grains['fqdn'] == 'server3' %}
state: BACKUP
vrid: 43
priority: 50
{% endif %}
[root@server1 salt]# cat ../pillar/top.sls
base:
'*':
- web.vars
- 5、推送部署
[root@server1 salt]# salt '*' state.highstate
VIP在server2
server2部署的为httpd,因此,浏览器访问VIP,应该是apache默认页
将keepalived停止后,VIP飘移在server3上,浏览器访问应该为nginx的默认页
高可用已经实现,如果需要实现负载均衡可以使用haproxy或者nginx来通过saltstack部署