物理服务器高可用集群搭建:Keepalived+Nginx负载均衡配置实战

服务器高可用集群是保障业务连续性的基础架构。通过Keepalived实现VIP故障自动转移,配合Nginx做负载均衡,可以在物理机故障时实现秒级切换,将服务中断时间控制在极短范围内。本文记录一套完整的搭建流程。

高可用集群架构设计

典型的双机高可用集群由两台物理服务器组成,分别运行Keepalived和Nginx。两台服务器共享一个虚拟IP(VIP),对外提供服务。正常情况下主服务器持有VIP并处理请求,备服务器处于待命状态。主服务器故障时,备服务器在几秒内接管VIP,继续提供服务。

架构组件说明:

  • Keepalived:基于VRRP协议实现VIP故障转移,检测后端服务健康状态
  • Nginx:七层负载均衡,将请求分发到后端应用服务器
  • VIP:虚拟IP,客户端访问的统一入口
  • Real Server:后端应用服务器集群

环境准备与基础配置

两台服务器的基础环境:

# 服务器A (MASTER)
主机名:lb-01
IP地址:192.168.1.10
操作系统:CentOS 7.9

# 服务器B (BACKUP)
主机名:lb-02
IP地址:192.168.1.11
操作系统:CentOS 7.9

# 虚拟IP (VIP)
192.168.1.100

# 后端应用服务器
192.168.1.20:8080
192.168.1.21:8080
192.168.1.22:8080

两台服务器均需安装Keepalived和Nginx:

yum install -y keepalived nginx
systemctl enable keepalived nginx

关闭SELinux和防火墙(生产环境建议配置防火墙规则放行VRRP协议):

setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
firewall-cmd --permanent --add-rich-rule='rule protocol value="vrrp" accept'
firewall-cmd --reload

Keepalived配置详解

服务器A(MASTER)的Keepalived配置文件/etc/keepalived/keepalived.conf:

global_defs {
    router_id LVS_DEVEL_01
}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 2
    weight -20
    fall 3
    rise 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass Yunthe@2026
    }

    virtual_ipaddress {
        192.168.1.100/24
    }

    track_script {
        chk_nginx
    }

    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

服务器B(BACKUP)的配置基本相同,仅需修改以下参数:

state BACKUP
priority 90
router_id LVS_DEVEL_02

priority值决定了主备选举优先级,MASTER设为100,BACKUP设为90。当MASTER的Nginx进程异常退出,check_nginx脚本检测失败,weight -20使MASTER优先级降至80,低于BACKUP的90,触发VIP切换。

Nginx健康检查脚本配置

创建Nginx进程检测脚本/etc/keepalived/check_nginx.sh:

#!/bin/bash
if [ -z "$(pidof nginx)" ]; then
    systemctl start nginx
    sleep 2
    if [ -z "$(pidof nginx)" ]; then
        systemctl stop keepalived
    fi
fi

赋予执行权限:

chmod +x /etc/keepalived/check_nginx.sh

脚本逻辑:检测Nginx进程是否存在,不存在则尝试启动,启动失败则停止Keepalived触发VIP转移。

Nginx负载均衡配置

两台服务器的Nginx配置相同,编辑/etc/nginx/nginx.conf:

upstream backend {
    server 192.168.1.20:8080 weight=1 max_fails=3 fail_timeout=30s;
    server 192.168.1.21:8080 weight=1 max_fails=3 fail_timeout=30s;
    server 192.168.1.22:8080 weight=1 max_fails=3 fail_timeout=30s;
    keepalive 32;
}

server {
    listen 80;
    server_name 192.168.1.100;

    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 5s;
        proxy_read_timeout 60s;
    }

    location /health {
        access_log off;
        return 200 "ok\n";
    }
}

max_fails和fail_timeout参数控制后端健康检查。某个后端服务器在30秒内连续3次请求失败,Nginx会暂时将其从负载均衡池中移除。

故障切换测试与验证

配置完成后启动服务:

systemctl start keepalived
systemctl start nginx

在客户端持续访问VIP验证服务可用性:

while true; do curl -s http://192.168.1.100/health; sleep 1; done

模拟主服务器故障,在lb-01上停止Keepalived:

systemctl stop keepalived

观察客户端输出,服务应在1到3秒内恢复。在lb-02上检查VIP是否已接管:

ip addr show eth0 | grep 192.168.1.100

恢复lb-01的Keepalived,由于priority更高,VIP会自动切回MASTER:

systemctl start keepalived

服务器安全加固方面,建议限制VRRP认证密码复杂度,配置防火墙仅允许集群节点间VRRP通信,定期检查Keepalived日志排查脑裂问题。日志路径:/var/log/messages,通过grep keepalived过滤相关条目。

原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/wu-li-fu-wu-qi-gao-ke-yong-ji-qun-da-jian-keepalivednginx/

(0)
小编小编
上一篇 15小时前
下一篇 15小时前

相关推荐