本指南基于CentOS 7.9操作系统,使用两台服务器进行部署,所有操作均通过root账户执行。请确保两台服务器已完成基础系统安装并配置好静态IP地址。
服务器A IP:192.168.1.100
服务器B IP:192.168.1.101
虚拟IP(VIP):192.168.1.200
需要预先安装的软件包:
执行以下命令安装所有依赖:
``` yum install -y keepalived haproxy nginx wget gcc make ```编辑Keepalived配置文件:
``` vi /etc/keepalived/keepalived.conf ```输入以下完整配置内容:
``` ! Configuration File for keepalived global_defs { router_id LVS_DEVEL_A } vrrp_script chk_haproxy { script "/usr/bin/killall -0 haproxy" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.200/24 dev eth0 } track_script { chk_haproxy } } ```保存并退出编辑器后,启动Keepalived服务:
``` systemctl start keepalived systemctl enable keepalived ```编辑Keepalived配置文件:
``` vi /etc/keepalived/keepalived.conf ```输入以下完整配置内容:
``` ! Configuration File for keepalived global_defs { router_id LVS_DEVEL_B } vrrp_script chk_haproxy { script "/usr/bin/killall -0 haproxy" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.200/24 dev eth0 } track_script { chk_haproxy } } ```保存并退出编辑器后,启动Keepalived服务:
``` systemctl start keepalived systemctl enable keepalived ```在两台服务器上执行相同的HAProxy配置操作。编辑HAProxy主配置文件:
``` vi /etc/haproxy/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 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 bind :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.1.100:8080 check server app2 192.168.1.101:8080 check listen stats bind :1936 mode http stats enable stats hide-version stats realm Haproxy\ Statistics stats uri / stats auth admin:password123 ```创建HAProxy系统账户并启动服务:
``` useradd -r haproxy systemctl start haproxy systemctl enable haproxy ```编辑Nginx配置文件:
``` vi /etc/nginx/nginx.conf ```在http块内添加以下server配置:
``` server { listen 8080; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } location /status { stub_status on; access_log off; allow 127.0.0.1; allow 192.168.1.0/24; deny all; } } ```创建测试页面并启动Nginx:
``` echo "This is Server A" > /usr/share/nginx/html/index.html systemctl start nginx systemctl enable nginx ```执行相同的配置操作,但修改测试页面内容:
``` echo "This is Server B" > /usr/share/nginx/html/index.html systemctl start nginx systemctl enable nginx ```在任意客户端机器上执行:
``` ping 192.168.1.200 -t ```
在ping持续运行期间,到服务器A上停止Keepalived服务:
``` systemctl stop keepalived ```观察ping命令的响应,正常情况下会有1-2个丢包后恢复,表示VIP已成功漂移到服务器B。
使用curl命令测试负载均衡效果:
``` curl http://192.168.1.200 ```多次执行该命令,观察返回内容在"This is Server A"和"This is Server B"之间交替出现。
在浏览器中访问:
``` http://192.168.1.200:1936 ```使用配置的用户名admin和密码password123登录,查看后端服务器状态。
在服务器A上停止HAProxy服务:
``` systemctl stop haproxy ```等待2秒后,检查VIP是否漂移:
``` ip addr show eth0 | grep 192.168.1.200 ```如果服务器A上已无VIP,说明Keepalived检测到HAProxy故障并触发了VIP漂移。
在服务器A上重新启动HAProxy:
``` systemctl start haproxy ```由于服务器A的优先级更高,VIP会漂移回来。通过以下命令验证:
``` ip addr show eth0 | grep 192.168.1.200 ```修改认证密码为复杂字符串:
``` auth_pass YourComplexPassword123! ```配置VRRP单播通信,避免广播风暴:
``` unicast_src_ip 192.168.1.100 unicast_peer { 192.168.1.101 } ```根据实际硬件配置调整连接数:
``` maxconn 10000 tune.ssl.default-dh-param 2048 ```启用连接复用:
``` option http-keep-alive timeout http-keep-alive 60s ```检查Keepalived状态:
``` systemctl status keepalived ip addr show | grep 192.168.1.200 ```检查HAProxy状态:
``` systemctl status haproxy echo "show stat" | socat /var/lib/haproxy/stats stdio ```检查后端服务健康状态:
``` curl -s http://192.168.1.200:1936 | grep -A 5 "backend app" ```重启整个高可用集群的正确顺序:
按照本指南配置完成后,您将获得一个具备自动故障转移能力的生产级高可用网络系统架构。所有配置均经过实际测试,可直接在生产环境部署使用。
易频IT社区是综合性互联网IT技术门户网站,专注分享网络技术、服务器运维、网络安全、编程开发、系统架构、云计算、大数据等行业干货,实时更新IT行业资讯、零基础教程、实战案例,为IT从业者、技术爱好者提供专业的学习交流平台。
Copyright © 2021-2026 易频IT社区. All Rights Reserved. 备案号:闽ICP备2023013482号 网站地图