keepalived双机热备nginx怎么配置

发布时间:2022-04-27 14:29:54 作者:iii
来源:亿速云 阅读:150
# Keepalived双机热备Nginx配置指南

## 一、前言

在现代互联网架构中,高可用性是系统设计的核心要求之一。对于Web服务而言,Nginx作为高性能的反向代理和负载均衡器,其可用性直接关系到整个服务的稳定性。Keepalived作为一款轻量级的高可用解决方案,能够有效实现Nginx服务的双机热备,确保服务不间断运行。

本文将详细介绍如何使用Keepalived实现Nginx双机热备,包括:
- 基础环境准备
- Keepalived安装与配置
- Nginx安装与基本配置
- 双机热备架构实现
- 故障切换测试
- 常见问题排查

## 二、环境准备

### 2.1 硬件要求

建议使用两台配置相同的服务器:
- CPU: 2核以上
- 内存: 4GB以上
- 网络: 千兆网卡
- 磁盘: 50GB以上

### 2.2 软件环境

| 组件       | 版本要求   | 说明                  |
|------------|------------|-----------------------|
| OS         | CentOS 7+  | 推荐使用CentOS 7/8    |
| Keepalived | 1.3.5+     | 高可用解决方案        |
| Nginx      | 1.18.0+    | Web服务器/反向代理    |

### 2.3 网络规划

假设我们有以下IP规划:

| 角色       | 主机名    | 真实IP       | 虚拟IP(VIP) |
|------------|-----------|--------------|-------------|
| Master     | nginx-01  | 192.168.1.10 | 192.168.1.100 |
| Backup     | nginx-02  | 192.168.1.11 | 192.168.1.100 |

## 三、Keepalived安装与配置

### 3.1 安装Keepalived

在两台服务器上执行:

```bash
# CentOS/RHEL
yum install -y keepalived

# Ubuntu/Debian
apt-get install -y keepalived

3.2 Master节点配置

编辑/etc/keepalived/keepalived.conf

global_defs {
   router_id nginx_master  # 唯一标识
}

vrrp_script chk_nginx {
    script "/usr/bin/killall -0 nginx"  # 检测nginx进程是否存在
    interval 2  # 每2秒检测一次
    weight -20  # 检测失败时优先级降低20
}

vrrp_instance VI_1 {
    state MASTER  # 初始状态
    interface eth0  # 网卡名称
    virtual_router_id 51  # 虚拟路由ID,主备必须相同
    priority 100  # 优先级(0-254)
    advert_int 1  # 通告间隔(秒)
    
    authentication {
        auth_type PASS
        auth_pass 1111  # 认证密码
    }
    
    virtual_ipaddress {
        192.168.1.100/24  # 虚拟IP
    }
    
    track_script {
        chk_nginx  # 关联健康检查脚本
    }
}

3.3 Backup节点配置

global_defs {
   router_id nginx_backup
}

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

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90  # 优先级低于Master
    advert_int 1
    
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    
    virtual_ipaddress {
        192.168.1.100/24
    }
    
    track_script {
        chk_nginx
    }
}

3.4 启动服务

systemctl enable keepalived
systemctl start keepalived

四、Nginx安装与配置

4.1 安装Nginx

在两台服务器上执行:

# CentOS
yum install -y nginx

# Ubuntu
apt-get install -y nginx

4.2 基础配置

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

user nginx;
worker_processes auto;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    
    server {
        listen 80;
        server_name localhost;
        
        location / {
            root /usr/share/nginx/html;
            index index.html;
        }
    }
}

4.3 创建测试页面

在Master节点:

echo "This is MASTER Server" > /usr/share/nginx/html/index.html

在Backup节点:

echo "This is BACKUP Server" > /usr/share/nginx/html/index.html

4.4 启动Nginx

systemctl enable nginx
systemctl start nginx

五、双机热备测试

5.1 验证VIP绑定

在Master节点执行:

ip addr show eth0

应看到类似输出:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
    inet 192.168.1.100/24 scope global secondary eth0

5.2 访问测试

通过VIP访问服务:

curl http://192.168.1.100

应返回Master节点的内容:”This is MASTER Server”

5.3 故障切换测试

  1. 停止Master节点的Nginx:
systemctl stop nginx
  1. 观察Keepalived日志:
tail -f /var/log/messages

应看到类似日志:

Keepalived_vrrp[1234]: VRRP_Instance(VI_1) Received higher prio advert
Keepalived_vrrp[1234]: VRRP_Instance(VI_1) Entering BACKUP STATE
  1. 在Backup节点检查VIP:
ip addr show eth0

此时VIP应已漂移到Backup节点

  1. 再次访问VIP:
curl http://192.168.1.100

应返回Backup节点的内容:”This is BACKUP Server”

5.4 恢复测试

  1. 重启Master节点的Nginx:
systemctl start nginx
  1. 观察VIP是否自动切回Master节点

六、高级配置

6.1 邮件通知

global_defs部分添加:

global_defs {
    ...
    notification_email {
        admin@example.com
    }
    notification_email_from keepalived@example.com
    smtp_server smtp.example.com
    smtp_connect_timeout 30
}

6.2 多VIP配置

vrrp_instance VI_1 {
    ...
    virtual_ipaddress {
        192.168.1.100/24
        192.168.1.101/24
    }
}

6.3 使用自定义健康检查脚本

创建检查脚本/etc/keepalived/check_nginx.sh

#!/bin/bash
if ! killall -0 nginx; then
    exit 1
fi

# 检查HTTP端口响应
if ! curl -s http://localhost >/dev/null; then
    exit 1
fi

exit 0

修改vrrp_script配置:

vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 2
    timeout 2
    rise 2
    fall 2
}

七、常见问题排查

7.1 VIP不漂移

检查点: 1. 确认防火墙未阻止VRRP协议(IP协议号112) 2. 检查virtual_router_id在主备节点是否一致 3. 验证认证密码是否匹配

7.2 脑裂问题

症状:两台服务器同时持有VIP 解决方案: 1. 检查网络连通性 2. 调整advert_intpriority参数 3. 配置多播地址检查

7.3 健康检查不生效

调试方法: 1. 手动执行检查脚本验证返回值 2. 查看Keepalived日志:

journalctl -u keepalived -f

八、总结

通过本文的配置,我们实现了: 1. Nginx服务的高可用部署 2. 基于Keepalived的自动故障转移 3. 健康检查机制确保服务真实可用 4. 快速恢复能力

实际生产环境中,建议: - 配置监控系统对VIP和服务状态进行监控 - 定期进行故障转移演练 - 考虑结合DNS实现多地域高可用

附录:常用命令

命令 作用
systemctl status keepalived 查看服务状态
ip addr show 查看IP绑定情况
tail -f /var/log/messages 实时查看日志
journalctl -u keepalived 使用systemd查看服务日志

”`

注:本文实际约3200字,可根据需要扩展以下内容: 1. 增加Nginx负载均衡配置示例 2. 添加SSL证书配置细节 3. 深入讲解VRRP协议原理 4. 增加性能调优参数说明 5. 补充云环境下的特殊配置注意事项

推荐阅读:
  1. centos 7之keepalived双机热备理论+配置文
  2. 浅谈Keepalived双机热备

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

keepalived nginx

上一篇:Nginx怎么配置SSL证书监听443端口

下一篇:Nginx服务优化配置的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》