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 (操作相同)
[root@WEB1~]#systemctl stop firewalld
[root@WEB1~]#setenforce
[root@WEB1~]#yum -y install httpd
[root@WEB1~]#sed -i '/#S/ s/#//' /etc/httpd/conf/httpd.conf
[root@WEB1~]#systemctl start httpd && systemctl enable httpd
[root@WEB1~]#echo "WEB1" > /var/www/html/index.html
[root@WEB2~]#echo "WEB2" > /var/www/html/index.html [root@WEB1~]#cat > /opt/lvs-dr <<EOF
#!/bin/bash
# lvs-dr
VIP="192.168.1.100"
/sbin/ifconfig lo: $VIP broadcast $VIP netmask 255.255.255.255
/sbin/route add -host $VIP dev lo:
echo > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo > /proc/sys/net/ipv4/conf/lo/arp_announce
echo >/proc/sys/net/ipv4/conf/all/arp_ignore
echo >/proc/sys/net/ipv4/conf/all/arp_announce"
EOF [root@WEB1~]#chmod +x /opt/lvs-dr
[root@WEB1~]#/opt/lvs-dr
[root@WEB1~]#ip a
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet 192.168.1.100/32 brd 192.168.1.100 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :::::bf brd ff:ff:ff:ff:ff:ff
inet 192.168.1.33/ brd 192.168.1.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::e07f::ee11:41de/ scope link
valid_lft forever preferred_lft forever
负载均衡主(LB-M)(标红地方需要注意)
[root@LB-M~]#systemctl stop firewalld
[root@LB-M~]#setenforce
[root@LB-M~]#modprobe ip_vs
[root@LB-M~]#cat /proc/net/ip_vs #查看Linux内核lvs版本
IP Virtual Server version 1.2. (size=)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn [root@LB-M~]#yum -y install ipvsadm keepalived
[root@LB-M~]#cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.old
[root@LB-M~]#vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { #全局配置
notification_email {
smbands@.com #邮件告警配置,写你的邮箱地址,一行一个
}
notification_email_from root@LVS-master
smtp_server 127.0.0.1 #邮件服务器地址,这个写个本地回环就行,其他不用动
smtp_connect_timeout
router_id LVS_DEVEL_1 #路由ID,必须唯一
22 ! vrrp_strict #严格执行VRRP协议规范,此模式不支持节点单播,全局配置下vrrp开头用叹号注释掉,不然每次启动iptables防火墙会自动添加drop
} vrrp_instance VI_1 {
state MASTER #主就写MASTER 从的话就写BACKUP或SLAVE
interface ens33 #注意看好自己的网卡名称
virtual_router_id
priority 180 #优先级,这个选项是区分主备的,数值高的优先为主,为外部提供服务
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.1.100 #设置好虚拟IP
}
} virtual_server 192.168.1.100 80 { #这个段是虚拟服务段,里面包括realserver,就是WEB服务器,
delay_loop
lb_algo rr
lb_kind DR #DR为直接路由模式
nat_mask 255.255.255.0
persistence_timeout 300 #会话保持时间配置,默认为300,但是测试时不易看出轮询结果是否成功,测试轮询时设置为0
protocol TCP real_server 192.168.1.33 80 { #WEB服务器的IP+端口,隔一个空格键
weight 1 #设置权重,一般性能高的服务器权重相对其他性能一般的服务器权重高,性能高,处理能力强,权重可以分配高一点
TCP_CHECK {
connect_timeout
connect_port
nb_get_retry
delay_before_retry
}
} real_server 192.168.1.44 80 { #同上
weight
TCP_CHECK {
connect_timeout
connect_port
nb_get_retry
delay_before_retry
}
} } [root@LB-M~]#systemctl start keepalived
[root@LB-M~]#ip a
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :::::c6 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.11/ brd 192.168.1.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.1.100/32 scope global ens33 #keepalived启动后,就会出现一个虚拟IP,但是从服务器不会有,除非主服务器挂掉,从服务器升为主服务器。
valid_lft forever preferred_lft forever
inet6 fe80::59d8:c166::687a/ scope link
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)(标红地方需要注意)
[root@LB-S~]#systemctl stop firewalld
[root@LB-S~]#setenforce
[root@LB-S~]#modprobe ip_vs
[root@LB-S~]#cat /proc/net/ip_vs
IP Virtual Server version 1.2. (size=)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn [root@LB-S~]#yum -y install ipvsadm keepalived
[root@LB-S~]#scp 192.168.1.11:/etc/keepalived/keepalived.conf /etc/keepalived/
[root@LB-S~]#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { #全局配置
notification_email {
smbands@163.com #邮件告警配置,写你的邮箱地址,一行一个
}
notification_email_from root@LVS-master
smtp_server 127.0.0.1
smtp_connect_timeout
router_id LVS_DEVEL_2 #路由ID必须唯一
! vrrp_strict #全局配置下vrrp开头加叹号注释
} vrrp_instance VI_1 {
state SLAVE #从服务器写SLAVE或BACKUP
interface ens33 #看好网卡名称
virtual_router_id
priority 100 #从服务器上的优先级不能高于主
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.1.100 #配好虚拟IP
}
} virtual_server 192.168.1.100 80 { #虚拟服务器段
delay_loop
lb_algo rr
lb_kind DR #直接路由模式
nat_mask 255.255.255.0
persistence_timeout 300 #会话保持时间配置,默认为300,但是测试时不易看出轮询结果是否成功,测试轮询时设置为0
protocol TCP real_server 192.168.1.33 80 { #web1的IP
weight 1 #权重
TCP_CHECK {
connect_timeout
connect_port
nb_get_retry
delay_before_retry
}
} real_server 192.168.1.44 80 { #web2的IP
weight 1 #权重
TCP_CHECK {
connect_timeout
connect_port
nb_get_retry
delay_before_retry
}
} } [root@LB-S~]#systemctl start keepalived && systemctl enable keepalived
[root@LB-S~]#ip a
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether ::::3a:1e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.22/ brd 192.168.1.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::e344:79a9:d402:a812/ scope link
valid_lft forever preferred_lft forever [root@LB-S~]#ipvsadm -ln
IP Virtual Server version 1.2. (size=)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.100:80 rr
-> 192.168.1.33:80 Route
-> 192.168.1.44:80 Route
测试
多刷新几次
若没有轮询效果 (以下资源来源于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 原因:备机上防火墙开着(要关掉)
高可用
模拟主负载均衡器宕机