HAProxy 负载均衡实战:从环境搭建到 Frontend/Backend 与 Listen 模式详解

HAProxy 负载均衡实战:从环境搭建到 Frontend/Backend 与 Listen 模式详解 Haproxy实验环境设定与两种主流配置模式介绍1.haproxy主机#使用脚本快速配置网络[roothaproxy ~]# vmset.sh eth0 172.25.254.100 haproxy连接已成功激活D-Bus 活动路径/org/freedesktop/NetworkManager/ActiveConnection/72: eth0:BROADCAST,MULTICAST,UP,LOWER_UPmtu1500qdisc mq state UP group default qlen1000link/ether 00:0c:29:0c:6f:ee brd ff:ff:ff:ff:ff:ff altname enp3s0 altname ens160 inet172.25.254.100/24 brd172.25.254.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe0c:6fee/64 scopelinktentative noprefixroute valid_lft forever preferred_lft forever haproxy[roothaproxy ~]# vmset.sh eth1 192.168.0.100 haproxy norouter连接已成功激活D-Bus 活动路径/org/freedesktop/NetworkManager/ActiveConnection/83: eth1:BROADCAST,MULTICAST,UP,LOWER_UPmtu1500qdisc mq state UP group default qlen1000link/ether 00:0c:29:0c:6f:f8 brd ff:ff:ff:ff:ff:ff altname enp19s0 altname ens224 inet192.168.0.100/24 brd192.168.0.255 scope global noprefixroute eth1 valid_lft forever preferred_lft forever inet6 fe80::4ca7:8cde:1244:8df/64 scopelinktentative noprefixroute valid_lft forever preferred_lft forever haproxy#配置内核路由功能[roothaproxy ~]# echo net.ipv4.ip_forward1 /etc/sysctl.conf[roothaproxy ~]# sysctl -pnet.ipv4.ip_forward12.webserver1[rootwebserver1 ~]# vmset.sh eth0 192.168.0.10 webserver1 noroute连接已成功激活D-Bus 活动路径/org/freedesktop/NetworkManager/ActiveConnection/42: eth0:BROADCAST,MULTICAST,UP,LOWER_UPmtu1500qdisc mq state UP group default qlen1000link/ether 00:0c:29:8c:96:72 brd ff:ff:ff:ff:ff:ff altname enp3s0 altname ens160 inet192.168.0.10/24 brd192.168.0.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe8c:9672/64 scopelinktentative noprefixroute valid_lft forever preferred_lft forever webserver1#使用Apache作为服务辅助测试[rootwebserver1 ~]# dnf install httpd -yrootwebserver1 ~]# echo webserver1 - 192.168.0.10 /var/www/html/index.html[rootwebserver1 ~]# systemctl enable --now httpdCreated symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.3.webserver2[rootwebserver2 ~]# vmset.sh eth0 192.168.0.20 webserver2 noroute连接已成功激活D-Bus 活动路径/org/freedesktop/NetworkManager/ActiveConnection/42: eth0:BROADCAST,MULTICAST,UP,LOWER_UPmtu1500qdisc mq state UP group default qlen1000link/ether 00:0c:29:8c:96:72 brd ff:ff:ff:ff:ff:ff altname enp3s0 altname ens160 inet192.168.0.20/24 brd192.168.0.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe8c:9672/64 scopelinktentative noprefixroute valid_lft forever preferred_lft forever webserver2[rootwebserver2 ~]# dnf install httpd -y[rootwebserver2 ~]# echo webserver2 - 192.168.0.20 /var/www/html/index.html[rootwebserver2 ~]# systemctl enable --now httpdCreated symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.4.验证环境#在haproxy中访问[roothaproxy ~]# curl 192.168.0.10webserver1 -192.168.0.10[roothaproxy ~]# curl 192.168.0.20webserver2 -192.168.0.20一.frontendbackend1.安装haproxy[roothaproxy ~]# dnf install haproxy -y[roothaproxy ~]# systemctl enable --now haproxy.serviceCreated symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.2.配置七层负载策略分离式配置frontend定义监听端口、接收客户端、匹配路由ACLbackend定义后端服务器池、负载均衡策略优势一个 backend 可以被多个 frontend 共用[roothaproxy ~]# vim /etc/haproxy/haproxy.cfg....省略....frontend webclusterbind*:80 mode http use_backend web-80 backend web-80 balance roundrobin server web1192.168.0.10:80 check server web2192.168.0.20:80 check....省略....[roothaproxy ~]# systemctl restart haproxy.service[roothaproxy ~]# netstat -antlupe | grep haproxytcp000.0.0.0:800.0.0.0:* LISTEN06167329059/haproxy#测试[Administrator.DESKTOP-VJ307M3]➤foriin{1..5}docurl172.25.254.100done192.168.0.20 - web2192.168.0.10 - web1192.168.0.20 - web2192.168.0.10 - web1192.168.0.20 - web2[!NOTE]#设置一个TAB长度为四个空格[roothaproxy ~]# vim ~/.vimrcsetts4sw4ai二.Listen方式做Listen方式前请注释之前做的frontendbackend的内容。listen 把「前端监听 后端集群」写在同一个块内一一绑定无法复用 backend。适合单一端口、单一业务场景。[roothaproxy ~]# vim /etc/haproxy/haproxy.cfglisten webclusterbind*:80 mode http balance roundrobin server web1192.168.0.10:80 check server web2192.168.0.20:80 check#frontend webcluster# bind *:80# mode http# use_backend web-80##backend web-80# balance roundrobin# server web1 192.168.0.10:80 check# server web2 192.168.0.20:80 check#[roothaproxy ~]# netstat -antlupe | grep haproxytcp000.0.0.0:800.0.0.0:* LISTEN06214729144/haproxy#测试[Administrator.DESKTOP-VJ307M3]➤foriin{1..5};docurl172.25.254.100;done192.168.0.10 - web1192.168.0.20 - web2192.168.0.10 - web1192.168.0.20 - web2192.168.0.10 - web1