使用playbook实现haproxy负载均衡

前期准备

  • vim ansible/hosts 建立webservers组

  • visudo中添加

  • 安装haproxy

  • 将/etc/haproxy/haproxy.cfg 拷贝到 ansible/templates中
[devops@server8 ~]$ cp /etc/haproxy/haproxy.cfg ansible/templates/haproxy.cfg.j2

haproxy实现负载均衡

编写playbook.yml

常见出错点:

  • 编写的时候一定要记住缩进问题;
  • 在config环节中,src 和 dest 一定要写正确;
  • 之前的练习中 http 和 firewalld 的打开是分开的;在本次实验中,使用了 loop 循环,就可以和并在一起简化代码了。
  • vim playbook4.yml
[devops@server8 ansible]$ cat playbook4.yml 
---
- name: deploy apache
  gather_facts: yes
  hosts: webservers
  vars:
    http_port: 80
  tasks:
  - name: install apache
    yum:
      name:
        - httpd
        - php
      state: present

  - name: copy index.html
    copy:
      content: "{{ ansible_facts['hostname'] }}\n" 
      dest: /var/www/html/index.html

  - name: config apache
    template:
      src: templates/httpd.conf.j2
      dest: /etc/httpd/conf/httpd.conf
    notify: restart apache

  - name: start apache
    service:
      name: "{{ item }}"
      state: started
      enabled: yes
    loop:
      - httpd
      - firewalld

  - name: enable http
    firewalld:
      service: http
      permanent: yes
      immediate: yes
      state: enabled

  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted

- name: deploy haproxy
  hosts: localhost
  tasks:
  - name: install haproxy
    yum:
      name: haproxy
      state: present

  - name: config haproxy
    template:
      src: templates/haproxy.cfg.j2
      dest: /etc/haproxy/haproxy.cfg
    notify: restart haproxy

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

  handlers:
    - name: restart haproxy
      service:
        name: haproxy
        state: restarted
[devops@server8 ansible]$ 
  • 编辑 hosts
[devops@server8 ansible]$ vim hosts
[test]
server9

[prod]
server10

[webservers:children]
test
prod
  • 编辑 haproxy.cfg.j2

此处是 /home/devops/ansible/templates/haproxy.cfg.j2 文件。之后还有一个 /etc/haproxy/haproxy.cfg 文件。大家一定要区分清楚

[devops@server8 ansible]$ pwd
/home/devops/ansible
[devops@server8 ansible]$ cat templates/haproxy.cfg.j2 
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
{% for host in groups['webservers'] %}
   server {{ hostvars[host]['ansible_facts']['hostname'] }} {{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }}:80 check
{% endfor %}

检验负载均衡

  •  ansible-playbook playbook4.yml

 

  • 查看 /etc/haproxy/haproxy.cfg
[devops@server8 ansible]$ tail -3 /etc/haproxy/haproxy.cfg 
    balance     roundrobin
   server server9 172.25.14.9:80 check
   server server10 172.25.14.10:80 check

实现了负载均衡

 使用页面监控

  • vim templates/haproxy.cfg/j2
[devops@server8 ansible]$ vim templates/haproxy.cfg.j2
    59  stats uri /status    
  • 使用页面访问

  • 关闭server9的haproxy并进行访问

  • 打开server9中的haproxy

 打开后从原先的只能访问到server10变成了负载均衡

 

检验:hosts中组的改变对haproxy的影响

实验1:删除webservers中的prod

  • 修改 hosts 文件

[devops@server8 ansible]$ vim hosts
[test]
server9

[prod]
server10

[webservers:children]    #删除了prod
test                      
  •  ansible-playbook playbook4.yml

  •  此时查看 /etc/haproxy/haproxy.cfg 只存在server9
[devops@server8 ansible]$ tail -3 /etc/haproxy/haproxy.cfg 
backend app
    balance     roundrobin
   server server9 172.25.14.9:80 check

 访问结果如下

实验2:修改webservers中的内容

  • 修改 hosts

  • ansible-playbook 之后,查看/etc/haproxy/haproxy.cfg 可以发现还是webservers中的server9与server10

 完成了负载均衡

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值