Keepalived 装置部署
- 下载地址:
点我下载 - 通过 ftp 工具上传到 linux 中,/home/software
- 解压
tar -zxvf keepalived-2.0.18.tar.gz
- 解压后进入到解压进去的目录,看到会有 configure,那么就能够做配置了(配置装置和 nginx 截然不同)
配置 Keepalived – 主
-
通过命令 vim keepalived.conf 关上配置文件
global_defs { # 路由 id:以后装置 keepalived 的节点主机标识符,保障全局惟一 router_id keep_171 } 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.1.161} }
附:查看网卡名称
- 启动 Keepalived
在 sbin 目录中进行启动(同 nginx),如下图:
配置 Keepalived – 备
-
通过命令 vim keepalived.conf 关上配置文件
global_defs {router_id keep_172} 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.1.161 } }
-
启动 Keepalived
# 启动 keepalived systemctl start keepalived # 进行 keepalived systemctl stop keepalived # 重启 keepalived systemctl restart keepalived
-
查看过程
ps -ef|grep keepalived
Keepalived 配置 Nginx 主动重启
-
减少 Nginx 重启检测脚本
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
-
配置 keepalived 监听 nginx 脚本
vrrp_script check_nginx_alive { script "/etc/keepalived/check_nginx_alive_or_not.sh" interval 2 # 每隔两秒运行上一行脚本 weight 10 # 如果脚本运行失败,则降级权重 +10 }
-
在 vrrp_instance 中新增监控的脚本
track_script {check_nginx_alive # 追踪 nginx 脚本}
-
重启 Keepalived 使得配置文件失效
systemctl restart keepalived
配置 Keepalived 双主热备
规定:以一个虚构 ip 分组归为同一个路由
主节点配置:
global_defs {router_id keep_171}
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.1.161}
}
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.1.162}
}
备用节点配置
global_defs {router_id keep_172}
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.1.161}