Chinaunix
标题:
Keepalived+Redis双机热备无法正确绑定VIP
[打印本页]
作者:
zrweng
时间:
2014-12-04 10:34
标题:
Keepalived+Redis双机热备无法正确绑定VIP
本帖最后由 zrweng 于 2014-12-07 20:31 编辑
Ubuntu14.04 + Keepalived1.2.13
172.18.8.74 (master,vip:172.18.8.100)
172.18.8.73 (slave,vip:172.18.8.100)
通过keepalived 实现redis主从备份零切换
keepalived.conf配置文件,如下:
! Configuration File for keepalived
global_defs {
router_id ubuntu-lvs-74
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101 # 备机 优先级为100
authentication {
auth_type PASS
auth_pass redis
}
track_script {
chk_redis
}
virtual_ipaddress {
172.18.8.100/32
}
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_backup.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}
virtual_server 172.18.8.100 6379 {
delay_loop 1
lb_algo rr
lb_kind DR #NAT
#nat_mask 255.255.255.255
persistence_timeout 50
protocol TCP
real_server 172.18.8.74 6379 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 6379
}
}
real_server 172.18.8.73 6379 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 6379
}
}
}
复制代码
正确的结果应该是:
当前启动主keepalived时候,虚拟主机IP应该被主机监听绑定,备机不绑定虚拟IP;当主机keepalived宕掉时,备机进行绑定VIP
为什么我keepalived主备都会监听虚拟IP?
#sudo ip a
都是输出:
root@ubuntu-lvs-74:/opt/lvs# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
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
[b]inet 172.18.8.100/32 brd 172.18.8.100 scope global lo:0[/b]
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
复制代码
/etc/sysctl.conf 设置了:
net.ipv4.ip_nonlocal_bind = 1
VIP设置如下,ifconfig:
root@ubuntu-lvs-74:/opt/lvs# ifconfig
eth0 Link encap:Ethernet HWaddr 52:54:00:ee:21:49
inet addr:172.18.8.74 Bcast:172.18.8.127 Mask:255.255.255.128
inet6 addr: fe80::5054:ff:feee:2149/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3038779 errors:0 dropped:1356 overruns:0 frame:0
TX packets:564682 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:155424872 (155.4 MB) TX bytes:39263783 (39.2 MB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:703904 errors:0 dropped:0 overruns:0 frame:0
TX packets:703904 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:39418624 (39.4 MB) TX bytes:39418624 (39.4 MB)
lo:0 Link encap:Local Loopback
inet addr:172.18.8.100 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:65536 Metric:1
复制代码
请教,这是哪里出了问题了?
作者:
zrweng
时间:
2014-12-04 15:13
是否需要配置LVS?
作者:
苏州微信
时间:
2014-12-05 15:34
提示:
作者被禁止或删除 内容自动屏蔽
作者:
二货康康
时间:
2014-12-05 15:38
主备 都是配的 master ??
作者:
二货康康
时间:
2014-12-05 15:41
一般是 主备都配成 slave 然后配不同的优先级 优先级高的再配一个nopreempt
作者:
zrweng
时间:
2014-12-05 17:07
keepalived.conf配置就如上,从/var/log/keepalived.log日志看到master/slave是有效的
master keepalived日志
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived[1155]: Starting Keepalived v1.2.13 (12/02,2014)
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived[1156]: Starting Healthcheck child process, pid=1158
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived[1156]: Starting VRRP child process, pid=1159
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Netlink reflector reports IP 172.18.8.74 added
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Netlink reflector reports IP fe80::5054:ff:feee:2149 added
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Registering Kernel netlink reflector
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Registering Kernel netlink command channel
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Registering gratuitous ARP shared channel
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Opening file '/etc/keepalived/keepalived.conf'.
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Initializing ipvs 2.6
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Configuration is using : 61452 Bytes
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Using LinkWatch kernel netlink reflector...
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Netlink reflector reports IP 172.18.8.74 added
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Netlink reflector reports IP fe80::5054:ff:feee:2149 added
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Registering Kernel netlink reflector
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Registering Kernel netlink command channel
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Opening file '/etc/keepalived/keepalived.conf'.
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Configuration is using : 13705 Bytes
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Using LinkWatch kernel netlink reflector...
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Activating healthchecker for service [172.18.8.74]:6379
Dec 5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Activating healthchecker for service [172.18.8.73]:6379
Dec 5 16:29:57 ubuntu-lvs-74 Keepalived_vrrp[1159]: VRRP_Instance(VI_1{) Transition to MASTER STATE
Dec 5 16:29:58 ubuntu-lvs-74 Keepalived_vrrp[1159]: VRRP_Instance(VI_1{) Entering MASTER STATE
Dec 5 16:30:05 ubuntu-lvs-74 Keepalived_vrrp[1159]: VRRP_Instance(VI_1{) Received lower prio advert, forcing new election
复制代码
从keepalived日志
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived[1153]: Starting Keepalived v1.2.13 (12/02,2014)
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived[1154]: Starting Healthcheck child process, pid=1156
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived[1154]: Starting VRRP child process, pid=1157
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Netlink reflector reports IP 172.18.8.73 added
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Netlink reflector reports IP fe80::5054:ff:fe6d:9c66 added
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Registering Kernel netlink reflector
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Registering Kernel netlink command channel
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Registering gratuitous ARP shared channel
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Opening file '/etc/keepalived/keepalived.conf'.
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Configuration is using : 61452 Bytes
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Using LinkWatch kernel netlink reflector...
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Initializing ipvs 2.6
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Netlink reflector reports IP 172.18.8.73 added
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Netlink reflector reports IP fe80::5054:ff:fe6d:9c66 added
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Registering Kernel netlink reflector
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Registering Kernel netlink command channel
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Opening file '/etc/keepalived/keepalived.conf'.
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Configuration is using : 13705 Bytes
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Using LinkWatch kernel netlink reflector...
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Activating healthchecker for service [172.18.8.74]:6379
Dec 5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Activating healthchecker for service [172.18.8.73]:6379
Dec 5 16:30:05 ubuntu-lvs-73 Keepalived_vrrp[1157]: VRRP_Instance(VI_1{) Transition to MASTER STATE
Dec 5 16:30:05 ubuntu-lvs-73 Keepalived_vrrp[1157]: VRRP_Instance(VI_1{) Received higher prio advert
Dec 5 16:30:05 ubuntu-lvs-73 Keepalived_vrrp[1157]: VRRP_Instance(VI_1{) Entering BACKUP STATE
复制代码
参考文章:heylinux.com/archives/1942.html
现在遇到的问题:
1、keepalived主备都会监听虚拟IP?
VIP创建代码:
#!/bin/bash
vip=172.18.8.100
ifconfig lo:0 $vip netmask 255.255.255.255 broadcast $vip up
route add -host $vip dev lo:0 #add router
#echo "0" >/proc/sys/net/ipv4/ip_forward #close real server ip forward
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
复制代码
root@ubuntu-lvs-73:/var/lock# ip a #执行查看VIP绑定,结果主备都一样
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
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
inet 172.18.8.100/32 brd 172.18.8.100 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2、keepalived.conf中vrrp_script,vrrp_instance中的shell脚本不执行(sh脚本运行会输出日志,但实际没有发现日志文件)
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval 2
}
vrrp_instance VI_1{
state MASTER
interface eth0
virtual_router_id 51
priority 101
authentication{
auth_type PASS
auth_pass redis
}
track_script{
chk_redis
}
virtual_ipaddress{
172.18.8.100
}
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_backup.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}
复制代码
作者:
zrweng
时间:
2014-12-05 17:27
补充,所有的sh脚本都加了执行权限 chmod 775 *.sh
作者:
zrweng
时间:
2014-12-05 18:02
找到keepalived中的sh脚本无法执行的原因了:
配置文件格式一定要注意,比如:track_script {,在“track_script”和“{”之间一定要加空格
,调试了很久,非常郁闷!
作者:
zrweng
时间:
2014-12-05 19:18
本帖最后由 zrweng 于 2014-12-07 20:35 编辑
还有个问题,keepalived启动后,出现FALUT
Dec 5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Netlink reflector reports IP fe80::5054:ff:fe6d:9c66 added
Dec 5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Registering Kernel netlink reflector
Dec 5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Registering Kernel netlink command channel
Dec 5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Opening file '/etc/keepalived/keepalived.conf'.
Dec 5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Configuration is using : 13617 Bytes
Dec 5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Using LinkWatch kernel netlink reflector...
Dec 5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Activating healthchecker for service [172.18.8.74]:6379
Dec 5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Activating healthchecker for service [172.18.8.73]:6379
Dec 5 19:04:54 ubuntu-lvs-73 Keepalived_vrrp[2123]: VRRP_Instance(VI_1) Now in FAULT state
Dec 5 19:04:54 ubuntu-lvs-73 Keepalived_vrrp[2123]: Opening script file /etc/keepalived/scripts/redis_fault.sh
复制代码
ifconfig
root@ubuntu-lvs-73:/opt/lvs# ifconfig
eth0 Link encap:Ethernet HWaddr 52:54:00:6d:9c:66
inet addr:172.18.8.73 Bcast:172.18.8.127 Mask:255.255.255.128
inet6 addr: fe80::5054:ff:fe6d:9c66/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:19755 errors:0 dropped:9 overruns:0 frame:0
TX packets:2741 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1031508 (1.0 MB) TX bytes:315069 (315.0 KB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:1088 errors:0 dropped:0 overruns:0 frame:0
TX packets:1088 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:60928 (60.9 KB) TX bytes:60928 (60.9 KB)
lo:0 Link encap:Local Loopback
inet addr:172.18.8.100 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:65536 Metric:1
复制代码
请教,VRRP_Instance(VI_1) Now in FAULT state 可能是哪方面原因造成的?
作者:
zrweng
时间:
2014-12-07 17:49
route信息
Destination Gateway Genmask Flags Metric Ref Use Iface
default 172.18.8.1 0.0.0.0 UG 0 0 0 eth0
172.18.8.0 * 255.255.255.128 U 0 0 0 eth0
172.18.8.100 * 255.255.255.255 UH 0 0 0 lo
复制代码
VIP:172.18.8.100 ,在73,74之间可以互相ping通
作者:
zrweng
时间:
2014-12-08 10:35
本帖最后由 zrweng 于 2014-12-08 10:37 编辑
最新验证的结果是:
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval 2
}
复制代码
script "/etc/keepalived/scripts/redis_check.sh" 有这句就会报 VRRP_Instance(VI_1) Now in FAULT state
但是,redis_check.sh测试没问题,,郁闷!
作者:
zrweng
时间:
2014-12-08 11:06
经过测试验证:redis_check.sh脚本有误!!!
几乎99%网络资料,都是这么写的
ALIVE=`/opt/redis/bin/redis-cli PING`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi
复制代码
实际上ALIVE=`/opt/redis/bin/redis-cli PING` 应该是
ALIVE=$(/opt/redis/bin/redis-cli PING)
作者:
zbhdpx
时间:
2014-12-10 14:55
实际上ALIVE=`/opt/redis/bin/redis-cli PING` 应该是 ALIVE=$(/opt/redis/bin/redis-cli PING)
这两个只是写法不同,实际上是都可以,只是推荐使用第二种写法。
作者:
llxling0608
时间:
2015-03-18 15:46
您好,我最近也在keepalived双机热备,也遇到和您同样的问题,请问您:keepalived主备都会监听虚拟IP?这个问题是怎么解决的?谢谢~~
回复
1#
zrweng
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2