saltstack自动化运维一键部署haproxy负载均衡

本文介绍如何使用SaltStack实现HAProxy的自动化安装配置,并通过编写Salt状态模块实现一键部署到多个节点,同时展示了httpd和nginx服务的自动化部署过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置环境:
hostname | ip | 节点| 部署服务|os

  • | :-: | -:
    server1|172.25.12.1|salt-manster salt-minion|haproxy| rhel6.5
    server2|172.25.12.2|salt-minion|httpd |rhel6.5
    server3|172.25.12.3|salt-minion | nginx|rhel6.5

一、编写haproxy的安装服务模块

[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: ture
    - watch:
      - file: haproxy-install
[root@server1 haproxy]# mkdir files
[root@server1 haproxy]# cd files.
[root@server1 files]# ls
haproxy.cfg
[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
    #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
    default_backend             app

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

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

[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# ls
haproxy  httpd  nginx  pkgs  top.sls  users
[root@server1 salt]# cat top.sls 
base:
  'server1':
    - haproxy.install
  'server2':
    - httpd.service
  'server3':
    - nginx.service

httpd和nginx的自动化部署可查看上一篇https://blog.youkuaiyun.com/weixin_41789003/article/details/81107111

3、一键高级推送所有节点

[root@server1 salt]# salt '*' state.highstate
server3:
----------
          ID: nginx-make
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 15:06:20.461271
    Duration: 431.393 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: 15:06:20.894364
    Duration: 97.767 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: 15:06:20.992845
    Duration: 0.395 ms
     Changes:   
----------
          ID: nginx-group
    Function: group.present
        Name: nginx
      Result: True
     Comment: Group nginx is present and up to date
     Started: 15:06:20.993548
    Duration: 0.337 ms
     Changes:   
----------
          ID: nginx-user
    Function: user.present
        Name: nginx
      Result: True
     Comment: User nginx is present and up to date
     Started: 15:06:20.994394
    Duration: 1.203 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:06:20.995740
    Duration: 54.552 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:06:21.050428
    Duration: 33.289 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: The service nginx is already running
     Started: 15:06:21.085242
    Duration: 42.04 ms
     Changes:   

Summary for server3
------------
Succeeded: 8
Failed:    0
------------
Total states run:     8
Total run time: 660.976 ms
server2:
----------
          ID: apache-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 15:06:19.797543
    Duration: 1357.509 ms
     Changes:   
----------
          ID: apache-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: 15:06:21.178843
    Duration: 116.952 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 15:06:21.296877
    Duration: 81.54 ms
     Changes:   

Summary for server2
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3
Total run time:   1.556 s
server1:
----------
          ID: haproxy-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 15:06:20.129688
    Duration: 1576.999 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: 15:06:21.753298
    Duration: 79.789 ms
     Changes:   
----------
          ID: haproxy-install
    Function: service.running
        Name: haproxy
      Result: True
     Comment: Started Service haproxy
     Started: 15:06:21.834092
    Duration: 179.49 ms
     Changes:   
              ----------
              haproxy:
                  True

Summary for server1
------------
Succeeded: 3 (changed=1)
Failed:    0
------------
Total states run:     3
Total run time:   1.836 s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值