keepalived最初专为LVS设计用来监控LVS集群系统中各个节点的状态,后来加入VRRP功能。因此也可以做nginx,haproxy,mysql的高可用。

VRRP:虚拟路由器冗余协议,解决静态路由单点故障。
通过竞选协议来实现虚拟路由器的功能,所有的协议报文通过ip多播(多播地址224.0.0.18),虚拟路由器的vrid对外的MAC地址
00-00-5e-00-01-vrid.master会一直发VRRP广告包,如果BACKUP收不到广播包,会自己配置vip起服务。

#############################################################
安装keepalived
yum install keepalived -y

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
lmkmike#qq.com
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LV-master #每一台keepalived不一样

}

vrrp_instance VI_1 {
state MASTER #角色 另外改BACKUP
interface eth0 #网卡
virtual_router_id 51 #不能重复
priority 150 #权重 另外改100
advert_int 1 #检查时间默认1s
authentication {
auth_type PASS
auth_pass haproxy_ha
}
virtual_ipaddress {
192.168.56.10/24
}

}

vrrp_instance VI_2 {
state BACKUP #角色 另外改MASTER
interface eth0 #网卡
virtual_router_id 60 #不能重复
priority 150 #权重 另外改100
advert_int 1 #检查时间默认1s
authentication {
auth_type PASS
auth_pass haproxy_ha
}
virtual_ipaddress {
192.168.56.11/24
}
}

virtual_server 192.168.56.10 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 300
protocol TCP

real_server 192.168.56.100 80 {
    weight 1
    TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
    }
}
    real_server 192.168.56.21 80 {
    weight 1
    TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
    }
}
}

#############################################################
RS要配置VIP和抑制ARP
其他节点绑定vip
ifconfig lo:0 192.168.56.10/32 up
抑制ARP
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
#############################################################
启动keepalived
service keepalived start

查看lvs当前链接的状态
ipvsadm -lnc

上线RS时通过修改keepalived.cnf后优雅重启/etc/init.d/keepalived reload