欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

lvs+keepalive实现双主模式(采用DR),同时实现TCP和UDP检测实现非web端的负载均衡,同时实现跨网段的通讯

程序员文章站 2022-09-16 18:19:08
因业务需求,需要把lvs备机也使用上,故! 使用双主,相互是主的同时也相互是备机。本人用nat测试发现RS无法实现负载均衡,故采用DR模式来实现非web端的负载均衡 lvs1: DIP 10.60.196.183 VIP 10.60.196.185 lvs2:DIP 10.60.196.184 VI ......

    因业务需求,需要把lvs备机也使用上,故! 使用双主,相互是主的同时也相互是备机。本人用nat测试发现RS无法实现负载均衡,故采用DR模式来实现非web端的负载均衡

 

lvs1: DIP 10.60.196.183

        VIP 10.60.196.185

lvs2:DIP 10.60.196.184

         VIP 10.60.196.186

DR1:10.60.196.181

DR2:10.60.196.182

--------------------------------------------------------

我这里TCP和UDP同时使用

LVS:需要开启IP转发

vim /etc/sysctl.conf

keepalive配置:

! Configuration File for keepalived

global_defs {
notification_email {
root@localhost
}
notification_email_from admin@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state BACKUP                                  #另外一台keepalive这里换成MASTER
interface eth0
virtual_router_id 51 
priority 98                                         #这里100
advert_int 1
authentication {
auth_type PASS
auth_pass 11112222
}
virtual_ipaddress {
10.60.196.186/32 label eth0:0
}
}

vrrp_instance VI_2 {
state MASTER            #另一台BUCKUP
interface eth0
virtual_router_id 150
priority 100          # 这里99
advert_int 1
authentication {
auth_type PASS
auth_pass 11112222
}
virtual_ipaddress {
10.60.196.185/32 label eth0:1
}
}

virtual_server 10.60.196.186 33001 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
persistence_timeout 0

real_server 10.60.196.181 33001 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}

real_server 10.60.196.182 33001 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

 

virtual_server 10.60.196.186 33001 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol UDP
persistence_timeout 0

real_server 10.60.196.181 33001 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}

real_server 10.60.196.182 33001 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}

 

virtual_server 10.60.196.185 33001 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
persistence_timeout 0

real_server 10.60.196.181 33001 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}

real_server 10.60.196.182 33001 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

 

virtual_server 10.60.196.185 33001 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol UDP
persistence_timeout 0

real_server 10.60.196.181 33001 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}

real_server 10.60.196.182 33001 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}

配置好以后添加路由

route add -host 10.60.196.185 dev eth0:1

route add -host 10.60.196.186 dev eth0:0

 

rs:需要添加路由同时绑定VIP

RS1和RS2配置相同

ifconfig eth0:0 10.60.196.186 netmask 255.255.255.0

ifconfig eth0:1 10.60.196.185 netmask 255.255.255.0

route add -host 10.60.196.185 dev eth0:1

route add -host 10.60.196.186 dev eth0:0

 

如果还需要外网可以通过另外搭建搭建两台机器做iptables转发   上面需要有一个公网IP和一个内网IP,通过iptables把外网请求全部转发到VIP上面,一台做一个VIP转发 (iptables这里就不写了),这样就可以实现跨网段的通讯了!    不同网段原因一样