Saltstack部署keepalived实现haproxy高可用

Saltstack部署keepalived实现haproxy高可用

图解:
Saltstack部署keepalived实现haproxy高可用_第1张图片
环境:
Server1(salt-master,keepalived-backup,haproxy)172.25.254.1
Server2(salt-minion,keepalived-master,haproxy)172.25.254.2
Server3(salt-minion,RS,httpd)172.25.254.3
Server4(salt-minion,RS,httpd)172.25.254.4
vip:172.25.254.100

先配置好salt
Saltstack部署keepalived实现haproxy高可用_第2张图片

haproxy实现nginx负载均衡就采用之前的部署文件,详见Saltstack安装部署及实践
现在只需配置keepalived部署即可

cd /srv/salt
mkdir keepalived/files -p
vim pkgs/install.sls 
make:
  pkg.installed:
    - pkgs:
      - gcc
      - openssl-devel
      - pcre-devel

vim keepalived/install.sls
include:
  - pkgs.install

keep-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 --with-init=SYSV --prefix=/usr/local/keepalived &> /dev/null && make &> /dev/null && make install &> /dev/null && cd .. && rm -fr keepalived-2.0.6
    - 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

Saltstack部署keepalived实现haproxy高可用_第3张图片
Saltstack部署keepalived实现haproxy高可用_第4张图片
先测试一下编译和软连接是否有错
需在/srv/salt/keepalived/files 目录下放入keepalived源码包
Saltstack部署keepalived实现haproxy高可用_第5张图片
没有报错
Saltstack部署keepalived实现haproxy高可用_第6张图片
接下来编写keepalived启动文件
将server2上的keepalived启动脚本和keepalived配置文件放在server1的
/srv/salt/keepalived/files 目录下

scp /usr/local/keepalived/etc/keepalived/keepalived.conf  server1:/srv/salt/keepalived/files
scp /usr/local/keepalived/etc/rc.d/init.d/keepalived server1:/srv/salt/keepalived/files

Saltstack部署keepalived实现haproxy高可用_第7张图片

vim keepalived/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_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_haproxy {
    script  "killall -0 haproxy"
    interval  1
}

vrrp_instance VI_1 {
    state {{ STATE }}
    interface eth0
    virtual_router_id 24
    priority {{ PRIORITY }}
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100
}
    track_script {
        check_haproxy
    }
}





vim keepalived/service.sls
include:
  - keepalived.install

/etc/keepalived/keepalived.conf:
  file.managed:
    - source: salt://keepalived/files/keepalived.conf
    - template: jinja
    - context:
      STATE: {{ pillar['state'] }}
      PRIORITY: {{ pillar['priority'] }}

keep-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

vim /etc/salt/master

Saltstack部署keepalived实现haproxy高可用_第8张图片

/etc/init.d/salt-master restart
mkdir /srv/pillar
cd /srv/pillar
mkdir keepalived
vim keepalived/state.sls 
vim keepalived/priority.sls 

Saltstack部署keepalived实现haproxy高可用_第9张图片

vim top.sls

Saltstack部署keepalived实现haproxy高可用_第10张图片

salt '*' saltutil.refresh_pillar   #刷新pillar
salt '*' pillar.items            #查看

Saltstack部署keepalived实现haproxy高可用_第11张图片
Saltstack部署keepalived实现haproxy高可用_第12张图片
Saltstack部署keepalived实现haproxy高可用_第13张图片
测试在server2上是否启动,并查看配置文件参数

salt server2 state.sls keepalived.service

Saltstack部署keepalived实现haproxy高可用_第14张图片

在server1中修改keepalived配置文件
vim /srv/salt/keepalived/files/keepalived.conf
#自定义一个脚本函数,函数名check_haproxy可以自定义

vrrp_script check_haproxy {
    script  "killall -0 haproxy"     #此脚本需自己编写
    interval  1                    #每个多长时间探测一次
}

vrrp_instance VI_1 {
    track_script {
        check_haproxy
    }
}

Saltstack部署keepalived实现haproxy高可用_第15张图片

vim top.sls 
base:
  'server1':
    - haproxy.install
    - keepalived.service
  'server2':
    - haproxy.install
    - keepalived.service
  'server3':
    - nginx.install3
  'server4':
    - nginx.install4


salt '*' state.highstate   #没有报错

Saltstack部署keepalived实现haproxy高可用_第16张图片
Saltstack部署keepalived实现haproxy高可用_第17张图片
Saltstack部署keepalived实现haproxy高可用_第18张图片
Saltstack部署keepalived实现haproxy高可用_第19张图片

测试:
此时vip在server2上
Saltstack部署keepalived实现haproxy高可用_第20张图片

在server1上

curl 172.25.254.100     #实现负载均衡

Saltstack部署keepalived实现haproxy高可用_第21张图片

关闭server2的haproxy,发现vip飘到server1上,此时仍可以实现负载均衡
在这里插入图片描述
Saltstack部署keepalived实现haproxy高可用_第22张图片
当server2上haproxy启动时,vip又飘到server2上,仍可以实现负载均衡,实现了haproxy高可用
Saltstack部署keepalived实现haproxy高可用_第23张图片
Saltstack部署keepalived实现haproxy高可用_第24张图片

你可能感兴趣的:(Saltstack部署keepalived实现haproxy高可用)