环境:
servera(Haproxy):192.168.233.132
serverb(web1):192.168.233.144
serverc(web2):192.168.233.140
serverd(客户端):192.168.233.141
servera(Haproxy):
yum install haproxy -y
vim /etc/haproxy/haproxy.cfg(配置文件)
# 设置日志,记录在本地3号设备上,记录级别为info
global
log 127.0.0.1 local3 info
# 最大连接数为4096
maxconn 4096
# 运行haproxy的用户和组
user nobody
group nobody
# 以守护进程方式运行
daemon
# 进程数为1
nbproc 1
# PID文件路径
pidfile /run/haproxy.pid
# 默认配置
defaults
# 全局日志记录
log global
# 模式为HTTP
mode http
# 最大连接数为2048
maxconn 2048
# 重试次数为3
retries 3
# 开启转发
option redispatch
# 连接超时时间为5秒
timeout connect 5000
# 客户端超时时间为50秒
timeout client 50000
# 服务器超时时间为50秒
timeout server 50000
# 关闭连接时强制关闭
option abortonclose
# 配置统计页面
stats uri /admin?stats
stats realm Private lands
stats auth admin:password
stats hide-version
# 前端配置
frontend http-in
# 监听所有IP的80端口
bind 0.0.0.0:80
# 使用HTTP模式
mode http
# 记录日志
log global
# 开启httplog选项
option httplog
# 关闭HTTP长连接
option httpclose
# 定义acl规则
acl html url_reg -i \.html$
# 如果是HTML文件,则使用html-server后端
use_backend html-server if html
# 默认使用html-server后端
default_backend html-server
# 后端配置
backend html-server
# 使用HTTP模式
mode http
# 负载均衡算法为轮询
balance roundrobin
# 健康检查请求为GET /index.html
option httpchk GET /index.html
# 插入SERVERID COOKIE,并且间接设置,不缓存
cookie SERVERID insert indirect nocache
# 配置服务器A
server html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
# 配置服务器B
server html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
systemctl start haproxy
serverb(web1):
setenforce 0
systemctl stop firewalld
yum install httpd -y
echo web1 > /var/www/html/index.html
systemctl enable httpd
systemctl start httpd
serverc(web2):
setenforce 0
systemctl stop firewalld
yum install httpd -y
echo web2 > /var/www/html/index.html
systemctl enable httpd
systemctl start httpd
serverd(客户端):
vim /etc/hosts
192.168.233.141 client
192.168.233.144 web1
192.168.233.140 web2
192.168.233.132 haproxy
所有server及都要配hosts
在haproxy端访问:
lynx --dump http://haproxy
测试Haproxy状态:
- Haproxy状态连接:
http://192.168.233.132/admin?stats
- 输入信息:用户名:admin,密码:password(配置文件的defaults选项)
- Haproxy状态页面: