Keepalived
- 解决单点故障问题
- 组件免费
- 可以实现高可用HA机制
- 基于VRRP协议
虚拟路由冗余协议VRRP
- Virtual Router Redundancy Protocol
- 解决内网单机故障的路由协议
- 构建有多个路由器 MASTER BACKUP
- 虚拟IP - VIP (Virtual IP Address)
Keepalived 双机主备原理
Keepalived 双主热备原理
双机主备 可能出现资源的浪费, 备用机用不上!
Keepalived 安装和配置
规划:
主nginx: 192.168.123.26
备用nginx: 192.168.123.129
安装
# CentOS
yum install openssl-devel libnl3-devel
wget https://www.keepalived.org/software/keepalived-2.1.5.tar.gz
tar -zxvf keepalived-2.1.5.tar.gz
cd keepalived-2.1.5
# --prefix 安装位置
# --sysconf 核心配置文件
./configure --prefix=/usr/local/keepalived --sysconf=/etc
make && make install
同一个虚拟ip的为一组, 一主一备份节点, 实现高可用.
配置主节点
vim /etc/keepalived/keepalived.conf
global_defs {
router_id keep_26
}
# 计算机节点
vrrp_instance VI_1 {
# 当前的 26 为主节点 如果是备用机的话, 就是BACKUP
state MASTER
# 当前实例绑定的网卡
interface eno1
# 保证主备id一致即可
virtual_router_id 51
# 优先级 权重 master宕机之后, 就会进行选举, 谁的优先级高, 就能成为master
priority 100
# 心跳同步检查的时间间隔, 默认1秒
advert_int 1
# 认证授权的密码, 防止非法节点的进入
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟ip
virtual_ipaddress {
192.168.123.115
}
}
配置备机节点
global_defs {
router_id keep_129
}
# 计算机节点
vrrp_instance VI_1 {
# 当前的 129 为主节点 如果是备用机的话, 就是BACKUP
state BACKUP
# 当前实例绑定的网卡
interface ens33
# 保证主备id一致即可
virtual_router_id 51
# 优先级 权重 master宕机之后, 就会进行选举, 谁的优先级高, 就能成为master
priority 80
# 心跳同步检查的时间间隔, 默认1秒
advert_int 1
# 认证授权的密码, 防止非法节点的进入
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟ip
virtual_ipaddress {
192.168.123.115
}
}
启动和停止
# 启动
./keepalived
# 停止 - 杀掉进程
ps -ef | grep keepalived
kill -9 PID
# 注册成系统服务
cd /home/software/keepalived-2.1.5/keepalived/etc
# 如果是centos的话
cp init.d/keepalived /etc/init.d/
# 如果是archlinux
cd ..
cp keepalived.service /usr/lib/systemd/system
# 下面这个是都要的
cp sysconfig/keepalived /etc/sysconfig/
# 刷新
systemctl daemon-reload
# 之后就可以通过systemctl指令来使用了
systemctl start keepalived
systemctl stop keepalived
systemctl restart keepalived
systemctl status keepalived
Keepalived 配置Nginx自动重启脚本
使用脚本实现. check_nginx_alive_or_not.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
# 判断nginx是否宕机,如果宕机了,尝试重启
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
# 等待一小会再次检查nginx,如果没有启动成功,则停止keepalived,使其启动备用机
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
修改权限
chmod +x check_nginx_alive_or_not.sh
keepalived
与script
关联
vrrp_script check_nginx_alive {
script "/etc/keepalived/check_nginx_alive_or_not.sh"
interval 2 # 每隔两秒运行上一行脚本
weight 10 # 如果脚本运行成功,则升级权重+10
# weight -10 # 如果脚本运行成功,则降低权重-10
}
vrrp_instance VI_1 {
# ...
track_script {
check_nginx_alive # 追踪 nginx 脚本
}
#...
}
Keepalived 双主热备配置
规则:以一个虚拟ip分组归为同一个路由. 互相为各自的master和backup.
配置keepalived_hot_1
global_defs {
router_id keep_129
}
vrrp_instance VI_1 {
# 当前的 129 为主节点 如果是备用机的话, 就是 BACKUP
state MASTER
# 当前实例绑定的网卡
interface ens33
# 保证主备id一致即可
virtual_router_id 51
# 优先级 权重 master宕机之后, 就会进行选举, 谁的优先级高, 就能成为master
priority 100
# 心跳同步检查的时间间隔, 默认1秒
advert_int 1
# 认证授权的密码, 防止非法节点的进入
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟ip
virtual_ipaddress {
192.168.123.115
}
}
vrrp_instance VI_2 {
state BACKUP
interface ens33
virtual_router_id 52
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟ip
virtual_ipaddress {
192.168.123.116
}
}
配置keepalived_hot_2
global_defs {
router_id keep_26
}
vrrp_instance VI_1 {
# 当前的 129 为主节点 如果是备用机的话, 就是 BACKUP
state BACKUP
# 当前实例绑定的网卡
interface eno1
# 保证主备id一致即可
virtual_router_id 51
# 优先级 权重 master宕机之后, 就会进行选举, 谁的优先级高, 就能成为master
priority 80
# 心跳同步检查的时间间隔, 默认1秒
advert_int 1
# 认证授权的密码, 防止非法节点的进入
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟ip
virtual_ipaddress {
192.168.123.115
}
}
vrrp_instance VI_2 {
state MASTER
interface eno1
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟ip
virtual_ipaddress {
192.168.123.116
}
}