本篇博客承接saltstack安装部署和saltstack—— grains,pillar,jinja模块的使用
[root@server1 salt]# ls
_grains httpd nginx top.sls
[root@server1 salt]# mkdir keepalived
[root@server1 salt]# cd keepalived/
[root@server1 keepalived]# mkdir files
创建目录。
[root@server1 keepalived]# vim 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.26.100 ##vip
}
}
编写keepalived的主配置文件。
[root@server1 keepalived]# vim /srv/pillar/vars.sls
{% if grains['fqdn'] == 'server3' %}
state: BACKUP
vrid: 26
priority: 50 ##优先值
ip: 172.25.26.3
{% elif grains['fqdn'] == 'server2' %}
state: MASTER
vrid: 26
priority: 100
ip: 172.25.26.2
{% endif %}
编写pillar目录下的文件,设置主备机以及vip的优先值,我在server2设置的优先值为100,所以vip默认在server2.
[root@server1 keepalived]# vim install.sls
kp-install:
pkg.installed:
- pkgs:
- keepalived ##安装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'] }} ##pillar目录中设置的值。
service.running: ##开启服务
- name: keepalived
- enable: true
- reload: true
- watch:
- file: kp-install
编写自动部署的sls文件。
[root@server1 keepalived]# salt server2 state.sls keepalived.install
在server2中查看到vip:172.25.26.100.
[root@server1 keepalived]# vim /srv/salt/top.sls
base:
'server2':
- keepalived.install
'server3':
- keepalived.install
因为要在两台主机上部署,所以在salt目录写一个top.sls文件。
[root@server1 keepalived]# salt '*' state.highstate
[root@server2 keepalived]# systemctl stop keepalived
将server2的keepalived服务停止。
此时vip已经飘到server3了。
[root@server2 keepalived]# systemctl start keepalived