salt实现haproxy的负载均衡

配置环境:

http和nginx的推送查看上一篇博客

https://blog.csdn.net/u010489158/article/details/81744757

server1    172.25.12.1    salt-manster   salt-minion    haproxy    
server2    172.25.12.2    salt-minion    httpd    
server3    172.25.12.3    salt-minion    nginx    

配置高可用yum源(安装haproxy)

[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.12.250/rhel6.5
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

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

[HighAvailability]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.12.250/rhel6.5/HighAvailability
enabled=1
gpgcheck=1

[LoadBalancer]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.12.250/rhel6.5/LoadBalancer
enabled=1
gpgcheck=1

[ResilientStorage]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.12.250/rhel6.5/ResilientStorage
enabled=1
gpgcheck=1

[ScalableFileSystem]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.12.250/rhel6.5/ScalableFileSystem
enabled=1
gpgcheck=1

 编写安装haproxy的脚本

[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# mkdir haproxy
[root@server1 salt]# cd haproxy/
[root@server1 haproxy]# vim install.sls
[root@server1 haproxy]# cat install.sls 

haproxy-install:
  pkg.installed:
    - pkgs:
      - haproxy

  file.managed:
    - name: /etc/haproxy/haproxy.cfg
    - source: salt://haproxy/files/haproxy.cfg

  service.running:
    - name: haproxy
    - reload: True
    - watch:
      - file: haproxy-install

将haproxy的配置文件haproxy.cfg放到 /srv/salt/haproxy/files

[root@server1 files]# cat haproxy.cfg 
#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:80
    default_backend             app

backend app
    balance     roundrobin
    server  app1 172.25.12.2:80 check
    server  app2 172.25.12.3:80 check

 编写全局推送文件,一键推送所有节点

[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# vim top.sls 
base:
  'server1':
    - haproxy.install
  'server2':
    - httpd.install
  'server3':
    - nginx.service
[root@server1 salt]# salt '*' state.highstate
server3:
----------
          ID: nginx-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 15:10:20.627212
    Duration: 496.55 ms
     Changes:   
----------
          ID: nginx-install
    Function: file.managed
        Name: /mnt/nginx-1.14.0.tar.gz
      Result: True
     Comment: File /mnt/nginx-1.14.0.tar.gz is in the correct state
     Started: 15:10:21.125642
    Duration: 143.495 ms
     Changes:   
----------
          ID: nginx-install
    Function: cmd.run
        Name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./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: 15:10:21.270352
    Duration: 0.631 ms
     Changes:   
----------
          ID: nginx-group
    Function: group.present
        Name: nginx
      Result: True
     Comment: Group nginx is present and up to date
     Started: 15:10:21.271583
    Duration: 0.608 ms
     Changes:   
----------
          ID: nginx-user
    Function: user.present
        Name: nginx
      Result: True
     Comment: User nginx is present and up to date
     Started: 15:10:21.273152
    Duration: 1.565 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: 15:10:21.274909
    Duration: 60.347 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: 15:10:21.335492
    Duration: 56.476 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: The service nginx is already running
     Started: 15:10:21.393489
    Duration: 68.582 ms
     Changes:   

Summary for server3
------------
Succeeded: 8
Failed:    0
------------
Total states run:     8
Total run time: 828.254 ms
server2:
----------
          ID: apache-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 15:10:20.899443
    Duration: 520.15 ms
     Changes:   
----------
          ID: apache-install
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 15:10:21.421440
    Duration: 42.885 ms
     Changes:   
----------
          ID: apache-install
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 15:10:21.465174
    Duration: 26.428 ms
     Changes:   

Summary for server2
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3
Total run time: 589.463 ms
server1:
----------
          ID: haproxy-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 15:10:21.438008
    Duration: 490.119 ms
     Changes:   
----------
          ID: haproxy-install
    Function: file.managed
        Name: /etc/haproxy/haproxy.cfg
      Result: True
     Comment: File /etc/haproxy/haproxy.cfg updated
     Started: 15:10:21.930460
    Duration: 130.205 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -67,3 +67,4 @@
                       balance     roundrobin
                       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: Service reloaded
     Started: 15:10:22.091855
    Duration: 43.77 ms
     Changes:   
              ----------
              haproxy:
                  True

Summary for server1
------------
Succeeded: 3 (changed=2)
Failed:    0
------------
Total states run:     3
Total run time: 664.094 ms

 测试:

server2和server3的80端口已经打开

猜你喜欢

转载自blog.csdn.net/u010489158/article/details/81777758