Saltstack自动化运维工具 ---- 一键部署haproxy并实现负载均衡

建立在上一个博客的条件的基础上 https://blog.youkuaiyun.com/JaneNancy/article/details/81775682

1、搭建环境

这里为了方便直接在master server4端安装

现在server4上安装minion,并加入salt-key

[root@server1 ~]# cd /etc/salt
[root@server1 salt]# ls
cloud         cloud.deploy.d  cloud.profiles.d   master    minion    pki    proxy.d
cloud.conf.d  cloud.maps.d    cloud.providers.d  master.d  minion.d  proxy  roster
[root@server1 salt]# vim minion
16 master: 172.25.50.1

这里写图片描述

确保 httpd 的端口是 80 ,并开启服务

[root@server1 salt]# ls
haproxy  httpd  nginx  pkgs  top.sls  users
[root@server1 salt]# cd httpd/
[root@server1 httpd]# ls
files  install.sls
[root@server1 httpd]# cd files/
[root@server1 files]# ls
httpd.conf
[root@server1 files]# vim httpd.conf 

更改端口为80
 136 Listen 80

[root@server2 ~]# cd /var/www/html
[root@server2 html]# ls
[root@server2 html]# vim index.html  ##写入内容,过后可以在网页上可以查看

这里写图片描述

确保 nginx 的端口是 80 ,并开启服务

[root@server3 conf]# cd /usr/local/nginx/
[root@server3 nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@server3 nginx]# cd html/
[root@server3 html]# ls
50x.html  index.html

这里写图片描述

2、配置高可用yum源

实现负载均衡,在yun源里加入LoadBalancer

[root@server1 ~]# cd /etc/yum.repos.d/
[root@server1 yum.repos.d]# ls
rhel-source.repo
[root@server1 yum.repos.d]# vim rhel-source.repo 
加入:

[LoadBalancer]
name=LoadBalancer
baseurl=http://172.25.50.250/rhel6.5/LoadBalancer
gpgcheck=0

这里写图片描述

3、编写高可用文件

[root@server1 salt]# cd /srv/salt
[root@server1 salt]# ls
httpd  nginx  pkgs  users
[root@server1 salt]# mkdir haproxy
[root@server1 salt]# cd haproxy/
[root@server1 haproxy]# ls
[root@server1 haproxy]# pwd
/srv/salt/haproxy
[root@server1 haproxy]# cd /etc/haproxy/
[root@server1 haproxy]# ls
haproxy.cfg
[root@server1 haproxy]# mkdir /srv/salt/haproxy/files
[root@server1 haproxy]# cp haproxy.cfg /srv/salt/haproxy/files/
[root@server1 haproxy]# cd /srv/salt/haproxy/
[root@server1 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@server1 files]# vim haproxy.cfg ##实现负载均衡,更改全局base目录下的haproxy.cfg配置文件

[root@server1 files]# /etc/init.d/haproxy start
Starting haproxy:                                          [  OK  ]
[root@server1 files]# /etc/init.d/haproxy stop
Stopping haproxy:                                          [  OK  ]

[root@server1 files]# salt server1 state.sls haproxy.install

这里写图片描述

运行过程中报错,只完成了安装,这是因为没有配置文件,将安装好产生的配置文件发送到insatll.sls里指定目录下再次运行即可

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

完成后可以单独执行install.sls,也可以其加入到一见推送top.sls里

[root@server1 files]# vim haproxy.cfg 
端口改为 80 
[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# ls
haproxy  httpd  nginx  pkgs  users
[root@server1 salt]# vim top.sls

base:
  'server1':
    - haproxy.install
  'server2':
    - httpd.install
  'server3':
    - nginx.service

在之前server2部署的httpd和server3部署的nginx默认发布目录下写入内容,方便测试

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

在网页上输入 server1 的 IP ,不断刷新,可以看到负载均衡

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

当我们关掉 server2 的 httpd ,网页上就会只显示出nginx的界面,所以我们在server1中再次推送,就有会实现负载均衡。

[root@server1 salt]# salt server2 grains.items   ##没有报错就是好的

这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值