$ yum -y install keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #备用服务器上为 BACKUP
interface eth0 # 网卡
virtual_router_id 110 # 注意不要和其他keepalived集群冲突
priority 100 #优先级,数值越大优先级越高;备用服务器上为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.110
}
}
virtual_server 192.168.2.110 80 {
delay_loop 6 #(每隔6秒查询realserver状态,是否存活)
lb_algo wlc #(加权轮询算法)
lb_kind DR #(DR模式)
persistence_timeout 0 #(同一IP的连接多少秒内被分配到同一台realserver,0表示不连接)
protocol TCP #(用TCP协议检查realserver状态)
real_server 192.168.2.104 80 {
weight 100 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.2.105 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
virtual_server 192.168.2.110 81 {
delay_loop 6
lb_algo wlc
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 192.168.2.104 81 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 81
}
}
real_server 192.168.2.105 81 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 81
}
}
}
$ systemctl enable keepalived && systemctl start keepalived
DEVICE=lo
IPADDR=192.168.2.110
NETMASK=255.255.255.255
BROADCAST=192.168.2.110
ONBOOT=yes
NAME=loopback
$ route add -host 192.168.2.110 dev lo
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2