环境示例:

[root@localhost ~]# uname -a
Linux localhost.localdomain 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
内存:512M 硬盘:20G
操作系统:Linux Cent OS 6.6 64位
软件: 略
依赖rpm包:略

  1. rpm -ivh keyutils-libs-devel-1.4-5.el6.x86_64.rpm
  2. rpm -ivh openssl098e-0.9.8e-18.el6_5.2.x86_64.rpm
  3. rpm -ivh perl-WWW-Curl-4.09-4.el6.x86_64.rpm
  4. rpm -ivh zlib-devel-1.2.3-29.el6.x86_64.rpm
  5. rpm -e e2fsprogs-libs-1.41.12-21.el6.x86_64 --nodeps
  6. rpm -ivh e2fsprogs-libs-1.41.12-22.el6.x86_64.rpm
  7. e2fsprogs-libs-1.41.12-22.el6.x86_64.rpm
  8. rpm -e libcom_err-1.41.12-21.el6.x86_64 --nodeps
  9. rpm -ivh libcom_err-1.41.12-22.el6.x86_64.rpm
  10. --->libcom_err-1.41.12-22.el6.x86_64.rpm
  11. rpm -e e2fsprogs-1.41.12-21.el6.x86_64 --nodeps
  12. rpm -ivh e2fsprogs-1.41.12-22.el6.x86_64.rpm
  13. e2fsprogs-1.41.12-22.el6.x86_64.rpm
  14. rpm -e libss-1.41.12-21.el6.x86_64 --nodeps
  15. rpm -ivh libss-1.41.12-22.el6.x86_64.rpm
  16. --->libss-1.41.12-22.el6.x86_64.rpm
  17. rpm -e krb5-libs-1.10.3-33.el6.x86_64 --nodeps
  18. rpm -ivh krb5-libs-1.10.3-42.el6.x86_64.rpm
  19. krb5-devel-1.10.3-42.el6.x86_64.rpm
  20. libcom_err-devel
  21. rpm -ivh libcom_err-devel-1.41.12-22.el6.x86_64.rpm
  22. libselinux-devel
  23. libsepol-devel
  24. rpm -ivh libsepol-devel-2.0.41-4.el6.x86_64.rpm
  25. rpm -ivh libselinux-devel-2.0.94-5.8.el6.x86_64.rpm
  26. rpm -e openssl-1.0.1e-30.el6.x86_64 --nodeps
  27. rpm -ivh openssl-1.0.1e-42.el6_7.2.x86_64.rpm
  28. rpm -ivh krb5-devel-1.10.3-42.el6.x86_64.rpm
  29. rpm -ivh openssl-devel-1.0.1e-42.el6_7.2.x86_64.rpm
  30. rpm -ivh openssl-perl-1.0.1e-42.el6_7.2.x86_64.rpm
  31. rpm -ivh openssl-static-1.0.1e-42.el6_7.2.x86_64.rpm
  32. rpm -ivh ipvsadm-1.26-4.el6.x86_64.rpm
  33. rpm -ivh libnl-devel-1.1.4-2.el6.x86_64.rpm
  34. rpm -ivh popt-devel-1.13-7.el6.x86_64.rpm
  35. rpm -ivh popt-static-1.13-7.el6.x86_64.rpm

安装Keeplived依赖

安装 openssl
yum install openssl -y
安装popt
yum install popt
-y
安装ipvsadm
yum install ipvsadm -y
安装libnl-dev
yum install libnl-dev* -y

安装Keepalived

keepalived安装包地址:
http://www.keepalived.org/software/keepalived-1.2.16.tar.gz
下载解压后编译配置
tar –zxvf keepalived-1.2.16.tar.gz
./configure —prefix=/usr/local/keepalived
编译配置需要确保一下几项为Yes状态:
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : Yes
Use VRRP Framework : Yes
然后就可以编译安装了:
make && make install
因为没有使用keepalived的默认路径安装(默认是/usr/local),安装完成之后,需要做一些工作:
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#复制keepalived启动文件到默认路径,也可以通过设置环境变量的path实现
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
#复制服务启动脚本到,以便可以通过service控制keepalived服务
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#复制keepalived服务脚本到默认的地址,也通过修改init.d/keepalived文件中的相应配置实现
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
#复制默认配置文件到默认路径,其实也可以在/etc/init.d/keepalived中设置路径
chkconfig keepalived on【可执行可不执行】
#开机启动服务

配置Keepalived

配置文件地址:/etc/keepalived/keepalived.conf
标识为红色的是需要跟修改的配置。详情请用ipconfig命令查看

主服务器

  1. global_defs
  2. {
  3. notification_email #通知email,根据实际情况配置
  4. {
  5. admin@example.com
  6. }
  7. notification_email_from admin@example.com
  8. smtp_server 127.0.0.1
  9. stmp_connect_timeout 30
  10. router_id node1 #节点名标识,主要用于通知中
  11. }
  12. vrrp_instance VI_NODE {
  13. state MASTER #配置为主服务器
  14. interface eth0 #通讯网卡
  15. virtual_router_id 100 #路由标识
  16. priority 200 #优先级,0-254
  17. advert_int 5 #通知间隔,实际部署时可以设置小一点,减少延时
  18. authentication {
  19. auth_type PASS
  20. auth_pass 123456 #验证密码,用于通讯主机间验证
  21. }
  22. virtual_ipaddress {
  23. 192.168.26.120 #虚拟ip,可以定义多个
  24. }
  25. }

从服务器

  1. global_defs {
  2. notification_email {
  3. admin@example.com
  4. }
  5. notification_email_from admin@example.com
  6. smtp_server 127.0.0.1
  7. stmp_connect_timeout 30
  8. router_id node2
  9. }
  10. vrrp_instance VI_NODE {
  11. state BACKUP #与主服务器对应
  12. interface eth0 #从服务器的通信网卡
  13. virtual_router_id 100 #路由标识,和主服务器相同
  14. priority 100 #优先级,小于主服务器即可
  15. advert_int 5 #这里是接受通知间隔,与主服务器要设置相同
  16. authentication {
  17. auth_type PASS
  18. auth_pass 123456 #验证密码,与主服务器相同
  19. }
  20. virtual_ipaddress {
  21. 192.168.26.120 #虚拟IP,也要和主服务器相同
  22. }
  23. }

上面的设置是最基础的设置,实现的功能是如果主服务器的Keepalived停止服务(一般情况下服务器宕机),则将虚拟IP切换至从服务器,主服务器恢复后从新切换回主服务器。

注意点:

  1. virtual_ipaddress必须配置一致

  2. adver_int的时间必须配置一致

  3. 当主重启启动的时候,必须将主的有限级降低一点。

启动Keepalived

  • 关闭防火墙

service iptables stop

  • 启动主的keepalived

service keepalived start

成功日志:

Jan 13 01:21:58 localhost Keepalived_healthcheckers[10544]: Netlink reflector reports IP 192.168.26.120 added
Jan 13 01:21:58 localhost Keepalived_vrrp[10545]: VRRP_Instance(VI_NODE) Sending gratuitous ARPs on eth0 for 192.168.26.120

查看IP是否绑定成功:
ip a show|awk ‘/inet\ /‘

如:
[root@localhost ~]# ip a show|awk ‘/inet\ /‘

  1. inet 127.0.0.1/8 scope host lo<br /> inet 192.168.26.130/24 brd 192.168.26.255 scope global eth0<br /> inet 192.168.26.120/32 scope global eth0

绑定成功!!!!

  • 启动从的keepalived

Service keepalived start

启动成功日志:

Jan 13 01:25:55 localhost Keepalived_vrrp[6115]: VRRP_Instance(VI_NODE) Sending gratuitous ARPs on eth0 for 192.168.26.120
Jan 13 01:25:55 localhost Keepalived_healthcheckers[6114]: Netlink reflector reports IP 192.168.26.120 added

  • 查看IP漂移

杀死主服务器:
Service keepalived stop | killall keepalived

主的日志:
Jan 13 01:25:54 localhost Keepalived_healthcheckers[10544]: Netlink reflector reports IP 192.168.26.120 removed
从的日志:
Sending gratuitous ARPs on eth0 for 192.168.26.120
Jan 13 01:25:55 localhost Keepalived_healthcheckers[6114]: Netlink reflector reports IP 192.168.26.120 added

  • 重新启动主keepalived不影响从运行

重新修改主的keepalived.conf文件
设置virtual_router_id的优先级小于从的优先级既可。
重启服务:service keepalived start

ISSUE

QA:./keepalived** –f **keepalived.conf导致启动的时候卡住

日志报错:
Jan 13 00:24:56 localhost Keepalived_healthcheckers[5596]: Using LinkWat
ch kernel netlink reflector…
日志停止

解决办法:后面keepalived.conf配置为绝对路径。

QA:** **采用绝对路径的方式执行过去有可能还会卡住。

Jan 13 00:36:11 localhost Keepalived_vrrp[10051]: VRRP_Instance(VI_NODE) Transition to MASTER STATE
Jan 13 00:36:12 localhost Keepalived_vrrp[10051]: VRRP_Instance(VI_NODE) Entering MASTER STATE

解决办法:采用服务的方式,service keepalived start。

QA:路由标识不一致时,导致启动失败。

Jan 13 00:37:34 localhost Keepalived_vrrp[5725]: ip address associated with VRID not present in received packet : 192.168.26.200
Jan 13 00:37:34 localhost Keepalived_vrrp[5725]: one or more VIP associated with VRID mismatch actual MASTER advert
Jan 13 00:37:34 localhost Keepalived_vrrp[5725]: bogus VRRP packet received on eth0 !!!
Jan 13 00:37:34 localhost Keepalived_vrrp[5725]: VRRP_Instance(VI_NODE) ignoring received advertisment…

解决办法:从服务器和主服务器之间的路由标识要配置相同。

比如
/etc/keepalived/keepalived.conf
virtual_router_id 60 主从方都要改,默认为51

QA:通知时间不一致,导致启动失败

Jan 13 00:50:30 localhost Keepalived_vrrp[5824]: VRRP_Instance(VI_NODE) Dropping received VRRP packet…
Jan 13 00:50:31 localhost Keepalived_vrrp[5824]: advertissement interval mismatch mine=5000000 rcved=1
Jan 13 00:50:31 localhost Keepalived_vrrp[5824]: Sync instance needed on eth0 !!!

解决办法:修改主和从的配置文件的时间,重启服务既可。

比如:
/etc/keepalived/keepalived.conf
将通知时间advert_int修改为一致即可。