您好,登录后才能下订单哦!
# 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
编辑/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 # 关联健康检查脚本
}
}
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
}
}
systemctl enable keepalived
systemctl start keepalived
在两台服务器上执行:
# CentOS
yum install -y nginx
# Ubuntu
apt-get install -y nginx
编辑/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;
}
}
}
在Master节点:
echo "This is MASTER Server" > /usr/share/nginx/html/index.html
在Backup节点:
echo "This is BACKUP Server" > /usr/share/nginx/html/index.html
systemctl enable nginx
systemctl start nginx
在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
通过VIP访问服务:
curl http://192.168.1.100
应返回Master节点的内容:”This is MASTER Server”
systemctl stop nginx
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
ip addr show eth0
此时VIP应已漂移到Backup节点
curl http://192.168.1.100
应返回Backup节点的内容:”This is BACKUP Server”
systemctl start nginx
在global_defs
部分添加:
global_defs {
...
notification_email {
admin@example.com
}
notification_email_from keepalived@example.com
smtp_server smtp.example.com
smtp_connect_timeout 30
}
vrrp_instance VI_1 {
...
virtual_ipaddress {
192.168.1.100/24
192.168.1.101/24
}
}
创建检查脚本/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
}
检查点:
1. 确认防火墙未阻止VRRP协议(IP协议号112)
2. 检查virtual_router_id
在主备节点是否一致
3. 验证认证密码是否匹配
症状:两台服务器同时持有VIP
解决方案:
1. 检查网络连通性
2. 调整advert_int
和priority
参数
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. 补充云环境下的特殊配置注意事项
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。