优势:
- 不需要手动配置lvs规则
- 实现后端rs的健康性检查
[root@lvs1 ~]# cat /etc/keepalived/keepalived.conf
geth0bal_defs {
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_iptables #不生成iptables规则
# vrrp_mcast_group4 224.0.0.19 #组播,默认情况下向224.0.0.18发送组播消息
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface eth0
#nopreempt
virtual_router_id 52 #虚拟路由ID
priority 100 ##master的值要大于BACKUP的
advert_int 1 ##探测时间
authentication { ##认证方式
auth_type PASS
auth_pass lujihui
}
virtual_ipaddress {
10.35.78.94 dev eth0 label eth0:1
}
}
virtual_server 10.35.78.94 80 {
delay_loop 3
lb_algo rr
lb_kind DR
#persistence_timeout 50 #先把持久连接注释,以验证调度功能
protocol TCP
real_server 10.35.78.91 80 {
weight 1
TCP_CHECK { ##keepalived对于后端rs进行tcp的健康性检查
connect_timeout 10 ##设置响应超时时间
nb_get_retry 3 ##设置超时重试次数
delay_before_retry 3 ##设置超时重试间隔时间
connect_port 80
}
}
real_server 10.35.78.92 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
[root@lvs2 keepalived]# cat /etc/keepalived/keepalived.conf
geth0bal_defs {
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_iptables #不生成iptables规则
# vrrp_mcast_group4 224.0.0.19 #组播,默认情况下向224.0.0.18发送组播消息
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
#nopreempt
virtual_router_id 52 #虚拟路由ID
priority 80 ##master的值要大于BACKUP的
advert_int 1 ##探测时间
authentication { ##认证方式
auth_type PASS
auth_pass lujihui
}
virtual_ipaddress {
10.35.78.94 dev eth0 label eth0:1
}
}
virtual_server 10.35.78.94 80 {
delay_loop 3
lb_algo rr
lb_kind DR
#persistence_timeout 50
protocol TCP
real_server 10.35.78.91 80 {
weight 1
TCP_CHECK {
connect_timeout 10 ##设置响应超时时间
nb_get_retry 3 ##设置超时重试次数
delay_before_retry 3 ##设置超时重试间隔时间
connect_port 80
}
}
real_server 10.35.78.92 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
##可以看到keepalived已经使用底层的IPVS生成LVS规则了
##在ip浮动至另外一台主机的时候,LVS备机就可以立马进行调度,实现LVS服务器的高可用
[root@lvs1 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.35.78.94:80 rr
-> 10.35.78.91:80 Route 1 0 0
-> 10.35.78.92:80 Route 1 0 0
[root@lvs2 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.35.78.94:80 rr
-> 10.35.78.91:80 Route 1 0 0
-> 10.35.78.92:80 Route 1 0 0
##后端的rs宕机,keepalived会将该rs从调度规则中剔除,如下所示
[root@lvs2 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.35.78.94:80 rr
-> 10.35.78.91:80 Route 1 0 0 (rs只剩一台)
rs的vip配置
#!/bin/bash
sign=$1 #start|stop
interface="lo"
vip="10.35.78.94"
case $sign in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/$interface/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/$interface/arp_announce
ifconfig $interface:1 $vip netmask 255.255.255.255 broadcast $vip up
;;
stop)
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/$interface/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/$interface/arp_announce
ifconfig $interface:1 $vip netmask 255.255.255.255 broadcast $vip down
;;
*)
echo "param stop|start ?"
esac