Keepalived简介;安装Keepalived;Keepalived实现双机主备高可用;高可用集群架构 Keepalived 双主热备。
可以监控主备机,主机出现问题就会切换到备用机,可配置邮件,通知管理员。
Keepalived双机主备原理:故障转移、故障切换,VRRP协议会定时向主机发送信号,一旦发现主机宕机,就会切换到备用机。
使用高可用时,一定要保证主备的硬件配置一样。
下载地址:https://www.keepalived.org/download.html
通过共享文件或ftp工具将keepalived-2.0.18.tar.gz上传到Linux中,/home/software。
解压 tar -zxvf keepalived-2.0.18.tar.gz
解压后进入到解压出来的目录,看到会有configure,那么就可以做配置了(配置安装和Nginx一样)。
使用configure命令配置安装目录与核心配置文件所在位置:
./configure --prefix=/usr/local/keepalived --sysconf=/etc
yum -y install libnl libnl-devel
make && make install
进入到/etc/keepalived ,该目录下为 keepalived 核心配置文件。
cd /etc/keepalived
whereis keepalived
global_defs {
# 路由id:当前安装keepalived的节点主机标识符,保证全局唯一
router_id keep_128
}
vrrp_instance VI_1 {
# 表示状态是MASTER主机还是备用机BACKUP
state MASTER
# 该实例绑定的网卡
interface ens33
# 保证主备节点一致即可
virtual_router_id 51
# 权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选
priority 100
# 主备之间同步检查时间间隔,单位秒
advert_int 2
# 认证权限密码,防止非法节点进入
authentication {
auth_type PASS
auth_pass 1111
}
#虚拟出来的ip,可以有多个(vip)
virtual_ipaddress {
192.168.2.110
}
}
附:查看网卡名称
ip addr
./keepalived
ps -ef|grep keepalived
4.查看vip
在网卡ens33下,多了一个192.168.2.110,这个就是虚拟ip
kill -9 进程号
进入keepalived安装目录
cd /home/software/keepalived-2.0.18
进入安装包目录中的 /keepalived/etc/
执行以下两条 copy 命令
cp init.d/keepalived /etc/init.d/
cp sysconfig/keepalived /etc/sysconfig/
刷新系统进程
systemctl daemon-reload
使用系统命令操作 keepalived
# 启动keepalived
systemctl start keepalived
# 停止keepalived
systemctl stop keepalived
# 重启keepalived
systemctl restart keepalived
global_defs {
router_id keep_129
}
vrrp_instance VI_1 {
# 备用机设置为BACKUP
state BACKUP
interface ens33
virtual_router_id 51
# 权重低于MASTER
priority 80
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# 注意:主备两台的vip都是一样的,绑定到同一个vip
192.168.2.110
}
}
# 启动keepalived
systemctl start keepalived
# 停止keepalived
systemctl stop keepalived
# 重启keepalived
systemctl restart keepalived
ps -ef|grep keepalived
vim /etc/keepalived/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 /etc/keepalived/check_nginx_alive_or_not.sh
vrrp_script check_nginx_alive {
script "/etc/keepalived/check_nginx_alive_or_not.sh"
interval 2 # 每隔两秒运行上一行脚本
weight 10 # 如果脚本运行失败,则升级权重+10
}
track_script {
check_nginx_alive # 追踪 nginx 脚本
}
systemctl restart keepalived
配置Keepalived双主热备,规则:以一个虚拟ip分组归为同一个路由。
global_defs {
router_id keep_128
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.110
}
}
vrrp_instance VI_2 {
state BACKUP
interface ens33
virtual_router_id 52
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.111
}
}
global_defs {
router_id keep_129
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.110
}
}
vrrp_instance VI_2 {
state MASTER
interface ens33
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.111
}
}
参考资料:
云服务的DNS解析配置与负载均衡:http://blog.zhaojishun.cn/articles/2019/11/06/1573004619025.html