Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群

在之前三篇博客的基础上,操作:

https://blog.csdn.net/JaneNancy/article/details/81775682
https://blog.csdn.net/JaneNancy/article/details/81778084
https://blog.csdn.net/JaneNancy/article/details/81782186

1、在server4 做好底层配置:

[root@server4 ~]# vim /etc/yum.repos.d/rhel-source.repo 
[root@server4 ~]# yum repolist   

[salt]
name=saltstack
baseurl=http://172.25.50.250/rhel6
enabled=1
gpgcheck=0

[LoadBalancer]
name=LoadBalancer
baseurl=http://172.25.50.250/rhel6.5/LoadBalancer
gpgcheck=0

2、进行配置salt-minion

[root@server4 ~]# yum install -y salt-minion
[root@server4 ~]# vim /etc/salt/minion

 16 master: 172.25.50.1

[root@server4 ~]# /etc/init.d/salt-minion restart
Stopping salt-minion:root:server4 daemon: OK
Starting salt-minion:root:server4 daemon: OK
[root@server4 ~]# vim /etc/hosts

3、配置高可用模块keepalived

[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# ls
haproxy  httpd  nginx  pkgs  top.sls  users
[root@server1 salt]# mkdir keepalived
[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# ls
haproxy  httpd  keepalived  nginx  pkgs  top.sls  users
[root@server1 salt]# cd keepalived/
[root@server1 keepalived]# ls
[root@server1 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

4、在server1编写keepalived的安装脚本进行推送

[root@server1 keepalived]# mkdir files
[root@server1 keepalived]# cd files
[root@server1 files]# ls
keepalived-2.0.6.tar.gz

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第1张图片

[root@server1 keepalived]# salt server4 state.sls keepalived.install    进行推送在server4安装keepalived

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第2张图片

在server4查看:

安装过程中在server4可以查看相应进程

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第3张图片

[root@server4 ~]# ll -d /usr/local/keepalived/
drwxr-xr-x 6 root root 4096 817 23:12 /usr/local/keepalived/
[root@server4 ~]# cd /mnt
[root@server4 mnt]# ls
keepalived-2.0.6  keepalived-2.0.6.tar.gz
[root@server4 mnt]# cd /usr/local/keepalived/
[root@server4 keepalived]# ls
bin  etc  sbin  share
[root@server4 keepalived]# cd etc/
[root@server4 etc]# cd rc.d/
[root@server4 rc.d]# cd init.d/
[root@server4 init.d]# ls
keepalived      传递keepalived的调用脚本
[root@server4 init.d]# scp keepalived  server1:/srv/salt/keepalived/files
The authenticity of host 'server1 (172.25.50.1)' can't be established.
RSA key fingerprint is 52:35:3e:8a:7b:70:bf:05:cb:6f:84:bc:a0:cf:4e:42.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server1,172.25.50.1' (RSA) to the list of known hosts.
root@server1's password: 
keepalived                                    100% 1308     1.3KB/s   00:00    
[root@server4 init.d]# cd ..
[root@server4 rc.d]# cd ..
[root@server4 etc]# ls
keepalived  rc.d  sysconfig
[root@server4 etc]# cd keepalived/
[root@server4 keepalived]# ls
keepalived.conf  samples   
[root@server4 keepalived]# scp keepalived.conf  server1:/srv/salt/keepalived/files  ##传递keepalived的主配置文件在server4的对应安装位置上已经有了keepalived
root@server1's password: 
keepalived.conf                               100% 3550     3.5KB/s   00:00    

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第4张图片

在server1配置安装脚本制作相应的软链接

[root@server1 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
[root@server1 keepalived]# salt server4 state.sls keepalived.install

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第5张图片

[root@server1 keepalived]# cd /srv/
[root@server1 srv]# ls
salt
[root@server1 srv]# mkdir pillar
[root@server1 srv]# cd /srv/pillar
[root@server1 pillar]# ls
[root@server1 pillar]# mkdir web
[root@server1 pillar]# cd web
[root@server1 web]# vim install.sls   ##没有写东西
[root@server1 web]# cd ..
[root@server1 pillar]# ls
web
[root@server1 pillar]# mkdir keepalived
[root@server1 pillar]# cd keepalived/
[root@server1 keepalived]# ls
[root@server1 keepalived]# vim install.sls

{% if grains['fqdn'] == 'server1' %}
state: MASTER
vrid: 51
priority: 100
{% elif grains['fqdn'] == 'server4' %}
state: MASTER
vrid: 51
priority: 50
{% endif %}
[root@server1 keepalived]# cd ..
[root@server1 pillar]# vim top.sls

base:
  '*':
    - web.install
    - keepalived.install
[root@server1 pillar]# ls
keepalived  top.sls  web
[root@server1 pillar]# cd ..
[root@server1 srv]# cd salt/
[root@server1 salt]# ls
haproxy  httpd  keepalived  nginx  pkgs  top.sls  users
[root@server1 salt]# cd keepalived/
[root@server1 keepalived]# ls
files  install.sls  service.sls
[root@server1 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

在server4可以看到对应的软链接代表推送成功

[root@server4 etc]# cd ..
[root@server4 keepalived]# cd sbin/
[root@server4 sbin]# ls
keepalived
[root@server4 sbin]# cd /etc/keepalived/
[root@server4 keepalived]# ll /etc/sysconfig/keepalived 
lrwxrwxrwx 1 root root 46 817 23:24 /etc/sysconfig/keepalived -> /usr/local/keepalived/etc/sysconfig/keepalived
[root@server4 keepalived]# ll /sbin/keepalived 
lrwxrwxrwx 1 root root 37 817 23:24 /sbin/keepalived -> /usr/local/keepalived/sbin/keepalived
[root@server4 keepalived]# ls
[root@server4 keepalived]# cd /usr/local/keepalived/
[root@server4 keepalived]# ls
bin  etc  sbin  share
[root@server4 keepalived]# cd etc/
[root@server4 etc]# ls
keepalived  rc.d  sysconfig
[root@server4 etc]# cd keepalived/
[root@server4 keepalived]# ls
keepalived.conf  samples
[root@server4 keepalived]# vim keepalived.conf 
[root@server4 keepalived]# pwd
/usr/local/keepalived/etc/keepalived
[root@server4 keepalived]# cd /etc/keepalived/
[root@server4 keepalived]# ls
keepalived.conf
[root@server4 keepalived]# ll /etc/init.d/keepalived 
-rwxr-xr-x 1 root root 1308 818 00:58 /etc/init.d/keepalived

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第6张图片

[root@server1 keepalived]# vim files/keepalived.conf 

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   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_instance VI_1 {
    state {{ STATE }}
    interface eth0
    virtual_router_id {{ VRID }}
    priority {{ PRIORITY }}
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.50.100
    }
}

[root@server1 keepalived]# cd ..
[root@server1 salt]# salt server4 state.sls keepalived.service

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第7张图片

server 4 查看:

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第8张图片

[root@server1 keepalived]# cd ..
[root@server1 salt]# ls
haproxy  httpd  keepalived  nginx  pkgs  top.sls  users
[root@server1 salt]# vim top.sls

base:
  'server1':
    - haproxy.install
    - keepalived.service
  'server4':
    - haproxy.install
    - keepalived.service
  'server2':
    - httpd.install
  'server3':
    - nginx.service

[root@server1 salt]# salt '*' state.highstate

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第9张图片

查看 VIP :

[root@server1 salt]# ip addr

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第10张图片

在网页上输入VIP :172.25.50.100 测试

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第11张图片

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第12张图片

测试:
当我们关掉 server1 高可用

[root@server1 salt]# /etc/init.d/keepalived stop
Stopping keepalived:                                       [  OK  ]

VIP 就会跳到 server4 上

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第13张图片

但,当我们重新打开

[root@server1 salt]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]

VIP 就会重新调回来
Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第14张图片

不管怎么,只要 有 VIP 在跳,那么,客户端(网页)就不会有出错。

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第15张图片

Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群_第16张图片

你可能感兴趣的:(Saltstack自动化运维工具 ---- 一键部署高可用负载均衡集群)