Haproxy搭建http负载均衡
LB、RS1、RS2都关闭防火墙和selinux
RS1和RS2部署httpd
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# echo RS1 > /var/www/html/index.html
[root@RS1 ~]# systemctl restart httpd
[root@RS1 ~]# systemctl enable httpd
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# echo RS2 > /var/www/html/index.html
[root@RS2 ~]# systemctl restart httpd
[root@RS2 ~]# systemctl enable httpd
修改LB的内核参数
[root@LB ~]# vim /etc/sysctl.conf
[root@LB ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
修改haproxy配置文件
[root@LB ~]# vim /etc/haproxy/haproxy.cfg
[root@LB ~]# cat /etc/haproxy/haproxy.cfg
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:80
default_backend servers
backend servers
server web01 192.168.253.130:80
server web02 192.168.253.135:80
启动haproxy服务
如果没有使用systemd管理,则执行
[root@LB ~]# haproxy -f /etc/haproxy/haproxy.cfg -c
//如果配置systemd管理haproxy服务
[root@LB ~]# systemctl restart haproxy
客户端验证
[root@localhost ~]# curl http://192.168.253.129
RS1
[root@localhost ~]# curl http://192.168.253.129
RS2
[root@localhost ~]# curl http://192.168.253.129
RS1
使用WEB网页访问测试
[root@LB ~]# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0 info
#log loghost local0 info
maxconn 20480
#chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
#maxconn 4000
user haproxy
group haproxy
daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#------------