架构
1.安装
yum install keepalived -ysystemctl enable keepalivedsystemctl restart keepalivedsystemctl status keepalived
2.部署配置样例
vim /etc/keepalived/keepalived.conf
global_defs {router_id PROXYSQL_HAscript_user rootenable_script_security
}
vrrp_script chk_proxysql_port {script "/etc/keepalived/check_proxysql.sh"interval 1weight -50fall 2rise 2
}
vrrp_instance VI_1 {state MASTERinterface eth0mcast_src_ip 192.124.107.129 unicast_peer {192.124.126.109 }virtual_router_id 50priority 100advert_int 1authentication {auth_type PASSauth_pass 1qaz2wsx}virtual_ipaddress {192.124.91.241/17 dev eth0 }track_script {chk_proxysql_port}
}
检查脚本
#!/bin/bash
/bin/netstat -na | grep -e '0.0.0.0:5432' &>/dev/null
全局定义块
这部分主要用来设置Keepalived的故障通知机制和Router ID标识
global_defs {router_id PROXYSQL_HAscript_user rootenable_script_security
}
VRRP实例定义模块
vrrp_instance VI_1 {state MASTERinterface eth0mcast_src_ip 192.124.107.129 #当前机器unicast_peer {192.124.126.109 #另一台机器}virtual_router_id 50priority 100advert_int 1authentication {auth_type PASSauth_pass 1qaz2wsx}virtual_ipaddress {192.124.91.241/17 dev eth0 # 设置虚拟ip,主节点自动添加}track_script {chk_proxysql_port}
}
- 如果两节点的上联交换机禁用了组播,则只能采用vrrp单播通告的方式
mcast_src_ip 192.124.107.129 #当前机器unicast_peer {192.124.126.109 #另一台机器}
- 定义实例名称为VI_1
- state 表示该实例的角色状态,有MASTER和BACKUP两种主备状态。
- interface 对外提供服务的网络接口eth0
- virtual_router_id 虚拟路由ID标识,主备服务器配置中相同实例的ID必须一致,否则将出现脑裂问题。
- priority表示实例优先级,数字越大,优先级越高。
- advert_int为同步通知间隔。主备之间通信检查的时间间隔,默认为1秒。
- authentication 权限认证配置。
- virtual_ipaddress 虚拟IP地址;可以配置多个IP,每个IP占一行。注意,这里的IP就是在工作中需要和域名绑定的ip,即可配置的高可用服务监听的ip保持一致。
3.切换日志分析
日志查看方式
tail -22f /var/log/messages | grep Keepalived
节点 192.160.200.3开启后备升为主
Nov 14 14:47:52 192-168-3-193 Keepalived[225718]: Starting VRRP child process, pid=225719
Nov 14 14:47:52 192-168-3-193 Keepalived_vrrp[225719]: Registering Kernel netlink reflector
Nov 14 14:47:52 192-168-3-193 Keepalived_vrrp[225719]: Registering Kernel netlink command channel
Nov 14 14:47:52 192-168-3-193 Keepalived_vrrp[225719]: Opening file '/etc/keepalived/keepalived.conf'.
Nov 14 14:47:52 192-168-3-193 Keepalived_vrrp[225719]: Assigned address 192.160.200.3 for interface eth0
Nov 14 14:47:52 192-168-3-193 Keepalived_vrrp[225719]: Assigned address fe80::f8a0:e0ff:fee8:b800 for interface eth0
Nov 14 14:47:52 192-168-3-193 Keepalived_vrrp[225719]: Registering gratuitous ARP shared channel
Nov 14 14:47:52 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) removing VIPs.
Nov 14 14:47:52 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) Entering BACKUP STATE (init)
Nov 14 14:47:52 192-168-3-193 Keepalived_vrrp[225719]: VRRP sockpool: [ifindex(2), family(IPv4), proto(112), unicast(1), fd(11,12)]
Nov 14 14:47:52 192-168-3-193 Keepalived_vrrp[225719]: VRRP_Script(chk_proxysql_port) succeeded
Nov 14 14:51:04 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) Backup received priority 0 advertisement
Nov 14 14:51:05 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) Receive advertisement timeout
Nov 14 14:51:05 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) Entering MASTER STATE
Nov 14 14:51:05 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) setting VIPs.
Nov 14 14:51:05 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:51:05 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.160.100.3
Nov 14 14:51:05 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:51:05 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:51:05 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:51:05 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:51:06 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) Master received advert from 192.160.220.118 with same priority 100 but higher IP address than ours
Nov 14 14:51:06 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) Entering BACKUP STATE
Nov 14 14:51:06 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) removing VIPs.
Nov 14 14:51:51 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) received lower priority (50) advert from 192.160.220.118 - discarding
Nov 14 14:51:52 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) received lower priority (50) advert from 192.160.220.118 - discarding
Nov 14 14:51:53 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) received lower priority (50) advert from 192.160.220.118 - discarding
Nov 14 14:51:54 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) Receive advertisement timeout
Nov 14 14:51:54 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) Entering MASTER STATE
Nov 14 14:51:54 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) setting VIPs.
Nov 14 14:51:54 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:51:54 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.160.100.3
Nov 14 14:51:54 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:51:54 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:51:54 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:51:54 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:51:59 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:51:59 192-168-3-193 Keepalived_vrrp[225719]: (VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.160.100.3
Nov 14 14:51:59 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:51:59 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:51:59 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:51:59 192-168-3-193 Keepalived_vrrp[225719]: Sending gratuitous ARP on eth0 for 192.160.100.3
节点 192.160.220.118开启后主降为备
关掉服务触发
Nov 14 14:47:51 192-160-220-118 Keepalived[20622]: Starting Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Nov 14 14:47:51 192-160-220-118 Keepalived[20622]: Opening file '/etc/keepalived/keepalived.conf'.
Nov 14 14:47:51 192-160-220-118 Keepalived[20623]: Starting Healthcheck child process, pid=20624
Nov 14 14:47:51 192-160-220-118 Keepalived[20623]: Starting VRRP child process, pid=20625
Nov 14 14:47:51 192-160-220-118 Keepalived_vrrp[20625]: Registering Kernel netlink reflector
Nov 14 14:47:51 192-160-220-118 Keepalived_vrrp[20625]: Registering Kernel netlink command channel
Nov 14 14:47:51 192-160-220-118 Keepalived_vrrp[20625]: Registering gratuitous ARP shared channel
Nov 14 14:47:51 192-160-220-118 Keepalived_vrrp[20625]: Opening file '/etc/keepalived/keepalived.conf'.
Nov 14 14:47:51 192-160-220-118 Keepalived_vrrp[20625]: VRRP_Instance(VI_1) removing protocol VIPs.
Nov 14 14:47:51 192-160-220-118 Keepalived_vrrp[20625]: Using LinkWatch kernel netlink reflector...
Nov 14 14:47:51 192-160-220-118 Keepalived_vrrp[20625]: VRRP sockpool: [ifindex(2), proto(112), unicast(1), fd(10,11)]
Nov 14 14:47:51 192-160-220-118 Keepalived_healthcheckers[20624]: Opening file '/etc/keepalived/keepalived.conf'.
Nov 14 14:47:51 192-160-220-118 Keepalived_vrrp[20625]: VRRP_Script(chk_proxysql_port) succeeded
Nov 14 14:47:51 192-160-220-118 Keepalived_vrrp[20625]: VRRP_Instance(VI_1) Transition to MASTER STATE
Nov 14 14:47:52 192-160-220-118 Keepalived_vrrp[20625]: VRRP_Instance(VI_1) Entering MASTER STATE
Nov 14 14:47:52 192-160-220-118 Keepalived_vrrp[20625]: VRRP_Instance(VI_1) setting protocol VIPs.
Nov 14 14:47:52 192-160-220-118 Keepalived_vrrp[20625]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:47:52 192-160-220-118 Keepalived_vrrp[20625]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.160.100.3
Nov 14 14:47:52 192-160-220-118 Keepalived_vrrp[20625]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:47:52 192-160-220-118 Keepalived_vrrp[20625]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:47:52 192-160-220-118 Keepalived_vrrp[20625]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:47:52 192-160-220-118 Keepalived_vrrp[20625]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:47:57 192-160-220-118 Keepalived_vrrp[20625]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:47:57 192-160-220-118 Keepalived_vrrp[20625]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.160.100.3
Nov 14 14:47:57 192-160-220-118 Keepalived_vrrp[20625]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:47:57 192-160-220-118 Keepalived_vrrp[20625]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:47:57 192-160-220-118 Keepalived_vrrp[20625]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:47:57 192-160-220-118 Keepalived_vrrp[20625]: Sending gratuitous ARP on eth0 for 192.160.100.3
Nov 14 14:48:34 192-160-220-118 Keepalived_vrrp[20625]: /etc/keepalived/check_proxysql.sh exited with status 1
Nov 14 14:48:35 192-160-220-118 Keepalived_vrrp[20625]: /etc/keepalived/check_proxysql.sh exited with status 1
Nov 14 14:48:35 192-160-220-118 Keepalived_vrrp[20625]: VRRP_Script(chk_proxysql_port) failed
Nov 14 14:48:36 192-160-220-118 Keepalived_vrrp[20625]: VRRP_Instance(VI_1) Changing effective priority from 100 to 50
Nov 14 14:48:36 192-160-220-118 Keepalived_vrrp[20625]: /etc/keepalived/check_proxysql.sh exited with status 1
Nov 14 14:48:37 192-160-220-118 Keepalived_vrrp[20625]: /etc/keepalived/check_proxysql.sh exited with status 1
Nov 14 14:48:38 192-160-220-118 Keepalived_vrrp[20625]: /etc/keepalived/check_proxysql.sh exited with status 1
Nov 14 14:48:39 192-160-220-118 Keepalived_vrrp[20625]: /etc/keepalived/check_proxysql.sh exited with status 1
Nov 14 14:48:39 192-160-220-118 Keepalived_vrrp[20625]: VRRP_Instance(VI_1) Received advert with higher priority 100, ours 50
Nov 14 14:48:39 192-160-220-118 Keepalived_vrrp[20625]: VRRP_Instance(VI_1) Entering BACKUP STATE
Nov 14 14:48:39 192-160-220-118 Keepalived_vrrp[20625]: VRRP_Instance(VI_1) removing protocol VIPs.
Nov 14 14:48:40 192-160-220-118 Keepalived_vrrp[20625]: /etc/keepalived/check_proxysql.sh exited with status 1
Nov 14 14:48:41 192-160-220-118 Keepalived_vrrp[20625]: /etc/keepalived/check_proxysql.sh exited with status 1
Nov 14 14:48:42 192-160-220-118 Keepalived_vrrp[20625]: /etc/keepalived/check_proxysql.sh exited with status 1
Nov 14 14:48:43 192-160-220-118 Keepalived_vrrp[20625]: /etc/keepalived/check_proxysql.sh exited with status 1
Nov 14 14:48:44 192-160-220-118 Keepalived_vrrp[20625]: /etc/keepalived/check_proxysql.sh exited with status 1
Nov 14 14:48:45 192-160-220-118 Keepalived_vrrp[20625]: /etc/keepalived/check_proxysql.sh exited with status 1