keepalived+nginx高可用
发表于|更新于
|字数总计:2.8k|阅读时长:10分钟
Keepalived + Nginx 实现高可用
原理
如果杀掉 MASTER(主)节点的 nginx
如果nginx进程不存在脚本返回1,nginx进程存在脚本返回0
脚本返回0,表示nginx进程存在,由于上面配置的weight为负数,priority 不会改变。
脚本返回1,表示nginx进程不存在,由于上面配置的weight为负数,priority 会减少,减少后的值 priority = priority+weight
失败一次,将自己的优先级-20,我这里MASTER的priority=100,BACKUP的priority=99
那么MASTER要失败1次后变为80,低于BACKUP的99,MASTER节点会降级为BACKUP,而BACKUP节点升级为MASTER
#!/bin/bash
result=`pidof nginx` if [ ! -z "${result}" ]; then exit 0 else exit 1 fi
|
提示:以上脚本判断推出码是0还是1,正常退出时0,非正常退出为1;
退出码1就把对应节点的优先级降低20(通常这个降低的值要大于两节点优先级之差就行,意思就是降低后的优先级要小于备份节点优先级,这样才有意义);
脚本执行连续3次检测都为成功状态(脚本退出码都为0),则keepalived就标记该实例为OK状态,并会一直检测下去,
如果连续3次检查都为失败状态(退出码1),则标记对应实例为KO状态;
一旦标记对应实例为失败状态就会触发当前节点的优先级降低;
从而在通告心跳时,会通告降低后的优先级,从而实现备份节点接管VIP来完成vip转移;
杀掉 MASTER(主)节点的 nginx 后,再次访问浮动VIP
编译安装 keepalived
可以去keepalived官网直接下载:https://www.keepalived.org/download.html
yum -y install gcc openssl openssl-devel libnl libnl-devel libnfnetlink-devel wget
wget --no-check-certificate https://www.keepalived.org/software/keepalived-2.2.7.tar.gz tar -zxvf keepalived-2.2.7.tar.gz cd keepalived-2.2.7
./configure --prefix=/usr/local/keepalived --disable-fwmark make -j 2 && make install mkdir /etc/keepalived cp -p /usr/local/keepalived/etc/keepalived/keepalived.conf.sample /etc/keepalived/keepalived.conf
systemctl daemon-reload systemctl enable --now keepalived.service
|
keepalived 配置说明
全剧配置
global_defs { notification_email { root@localhost root@wangxiaochun.com 29308620@qq.com } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id ka1.example.com vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 vrrp_mcast_group4 224.0.0.18 vrrp_iptables }
|
虚拟路由器配置
vrrp_instance <STRING> { 配置参数 ...... }
state MASTER|BACKUP#当前节点在此虚拟路由器上的初始状态,状态为MASTER或者BACKUP interface IFACE_NAME virtual_router_id VRID priority 100 advert_int 1 authentication { auth_type AH|PASS auth_pass <PASSWORD> } virtual_ipaddress { <IPADDR>/<MASK> brd <IPADDR> dev <STRING> scope <SCOPE> label <LABEL> 192.168.200.100 为/32 192.168.200.101/24 dev eth1 网卡 192.168.200.102/24 dev eth2 label eth2:1 } track_interface { eth0 eth1 … }
|
keepalived + nginx 配置样例
MASTER(主)节点配置文件
! Configuration File for keepalived
global_defs { router_id 192.168.111.201 }
vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 }
vrrp_instance VI_1 { state MASTER interface ens32 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.111.250 } track_script { chk_nginx } }
|
BACKUP(备)节点配置文件
! Configuration File for keepalived global_defs { router_id 192.168.111.202 } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 }
vrrp_instance VI_1 { state BACKUP interface ens32 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.111.250 } track_script { chk_nginx } }
|
Nginx 状态检测脚本
vi /etc/keepalived/nginx_check.sh
|
方式一:
如果 nginx 停止运行,尝试启动,如果无法启动则杀死本机的 keepalived 进程, keepalied将虚拟 ip 绑定到 BACKUP 机器上。
#!/bin/bash A=`ps -C nginx --no-header |wc -l` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx sleep 2 if [ $A -eq 0 ];then killall keepalived fi fi
|
killall命令默认未安装,会报错 killall: 未找到命令。安装方式如下:
方式二:
如下,返回0或者1。我比较喜欢这种方式。
这里换成其他组件,比如mysql 修改为pidof mysql
即可。
#!/bin/bash
result=`pidof nginx` if [ ! -z "${result}" ]; then exit 0 else exit 1 fi
|
给脚本添加执行权限
chmod +x /etc/keepalived/nginx_check.sh
|
开启组播防火墙
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ens32 --destination 224.0.0.18 --protocol vrrp -j ACCEPT firewall-cmd --reload
firewall-cmd --permanent --direct --get-all-rules
--direct:指定将要使用直接规则 --permanent:表示永久生效 ,没有此参数重启后失效 --add-rule ipv4 filter:表示添加一个新的策略 设置一条IPV4规则,表为防火墙表 :filter, 处理输入数据包 INPUT ,0 代表在头部添加。后面就是常用的iptables语法 --in-interface ens32:设置网卡名,这里我的网卡名是ens32 --destination 224.0.0.18:设置目标ip地址,也就是设置放行组播地址224.0.0.18 --protocol vrrp:设置拦截的协议 -j ACCEPT:表示放行,-j DROP表示丢弃(不放行)
|
启动 Keepalived
systemctl start keepalived.service
systemctl restart keepalived.service
systemctl stop keepalived.service
systemctl status keepalived
ps -ef|grep keepalived
|
验证VIP漂移
我们先关闭Master,验证VIP是否会漂移到BACKUP上。
关闭 MASTER(主)节点 的 keepalived:
systemctl stop keepalived.service
|
常见错误
keepalived 配置了虚拟ip,发现无法ping 通这个虚拟ip。
解决方法,把配置文件中的 vrrp_strict 给注释掉,重启 keepalived 服务后再次ping就可以了。
启动失败、卡住
使用命令 journalctl -xe 查看日志,有这样一行:
Failed to parse PID from file /usr/local/keepalived/var/run/keepalived.pid: Invalid argument
这里因为源码安装的方式,pid文件找不到,使用命令 systemctl status keepalived 查看 load文件路径:loaded (/usr/lib/systemd/system/keepalived.service)
修改/usr/lib/systemd/system/keepalived.service该文件内容:PIDFile=/var/run/keepalived.pid