LVS+Keepalived-DR模式负载均衡高可用集群

时间:2022-07-04 03:08:05

LVS+Keepalived DR模式负载均衡+高可用集群架构图

LVS+Keepalived-DR模式负载均衡高可用集群

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

工作原理:

Keepalived采用VRRP热备份协议实现Linux服务器的多机热备功能。

         VRRP,虚拟路由冗余协议,是针对路由器的一种备份解决方案。由多台路由器组成一个热备组,通过共用的虚拟IP地址对外提供服务。每个热备组内同一时刻只有一台主路由器提供服务,其他路由器处于冗余状态。若当前在线的路由器失败,则其他路由器会根据设置的优先级自动接替虚拟IP地址,继续提供服务。

实验环境:

主机名 IP地址 VIP 软件名
LB-M  192.168.1.11 192.168.1.100  ipvsadm  keepalived
LB-S  192.168.1.22 192.168.1.100  ipvsadm  keepalived
WEB1  192.168.1.33 192.168.1.100  httpd
WEB2  192.168.1.44 192.168.1.100  httpd

 

 

 

 

 

 

 

 

 

 

 

 

 

nfs共享存储本实验不再重复做,需要的看LVS基于DR模式搭建负载均衡群集

 

搭建

 WEB1、WEB2  (操作相同)

 1 [root@WEB1~]#systemctl stop firewalld  2 [root@WEB1~]#setenforce 0
 3 [root@WEB1~]#yum -y install httpd  4 [root@WEB1~]#sed -i '/#S/ s/#//' /etc/httpd/conf/httpd.conf  5 [root@WEB1~]#systemctl start httpd && systemctl enable httpd  6 [root@WEB1~]#echo "WEB1" > /var/www/html/index.html 7 
 8 [root@WEB2~]#echo "WEB2" > /var/www/html/index.html  9 
10 [root@WEB1~]#cat > /opt/lvs-dr <<EOF 11 #!/bin/bash 12 # lvs-dr 13 VIP="192.168.1.100"
14 /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255
15 /sbin/route add -host $VIP dev lo:0
16 echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore 17 echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce 18 echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore 19 echo 2 >/proc/sys/net/ipv4/conf/all/arp_announce" 
20 EOF 21 
22 [root@WEB1~]#chmod +x /opt/lvs-dr 23 [root@WEB1~]#/opt/lvs-dr 24 [root@WEB1~]#ip a 25 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
26     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
27     inet 127.0.0.1/8 scope host lo 28  valid_lft forever preferred_lft forever 29     inet 192.168.1.100/32 brd 192.168.1.100 scope global lo:0
30  valid_lft forever preferred_lft forever 31     inet6 ::1/128 scope host 32  valid_lft forever preferred_lft forever 33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
34     link/ether 00:50:56:37:26:bf brd ff:ff:ff:ff:ff:ff 35     inet 192.168.1.33/24 brd 192.168.1.255 scope global ens33 36  valid_lft forever preferred_lft forever 37     inet6 fe80::e07f:1562:ee11:41de/64 scope link 38        valid_lft forever preferred_lft forever

负载均衡主(LB-M)(标红地方需要注意)

 1 [root@LB-M~]#systemctl stop firewalld  2 [root@LB-M~]#setenforce 0
 3 [root@LB-M~]#modprobe ip_vs  4 [root@LB-M~]#cat /proc/net/ip_vs #查看Linux内核lvs版本  5 IP Virtual Server version 1.2.1 (size=4096)  6 Prot LocalAddress:Port Scheduler Flags  7   -> RemoteAddress:Port Forward Weight ActiveConn InActConn  8 
 9 [root@LB-M~]#yum -y install ipvsadm keepalived 10 [root@LB-M~]#cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.old 11 [root@LB-M~]#vim /etc/keepalived/keepalived.conf 12 ! Configuration File for keepalived 13 
14 global_defs { #全局配置 15  notification_email { 16         smbands@163.com #邮件告警配置,写你的邮箱地址,一行一个 17  } 18    notification_email_from root@LVS-master 19    smtp_server 127.0.0.1 #邮件服务器地址,这个写个本地回环就行,其他不用动
20    smtp_connect_timeout 30
21  router_id LVS_DEVEL_1 #路由ID,必须唯一 22 ! vrrp_strict #严格执行VRRP协议规范,此模式不支持节点单播,全局配置下vrrp开头用叹号注释掉,不然每次启动iptables防火墙会自动添加drop
23 24 } 25 26 vrrp_instance VI_1 { 27 state MASTER #主就写MASTER 从的话就写BACKUP或SLAVE 28 interface ens33 #注意看好自己的网卡名称 29 virtual_router_id 51 30 priority 180 #优先级,这个选项是区分主备的,数值高的优先为主,为外部提供服务 31 advert_int 2 32 authentication { 33 auth_type PASS 34 auth_pass 1111 35 } 36 virtual_ipaddress { 37 192.168.1.100 #设置好虚拟IP 38 } 39 } 40 41 virtual_server 192.168.1.100 80 { #这个段是虚拟服务段,里面包括realserver,就是WEB服务器,
42 delay_loop 2 43 lb_algo rr 44 lb_kind DR #DR为直接路由模式 45 nat_mask 255.255.255.0 46 persistence_timeout 300 #会话保持时间配置,默认为300,但是测试时不易看出轮询结果是否成功,测试轮询时设置为0 47 protocol TCP 48 49 real_server 192.168.1.33 80 { #WEB服务器的IP+端口,隔一个空格键 50 weight 1 #设置权重,一般性能高的服务器权重相对其他性能一般的服务器权重高,性能高,处理能力强,权重可以分配高一点 51 TCP_CHECK { 52 connect_timeout 3 53 connect_port 80 54 nb_get_retry 3 55 delay_before_retry 3 56 } 57 } 58 59 60 real_server 192.168.1.44 80 { #同上 61 weight 1 62 TCP_CHECK { 63 connect_timeout 3 64 connect_port 80 65 nb_get_retry 3 66 delay_before_retry 3 67 } 68 } 69 70 } 71 72 [root@LB-M~]#systemctl start keepalived 73 [root@LB-M~]#ip a 74 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 75 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 76 inet 127.0.0.1/8 scope host lo 77 valid_lft forever preferred_lft forever 78 inet6 ::1/128 scope host 79 valid_lft forever preferred_lft forever 80 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 81 link/ether 00:50:56:32:81:c6 brd ff:ff:ff:ff:ff:ff 82 inet 192.168.1.11/24 brd 192.168.1.255 scope global ens33 83 valid_lft forever preferred_lft forever 84 inet 192.168.1.100/32 scope global ens33 #keepalived启动后,就会出现一个虚拟IP,但是从服务器不会有,除非主服务器挂掉,从服务器升为主服务器。 85 valid_lft forever preferred_lft forever 86 inet6 fe80::59d8:c166:1834:687a/64 scope link 87 valid_lft forever preferred_lft forever
88[root@LB-M~]#ipvsadm -ln
89 IP Virtual Server version 1.2.1 (size=4096)
90 Prot LocalAddress:Port Scheduler Flags
91  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
92 TCP  192.168.1.100:80 rr
93  -> 192.168.1.33:80              Route   1      0          0         
94  -> 192.168.1.44:80              Route   1      0          0         

负载均衡从(LB-S)(标红地方需要注意)

 

 1 [root@LB-S~]#systemctl stop firewalld  2 [root@LB-S~]#setenforce 0
 3 [root@LB-S~]#modprobe ip_vs  4 [root@LB-S~]#cat /proc/net/ip_vs  5 IP Virtual Server version 1.2.1 (size=4096)  6 Prot LocalAddress:Port Scheduler Flags  7   -> RemoteAddress:Port Forward Weight ActiveConn InActConn  8 
 9 [root@LB-S~]#yum -y install ipvsadm keepalived 10 [root@LB-S~]#scp 192.168.1.11:/etc/keepalived/keepalived.conf /etc/keepalived/
11 [root@LB-S~]#vi /etc/keepalived/keepalived.conf 12 ! Configuration File for keepalived 13 
14 global_defs { #全局配置 15  notification_email { 16         smbands@163.com #邮件告警配置,写你的邮箱地址,一行一个 17  } 18    notification_email_from root@LVS-master 19    smtp_server 127.0.0.1
20    smtp_connect_timeout 30
21  router_id LVS_DEVEL_2 #路由ID必须唯一 22 ! vrrp_strict  #全局配置下vrrp开头加叹号注释23 
24 } 25 
26 vrrp_instance VI_1 { 27  state SLAVE #从服务器写SLAVE或BACKUP 28     interface ens33 #看好网卡名称 29     virtual_router_id 51
30     priority 100 #从服务器上的优先级不能高于主
31     advert_int 2
32  authentication { 33  auth_type PASS 34         auth_pass 1111
35  } 36  virtual_ipaddress { 37         192.168.1.100 #配好虚拟IP
38  } 39 } 40 
41 virtual_server 192.168.1.100 80 { #虚拟服务器段 42     delay_loop 2
43  lb_algo rr 44  lb_kind DR #直接路由模式 45     nat_mask 255.255.255.0
46     persistence_timeout 300 #会话保持时间配置,默认为300,但是测试时不易看出轮询结果是否成功,测试轮询时设置为0
47  protocol TCP 48 
49     real_server 192.168.1.33 80 { #web1的IP 50         weight 1 #权重
51  TCP_CHECK { 52             connect_timeout 3
53             connect_port 80
54             nb_get_retry 3
55             delay_before_retry 3
56  } 57  } 58 
59 
60     real_server 192.168.1.44 80 {  #web2的IP 61         weight 1 #权重 
62  TCP_CHECK { 63             connect_timeout 3
64             connect_port 80
65             nb_get_retry 3
66             delay_before_retry 3
67  } 68  } 69 
70 } 71 
72 [root@LB-S~]#systemctl start keepalived && systemctl enable keepalived 73 [root@LB-S~]#ip a 74 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
75     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
76     inet 127.0.0.1/8 scope host lo 77  valid_lft forever preferred_lft forever 78     inet6 ::1/128 scope host 79  valid_lft forever preferred_lft forever 80 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
81     link/ether 00:50:56:25:3a:1e brd ff:ff:ff:ff:ff:ff 82     inet 192.168.1.22/24 brd 192.168.1.255 scope global ens33 83  valid_lft forever preferred_lft forever 84     inet6 fe80::e344:79a9:d402:a812/64 scope link 85  valid_lft forever preferred_lft forever 86 
87 [root@LB-S~]#ipvsadm -ln 88 IP Virtual Server version 1.2.1 (size=4096) 89 Prot LocalAddress:Port Scheduler Flags 90   -> RemoteAddress:Port Forward Weight ActiveConn InActConn 91 TCP  192.168.1.100:80 rr 92   -> 192.168.1.33:80              Route   1      0          0         
93   -> 192.168.1.44:80              Route   1      0          0         

 

测试

LVS+Keepalived-DR模式负载均衡高可用集群

 

 

LVS+Keepalived-DR模式负载均衡高可用集群

 LVS+Keepalived-DR模式负载均衡高可用集群

多刷新几次

若没有轮询效果 (以下资源来源于https://blog.csdn.net/Juwenzhe_HEBUT/article/details/85254953)(感谢博主分享,侵删)

为什么LVS设置了轮询,浏览器测试还是不能轮询?这关系到两个地方的配置:
   一 ./etc/keepalived/keepalived.conf的persistence_timeout会话保持时间配置,测试轮询时设置为0;
   二 .查看ipvsadm默认超时时间(一直在浏览器刷不出LVS轮询)
      [root@LB-M~]# ipvsadm -L --timeout
      Timeout (tcp tcpfin udp): 900 120 300
      900 120 300这三个数值分别是TCP TCPFINUDP的时间.也就是说一条tcp的连接经过lvs后,lvs会把这台记录保存15分钟,就是因为这个时间过长,所以很多人都会发现做好LVS DR之后轮询现象并没有发生,实践中将此数值调整很小小,使用以下命令调整:
      [root@LB-M ~]# ipvsadm --set 1 2 1
    再次测试轮询效果,就可以了!而实际配置中还是按照默认配置,那么在大量IP访问VIP时,就有轮询效果?有待验证
  1.NG节点配置nginx.conf error-log日志级别为info,再reload一下,可以发现两台LVS的keepalived都在定是检测HG健康(注:记得将error-log级别改回来,不然日志很多,遗留问题:21:19:17 [info] 109687#0: *15087 recv() failed (104: Connection reset by peer) while waiting for request, client: 192.168.31.141, server: 0.0.0.0:18081 原因:这是正常的请求INFO日志,keepalive的tcp_cheack心跳检测,在nginx的配置中error.log输出级别为被设置为info,keepalived与MG只做了两次握手,所以NG一直包这个'错误')
  2.遇到的问题Received advert with lower priority 80, ours 100, forcing new election 原因:备机上防火墙开着(要关掉)

 高可用

模拟主负载均衡器宕机

LVS+Keepalived-DR模式负载均衡高可用集群

LVS+Keepalived-DR模式负载均衡高可用集群

 

 LVS+Keepalived-DR模式负载均衡高可用集群

LVS+Keepalived-DR模式负载均衡高可用集群

 

LVS+Keepalived-DR模式负载均衡高可用集群