mkdir -p /etc/keepalived/
vi /etc/keepalived/keepalived.conf
global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 stmp_connect_timeout 30 router_id HA_1 } vrrp_instance VMH1 { state MASTER interface eth0 virtual_router_id 100 priority 120 smtp_alert advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.73.203 } }
2.节点B
mkdir -p /etc/keepalived/
vi /etc/keepalived/keepalived.conf
global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 stmp_connect_timeout 30 router_id HA_2 } vrrp_instance VMH1 { state BACKUP interface eth0 virtual_router_id 100 priority 150 smtp_alert nopreempt advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.73.203 } }
3.测试
安装wireshark抓包工具。
#yum install wireshark。注意这样并无法使用wireshark命令和图形界面。但提供了抓包基本功能。
#yum install wireshark-gnome。这样就可以方便使用图形了。
service keepalived restart
查看日志
tail -f /var/log/messages
通过ip a可以看到VIP
在节点A中service keepalived stop后,可以在节点B的日志看到:
un 20 01:25:38 localhost Keepalived_vrrp[1728]: VRRP_Instance(VMH1) Transition to MASTER STATE Jun 20 01:25:39 localhost Keepalived_vrrp[1728]: VRRP_Instance(VMH1) Entering MASTER STATE Jun 20 01:25:39 localhost Keepalived_vrrp[1728]: VRRP_Instance(VMH1) setting protocol VIPs. Jun 20 01:25:39 localhost avahi-daemon[1760]: Registering new address record for 192.168.73.203 on eth0.IPv4. Jun 20 01:25:39 localhost Keepalived_healthcheckers[1727]: Netlink reflector reports IP 192.168.73.203 added Jun 20 01:25:39 localhost Keepalived_vrrp[1728]: VRRP_Instance(VMH1) Sending gratuitous ARPs on eth0 for 192.168.73.203 Jun 20 01:25:44 localhost Keepalived_vrrp[1728]: VRRP_Instance(VMH1) Sending gratuitous ARPs on eth0 for 192.168.73.203
可以通过ping来测试是否成功切换。
备注
1.关于nopreempt:
在实际生产环境中,当master故障停止以后,由backup接替工作,如果此时原先的master正常了,正常情况下,又会重新选举,来切换主控权,这是不能容忍的,所以有了这个选项。这个选项对应的是vrrp RFC文档中的preempt_mod,又来决定是否让高优先级的备份机来重新选举夺得低优先级的master的主控权。有两个值,一个是FALSE即不抢夺,一个是TRUE即抢夺,默认是TRUE,所以就有了 nopreempt。
nopreempt节点的初始状态必须是BACKUP,keepalived.conf中的man有说明
引用
# VRRP will normally preempt a lower priority
# machine when a higher priority machine comes
# online. "nopreempt" allows the lower priority
# machine to maintain the master role, even when
# a higher priority machine comes back online.
# NOTE: For this to work, the initial state of this
# entry must be BACKUP.
nopreempt
2.在虚拟机中的问题
在虚拟机中不能使用ifdown eth0这种方式来测试,因为我在测试的过程中遇到过ifdown之后,Backup中还是能收到Master的广播包。虚拟机中可以通过kill keepalived进程来测试。
在物理机中也同时不能使用ifdown eth0这种方式来测试。
参考:
http://blog.sina.com.cn/s/blog_5f190fff0101e9ht.html