# Keepalived+Nginx实现高可用
# 环境准备
操作系统:Ubuntu 24.04 LTS Nginx服务器A: 192.168.0.141 (master) Nginx服务器B: 192.168.0.142 (bakcup)
# 安装依赖
在安装 Keepalived 之前,确保系统上安装了必要的开发工具和库。通常需要以下依赖
sudo apt update
sudo apt install build-essential libssl-dev libnl-3-dev libnl-genl-3-dev libnl-route-3-dev
# 源码安装keepalived
仅在其中一台服务器上操作即可,如:Nginx服务器A
# 下载源码包
从 Keepalived 的官方网站或 GitHub 仓库下载最新版本的源码包 下载地址:https://www.keepalived.org/download.html
# 安装
将源码包 keepalived-2.3.2.tar.gz 上传至服务器192.168.0.141,并解压
tar -zvxf keepalived-2.3.2.tar.gz
编译和安装
cd keepalived-2.2.7
./configure --prefix=/usr/local/keepalived
make
sudo make install
# 配置keepalived
同样在Nginx服务器A上操作
# 配置systemd服务
安装完成后,会在源码包目录生成systemd服务文件 keepalived-2.3.2/keepalived/keepalived.service
# 复制服务文件到 /usr/lib/systemd/system/ 下,并重新加载配置
cp keepalived-2.3.2/keepalived/keepalived.service /usr/lib/systemd/system/
systemctl daemon-reload
# 创建健康检查脚本
创建一个脚本,用于通过 HTTP 请求检查 Nginx 的健康状态。假设 Nginx 在本地主机上运行,并且可以通过 http://localhost/seeyon/main.do 访问健康检查页面
vim /usr/local/keepalived/scripts/check_nginx.sh
#!/bin/bash
curl -sfI --connect-timeout 5 --max-time 15 http://localhost/seeyon/main.do || exit 1
# 修改keepalived配置
vim /usr/local/keepalived/etc/keepalived/keepalived.conf
global_defs {
router_id nginxA # keepalived节点唯一标识,可以修改为主机名或自定义
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check_nginx {
script "/usr/local/keepalived/scripts/check_nginx.sh"
interval 2 # 每2秒检查一次
weight -5 # 如果检查失败,减少5个优先级
}
vrrp_instance Nginx_HA { # vrrp实例名,可自定义
state MASTER # 设置初始vrrp实例角色为master
interface ens33 # 绑定的网卡名称,请根据实际网卡名修改
virtual_router_id 51 # 虚拟路由ID标识,范围0-255,同一实例中master与backup必须一致
priority 100 # 优先级,范围1-254,数字越大优先级越高;同一实例中master优先级高于backup
advert_int 1 # 主备之间同步检查时间间隔,单位:秒
authentication {
auth_type PASS
auth_pass Seeyon@123 # 自定义认证密码
}
virtual_ipaddress {
192.168.0.140 # 指定VIP
}
track_script {
check_nginx
}
}
# 启动&检查
systemctl start keepalived
systemctl status keepalived
服务器启动成功后可以看到ens33网卡上新绑定了一个vip: 192.168.0.140
# 配置其他节点
将NginxA服务器上的 /usr/local/keepalived 和 /usr/lib/systemd/system/keepalived.service 拷贝到NginxB服务器
# 如果没有安装rsync可以用 scp -r /usr/local/keepalived root@192.168.0.142:/usr/local/
rsync -av /usr/local/keepalived root@192.168.0.142:/usr/local/
rsync -av /usr/lib/systemd/system/keepalived.service root@192.168.0.142:/usr/lib/systemd/system/
连接NginxB服务器,修改配置文件
vim /usr/local/keepalived/etc/keepalived/keepalived.conf
global_defs {
router_id nginxB # keepalived节点唯一标识,可以修改为主机名或自定义
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check_nginx {
script "/usr/local/keepalived/scripts/check_nginx.sh"
interval 2 # 每2秒检查一次
weight -5 # 如果检查失败,减少5个优先级
}
vrrp_instance Nginx_HA { # vrrp实例名,可自定义
state BACKUP # 设置初始vrrp实例角色为master
interface ens33 # 绑定的网卡名称,请根据实际网卡名修改
virtual_router_id 51 # 虚拟路由ID标识,范围0-255,同一实例中master与backup必须一致
priority 99 # 优先级,范围1-254,数字越大优先级越高;同一实例中master优先级高于backup
advert_int 1 # 主备之间同步检查时间间隔,单位:秒
authentication {
auth_type PASS
auth_pass Seeyon@123 # 自定义认证密码
}
virtual_ipaddress {
192.168.0.140 # 指定VIP
}
track_script {
check_nginx
}
}
# 重载配置文件并启动keepalived,确认服务已经成功启动
systemctl daemon-reload
systemctl start keepalived
systemctl status keepalived
编撰人:wangyxyf
快速跳转
