saltstack部署高可用负载均衡

实验主机:
server9:master(keepalived,haproxy)
server7:minion(keepalived,haproxy)
server8:minion(nginx)
server10:minion(httpd)
1.安装keepalived

在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

saltstack部署高可用负载均衡_第1张图片
saltstack部署高可用负载均衡_第2张图片
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

saltstack部署高可用负载均衡_第3张图片
server9:完善安装脚本

[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

saltstack部署高可用负载均衡_第4张图片
saltstack部署高可用负载均衡_第5张图片
####配置pillar

[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 %}

saltstack部署高可用负载均衡_第6张图片
saltstack部署高可用负载均衡_第7张图片

[root@server9 keepalived]# cd ..
[root@server9 pillar]# vim top.sls 
base:
  '*':
    - web.install
    - keepalived.install   #全局声明

这里写图片描述
saltstack部署高可用负载均衡_第8张图片

2.更改配置keepalived文件

[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上配置
    }
}

这里写图片描述
saltstack部署高可用负载均衡_第9张图片
saltstack部署高可用负载均衡_第10张图片
编写keepalived服务配置脚本

[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

saltstack部署高可用负载均衡_第11张图片
saltstack部署高可用负载均衡_第12张图片
配置/srv/salt/top.sls

[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

这里写图片描述
saltstack部署高可用负载均衡_第13张图片
saltstack部署高可用负载均衡_第14张图片
查看虚拟ip
saltstack部署高可用负载均衡_第15张图片

3.测试

server10(apache)

[root@server10 ~]# vim /var/www/html/index.html
server10
[root@server10 ~]# /etc/init.d/httpd restart

saltstack部署高可用负载均衡_第16张图片
在浏览器访问172.25.25.100
saltstack部署高可用负载均衡_第17张图片
刷新
saltstack部署高可用负载均衡_第18张图片
关闭server9的keepalived

[root@server9 web]# /etc/init.d/keepalived stop
Stopping keepalived:                                       [  OK  ]

访问浏览器反复刷新
saltstack部署高可用负载均衡_第19张图片
saltstack部署高可用负载均衡_第20张图片
测试结果不受影响说明服务转到了server7,查看虚拟ip
saltstack部署高可用负载均衡_第21张图片

你可能感兴趣的:(saltstack部署高可用负载均衡)