lvs+keepalived实现双实例【双主模型】
基本架构如下图
分析:DR1,2 为两个互为主备的调度器,DR1上设置VIP:10.220.5.187为主,DR2上设置VIP:10.220.5.197为主。
第一步: 在DR上安装lvs和keepalived
# yum install libnl* popt* kernel-devel ipvsadm -y
# yum install keepalived -y
第二步:配置keepalived实现双实例
1.DR1配置keepalive实现双实例
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
修改vrrp_instance段如下:
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.220.5.187
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.220.5.197
}
}
注意:配置文件中global_defs段的router_id 为DR1 即 router_id DR2
2.DR2配置keepalive实现双实例
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
修改vrrp_instance段如下:
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.220.5.187
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.220.5.197
}
}
注意:配置文件中global_defs段的router_id 为DR1 即 router_id DR2
配置文件对比如下图:
3.修改DR1和DR2的/etc/keepalived/keepalived.conf
文件的virtual_server段如下
virtual_server 10.220.5.187 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 10.220.5.113 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connet_port 80
}
}
real_server 10.220.5.114 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connet_port 80
}
}
}
virtual_server 10.220.5.197 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 10.220.5.115 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connet_port 80
}
}
real_server 10.220.5.116 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connet_port 80
}
}
}
第三步:配置RS、测试
1.在后端RS节点上安装httpd
# yum install httpd -y
2.全部启动
# service httpd restart
3.在DR1和DR2上分别执行# ipvsadm -L -n
DR1和DR2的virtual_server段是相同的,所以# ipvsadm -L -n
得结果也是相同的,如上图。
但是DR1和DR2正常工作时,vip:10.220.5.187只在DR1上,vip:10.220.5.197只在DR2上,所以在DR1上的的10.220.5.197规则不会起作用,同理DR2上的10.220.5.187规则也不会起作用。
而当DR1宕机后vip:10.220.5.187会漂移到DR2上,此时两个规则同时在DR2上起作用。
当DR2宕机后vip:10.220.5.197会漂移到DR1上,两个规则同时在DR1上起作用。