说明:用haproxy实现的基本负载均衡
192.168.56.20 haproxy
192.168.56.21 apache01
192.168.56.22 apache02
[[email protected] html]# cat /etc/hosts 192.168.56.20 haproxy 192.168.56.21 apache01 192.168.56.22 apache02 [[email protected] html]#
1.三台服务器
yum install -y gcc glibc gcc-c++ make screen tree lrzsz
2.192.168.56.22 和 192.168.56.21
yum -y install httpd
3.192.168.56.21
echo "web01 192.168.56.21" > /var/www/html/index.html systemctl start httpd systemctl enable httpd 默认监听的是80端口 /etc/httpd/conf/httpd.conf
4.192.168.56.22
echo "web02 192.168.56.22" > /var/www/html/index.html systemctl start httpd systemctl enable httpd 默认监听的是80端口 /etc/httpd/conf/httpd.conf
5.192.168.56.20
yum -y install haproxy vi /etc/harproxy/haproxy.cfg global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 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 static balance roundrobin server static 127.0.0.1:4331 check backend app balance roundrobin server app1 192.168.56.21:80 check inter 2000 rise 3 fall 3 weight 1 server app2 192.168.56.22:80 check inter 2000 rise 3 fall 3 weight 1 备注: 每个2s 重启3次 3次失败 不可用 权重都是1 systemctl start haproxy systemctl enable haproxy
6.浏览器查看
刷新查看
时间: 2024-10-25 15:33:06