免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 20093 | 回复: 13

Keepalived+Redis双机热备无法正确绑定VIP [复制链接]

论坛徽章:
0
发表于 2014-12-04 10:34 |显示全部楼层
本帖最后由 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配置文件,如下:
  1. ! Configuration File for keepalived

  2. global_defs {
  3.    router_id ubuntu-lvs-74
  4. }

  5. vrrp_script chk_redis {
  6.     script "/etc/keepalived/scripts/redis_check.sh"
  7.     interval 2
  8. }

  9. vrrp_instance VI_1 {
  10.     state MASTER
  11.     interface eth0
  12.     virtual_router_id 51
  13.     priority 101 #  备机 优先级为100

  14.     authentication {
  15.         auth_type PASS
  16.         auth_pass redis
  17.     }

  18.     track_script {
  19.        chk_redis
  20.     }

  21.     virtual_ipaddress {
  22.        172.18.8.100/32
  23.     }

  24.     notify_master /etc/keepalived/scripts/redis_master.sh
  25.     notify_backup /etc/keepalived/scripts/redis_backup.sh
  26.     notify_fault  /etc/keepalived/scripts/redis_fault.sh
  27.     notify_stop   /etc/keepalived/scripts/redis_stop.sh

  28. }

  29. virtual_server 172.18.8.100 6379 {
  30.     delay_loop 1
  31.     lb_algo rr
  32.     lb_kind DR #NAT
  33.     #nat_mask 255.255.255.255
  34.     persistence_timeout 50
  35.     protocol TCP

  36.     real_server 172.18.8.74 6379 {
  37.         weight 1
  38.         TCP_CHECK {
  39.           connect_timeout 3
  40.           nb_get_retry 3
  41.           delay_before_retry 3
  42.           connect_port 6379
  43.         }
  44.     }

  45.     real_server 172.18.8.73 6379 {
  46.         weight 1
  47.         TCP_CHECK {
  48.           connect_timeout 3
  49.           nb_get_retry 3
  50.           delay_before_retry 3
  51.           connect_port 6379
  52.         }
  53.     }
  54. }
复制代码
正确的结果应该是:
当前启动主keepalived时候,虚拟主机IP应该被主机监听绑定,备机不绑定虚拟IP;当主机keepalived宕掉时,备机进行绑定VIP

为什么我keepalived主备都会监听虚拟IP?
#sudo ip a
都是输出:
  1. root@ubuntu-lvs-74:/opt/lvs# ip a
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
  3.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4.     inet 127.0.0.1/8 scope host lo
  5.        valid_lft forever preferred_lft forever
  6.     [b]inet 172.18.8.100/32 brd 172.18.8.100 scope global lo:0[/b]
  7.        valid_lft forever preferred_lft forever
  8.     inet6 ::1/128 scope host
复制代码
/etc/sysctl.conf 设置了:
net.ipv4.ip_nonlocal_bind = 1

VIP设置如下,ifconfig:
  1. root@ubuntu-lvs-74:/opt/lvs# ifconfig
  2. eth0      Link encap:Ethernet  HWaddr 52:54:00:ee:21:49  
  3.           inet addr:172.18.8.74  Bcast:172.18.8.127  Mask:255.255.255.128
  4.           inet6 addr: fe80::5054:ff:feee:2149/64 Scope:Link
  5.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
  6.           RX packets:3038779 errors:0 dropped:1356 overruns:0 frame:0
  7.           TX packets:564682 errors:0 dropped:0 overruns:0 carrier:0
  8.           collisions:0 txqueuelen:1000
  9.           RX bytes:155424872 (155.4 MB)  TX bytes:39263783 (39.2 MB)

  10. lo        Link encap:Local Loopback  
  11.           inet addr:127.0.0.1  Mask:255.0.0.0
  12.           inet6 addr: ::1/128 Scope:Host
  13.           UP LOOPBACK RUNNING  MTU:65536  Metric:1
  14.           RX packets:703904 errors:0 dropped:0 overruns:0 frame:0
  15.           TX packets:703904 errors:0 dropped:0 overruns:0 carrier:0
  16.           collisions:0 txqueuelen:0
  17.           RX bytes:39418624 (39.4 MB)  TX bytes:39418624 (39.4 MB)

  18. lo:0      Link encap:Local Loopback  
  19.           inet addr:172.18.8.100  Mask:255.255.255.255
  20.           UP LOOPBACK RUNNING  MTU:65536  Metric:1
复制代码
请教,这是哪里出了问题了?

论坛徽章:
0
发表于 2014-12-04 15:13 |显示全部楼层
是否需要配置LVS?

论坛徽章:
0
发表于 2014-12-05 15:34 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
发表于 2014-12-05 15:38 |显示全部楼层
主备 都是配的 master  ??

论坛徽章:
0
发表于 2014-12-05 15:41 |显示全部楼层
一般是  主备都配成 slave   然后配不同的优先级   优先级高的再配一个nopreempt

论坛徽章:
0
发表于 2014-12-05 17:07 |显示全部楼层
keepalived.conf配置就如上,从/var/log/keepalived.log日志看到master/slave是有效的
master keepalived日志
  1. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived[1155]: Starting Keepalived v1.2.13 (12/02,2014)
  2. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived[1156]: Starting Healthcheck child process, pid=1158
  3. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived[1156]: Starting VRRP child process, pid=1159
  4. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Netlink reflector reports IP 172.18.8.74 added
  5. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Netlink reflector reports IP fe80::5054:ff:feee:2149 added
  6. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Registering Kernel netlink reflector
  7. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Registering Kernel netlink command channel
  8. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Registering gratuitous ARP shared channel
  9. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Opening file '/etc/keepalived/keepalived.conf'.
  10. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Initializing ipvs 2.6
  11. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Configuration is using : 61452 Bytes
  12. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: Using LinkWatch kernel netlink reflector...
  13. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_vrrp[1159]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
  14. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Netlink reflector reports IP 172.18.8.74 added
  15. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Netlink reflector reports IP fe80::5054:ff:feee:2149 added
  16. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Registering Kernel netlink reflector
  17. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Registering Kernel netlink command channel
  18. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Opening file '/etc/keepalived/keepalived.conf'.
  19. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Configuration is using : 13705 Bytes
  20. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Using LinkWatch kernel netlink reflector...
  21. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Activating healthchecker for service [172.18.8.74]:6379
  22. Dec  5 16:29:56 ubuntu-lvs-74 Keepalived_healthcheckers[1158]: Activating healthchecker for service [172.18.8.73]:6379
  23. Dec  5 16:29:57 ubuntu-lvs-74 Keepalived_vrrp[1159]: VRRP_Instance(VI_1{) Transition to MASTER STATE
  24. Dec  5 16:29:58 ubuntu-lvs-74 Keepalived_vrrp[1159]: VRRP_Instance(VI_1{) Entering MASTER STATE
  25. Dec  5 16:30:05 ubuntu-lvs-74 Keepalived_vrrp[1159]: VRRP_Instance(VI_1{) Received lower prio advert, forcing new election
复制代码
从keepalived日志
  1. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived[1153]: Starting Keepalived v1.2.13 (12/02,2014)
  2. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived[1154]: Starting Healthcheck child process, pid=1156
  3. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived[1154]: Starting VRRP child process, pid=1157
  4. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Netlink reflector reports IP 172.18.8.73 added
  5. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Netlink reflector reports IP fe80::5054:ff:fe6d:9c66 added
  6. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Registering Kernel netlink reflector
  7. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Registering Kernel netlink command channel
  8. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Registering gratuitous ARP shared channel
  9. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Opening file '/etc/keepalived/keepalived.conf'.
  10. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Configuration is using : 61452 Bytes
  11. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: Using LinkWatch kernel netlink reflector...
  12. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_vrrp[1157]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
  13. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Initializing ipvs 2.6
  14. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Netlink reflector reports IP 172.18.8.73 added
  15. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Netlink reflector reports IP fe80::5054:ff:fe6d:9c66 added
  16. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Registering Kernel netlink reflector
  17. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Registering Kernel netlink command channel
  18. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Opening file '/etc/keepalived/keepalived.conf'.
  19. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Configuration is using : 13705 Bytes
  20. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Using LinkWatch kernel netlink reflector...
  21. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Activating healthchecker for service [172.18.8.74]:6379
  22. Dec  5 16:30:04 ubuntu-lvs-73 Keepalived_healthcheckers[1156]: Activating healthchecker for service [172.18.8.73]:6379
  23. Dec  5 16:30:05 ubuntu-lvs-73 Keepalived_vrrp[1157]: VRRP_Instance(VI_1{) Transition to MASTER STATE
  24. Dec  5 16:30:05 ubuntu-lvs-73 Keepalived_vrrp[1157]: VRRP_Instance(VI_1{) Received higher prio advert
  25. 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创建代码:
  1. #!/bin/bash
  2. vip=172.18.8.100
  3. ifconfig lo:0 $vip netmask 255.255.255.255 broadcast $vip up
  4. route add -host $vip dev lo:0  #add router
  5. #echo "0" >/proc/sys/net/ipv4/ip_forward #close real server ip forward
  6. echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  7. echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
  8. echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
  9. 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脚本运行会输出日志,但实际没有发现日志文件)
  1. vrrp_script chk_redis {
  2.     script "/etc/keepalived/scripts/redis_check.sh"
  3.     interval 2
  4. }

  5. vrrp_instance VI_1{
  6.     state MASTER
  7.     interface eth0
  8.     virtual_router_id 51
  9.     priority 101
  10.     authentication{
  11.         auth_type PASS
  12.         auth_pass redis
  13.     }
  14.     track_script{
  15.        chk_redis
  16.     }
  17.     virtual_ipaddress{
  18.        172.18.8.100
  19.     }
  20.     notify_master /etc/keepalived/scripts/redis_master.sh
  21.     notify_backup /etc/keepalived/scripts/redis_backup.sh
  22.     notify_fault  /etc/keepalived/scripts/redis_fault.sh
  23.     notify_stop   /etc/keepalived/scripts/redis_stop.sh

  24. }
复制代码

论坛徽章:
0
发表于 2014-12-05 17:27 |显示全部楼层
补充,所有的sh脚本都加了执行权限 chmod 775 *.sh

论坛徽章:
0
发表于 2014-12-05 18:02 |显示全部楼层
找到keepalived中的sh脚本无法执行的原因了:

配置文件格式一定要注意,比如:track_script   {,在“track_script”和“{”之间一定要加空格 ,调试了很久,非常郁闷!

论坛徽章:
0
发表于 2014-12-05 19:18 |显示全部楼层
本帖最后由 zrweng 于 2014-12-07 20:35 编辑

还有个问题,keepalived启动后,出现FALUT
  1. Dec  5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Netlink reflector reports IP fe80::5054:ff:fe6d:9c66 added
  2. Dec  5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Registering Kernel netlink reflector
  3. Dec  5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Registering Kernel netlink command channel
  4. Dec  5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Opening file '/etc/keepalived/keepalived.conf'.
  5. Dec  5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Configuration is using : 13617 Bytes
  6. Dec  5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Using LinkWatch kernel netlink reflector...
  7. Dec  5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Activating healthchecker for service [172.18.8.74]:6379
  8. Dec  5 19:04:53 ubuntu-lvs-73 Keepalived_healthcheckers[2122]: Activating healthchecker for service [172.18.8.73]:6379
  9. Dec  5 19:04:54 ubuntu-lvs-73 Keepalived_vrrp[2123]: VRRP_Instance(VI_1) Now in FAULT state
  10. Dec  5 19:04:54 ubuntu-lvs-73 Keepalived_vrrp[2123]: Opening script file /etc/keepalived/scripts/redis_fault.sh
复制代码
ifconfig
  1. root@ubuntu-lvs-73:/opt/lvs# ifconfig
  2. eth0      Link encap:Ethernet  HWaddr 52:54:00:6d:9c:66  
  3.           inet addr:172.18.8.73  Bcast:172.18.8.127  Mask:255.255.255.128
  4.           inet6 addr: fe80::5054:ff:fe6d:9c66/64 Scope:Link
  5.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
  6.           RX packets:19755 errors:0 dropped:9 overruns:0 frame:0
  7.           TX packets:2741 errors:0 dropped:0 overruns:0 carrier:0
  8.           collisions:0 txqueuelen:1000
  9.           RX bytes:1031508 (1.0 MB)  TX bytes:315069 (315.0 KB)

  10. lo        Link encap:Local Loopback  
  11.           inet addr:127.0.0.1  Mask:255.0.0.0
  12.           inet6 addr: ::1/128 Scope:Host
  13.           UP LOOPBACK RUNNING  MTU:65536  Metric:1
  14.           RX packets:1088 errors:0 dropped:0 overruns:0 frame:0
  15.           TX packets:1088 errors:0 dropped:0 overruns:0 carrier:0
  16.           collisions:0 txqueuelen:0
  17.           RX bytes:60928 (60.9 KB)  TX bytes:60928 (60.9 KB)

  18. lo:0      Link encap:Local Loopback  
  19.           inet addr:172.18.8.100  Mask:255.255.255.255
  20.           UP LOOPBACK RUNNING  MTU:65536  Metric:1
复制代码
请教,VRRP_Instance(VI_1) Now in FAULT state 可能是哪方面原因造成的?

论坛徽章:
0
发表于 2014-12-07 17:49 |显示全部楼层
route信息

  1. Destination     Gateway         Genmask              Flags  Metric Ref    Use Iface
  2. default            172.18.8.1      0.0.0.0                UG     0      0        0    eth0
  3. 172.18.8.0      *                    255.255.255.128 U       0      0        0    eth0
  4. 172.18.8.100  *                    255.255.255.255 UH     0      0        0     lo
复制代码
VIP:172.18.8.100 ,在73,74之间可以互相ping通
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP