欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一键部署nginx负载均衡

程序员文章站 2022-07-12 16:55:06
...

先建立好httpd和nginx
在前两篇博客

[root@server1 ~]# yum install -y salt-minion
[root@server1 ~]# cd /etc/salt
[root@server1 salt]# vim minion
 16 master: 172.25.40.1
[root@server1 salt]# /etc/init.d/salt-minion start
[root@server1 salt]# salt-key -L
[root@server1 salt]# salt-key -A server1

一键部署nginx负载均衡

[aaa@qq.com salt]# vim /etc/yum.repos.d/rhel-source.repo  #添加负载均衡yum源
[LoadBalancer]
name=LoadBalancer
baseurl=http://172.25.40.250/rhel6.5/LoadBalancer
gpgcheck=0
[aaa@qq.com salt]# cd /srv/salt/
[aaa@qq.com salt]# mkdir haproxy
[aaa@qq.com salt]# cd haproxy/
[aaa@qq.com 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
[aaa@qq.com salt]# salt server1 state.sls haproxy.install

将脚本写完后推送时有报错:
一键部署nginx负载均衡
解决方法:

[root@server1 salt]# cd /etc/haproxy
[root@server1 haproxy]# mkdir /srv/salt/haproxy/files
[root@server1 haproxy]# cp haproxy.cfg /srv/salt/haproxy/files/
[root@server1 haproxy]# salt server1 state.sls haproxy.install   #再次推送

一键部署nginx负载均衡

[root@server1 haproxy]# cd /srv/salt/haproxy/files/
[root@server1 files]# vim haproxy.cfg 

一键部署nginx负载均衡

[root@server1 files]# /etc/init.d/haproxy restart
[root@server1 files]# netstat -antlp

一键部署nginx负载均衡

将server2端口改为80

[root@server1 salt]# vim httpd/files/httpd.conf 
 136 Listen 80
在server2上写入默认发布目录
[root@server2 ~]# cd /var/www/html
[root@server2 html]# vim index.html
server2

推送负载均衡

[root@server1 files]# cd /srv/salt/
[root@server1 salt]# cd haproxy/
[root@server1 haproxy]# 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

测试:

一键部署nginx负载均衡
一键部署nginx负载均衡