Haproxy实验
环境:
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
globallog 127.0.0.1 local3 info# 最大连接数为4096maxconn 4096# 运行haproxy的用户和组user nobodygroup nobody# 以守护进程方式运行daemon# 进程数为1nbproc 1# PID文件路径pidfile /run/haproxy.pid# 默认配置
defaults# 全局日志记录log global# 模式为HTTPmode http# 最大连接数为2048maxconn 2048# 重试次数为3retries 3# 开启转发option redispatch# 连接超时时间为5秒timeout connect 5000# 客户端超时时间为50秒timeout client 50000# 服务器超时时间为50秒timeout server 50000# 关闭连接时强制关闭option abortonclose# 配置统计页面stats uri /admin?statsstats realm Private landsstats auth admin:passwordstats 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.htmloption httpchk GET /index.html# 插入SERVERID COOKIE,并且间接设置,不缓存cookie SERVERID insert indirect nocache# 配置服务器Aserver html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5# 配置服务器Bserver html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5systemctl start haproxy
serverb(web1):
setenforce 0systemctl stop firewalldyum install httpd -yecho web1 > /var/www/html/index.htmlsystemctl enable httpdsystemctl start httpd
serverc(web2):
setenforce 0systemctl stop firewalldyum install httpd -yecho web2 > /var/www/html/index.htmlsystemctl enable httpdsystemctl 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