插件窝 干货文章 nginx+keepalived高可用主从配置的方法

nginx+keepalived高可用主从配置的方法

服务器 Nginx Keepalived 配置 729    来源:    2025-04-21

Nginx + Keepalived 高可用主从配置指南

一、配置前准备

  1. 服务器准备

    • 两台服务器(主服务器和备服务器)
    • 每台服务器安装Nginx和Keepalived
    • 确保两台服务器网络互通
  2. 虚拟IP(VIP)

    • 准备一个虚拟IP地址,这个IP将在主服务器故障时自动漂移到备服务器

二、安装必要软件

在两台服务器上执行:

# 对于CentOS/RHEL系统
yum install -y nginx keepalived

# 对于Ubuntu/Debian系统
apt-get install -y nginx keepalived

三、配置Keepalived

主服务器配置 (/etc/keepalived/keepalived.conf)

! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
    script "/usr/bin/killall -0 nginx"  # 检查nginx进程是否存在
    interval 2                          # 每2秒检查一次
    weight 2                            # 权重
}

vrrp_instance VI_1 {
    state MASTER                        # 主服务器设置为MASTER
    interface eth0                      # 网卡名称,根据实际情况修改
    virtual_router_id 51                # 虚拟路由ID,主备必须相同
    priority 100                        # 优先级,主服务器应高于备服务器
    advert_int 1                        # 检查间隔,单位秒

    authentication {
        auth_type PASS
        auth_pass 1111                   # 认证密码,主备必须相同
    }

    virtual_ipaddress {
        192.168.1.100/24                # 虚拟IP地址,根据实际情况修改
    }

    track_script {
        chk_nginx                       # 引用上面定义的nginx检查脚本
    }
}

备服务器配置 (/etc/keepalived/keepalived.conf)

! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
    script "/usr/bin/killall -0 nginx"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP                        # 备服务器设置为BACKUP
    interface eth0                      # 网卡名称,根据实际情况修改
    virtual_router_id 51                # 必须与主服务器相同
    priority 90                         # 优先级低于主服务器
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass 1111                   # 必须与主服务器相同
    }

    virtual_ipaddress {
        192.168.1.100/24                # 虚拟IP地址,与主服务器相同
    }

    track_script {
        chk_nginx
    }
}

四、配置Nginx

在两台服务器上配置相同的Nginx服务,确保服务可用性。

示例配置 (/etc/nginx/nginx.conf):

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
}

五、启动服务

在两台服务器上执行:

systemctl start nginx
systemctl enable nginx
systemctl start keepalived
systemctl enable keepalived

六、验证配置

  1. 检查虚拟IP是否绑定在主服务器上:

    ip addr show eth0
    

    应该能看到虚拟IP(192.168.1.100)绑定在主服务器的eth0网卡上

  2. 模拟主服务器故障:

    • 停止主服务器的Nginx服务: bash systemctl stop nginx
    • 检查备服务器是否接管了虚拟IP
  3. 恢复主服务器:

    • 启动主服务器的Nginx服务: bash systemctl start nginx
    • 检查虚拟IP是否漂移回主服务器

七、常见问题解决

  1. 虚拟IP不漂移

    • 检查防火墙是否阻止了VRRP协议通信
    • 确保主备服务器的virtual_router_idauth_pass一致
    • 检查网络是否通畅
  2. Nginx检查脚本不工作

    • 确保脚本路径正确
    • 给脚本执行权限:chmod +x /path/to/script
  3. 日志查看

    journalctl -u keepalived -f
    tail -f /var/log/messages
    

八、高级配置选项

  1. 邮件通知: 在global_defs部分添加:

    global_defs {
      notification_email {
        admin@example.com
      }
      notification_email_from keepalived@example.com
      smtp_server 127.0.0.1
      smtp_connect_timeout 30
    }
    
  2. 多VIP配置: 可以在virtual_ipaddress部分添加多个VIP:

    virtual_ipaddress {
      192.168.1.100/24
      192.168.1.101/24
    }
    
  3. 更复杂的健康检查: 可以修改chk_nginx脚本为更复杂的检查:

    vrrp_script chk_nginx {
       script "/usr/bin/curl -s http://localhost/health-check > /dev/null"
       interval 2
       timeout 2
       fall 2
       rise 2
    }
    

通过以上配置,您已经建立了一个高可用的Nginx服务,当主服务器出现故障时,备服务器会自动接管服务,确保服务的持续可用性。