在server9上
# 建立目录
[root@server9 ~]# cd /srv/salt/
[root@server9 salt]# mkdir keepalived
[root@server9 salt]# cd keepalived/
[root@server9 keepalived]# mkdir files
# 自行下载安装包到此目录下
[root@server9 keepalived]# cd files/
[root@server9 files]# ls
keepalived-2.0.6.tar.gz
[root@server9 keepalived]# vim install.sls
include:
- pkgs.make
kp-install:
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
# 推送给server7,无报错则成功
[root@server9 keepalived]# salt server7 state.sls keepalived.install
server7
把配置文件传到server9的/srv/salt/keepalived/files目录下:
[root@server7 ~]# cd /usr/local/keepalived/etc/rc.d/init.d/
[root@server7 init.d]# scp keepalived [email protected]:/srv/salt/keepalived/files
[root@server7 init.d]# cd /usr/local/keepalived/etc/keepalived/
[root@server7 keepalived]# scp keepalived.conf [email protected]:/srv/salt/keepali
[root@server9 keepalived]# vim install.sls
include:
- pkgs.make
kp-install:
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/etc/sysconfig/keepalived
/sbin/keepalived:
file.symlink:
- target: /usr/local/keepalived/sbin/keepalived
[root@server9 keepalived]# salt server7 state.sls keepalived.install
[root@server9 keepalived]# cd /srv/pillar/
[root@server9 pillar]# mkdir keepalived
[root@server9 pillar]# cd keepalived/
[root@server9 keepalived]# cp ../web/install.sls .
[root@server9 keepalived]# vim install.sls
{% if grains['fqdn'] == 'server9' %}
state: MASTER # 状态
vrid: 25 # 虚拟id
prority: 100 # 优先级
{% elif grains['fqdn'] == 'server7' %}
state: BACKUP
vrid: 25
prority: 50
{% endif %}
[root@server9 keepalived]# cd ..
[root@server9 pillar]# vim top.sls
base:
'*':
- web.install
- keepalived.install #全局声明
[root@server9 ~]# cd /srv/salt/keepalived/files/
[root@server9 files]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state {{ STATE }}
interface eth0
virtual_router_id {{ VRID }}
priority {{ PRORITY }}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.25.100 #虚拟IP,在keepalived的master上配置
}
}
[root@server9 keepalived]# vim 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'] }}
PRORITY: {{ pillar['prority'] }}
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
[root@server9 keepalived]# salt server7 state.sls keepalived.service
[root@server9 keepalived]# cd ..
[root@server9 salt]# vim top.sls
base:
'server7':
- haproxy.install
- keepalived.service
'server8':
- nginx.service
'server9':
- haproxy.install
- keepalived.service
'server10':
- httpd.install
[root@server9 salt]# salt '*' state.highstate
server10(apache)
[root@server10 ~]# vim /var/www/html/index.html
server10
[root@server10 ~]# /etc/init.d/httpd restart
在浏览器访问172.25.25.100
刷新
关闭server9的keepalived
[root@server9 web]# /etc/init.d/keepalived stop
Stopping keepalived: [ OK ]