环境准备
IP | 主机名 | 服务 | 系统 |
---|---|---|---|
192.168.47.10 | node1 | ansible | centos8 |
192.168.47.20 | node2 | lamp | centos8 |
192.168.47.30 | node3 | apache | centos8 |
192.168.47.40 | node4 | haproxy | centos8 |
基于上一篇的文章在node3主机上部署apache
一、部署haproxy
准备主机清单组织
//创建角色
[student@server roles]$ ansible-galaxy init haproxy
[student@server ansible]$ cat inventory
[webservers]
node1
node2
[haproxy]
node3
主清单任务
[student@server haproxy]$ cat tasks/main.yml
---
# tasks file for haproxy
- name: install haproxy
yum:
name: haproxy
state: present
- name: cp config
template:
src: haproxy.cfg.j2 ——将所修改的配置覆盖到指定的配置文件
dest: /etc/haproxy/haproxy.cfg
- name: restart haproxy
service:
name: haproxy
state: restarted
enabled: yes
先在root中下载好haproxy服务,以便读取配置文件
[root@node3 network-scripts]# yum -y install haproxy
Last metadata expiration check: 0:22:53 ago on Tue 08 Nov 2022 11:47:21 AM CST.
Package haproxy-1.8.27-2.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[student@server haproxy]$ cp /etc/haproxy/haproxy.cfg templates/haproxy.cfg.j2
//定义好主机清单
[student@server haproxy]$ cat templates/haproxy.cfg.j2
frontend main
bind *:80 ——修改为80端口
acl url_static path_beg -i /static /images /javascript /stylesheets
backend app
balance roundrobin
{% for hh in groups.webservers %} ——定义其主机清单中的webservers
server {{ hostvars[hh].ansible_fqdn }} {{ hostvars[hh].ansible_default_ipv4.address }}:80 check
{% endfor %}
第一个匹配完全域名,第二个匹配清单中的默认IP地址
调用变量后所显示
分别对node2的lamp架构与node3的apache进行负载均衡(这里主机名没打对)
用haproxyIP访问网页测试