1. 程式人生 > >SaltStack遠程配置keepalived

SaltStack遠程配置keepalived

SaltStack

############################################################
*
* SaltStack遠程配置keepalived

############################################################

#準備工作:
[root@M01 ~]# cd /usr/local/src/
[root@M01 src]# wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
[root@M01 src]# tar xf keepalived-1.2.19.tar.gz

[root@M01 src]# cd keepalived-1.2.19
[root@M01 keepalived-1.2.19]# ./configure --prefix=/usr/local/keepalived --disable-fwmark
[root@M01 keepalived-1.2.19]# make && make install

[root@M01 init.d]# mkdir -p /srv/salt/keepalived/files

[root@M01 src]# cp /usr/local/src/keepalived-1.2.19.tar.gz /srv/salt/keepalived/files/

[root@M01 init.d]# cp /usr/local/src/keepalived-1.2.19/keepalived/etc/init.d/keepalived.init /srv/salt/keepalived/files/
[root@M01 init.d]# cp /usr/local/src/keepalived-1.2.19/keepalived/etc/keepalived/keepalived.conf /srv/salt/keepalived/files/
[root@M01 sysconfig]# cp /usr/local/keepalived/etc/sysconfig/keepalived /srv/salt/keepalived/files/keepalived.sysconfig

#修改keepalived.init啟動的配置文件
[root@M01 ~]# sed -i s#daemon\ keepalived#daemon\ /usr/local/keepalived/sbin/keepalived#g /srv/salt/keepalived/files/keepalived.init
或vim修改
cat /srv/salt/keepalived/files/keepalived.init
原:daemon keepalived
修改:daemon /usr/local/keepalived/sbin/keepalived

#查看下keepalived的結構
[root@M01 keepalived]# tree /srv/salt/keepalived/
/srv/salt/keepalived/
├── files
│?? ├── keepalived-1.2.19.tar.gz
│?? ├── keepalived.conf
│?? ├── keepalived.init
│?? └── keepalived.sysconfig
└── keepalived-install.sls

#keepalived-install配置
[root@M01 keepalived]# cat /srv/salt/keepalived/keepalived-install.sls
pkg-init:
pkg.installed:

  • names:
    • gcc
    • gcc-c++
    • glibc
    • make
    • autoconf
    • openssl
    • openssl-devel

keepalived-install:
file.managed:

  • name: /usr/local/src/keepalived-1.2.19.tar.gz
  • source: salt://keepalived/files/keepalived-1.2.19.tar.gz
  • user: root
  • group: root
  • mode: 755
    cmd.run:
  • name: cd /usr/local/src/ && tar xf keepalived-1.2.19.tar.gz && cd keepalived-1.2.19 && ./configure --prefix=/usr/local/keepalived --disable-fwmark && make && make install
  • unless: test -d /usr/local/keepalived
  • require:
    • pkg: pkg-init
    • file: keepalived-install

keepalived-init:
file.managed:

  • name: /etc/init.d/keepalived
  • source: salt://keepalived/files/keepalived.init
  • user: root
  • group: root
  • mode: 755
    cmd.run:
  • name: chkconfig --add keepalived
  • unless: chkconfig --list |grep keepalived
  • require:
    • file: keepalived-init

/etc/sysconfig/keepalived:
file.managed:

  • source: salt://keepalived/files/keepalived.sysconfig
  • user: root
  • group: root
  • mode: 644

/etc/keepalived:
file.directory:

  • user: root
  • group: root
  • mode: 755

[root@M01]# salt ‘WEB0?‘ state.sls keepalived.keepalived-install

[root@M01 files]# cat /srv/salt/top.sls
base:
‘*‘:

  • apache.apache-install
  • dns.dns-install
  • hosts.hosts-install
  • history.history-install
  • sysctl.sysctl-install
  • haproxy.haproxy-install
  • cluster.haproxy-outside
  • keepalived.keepalived-install

#測試執行,沒問題才更新到客戶端
[root@M01 base]# salt ‘WEB0?‘ state.highstate test=True

#keepalived.conf
[root@M01 files]# tree /srv/salt/cluster/
/srv/salt/cluster/
├── files
│?? └── haproxy-outside-keepalived.conf
├── haproxy-outside-keepalived.sls

#修改keepalived.conf的配置文件
[root@M01 files]# cat /srv/salt/cluster/files/haproxy-outside-keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {[email protected]
br/>[email protected]
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ROUTEID
}
vrrp_instance haproxy_ha {
state STATEID
interface eth0
virtual_router_id 36
priority PRIORITYID
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.44.91
}
}

#添加群集的高可用
[root@M01 cluster]# cat /srv/salt/cluster/haproxy-outside-keepalived.sls
include:

  • keepalived.keepalived-install

keepalived-server:
file.managed:

  • name: /etc/keepalived/keepalived.conf
  • source: salt://cluster/files/haproxy-outside-keepalived.conf
  • mode: 644
  • user: root
  • group: root
  • template: jinja
    {% if grains[‘fqdn‘] == ‘WEB01‘ %}
  • ROUTEID: haproxy_ha
  • STATEID: MASTER
  • PRIORITYID: 150
    {% elif grains[‘fqdn‘] == ‘WEB02‘ %}
  • ROUTEID: haproxy_ha
  • STATEID: BACKUP
  • PRIORITYID: 100
    {% endif %}
    service.running:
  • name: keepalived
  • enable: True
  • watch:
    • file: keepalived-server

[root@M01 files]# salt ‘WEB0?‘ state.sls cluster.haproxy-outside-keepalived

[root@M01 files]# cat /srv/salt/top.sls
base:
‘*‘:

  • apache.apache-install
  • dns.dns-install
  • hosts.hosts-install
  • history.history-install
  • sysctl.sysctl-install
  • haproxy.haproxy-install
  • cluster.haproxy-outside
  • keepalived.keepalived-install
  • cluster.haproxy-outside-keepalived

#測試執行,沒問題才更新到客戶端
[root@M01 base]# salt ‘WEB0?‘ state.highstate test=True

#測試一下
#WEB01上有虛擬的192.168.44.91/32 ,說明是MASTER
[root@WEB01 ~]# ip add li
inet 192.168.44.7/24 brd 192.168.44.255 scope global eth0
inet 192.168.44.91/32 scope global eth0
#WEB02沒有虛擬的192.168.44.91/32 ,說明是BACKUP
[root@WEB02 ~]# ip add li
inet 192.168.44.8/24 brd 192.168.44.255 scope global eth0

#現在把MASTER這臺關掉
[root@WEB01 ~]# /etc/init.d/keepalived stop
[root@WEB01 ~]# ip add li
inet 192.168.44.7/24 brd 192.168.44.255 scope global eth0
[root@WEB02 ~]# ip add li
inet 192.168.44.8/24 brd 192.168.44.255 scope global eth0
inet 192.168.44.91/32 scope global eth0

#現在把MASTER這臺關開起來
[root@WEB01 ~]# /etc/init.d/keepalived start
[root@WEB01 ~]# ip add li
inet 192.168.44.7/24 brd 192.168.44.255 scope global eth0
inet 192.168.44.91/32 scope global eth0
[root@WEB02 ~]# ip add li
inet 192.168.44.8/24 brd 192.168.44.255 scope global eth0

#測試虛擬ip,出現是WEB01
http://192.168.44.91:8080/
內容:Welcome-bbs.yehaixiao.com (WEB01-192.168.44.7)
備註:當WEB01掛掉,虛擬IP會切換到WEB02
當WEB02掛掉,虛擬IP會切換到WEB01,這是因為用了salt方法

[root@M01 prod]# cat /srv/salt/prod/cluster/files/haproxy-outside.cfg
#balance source # 根據請求源IP,建議使用
balance roundrobin # 輪詢,軟負載均衡基本都具備這種算法

#修改haproxy-outside.cfg,需要要下發配置文件到客戶端
[root@M01 prod]# salt ‘WEB0?‘ state.highstate

#修改配置為輪巡方法,這樣只要你刷新1次就切換一次
http://192.168.44.91
#接F5會在下列地址中切換
Welcome-bbs.yehaixiao.com (WEB01-192.168.44.7)
Welcome-bbs.yehaixiao.cn (WEB02-192.168.44.8)

SaltStack遠程配置keepalived