saltstack自动化运维之一键部署keepalived高可用+haproxy负载均衡集群

hostname|ip|角色|安装服务

  • | :-: | -:
    server1|172.25.12.1|salt-master、salt-minion|haproxy、keepalived
    server2|172.25.12.2|salt-minion |httpd
    server3|172.25.12.3|salt-minion|nginx
    server4|172.25.12.4|salt-minion|haproxy、keepalived
    httpd和nginx的模板编写参考http和nginx一键推送模板

一、haproxy模板编写

1、查看所有minion状态

[root@server1 nginx]# salt '*' test.ping
server3:
    True
server2:
    True
server1:
    True
server4:
    True

2、编写haproxy模板的安装方法

[root@server1 salt]# mkdir haproxy
[root@server1 salt]# cd haproxy/
[root@server1 haproxy]# vim install.sls
haproxy-install:
  pkg.installed:
    - pkgs:
      - haproxy
[root@server1 haproxy]# salt server1 state.sls haproxy.install  ##推送

3、haproxy安装成功后将生成的配置文件拷贝到/srv/salt/haproxy/files目录下,并修改配置文件

[root@server1 haproxy]# mkdir files
[root@server1 haproxy]# cd files/
[root@server1 files]# cp /etc/haproxy/haproxy.cfg  .
[root@server1 files]# vim haproxy.cfg 
 63 frontend  main *:80
 64     #acl url_static       path_beg       -i /static /images /javascript /sty    lesheets
 65     #acl url_static       path_end       -i .jpg .gif .png .css .js
 66 
 67     #use_backend static          if url_static
 68     default_backend             app
 69 
 70 #---------------------------------------------------------------------
 71 # static backend for serving up images, stylesheets and such
 72 #---------------------------------------------------------------------
 73 backend app
 74     balance     roundrobin
 75     server  app1 172.25.12.2:80 check
 76     server  app2 172.25.12.3:80 check

4、修改install.sls文件

  1 haproxy-install:
  2   pkg.installed:
  3     - pkgs:
  4       - haproxy
  5   file.managed:
  6     - name: /etc/haproxy/haproxy.cfg
  7     - source: salt://haproxy/files/haproxy.cfg
  8 
  9   service.running:
 10     - name: haproxy
 11     - reload: True
 12     - watch:
 13       - file: haproxy-install

5、编写一键推送文件

[root@server1 files]# cd /srv/salt/
[root@server1 salt]# vim top.sls
  1 base:
  2   'server1':
  3     - haproxy.install
  4   'server2':
  5     - httpd.service
  6   'server3':
  7     - nginx.service

6、开启一键推送

一键推送是推送所有的minion,因为server4没有执行操作,所有有一个报错

[root@server1 salt]# salt '*' state.highstate
server4:
----------
          ID: states
    Function: no.None
      Result: False
     Comment: No Top file or external nodes data matches found.
     Changes:   

Summary for server4
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   0.000 ms
server1:
----------
          ID: haproxy-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 09:56:40.709006
    Duration: 650.1 ms
     Changes:   
----------
          ID: haproxy-install
    Function: file.managed
        Name: /etc/haproxy/haproxy.cfg
      Result: True
     Comment: File /etc/haproxy/haproxy.cfg updated
     Started: 09:56:41.361689
    Duration: 93.553 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -60,27 +60,18 @@
                   #---------------------------------------------------------------------
                   # main frontend which proxys to the backends
                   #---------------------------------------------------------------------
                  -frontend  main *:5000
                  -    acl url_static       path_beg       -i /static /images /javascript /stylesheets
                  -    acl url_static       path_end       -i .jpg .gif .png .css .js
                  +frontend  main *:80
                  +    #acl url_static       path_beg       -i /static /images /javascript /stylesheets
                  +    #acl url_static       path_end       -i .jpg .gif .png .css .js
                   
                  -    use_backend static          if url_static
                  +    #use_backend static          if url_static
                       default_backend             app
                   
                   #---------------------------------------------------------------------
                   # static backend for serving up images, stylesheets and such
                   #---------------------------------------------------------------------
                  -backend static
                  -    balance     roundrobin
                  -    server      static 127.0.0.1:4331 check
                  -
                  -#---------------------------------------------------------------------
                  -# round robin balancing between the various backends
                  -#---------------------------------------------------------------------
                   backend app
                       balance     roundrobin
                  -    server  app1 127.0.0.1:5001 check
                  -    server  app2 127.0.0.1:5002 check
                  -    server  app3 127.0.0.1:5003 check
                  -    server  app4 127.0.0.1:5004 check
                  +    server  app1 172.25.12.2:80 check
                  +    server  app2 172.25.12.3:80 check
                   
----------
          ID: haproxy-install
    Function: service.running
        Name: haproxy
      Result: True
     Comment: Started Service haproxy
     Started: 09:56:41.456288
    Duration: 185.751 ms
     Changes:   
              ----------
              haproxy:
                  True

Summary for server1
------------
Succeeded: 3 (changed=2)
Failed:    0
------------
Total states run:     3
Total run time: 929.404 ms
server3:
----------
          ID: nginx-make
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 09:56:40.306080
    Duration: 473.41 ms
     Changes:   
----------
          ID: nginx-install
    Function: file.managed
        Name: /mnt/nginx-1.12.0.tar.gz
      Result: True
     Comment: File /mnt/nginx-1.12.0.tar.gz is in the correct state
     Started: 09:56:40.782779
    Duration: 150.571 ms
     Changes:   
----------
          ID: nginx-install
    Function: cmd.run
        Name: cd /mnt && tar zxf nginx-1.12.0.tar.gz && cd nginx-1.12.0 && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make > /dev/null && make install > /dev/null
      Result: True
     Comment: /usr/local/nginx exists
     Started: 09:56:40.934677
    Duration: 0.651 ms
     Changes:   
----------
          ID: nginx-group
    Function: group.present
        Name: nginx
      Result: True
     Comment: Group nginx is present and up to date
     Started: 09:56:40.935968
    Duration: 0.738 ms
     Changes:   
----------
          ID: nginx-user
    Function: user.present
        Name: nginx
      Result: True
     Comment: User nginx is present and up to date
     Started: 09:56:40.937834
    Duration: 1.856 ms
     Changes:   
----------
          ID: /usr/local/nginx/conf/nginx.conf
    Function: file.managed
      Result: True
     Comment: File /usr/local/nginx/conf/nginx.conf is in the correct state
     Started: 09:56:40.939880
    Duration: 60.066 ms
     Changes:   
----------
          ID: nginx-service
    Function: file.managed
        Name: /etc/init.d/nginx
      Result: True
     Comment: File /etc/init.d/nginx is in the correct state
     Started: 09:56:41.000157
    Duration: 61.008 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: Started Service nginx
     Started: 09:56:41.063384
    Duration: 172.331 ms
     Changes:   
              ----------
              nginx:
                  True

Summary for server3
------------
Succeeded: 8 (changed=1)
Failed:    0
------------
Total states run:     8
Total run time: 920.631 ms
server2:
----------
          ID: httpd-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 09:56:41.719729
    Duration: 621.672 ms
     Changes:   
----------
          ID: httpd-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 09:56:42.343891
    Duration: 42.934 ms
     Changes:   
----------
          ID: httpd-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is running
     Started: 09:56:42.387705
    Duration: 156.887 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for server2
------------
Succeeded: 3 (changed=1)
Failed:    0
------------
Total states run:     3
Total run time: 821.493 ms
ERROR: Minions returned with non-zero exit code

7、分别在server2和server3的web默认发发布目录下建立index.html文件,来测试

[root@server2 html]# cat /var/www/html/index.html
server2
[root@server3 ~]# cat /usr/local/nginx/html/index.html 
server3

测试
负载均衡效果实现

[root@foundation12 ~]# for i in {1..20};do curl 172.25.12.1;done
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2

二、keepalived模板编写

##1、编写keepalived源码安装方法 ##

[root@server1 salt]# cd 
[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# mkdir keepalived
[root@server1 salt]# cd keepalived/
[root@server1 keepalived]# mkdir files
[root@server1 keepalived]# cd files/
[root@server1 files]# ls
keepalived-1.4.3.tar.gz
[root@server1 files]# cd ..
[root@server1 keepalived]# vim install.sls
include:     ##导入
  - pkgs.make
kp-install:
  file.managed:       
    - name: /mnt/keepalived-1.4.3.tar.gz
    - source: salt://keepalived/files/keepalived-1.4.3.tar.gz

  cmd.run:
    - name: cd /mnt && tar zxf keepalived-1.4.3.tar.gz && cd keepalived-1.4.3 && ./configure --prefix=/usr/local/keepalived --with-init=SYSV &> /dev/null && make &> /dev/null && make install &> /dev/null
    - creates: /usr/local/keepalived

/etc/sysconfig/keepalived:
  file.symlink:    ##软连接模块
    - target: /usr/local/keepalived/etc/sysconfig/keepalived

/sbin/keepalived:
  file.symlink:
    - target: /usr/local/keepalived/sbin/keepalived

/etc/keepalived:
  file.directory:    ##目录管理模块
    - mode: 755

2、源码安装推送

[root@server1 keepalived]# salt server4 state.sls keepalived.install
server4:
----------
          ID: nginx-make
    Function: pkg.installed
      Result: True
     Comment: 3 targeted packages were installed/updated.
     Started: 10:31:34.480282
    Duration: 18927.874 ms
     Changes:   
              ----------
              cloog-ppl:
                  ----------
                  new:
                      0.15.7-1.2.el6
                  old:
              cpp:
                  ----------
                  new:
                      4.4.7-4.el6
                  old:
              gcc:
                  ----------
                  new:
                      4.4.7-4.el6
                  old:
              glibc-devel:
                  ----------
                  new:
                      2.12-1.132.el6
                  old:
              glibc-headers:
                  ----------
                  new:
                      2.12-1.132.el6
                  old:
              kernel-headers:
                  ----------
                  new:
                      2.6.32-431.el6
                  old:
              keyutils-libs-devel:
                  ----------
                  new:
                      1.4-4.el6
                  old:
              krb5-devel:
                  ----------
                  new:
                      1.10.3-10.el6_4.6
                  old:
              libcom_err-devel:
                  ----------
                  new:
                      1.41.12-18.el6
                  old:
              libgomp:
                  ----------
                  new:
                      4.4.7-4.el6
                  old:
              libselinux-devel:
                  ----------
                  new:
                      2.0.94-5.3.el6_4.1
                  old:
              libsepol-devel:
                  ----------
                  new:
                      2.0.41-4.el6
                  old:
              mpfr:
                  ----------
                  new:
                      2.4.1-6.el6
                  old:
              openssl-devel:
                  ----------
                  new:
                      1.0.1e-15.el6
                  old:
              pcre-devel:
                  ----------
                  new:
                      7.8-6.el6
                  old:
              ppl:
                  ----------
                  new:
                      0.10.2-11.el6
                  old:
              zlib-devel:
                  ----------
                  new:
                      1.2.3-29.el6
                  old:
----------
          ID: kp-install
    Function: file.managed
        Name: /mnt/keepalived-1.4.3.tar.gz
      Result: True
     Comment: File /mnt/keepalived-1.4.3.tar.gz updated
     Started: 10:31:53.414616
    Duration: 212.186 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644
----------
          ID: kp-install
    Function: cmd.run
        Name: cd /mnt && tar zxf keepalived-1.4.3.tar.gz && cd keepalived-1.4.3 && ./configure --prefix=/usr/local/keepalived --with-init=SYSV &> /dev/null && make &> /dev/null && make install &> /dev/null
      Result: True
     Comment: Command "cd /mnt && tar zxf keepalived-1.4.3.tar.gz && cd keepalived-1.4.3 && ./configure --prefix=/usr/local/keepalived --with-init=SYSV &> /dev/null && make &> /dev/null && make install &> /dev/null" run
     Started: 10:31:53.627701
    Duration: 17740.401 ms
     Changes:   
              ----------
              pid:
                  1587
              retcode:
                  0
              stderr:
              stdout:
----------
          ID: /etc/sysconfig/keepalived
    Function: file.symlink
      Result: True
     Comment: Created new symlink /etc/sysconfig/keepalived -> /usr/local/keepalived/etc/sysconfig/keepalived
     Started: 10:32:11.368367
    Duration: 4.133 ms
     Changes:   
              ----------
              new:
                  /etc/sysconfig/keepalived
----------
          ID: /sbin/keepalived
    Function: file.symlink
      Result: True
     Comment: Created new symlink /sbin/keepalived -> /usr/local/keepalived/sbin/keepalived
     Started: 10:32:11.372614
    Duration: 69.645 ms
     Changes:   
              ----------
              new:
                  /sbin/keepalived
----------
          ID: /etc/keepalived
    Function: file.directory
      Result: True
     Comment: Directory /etc/keepalived updated
     Started: 10:32:11.444697
    Duration: 9.621 ms
     Changes:   
              ----------
              /etc/keepalived:
                  New Dir

Summary for server4
------------
Succeeded: 6 (changed=6)
Failed:    0
------------
Total states run:     6
Total run time:  36.964 s

3、server4安装完成后,将生成的配置文件和脚本文件复制到server1的相关目录下

[root@server4 ~]# scp /usr/local/keepalived/etc/rc.d/init.d/keepalived  server1:/srv/salt/keepalived/files
root@server1's password: 
keepalived                                         100% 1308     1.3KB/s   00:00    
[root@server4 ~]# scp /usr/local/keepalived/etc/keepalived/keepalived.conf  server1:/srv/salt/keepalived/files
root@server1's password: 
keepalived.conf                                    100% 3550     3.5KB/s   00:00 
[root@server1 keepalived]# ls files/
keepalived  keepalived-1.4.3.tar.gz  keepalived.conf

4、keepalived配置文件修改

[root@server1 keepalived]# cd files/
[root@server1 files]# vim keepalived.conf 
[root@server1 files]# cat 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_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.12.100   ##vip
    }
}

5、服务文件(jinja模板)

[root@server1 pillar]# cd /srv/salt/keepalived/
[root@server1 keepalived]# cat 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

6、变量的定义

此操作需要打开/etc/salt/master文件中的pillar和 并restart salt-master

[root@server1 pillar]# cd 
[root@server1 ~]# cd /srv/pillar/
[root@server1 pillar]# mkdir keepalived
[root@server1 pillar]# ls
keepalived  top.sls  web
[root@server1 pillar]# cd keepalived/
[root@server1 keepalived]# vim install.sls 
{% if grains['fqdn'] == 'server1' %}
state: MASTER
vrid: 24
priority: 100
{% elif grains['fqdn'] == 'server4' %}
state: BACKUP
vrid: 24
priority: 50
{% endif %}
[root@server1 ~]# cd /srv/pillar/
[root@server1 pillar]# ls
keepalived  top.sls  web
[root@server1 pillar]# cd web/
[root@server1 web]# vim install.sls 
[root@server1 web]# cat install.sls 
{% if grains['fqdn'] == 'server2' %}
webserver: httpd
port: 80
bind: 172.25.51.2
{% elif grains['fqdn'] == 'server3' %}
webserver: nginx
port: 80
bind: 172.25.51.3
{% endif %}

7、修改base

[root@server1 pillar]# pwd
/srv/pillar
[root@server1 pillar]# vim top.sls 

base:
  'server2':
    - web.install
  'server3':
    - web.install
  'server1':
    - keepalived.install
  'server4':
    - keepalived.install

8、make.sls添加mailx安装

因为配置文件中定义了邮件

[root@server1 ~]# vim /srv/salt/pkgs/make.sls 

make:
  pkg.installed:
    - pkgs:
      - gcc
      - pcre-devel
      - openssl-devel
      - mailx

9、高级推送文件

[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# ls
_grains  haproxy  httpd  keepalived  lib.sls  nginx  pkgs  top.sls  users
[root@server1 salt]# vim top.sls 
base:
  'server1':
    - haproxy.install
    - keepalived.service
  'server4':
    - haproxy.install
    - keepalived.service
  'server2':
    - httpd.service
  'server3':
    - nginx.service

10、开始推送

[root@server1 pillar]# salt '*' pillar.items
server2:
    ----------
    bind:
        172.25.51.2
    port:
        80
    webserver:
        httpd
server4:
    ----------
    priority:
        50
    state:
        BACKUP
    vrid:
        24
server3:
    ----------
    bind:
        172.25.51.3
    port:
        80
    webserver:
        nginx
server1:
    ----------
    priority:
        100
    state:
        MASTER
    vrid:
        24
[root@server1 pillar]# salt '*' state.highstate
server2:
----------
          ID: httpd-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 11:23:07.306102
    Duration: 767.656 ms
     Changes:   
----------
          ID: httpd-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 11:23:08.077517
    Duration: 101.08 ms
     Changes:   
----------
          ID: httpd-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 11:23:08.180062
    Duration: 53.816 ms
     Changes:   

Summary for server2
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3
Total run time: 922.552 ms
server3:
----------
          ID: nginx-make
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 11:23:06.611156
    Duration: 742.486 ms
     Changes:   
----------
          ID: nginx-install
    Function: file.managed
        Name: /mnt/nginx-1.12.0.tar.gz
      Result: True
     Comment: File /mnt/nginx-1.12.0.tar.gz is in the correct state
     Started: 11:23:07.356649
    Duration: 137.385 ms
     Changes:   
----------
          ID: nginx-install
    Function: cmd.run
        Name: cd /mnt && tar zxf nginx-1.12.0.tar.gz && cd nginx-1.12.0 && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make > /dev/null && make install > /dev/null
      Result: True
     Comment: /usr/local/nginx exists
     Started: 11:23:07.495411
    Duration: 0.714 ms
     Changes:   
----------
          ID: nginx-group
    Function: group.present
        Name: nginx
      Result: True
     Comment: Group nginx is present and up to date
     Started: 11:23:07.496818
    Duration: 0.808 ms
     Changes:   
----------
          ID: nginx-user
    Function: user.present
        Name: nginx
      Result: True
     Comment: User nginx is present and up to date
     Started: 11:23:07.498884
    Duration: 1.87 ms
     Changes:   
----------
          ID: /usr/local/nginx/conf/nginx.conf
    Function: file.managed
      Result: True
     Comment: File /usr/local/nginx/conf/nginx.conf is in the correct state
     Started: 11:23:07.500958
    Duration: 38.867 ms
     Changes:   
----------
          ID: nginx-service
    Function: file.managed
        Name: /etc/init.d/nginx
      Result: True
     Comment: File /etc/init.d/nginx is in the correct state
     Started: 11:23:07.539999
    Duration: 45.176 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: The service nginx is already running
     Started: 11:23:07.586301
    Duration: 44.436 ms
     Changes:   

Summary for server3
------------
Succeeded: 8
Failed:    0
------------
Total states run:     8
Total run time:   1.012 s
server4:
----------
          ID: haproxy-install
    Function: pkg.installed
      Result: True
     Comment: The following packages were installed/updated: haproxy
     Started: 11:23:07.372012
    Duration: 7199.255 ms
     Changes:   
              ----------
              haproxy:
                  ----------
                  new:
                      1.4.24-2.el6
                  old:
----------
          ID: haproxy-install
    Function: file.managed
        Name: /etc/haproxy/haproxy.cfg
      Result: True
     Comment: File /etc/haproxy/haproxy.cfg updated
     Started: 11:23:14.577545
    Duration: 127.966 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -60,27 +60,18 @@
                   #---------------------------------------------------------------------
                   # main frontend which proxys to the backends
                   #---------------------------------------------------------------------
                  -frontend  main *:5000
                  -    acl url_static       path_beg       -i /static /images /javascript /stylesheets
                  -    acl url_static       path_end       -i .jpg .gif .png .css .js
                  +frontend  main *:80
                  +    #acl url_static       path_beg       -i /static /images /javascript /stylesheets
                  +    #acl url_static       path_end       -i .jpg .gif .png .css .js
                   
                  -    use_backend static          if url_static
                  +    #use_backend static          if url_static
                       default_backend             app
                   
                   #---------------------------------------------------------------------
                   # static backend for serving up images, stylesheets and such
                   #---------------------------------------------------------------------
                  -backend static
                  -    balance     roundrobin
                  -    server      static 127.0.0.1:4331 check
                  -
                  -#---------------------------------------------------------------------
                  -# round robin balancing between the various backends
                  -#---------------------------------------------------------------------
                   backend app
                       balance     roundrobin
                  -    server  app1 127.0.0.1:5001 check
                  -    server  app2 127.0.0.1:5002 check
                  -    server  app3 127.0.0.1:5003 check
                  -    server  app4 127.0.0.1:5004 check
                  +    server  app1 172.25.12.2:80 check
                  +    server  app2 172.25.12.3:80 check
                   
----------
          ID: haproxy-install
    Function: service.running
        Name: haproxy
      Result: True
     Comment: Started Service haproxy
     Started: 11:23:14.711557
    Duration: 273.502 ms
     Changes:   
              ----------
              haproxy:
                  True
----------
          ID: nginx-make
    Function: pkg.installed
      Result: True
     Comment: The following packages were installed/updated: mailx
              The following packages were already installed: gcc, pcre-devel, openssl-devel
     Started: 11:23:14.993623
    Duration: 4578.817 ms
     Changes:   
              ----------
              mailx:
                  ----------
                  new:
                      12.4-7.el6
                  old:
----------
          ID: kp-install
    Function: file.managed
        Name: /mnt/keepalived-1.4.3.tar.gz
      Result: True
     Comment: File /mnt/keepalived-1.4.3.tar.gz is in the correct state
     Started: 11:23:19.578127
    Duration: 106.148 ms
     Changes:   
----------
          ID: kp-install
    Function: cmd.run
        Name: cd /mnt && tar zxf keepalived-1.4.3.tar.gz && cd keepalived-1.4.3 && ./configure --prefix=/usr/local/keepalived --with-init=SYSV &> /dev/null && make &> /dev/null && make install &> /dev/null
      Result: True
     Comment: /usr/local/keepalived exists
     Started: 11:23:19.688162
    Duration: 0.501 ms
     Changes:   
----------
          ID: /etc/sysconfig/keepalived
    Function: file.symlink
      Result: True
     Comment: Symlink /etc/sysconfig/keepalived is present and owned by root:root
     Started: 11:23:19.688821
    Duration: 6.754 ms
     Changes:   
----------
          ID: /sbin/keepalived
    Function: file.symlink
      Result: True
     Comment: Symlink /sbin/keepalived is present and owned by root:root
     Started: 11:23:19.695738
    Duration: 5.652 ms
     Changes:   
----------
          ID: /etc/keepalived
    Function: file.directory
      Result: True
     Comment: Directory /etc/keepalived is in the correct state
     Started: 11:23:19.701573
    Duration: 0.732 ms
     Changes:   
----------
          ID: /etc/keepalived/keepalived.conf
    Function: file.managed
      Result: True
     Comment: File /etc/keepalived/keepalived.conf updated
     Started: 11:23:19.702426
    Duration: 127.814 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644
----------
          ID: kp-service
    Function: file.managed
        Name: /etc/init.d/keepalived
      Result: True
     Comment: File /etc/init.d/keepalived updated
     Started: 11:23:19.830443
    Duration: 117.087 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0755
----------
          ID: kp-service
    Function: service.running
        Name: keepalived
      Result: True
     Comment: Started Service keepalived
     Started: 11:23:19.952643
    Duration: 202.686 ms
     Changes:   
              ----------
              keepalived:
                  True

Summary for server4
-------------
Succeeded: 12 (changed=7)
Failed:     0
-------------
Total states run:     12
Total run time:   12.747 s
server1:
----------
          ID: haproxy-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 11:23:07.261207
    Duration: 838.848 ms
     Changes:   
----------
          ID: haproxy-install
    Function: file.managed
        Name: /etc/haproxy/haproxy.cfg
      Result: True
     Comment: File /etc/haproxy/haproxy.cfg is in the correct state
     Started: 11:23:08.104334
    Duration: 72.219 ms
     Changes:   
----------
          ID: haproxy-install
    Function: service.running
        Name: haproxy
      Result: True
     Comment: The service haproxy is already running
     Started: 11:23:08.177544
    Duration: 56.507 ms
     Changes:   
----------
          ID: nginx-make
    Function: pkg.installed
      Result: True
     Comment: 4 targeted packages were installed/updated.
     Started: 11:23:08.234327
    Duration: 24613.139 ms
     Changes:   
              ----------
              cloog-ppl:
                  ----------
                  new:
                      0.15.7-1.2.el6
                  old:
              cpp:
                  ----------
                  new:
                      4.4.7-4.el6
                  old:
              gcc:
                  ----------
                  new:
                      4.4.7-4.el6
                  old:
              glibc-devel:
                  ----------
                  new:
                      2.12-1.132.el6
                  old:
              glibc-headers:
                  ----------
                  new:
                      2.12-1.132.el6
                  old:
              kernel-headers:
                  ----------
                  new:
                      2.6.32-431.el6
                  old:
              keyutils-libs-devel:
                  ----------
                  new:
                      1.4-4.el6
                  old:
              krb5-devel:
                  ----------
                  new:
                      1.10.3-10.el6_4.6
                  old:
              libcom_err-devel:
                  ----------
                  new:
                      1.41.12-18.el6
                  old:
              libgomp:
                  ----------
                  new:
                      4.4.7-4.el6
                  old:
              libselinux-devel:
                  ----------
                  new:
                      2.0.94-5.3.el6_4.1
                  old:
              libsepol-devel:
                  ----------
                  new:
                      2.0.41-4.el6
                  old:
              mailx:
                  ----------
                  new:
                      12.4-7.el6
                  old:
              mpfr:
                  ----------
                  new:
                      2.4.1-6.el6
                  old:
              openssl-devel:
                  ----------
                  new:
                      1.0.1e-15.el6
                  old:
              pcre-devel:
                  ----------
                  new:
                      7.8-6.el6
                  old:
              ppl:
                  ----------
                  new:
                      0.10.2-11.el6
                  old:
              zlib-devel:
                  ----------
                  new:
                      1.2.3-29.el6
                  old:
----------
          ID: kp-install
    Function: file.managed
        Name: /mnt/keepalived-1.4.3.tar.gz
      Result: True
     Comment: File /mnt/keepalived-1.4.3.tar.gz updated
     Started: 11:23:32.855030
    Duration: 233.072 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644
----------
          ID: kp-install
    Function: cmd.run
        Name: cd /mnt && tar zxf keepalived-1.4.3.tar.gz && cd keepalived-1.4.3 && ./configure --prefix=/usr/local/keepalived --with-init=SYSV &> /dev/null && make &> /dev/null && make install &> /dev/null
      Result: True
     Comment: Command "cd /mnt && tar zxf keepalived-1.4.3.tar.gz && cd keepalived-1.4.3 && ./configure --prefix=/usr/local/keepalived --with-init=SYSV &> /dev/null && make &> /dev/null && make install &> /dev/null" run
     Started: 11:23:33.102758
    Duration: 18468.513 ms
     Changes:   
              ----------
              pid:
                  12111
              retcode:
                  0
              stderr:
              stdout:
----------
          ID: /etc/sysconfig/keepalived
    Function: file.symlink
      Result: True
     Comment: Created new symlink /etc/sysconfig/keepalived -> /usr/local/keepalived/etc/sysconfig/keepalived
     Started: 11:23:51.571526
    Duration: 3.932 ms
     Changes:   
              ----------
              new:
                  /etc/sysconfig/keepalived
----------
          ID: /sbin/keepalived
    Function: file.symlink
      Result: True
     Comment: Created new symlink /sbin/keepalived -> /usr/local/keepalived/sbin/keepalived
     Started: 11:23:51.575567
    Duration: 72.793 ms
     Changes:   
              ----------
              new:
                  /sbin/keepalived
----------
          ID: /etc/keepalived
    Function: file.directory
      Result: True
     Comment: Directory /etc/keepalived updated
     Started: 11:23:51.650759
    Duration: 37.936 ms
     Changes:   
              ----------
              /etc/keepalived:
                  New Dir
----------
          ID: /etc/keepalived/keepalived.conf
    Function: file.managed
      Result: True
     Comment: File /etc/keepalived/keepalived.conf updated
     Started: 11:23:51.688879
    Duration: 119.068 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644
----------
          ID: kp-service
    Function: file.managed
        Name: /etc/init.d/keepalived
      Result: True
     Comment: File /etc/init.d/keepalived updated
     Started: 11:23:51.808080
    Duration: 119.987 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0755
----------
          ID: kp-service
    Function: service.running
        Name: keepalived
      Result: True
     Comment: Started Service keepalived
     Started: 11:23:51.935272
    Duration: 224.173 ms
     Changes:   
              ----------
              keepalived:
                  True

Summary for server1
-------------
Succeeded: 12 (changed=9)
Failed:     0
-------------
Total states run:     12
Total run time:   44.860 s

测试:
saltstack自动化运维之一键部署keepalived高可用+haproxy负载均衡集群_第1张图片

[root@foundation12 ~]# for i in {1..20};do curl 172.25.12.1;done
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2

关闭server1的keepalived

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

vip漂移到server4
saltstack自动化运维之一键部署keepalived高可用+haproxy负载均衡集群_第2张图片
访问正常

[root@foundation12 ~]# for i in {1..20};do curl 172.25.12.1;done
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2
server3
server2

你可能感兴趣的:(自动化运维)