前期准备
- 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

完成了负载均衡

本文详细介绍如何使用Ansible自动化工具部署Apache服务器和HAProxy负载均衡器,包括配置playbook文件、实现服务启动和重启自动化、以及通过模板文件动态配置HAProxy。实验展示了通过修改Ansible hosts文件来调整负载均衡策略的过程。

1037

被折叠的 条评论
为什么被折叠?



