基于saltstack自动化部署的Haproxy实现对http和nginx的负载均衡

实验环境:
172.25.23.7 haproxy master
172.25.23.8 httpd
172.25.23.9 nginx
服务器上下载

[root@server7 salt]# salt-key -a server7
The key glob 'server7' does not match any unaccepted keys.
[root@server7 salt]# salt-key
Accepted Keys:
server8
server9
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@server7 salt]# salt-key -a server7
The key glob 'server7' does not match any unaccepted keys.
[root@server7 salt]# vim /etc/hosts
[root@server7 salt]# /etc/init.d/salt-minion state
Usage: /etc/init.d/salt-minion {start|stop|status|restart|condrestart|try-restart|reload}
[root@server7 salt]# /etc/init.d/salt-minion status
salt-minion:root:server7 is running: 20450
[root@server7 salt]# salt-key -a server7
The key glob 'server7' does not match any unaccepted keys.
[root@server7 salt]# vim /etc/salt/minion
[root@server7 salt]# /etc/init.d/salt-minion restart
Stopping salt-minion:root:server7 daemon: OK
Starting salt-minion:root:server7 daemon: OK
[root@server7 salt]# salt-key -a server7
The following keys are going to be accepted:
Unaccepted Keys:
server7
Proceed? [n/Y] y
Key for minion server7 accepted.
[root@server7 salt]# yum install haproxy -y
[root@server7 salt]# mkdir haproxy
[root@server7 salt]# cd haproxy/
[root@server7 haproxy]# vim 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

[root@server7 haproxy]# salt server7 state.sls haproxy.install

测试:

[root@server7 haproxy]# rpm -q haproxy
haproxy-1.4.24-2.el6.x86_64

这里写图片描述


[root@server7 haproxy]# mkdir files
[root@server7 haproxy]# cd files/
[root@server7 files]# cp /etc/haproxy/haproxy.cfg .
[root@server7 files]# vim haproxy.cfg 
frontend  main *:80
    default_backend             app
backend app
    balance     roundrobin
    server  app1 172.25.23.8:80 check
    server  app2 172.25.23.9:80 check

这里写图片描述

[root@server7 salt]# vim top.sls
base:
  'server7':
    - haproxy.install
  'roles:apache':
    - match: grain
    - httpd.install
  'roles:nginx':
    - match: grain
    - nginx.service
[root@server7 salt]# salt server? test.ping
server8:
    True
server9:
    True
server7:
    True
[root@server7 salt]# salt server* test.ping
server9:
    True
server8:
    True
server7:
    True

这里写图片描述
测试:

[root@server7 salt]# salt server[7-9]] test.ping
No minions matched the target. No command was sent, no jid was assigned.
ERROR: No return received
[root@server7 salt]# salt server[7-9] test.ping
server7:
    True
server8:
    True
server9:
    True
[root@server7 pkgs]# salt '*' state.highstate
server7:
----------
          ID: haproxy-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 15:12:19.789372
    Duration: 407.607 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:12:20.200431
    Duration: 48.254

这里写图片描述

[root@server8 ~]# cd /var/www/html
[root@server8 html]# vim index.html 
server8

这里写图片描述
这里写图片描述

测试
浏览器输入172.25.23.7

这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值