本文共 1990 字,大约阅读时间需要 6 分钟。
说明:用haproxy实现的基本负载均衡
192.168.56.20 haproxy
192.168.56.21 apache01
192.168.56.22 apache02
1 2 3 4 5 | [root@apache01 html] # cat /etc/hosts 192.168.56.20 haproxy 192.168.56.21 apache01 192.168.56.22 apache02 [root@apache01 html] # |
1.三台服务器
1 | yum install -y gcc glibc gcc-c++ make screen tree lrzsz |
2.192.168.56.22 和 192.168.56.21
1 | yum -y install httpd |
3.192.168.56.21
1 2 3 4 5 | 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
1 2 3 4 5 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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.浏览器查看
刷新查看