
服务器名称IP地址VIP网卡名称备注local-100-101172.20.100.101172.20.100.250eth0主docker-100-108172.20.100.108172.20.100.250eth0备前提1、关闭防火墙和selinux2、确保安装keepalived的服务的各节点的用于集群服务的接口支持MULTICAST多播或组播通信如果网卡没有开启多播功能可通过命令# ip link set multicast on dev eth0开启以上条件确认好以后开始安装1、安装keepalived(主备都需要安装)yum -y install keepalived2、编辑配置文件主配置文件/etc/keepalived/keepalived.conf主程序文件/usr/sbin/keepalived#master global_defs { notification_email { 15321375963139.com 收件箱 } notification_email_from 15321375963189.cn 发件人 smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id 172.20.100.101 #vrrp_skip_check_adv_addr #vrrp_strict #vrrp_garp_interval 0 #vrrp_gna_interval 0 } vrrp_instance VI_1 { state MASTER #state指定instance(Initial)的初始状态 interface eth0 #实例绑定的网卡因为在配置虚拟IP的时候必须是在已有的网卡上添加的 lvs_sync_daemon_inteface eth0 virtual_router_id 51 #这里设置VRID这里非常重要相同的VRID为一个组他将决定多播的MAC地址 nopreempt #设置不抢占这里只能设置在state为backup的节点上而且这个节点的优先级必须别另外的高 preempt delay 60 #延迟抢占时间 priority 100 #设置本节点的优先级优先级高的为master advert_int 1 #检查间隔默认为1秒 authentication { auth_type PASS auth_pass rongkang } virtual_ipaddress { 172.20.100.250/24 } } #backup global_defs { notification_email { 15321375963139.com } notification_email_from 15321375963189.cn smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id 172.20.100.108 #vrrp_skip_check_adv_addr #vrrp_strict #vrrp_garp_interval 0 #vrrp_gna_interval 0 } vrrp_instance VI_1 { state BACKUP interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 51 nopreempt preempt delay 60 priority 90 advert_int 1 authentication { auth_type PASS auth_pass rongkang } virtual_ipaddress { 172.20.100.250/24 } }2、启动服务systemctl start keepalived systemctl enable keepalived3、查看网卡localhost-100-101eth0: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether fa:c6:4d:82:b3:00 brd ff:ff:ff:ff:ff:ff inet 172.20.100.101/24 brd 172.20.100.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet 172.20.100.250/24 scope global secondary eth0 valid_lft forever preferred_lft forever inet6 fe80::c14a:4493:465b:199b/64 scope link noprefixroute valid_lft forever preferred_lft foreverdocker-100-108eth0: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether fa:ca:f2:59:ee:00 brd ff:ff:ff:ff:ff:ff inet 172.20.100.108/24 brd 172.20.100.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::c14a:4493:465b:199b/64 scope link tentative dadfailed valid_lft forever preferred_lft forever4、抓包查看[rootlocalhost-100-101 ~]# tcpdump -i eth0 vrrp -n tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes 10:41:59.683248 IP 172.20.100.101 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20 10:42:00.684497 IP 172.20.100.101 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20 10:42:01.685782 IP 172.20.100.101 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20 10:42:02.687101 IP 172.20.100.101 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20 10:42:03.688397 IP 172.20.100.101 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20 10:42:04.689709 IP 172.20.100.101 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20从抓包可以看出主172.20.100.101作为master向外发送通告