Saltstack 實現keepalived高可用
阿新 • • 發佈:2018-11-11
實驗環境:在SaltStack部署完畢的前提下進行實驗:
系統: redhat6
server1 172.25.45.1 salt-master keepalived
server2 172.25.45.2 salt-minion apache
server2 172.25.45.3 salt-minion nginx
server4 172.25.45.4 salt-minion keepalived
server4的yum源配置負載均衡包:
[[email protected] ~]# vim /etc/yum.repos.d/rhel-source.repo [ rhel-source] name=Red Hat Enterprise Linux $releasever - $basearch - Source baseurl=http://172.25.45.250/rhel6.5 enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release [saltsack] name=saltsack baseurl=http://172.25.45.250/rhel6 enabled=1 gpgcheck=0 [LoadBalancer] name=LoadBalancer baseurl=http://172.25.45.250/rhel6.5/LoadBalancer gpgcheck=0
一、安裝keepalived(原始碼編譯)
[[email protected] ~]# cd /srv/salt/
[[email protected] salt]# mkdir keepalived
[[email protected] salt]# cd keepalived/
[[email protected] keepalived]# mkdir files
[[email protected] keepalived]# cd files/
[[email protected] files]# mv /root/keepalived-2.0.6.tar.gz .
[ [email protected] files]# ls
keepalived-2.0.6.tar.gz
[[email protected] files]# cd ..
[[email protected] keepalived]# vim install.sls
kp-install:
pkg.installed:
- pkgs:
- gcc:
- pcre-devel:
- openssl-devel
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
[[email protected] keepalived]# salt server4 state.sls keepalived.install #推送
在server4實時檢視程序:
安裝依賴包
建立預編譯環境
編譯中
編譯完成
檢視server4中keepalived安裝是否成功並將server4上的keepalived配置檔案傳給server1
[[email protected] ~]# cd /mnt/
[[email protected] mnt]# ls
keepalived-2.0.6 keepalived-2.0.6.tar.gz
[[email protected] mnt]# cd /usr/local/keepalived/
[[email protected] keepalived]# ls
bin etc sbin share
[[email protected] keepalived]# cd etc/rc.d/init.d/
[[email protected] init.d]# ls
keepalived
[[email protected] init.d]# scp keepalived server1:/srv/salt/keepalived/files/
[[email protected] init.d]# cd /usr/local/keepalived/etc/keepalived/
[[email protected] keepalived]# ls
keepalived.conf samples
[[email protected] keepalived]# scp keepalived.conf server1:/srv/salt/keepalived/files/
二·做連結
[[email protected] keepalived]# vim install.sls ##新增以下內容做軟連線
/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
[[email protected] keepalived]# salt server4 state.sls keepalived.install #推送
在server4中檢視:
[[email protected] keepalived]# ll /etc/sysconfig/keepalived
[[email protected] keepalived]# ll /sbin/keepalived
三、修改keepalived配置檔案
[[email protected] keepalived]# cd /srv/salt/keepalived/files/
[[email protected] files]# vim keepalived.conf ##刪除31行之後 修改vip及相關內容
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
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}}
advert_int 1
priority {{priority}}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.45.100
}
}
四、服務檔案(jinja模板)
[[email protected] files]# cd /srv/salt/keepalived/
[[email protected] keepalived]# ls
files install.sls
[[email protected] 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'] }}
priority: {{ pillar['priority'] }}
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
五、定義變數
[[email protected] ~]# cd /srv/pillar/
[[email protected] pillar]# mkdir keepalived
[[email protected] pillar]# cd keepalived/
[[email protected] keepalived]# vim install.sls
{% if grains['fqdn'] == 'server1' %}
state: MASTER
vrid: 45
priority: 100
{% elif grains['fqdn'] == 'server4' %}
state: BACKUP
vrid: 45
priority: 50
{% endif %}
[[email protected] keepalived]# cd /srv/pillar/
[[email protected] pillar]# ls
keepalived top.sls web
[[email protected] pillar]# cd web/
[[email protected] web]# vim install.sls ##之前做負載均衡的檔案
{% if grains['fqdn'] == 'server2' %}
webserver: apache
bind: 172.25.45.2
port: 80
{% elif grains['fqdn'] == 'server3' %}
webserver: nginx
{% endif %}
[[email protected] files]# salt server4 state.sls keepalived.service #推送
六、修改base
[[email protected] web]# cd /srv/pillar/
[[email protected] pillar]# vim top.sls
base:
'server2':
- web.install
'server3':
- web.install
'server1':
- keepalived.install
'server4':
- keepalived.install
七、安裝mailx 編輯高階推送檔案
[[email protected] salt]# yum install -y mailx
[[email protected] pillar]# cd /srv/salt/
[[email protected] salt]# ls
apache _grains haproxy keepalived _modules nginx pkgs top.sls users
[[email protected] salt]# vim top.sls
base:
'server1':
- haproxy.install
- keepalived.service
'server4':
- haproxy.install
- keepalived.service
'server2':
- apache.install
'server3':
- nginx.service
測試:
[[email protected] salt]# salt '*' state.highstate
[[email protected] salt]# ip addr
1、高階推送成功
在server4中:
2、訪問172.25.45.100(vip)實現負載均衡
當在server1中關閉keepalived時,負載均衡不會受影響.此時vip在server4上 實現高可用
server1開啟服務後,VIP還是會回到server1上