安装
1,安装
https://www.cnblogs.com/lfxx/p/17876757.html
https://www.cnblogs.com/wangchengshi/p/10912177.html
2,linux keepalived 日志,如何重定向Keepalived日志的输出路径
https://blog.csdn.net/weixin_39935571/article/details/116731816
https://www.cnblogs.com/mjli/p/13650074.html
3,配置
#1,如果 apt update 找不到就用 yum updateyum update#2,安装 runKeepalived yum install keepalived#3,安装完成后,您可以编辑Keepalived的配置文件来设置您的高可用性环境。配置文件通常位于/etc/keepalived/keepalived.conf。 #以下是一个简单的Keepalived配置示例,用于设置一个 虚拟IP地址:#主机 global_defs {router_id vip_active #主机 vip_active,备机 vip_standby, }##vrrp_script仅仅通过监控脚本返回的状态码来识别集群服务是否正常,如果返回状态码是0,那么就认为服务正常,反之亦然。 vrrp_script chk_nginx {script "/etc/keepalived/check_gateway.sh" # 服务探测,返回0说明服务是正常的interval 3 # 每隔几秒探测一次weight 2 # 权重 }vrrp_instance haproxy {state MASTER # 主机为MASTER,备机为BACKUPinterface eth0 # 监测网络端口,用ipconfig查看virtual_router_id 51 # 主备机必须相同priority 100 # 主备机取不同的优先级,主机要大。主机100,备机99advert_int 1 # VRRP Multicast广播周期秒数authentication {auth_type PASS # VRRP认证方式auth_pass 111111 # VRRP口令 主备机密码必须相同}##track_script:调用vrrp_script定义的脚本去监控资源,定义在实例之内,调用事先定义的vrrp_script。实现主备切换,保证服务高可用track_script {chk_nginx}track_interface {eth0}virtual_ipaddress { # VIP 漂移地址 即集群IP地址10.0.0.155} }### ####备机global_defs {router_id vip_standby #主机 vip_active,备机 vip_standby, }##vrrp_script仅仅通过监控脚本返回的状态码来识别集群服务是否正常,如果返回状态码是0,那么就认为服务正常,反之亦然。 vrrp_script chk_nginx {script "/etc/keepalived/check_gateway.sh" # 服务探测,返回0说明服务是正常的interval 3 # 每隔几秒探测一次weight 2 # 权重 }vrrp_instance haproxy {state BACKUP # 主机为 MASTER,备机为 BACKUPinterface eth0 # 监测网络端口,用ipconfig查看virtual_router_id 51 # 主备机必须相同priority 99 # 主备机取不同的优先级,主机要大。主机100,备机99advert_int 1 # VRRP Multicast广播周期秒数authentication {auth_type PASS # VRRP认证方式auth_pass 111111 # VRRP口令 主备机密码必须相同}##track_script:调用vrrp_script定义的脚本去监控资源,定义在实例之内,调用事先定义的vrrp_script。实现主备切换,保证服务高可用track_script {chk_nginx}track_interface {eth0}virtual_ipaddress { # VIP 漂移地址 即集群IP地址10.0.0.155} }######check_gateway.sh#!/bin/sh#虚似ip地址,155 VIP=10.0.0.155 GATEWAY=10.0.0.253 #/sbin/arping -I eth0 -c 5 -s 10.0.0.155 10.0.0.253 >/dev/null # ip 到路由是通的. if /sbin/arping -I eth0 -c 5 -s $VIP $GATEWAY >/dev/null thenecho "ip is ok" exit 0elseecho "ip is error error" exit 1 fi#这是nginx的判断. #cnt=`ps -C nginx --no-header | wc -l` # 0 进程id 不存在. #if [ $cnt -eq 0 ]; #then # echo "nginx ip is error" # exit 1 #else # echo "nginx ip is ok" # exit 0 #fi######4,更新日志文件等级 #Keepalived默认所有的日志都是写入到/var/log/message下的, 由于message的日志太多了,而Keepalived的日志又很难分离出来,所以本文提供了一个调整Keepalived日志输出路径的方法 cat /etc/sysconfig/keepalived vi /etc/sysconfig/keepalived 把KEEPALIVED_OPTIONS="-D" 修改为 KEEPALIVED_OPTIONS="-D -d -S 0"(a) vi /etc/sysconfig/keepalived#KEEPALIVED_OPTIONS="-D" //注释掉该行 KEEPALIVED_OPTIONS="-D -d -S 0" //添加该行 (b) vi /etc/rsyslog.conf添加该行: local0.* /var/log/keepalived.log(c) 重启日志服务:systemctl restart rsyslog 或者 /etc/init.d/rsyslog restart(d) 重启keepalive服务service keepalived restart(e) 查看日志tail -f /var/log/keepalived.log#设置syslog,修改/etc/syslog.conf,添加内容如下 vi /etc/rsyslog.conf# keepalived -S 0 local0.* /var/log/keepalived.log systemctl restart rsyslog###5,重启,添加 开机启动systemctl restart keepalived systemctl enable keepalived#6,加入防火墙 iptables sudo iptables -A INPUT -p vrrp -j ACCEPT sudo iptables -A OUTPUT -p vrrp -j ACCEPT