下载Keepalived稳定版本
在目标机器解压并安装
tar -zxvf keepalived-2.2.7.tar.gz
./configure --prefix=/usr/local/keepalived
yum -y install libnl libnl-devel # 用于ipvs和ipv6支持
make && make install
主节点配置
vi /etc/keepalived/keepalived.conf
---------------------------------------------------------------------------------------
! Configuration File for keepalived
global_defs {
router_id 33.2.0.4 # 唯一值,一般取本机host
}
vrrp_script checkNginx {
script "/data/keepalived/check_nginx.sh"
interval 10
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 24
priority 100 # 优先级,主节点值要高于备节点
advert_int 2
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
33.2.0.16 dev eth0 label eth0:21 # 虚拟ip
}
track_script {
checkNginx
}
unicast_peer {
33.2.0.5 # 单播目标节点ip
}
}
备节点配置
vi /etc/keepalived/keepalived.conf
---------------------------------------------------------------------------------------
! Configuration File for keepalived
global_defs {
router_id 33.2.0.5 # 唯一值,一般取本机host
}
vrrp_script checkNginx {
script "/data/keepalived/check_nginx.sh"
interval 10
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 24
priority 90 # 优先级,主节点值要高于备节点
advert_int 2
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
33.2.0.16 dev eth0 label eth0:21 # 虚拟ip
}
track_script {
checkNginx
}
unicast_peer {
33.2.0.4 # 单播目标节点ip
}
}
checkNginx脚本
#!/bin/bash
log_file=/data/keepalived/check_nginx.log
cur_date=`date '+%x %X'`
port=5672
listen_port=`netstat -plnt|grep ":$port "`
if [ "$listen_port" == "" ]
then
echo "$cur_date: port $port hasn't been listened." >> $log_file
else
echo "$cur_date: port $port is OK." >> $log_file
exit 0;
fi
exit 1;
验证服务
- 启动服务
systemctl start keepalived.service
systemctl status keepalived.service
systemctl enable keepalived.service
- 验证是否只在主备间单播
tcpdump -i eth0 -nn -p vrrp
------------------------------------------------------------------------------------------
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
19:28:41.132892 IP 33.2.0.4 > 33.2.0.5: VRRPv2, Advertisement, vrid 24, prio 100, authtype simple, intvl 2s, length 20
19:28:43.132926 IP 33.2.0.4 > 33.2.0.5: VRRPv2, Advertisement, vrid 24, prio 100, authtype simple, intvl 2s, length 20
19:28:45.132991 IP 33.2.0.4 > 33.2.0.5: VRRPv2, Advertisement, vrid 24, prio 100, authtype simple, intvl 2s, length 20
- 验证vip正确性
# 主节点执行
ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether fa:16:3e:ac:2a:7d brd ff:ff:ff:ff:ff:ff
inet 33.2.0.4/16 brd 33.2.255.255 scope global dynamic eth0
valid_lft 314740552sec preferred_lft 314740552sec
inet 33.2.0.16/32 scope global eth0:21 # 可见vip漂移在主节点
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:feac:2a7d/64 scope link
valid_lft forever preferred_lft forever
-----------------------------------------------------------------------------------------
# 备节点执行
ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether fa:16:3e:3a:a9:e6 brd ff:ff:ff:ff:ff:ff
inet 33.2.0.5/16 brd 33.2.255.255 scope global dynamic eth0
valid_lft 314740675sec preferred_lft 314740675sec
inet6 fe80::f816:3eff:fe3a:a9e6/64 scope link
valid_lft forever preferred_lft forever
-----------------------------------------------------------------------------------------
# kill主节点keepalived服务,可见vip已不再主节点网卡
systemctl stop keepalived.service
ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether fa:16:3e:ac:2a:7d brd ff:ff:ff:ff:ff:ff
inet 33.2.0.4/16 brd 33.2.255.255 scope global dynamic eth0
valid_lft 314740349sec preferred_lft 314740349sec
inet6 fe80::f816:3eff:feac:2a7d/64 scope link
valid_lft forever preferred_lft forever
-----------------------------------------------------------------------------------------
# 备节点查看ip
ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether fa:16:3e:3a:a9:e6 brd ff:ff:ff:ff:ff:ff
inet 33.2.0.5/16 brd 33.2.255.255 scope global dynamic eth0
valid_lft 314740481sec preferred_lft 314740481sec
inet 33.2.0.16/32 scope global eth0:21 # 可见vip漂移在备节点
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:fe3a:a9e6/64 scope link
valid_lft forever preferred_lft forever