ansible自动化部署haproxy+http负载均衡

如果不利用ansible自动化部署工具,其部署繁琐程度可通过点击这里查看

server1172.25.254.189轮询控制端
server2172.25.254.188轮询端
server3172.25.254.187轮询端
客户端17225.254.89测试

这里我们通过ansible 进行部署,只需要熟悉playbook书写相关规则:

[root@server1 ansible]# cat hosts
[test]
server2

[prod]
server3

[webserver:children]
test
prod

[lb]
server1
[root@server1 ansible]# cat ansible.cfg 
[defaults]
inventory      = ./hosts
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

部署apache:

[root@server1 ansible]# cat playbook1.yml
---
- hosts: webserver    ###在server2、server3主机上安装部署apache
  tasks:
  - name: install the latest version of Apache from the testing repo
    dnf:
      name: httpd
      state: present
  - name: add index.html
    copy:
      content: "{{ ansible_hostname }}"  ##将登陆的主机名字写入两个轮询主机发布目录中。
      dest: /var/www/html/index.html

  - name: start apache
    service:
      name: httpd
      state: started
      enabled: yes
  - name: add http to firewalld
    firewalld:
      service: http
      permanent: yes
      state: enabled
      immediate: yes
- import_playbook: haproxy.yml

部署haproxy:

[root@server1 ansible]# cat haproxy.yml 
--- 
- hosts: lb    ##在调度主机上安装haproxy服务。
  tasks:
  - name: install haproxy
    dnf: 
      name: haproxy
      state: present

  - name: configure haproxy    ##更改该目录下的haproxy.cfg文件,将与/etc/haproxy/haproxy.cfg进行对比,若发生更改就将其同步,并触发restart haproxy任务。
    template:
      src: haproxy.cfg
      dest: /etc/haproxy/haproxy.cfg
    notify: restart haproxy

  - name: start haproxy
    service:
      name: haproxy
      state: started

  - name: accept haproxy   
    firewalld:
      service: http
      permanent: yes
      immediate: yes
      state: enabled

  handlers:
  - name: restart haproxy
    service:
      name: haproxy
      state: restarted
[root@server1 ansible]# cat haproxy.cfg 

定义调度的端口:
在这里插入图片描述
将需要调度的主机写进配置文件:
在这里插入图片描述
在客户机端测试:
在这里插入图片描述
我们发现在server2和server3之间进行轮询切换。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值