一.LVS(Linux virtual server)集群
目录
1.集群Cluster的三种类型
LB:LoadBalancing(负载均衡)由多个主机组成,每个主机只承担一部分访问
HA:High Availiablity(高可用)SPOF(single Point Of failure)
MTBF:Mean Time Between Failure 平均无故障时间,正常时间
MTTR:Mean Time To Restoration( repair)平均恢复前时间,故障时间
A=MTBF/(MTBF+MTTR) (0,1):99%, 99.5%, 99.9%, 99.99%, 99.999% SLA:Service level agreement(服务等级协议)是在一定开销下为保障服务的性能和可用性,服 务提供商与用户间定义的一种双方认可的协定。通常这个开销是驱动提供服务质量的主要因素。在 常规的领域中,总是设定所谓的三个9,四个9来进行表示,当没有达到这种水平的时候,就会有一 些列的惩罚措施,而运维,最主要的目标就是达成这种服务水平。 停机时间又分为两种,一种是计划内停机时间,一种是计划外停机时间,而运维则主要关注计划外 停机时间
HPC:High-performance computing(高性能计算,国家战略资源,不在课程范围内)
2.分布式
分布式存储:Ceph,GlusterFs,FastDFS,MogileFs
分布式计算:hadoop,Spark
分布式常见应用 分布式应用-服务按照功能拆分,使用微服务 分布式静态资源--静态资源放在不同的存储集群上分布式数据和存储--使用key-value缓存系统 分布式计算--对特殊业务使用分布式计算,比如Hadoop集群
3.集群和分布式
集群:同一个业务系统,部署在多台服务器上,集群中,每一台服务器实现的功能没有差别,数据 和代码都是一样的。
分布式:一个业务被拆成多个子业务,或者本身就是不同的业务,部署在多台服务器上。分布式 中,每一台服务器实现的功能是有差别的,数据和代码也是不一样的,分布式每台服务器功能加起 来,才是完整的业务。(分布式是以缩短单个任务的执行时间来提升效率的,而集群则是通过提高单位时间内执行的任务数 来提升效率。)
对于大型网站,访问用户很多,实现一个群集,在前面部署一个负载均衡服务器,后面几台服务器 完成同一业务。如果有用户进行相应业务访问时,负载均衡器根据后端哪台服务器的负载情况,决 定由给哪一台去完成响应,并且台服务器垮了,其它的服务器可以顶上来。分布式的每一个节点, 都完成不同的业务,如果一个节点垮了,那这个业务可能就会失败
二.lvs运行原理
1.lvs简介
LVS:Linux Virtual Server,负载调度器,内核集成,章文嵩,阿里的四层SLB(Server LoadBalance)是基 于LVS+keepalived实现
LVS 官网: http://www.linuxvirtualserver.org/
LVS 相关术语 VS: Virtual Server,负责调度
RS:RealServer,负责真正提供服务
2.LVS概念
VS:Virtual Server
RS:Real Server
CIP:Client IP
VIP: Virtual serve IP VS外网的IP
DIP: Director IP VS内网的IP
RIP: Real server IP
访问流程:CIP VIP == DIP RIP
三.LVS集群类型及四层负载均衡
1.LVS的集群类型
lvs-nat: 修改请求报文的目标IP,多目标IP的DNAT lvs-dr: 操纵封装新的MAC地址
lvs-tun: 在原请求IP报文之外新加一个IP首部
lvs-fullnat: 修改请求报文的源和目标IP
2.Lvsadm的基本命令使用
[root@lvs ~]# systemctl stop ipvsadm.service
[root@lvs ~]# vim /etc/sysconfig/ipvsadm
[root@lvs ~]# systemctl restart ipvsadm.service
[root@lvs ~]# ipvsadm -C 清空
[root@lvs ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@lvs ~]# cat /etc/sysconfig/ipvsadm
-A -t 172.25.254.100:80 -s wrr
-a -t 172.25.254.100:80 -r 192.168.0.10:80 -m -w 2
-a -t 172.25.254.100:80 -r 192.168.0.20:80 -m -w 1
[root@lvs ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.25.254.100:80 wrr
-> 192.168.0.10:80 Masq 2 0 0
-> 192.168.0.20:80 Masq 1 0 0
A.lvs-nat模式原理及部署方法
a.本质是多目标IP的DNAT,通过将请求报文中的目标地址和目标端口修改为某挑出的RS的RIP和PORT实现转发
b.RIP和DIP应在同一个IP网络,且应使用私网地址;RS的网关要指向DIP
c.请求报文和响应报文都必须经由Director转发,Director易于成为系统瓶颈
d.支持端口映射,可修改请求报文的目标PORT
e.VS必须是Linux系统,RS可以是任意OS系统
实验环境
准备三台虚拟机,一台为LVS(需要添加一个网卡),另外两台是webser1,webserver2
LVS中:
[root@netmask ~]# vmset.sh eth0 172.25.254.100 netmask 255.255.255.0
[root@apache ~]# vim /etc/NetworkManager//system-connections/eth1.nmconnection
[root@apache ~]# cat /etc/NetworkManager//system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0
[ipv4]
address1=172.25.254.100/24,172.25.254.2
method=manual
dns=114.114.114.114;
[root@lvs ~]# nmcli connection reload
[root@lvs ~]# nmcli connection up eth1
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/6)
[root@lvs ~]# systemctl stop firewalld 关闭防火墙
下面两个webserver配置好了以后,最终在LVS中检测:
[root@lvs ~]# curl 192.168.0.10
webserver1 - 192.168.0.10
[root@lvs ~]# curl 192.168.0.20
webserver2 - 192.168.0.20
[root@lvs yum.repos.d]# dnf install ipvsadm -y 在LVS中安装LVS软件
[root@lvs yum.repos.d]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@lvs yum.repos.d]# ipvsadm -A -t 172.25.254.100 -s rr
Zero port specified for non-persistent service
[root@lvs yum.repos.d]# ipvsadm -A -t 172.25.254.100:80 -s rr
[root@lvs yum.repos.d]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.25.254.100:80 rr
[root@lvs yum.repos.d]# ipvsadm -a -t 172.25.254.100:80 -r 192.168.0.10:80 -m
[root@lvs yum.repos.d]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.25.254.100:80 rr
-> 192.168.0.10:80 Masq 1 0 0
webserver1:
[root@netmask ~]# vmset.sh eth0 192.168.0.10 netmask 255.255.255.0
[root@netmask ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[root@netmask ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0
[ipv4]
address1=192.168.0.10/24,192.168.0.100
method=manual
dns=114.114.114.114;
[root@lvs ~]# nmcli connection reload
[root@lvs ~]# nmcli connection up eth0
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/6)
[root@netmask ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.100 0.0.0.0 UG 100 0 0 eth0
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
[root@netmask ~]# dnf install httpd -y
[root@netmask ~]# echo webserver1 - 192.168.0.10 > /var/www/html/index.html
[root@netmask ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
webserver2:
[root@netmask ~]# vmset.sh eth0 192.168.0.20 netmask 255.255.255.0
[root@netmask ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[root@netmask ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0
[ipv4]
address1=192.168.0.20/24,192.168.0.100
method=manual
dns=114.114.114.114;
[root@lvs ~]# nmcli connection reload
[root@lvs ~]# nmcli connection up eth0
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/6)
[root@netmask ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.100 0.0.0.0 UG 100 0 0 eth0
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
[root@netmask ~]# yum install httpd -y yum不行就用dnf
[root@netmask ~]# echo webserver2 - 192.168.0.20 > /var/www/html/index.html
[root@netmask ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
B.LVS的DR模式
实验环境
client:
[root@client ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[root@client ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0
[ipv4]
address1=172.25.254.200/24,172.25.254.100
method=manual
dns=114.114.114.114;
[root@client ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.25.254.100 0.0.0.0 UG 100 0 0 eth0
172.25.254.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
最后在client上面测试:
[root@client ~]# for i in {1..10}; do curl 192.168.0.200; done;
webserver1 - 192.168.0.10
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver1 - 192.168.0.10
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver1 - 192.168.0.10
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver1 - 192.168.0.10
router:
两个网卡一个NAT 一个仅主机
[root@router ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[root@router ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0
[ipv4]
address1=172.25.254.100/24,172.25.254.2
method=manual
dns=114.114.114.114;
[root@router ~]# vim /etc/NetworkManager/system-connections/eth1.nmconnection
[root@router ~]# cat /etc/NetworkManager/system-connections/eth1.nmconnection
[connection]
id=eth1
type=ethernet
interface-name=eth1
[ipv4]
address1=192.168.0.100/24
method=manual
dns=114.114.114.114;
[root@router ~]# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 1 将0改为1
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0
[root@router ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.25.254.2 0.0.0.0 UG 100 0 0 eth0
172.25.254.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
192.168.0.0 0.0.0.0 255.255.255.0 U 101 0 0 eth1
[root@router ~]# vim /etc/sysctl.conf
[root@router ~]# cat /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_forward = 1
[root@router ~]# sysctl -p
net.ipv4.ip_forward = 1
LVS:
两个网卡只要eth1和回环,注意添加回环接口的掩码必须是32
[root@lvs ~]# vim /etc/NetworkManager/system-connections/eth1.nmconnection
[root@lvs ~]# cat /etc/NetworkManager/system-connections/eth1.nmconnection
[connection]
id=eth1
type=ethernet
interface-name=eth1
[ipv4]
address1=192.168.0.50/24,192.168.0.100
method=manual
dns=114.114.114.114;
[root@lvs ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.100 0.0.0.0 UG 100 0 0 eth1
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth1
[root@lvs ~]# ip a a 192.168.0.200/32 dev lo 给环回接口添加ip
[root@lvs ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 192.168.0.200/32 scope global lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@lvs ~]# ipvsadm -A -t 192.168.0.200:80 -s wrr
[root@lvs ~]# ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.10:80 -g -w 1
[root@lvs ~]# ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.20:80 -g -w 2
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.200:80 wrr
-> 192.168.0.10:80 Route 1 0 0
-> 192.168.0.20:80 Route 2 0 0
webserver1:使VIP不对外响应,给环回接口添加ip,设定vip
[root@netmask ~]# nmcli connection reload
[root@netmask ~]# nmcli connection up eth0
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
[root@netmask ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@netmask ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@netmask ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@netmask ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@netmask ~]# ip a a 192.168.0.200/32 dev lo 给环回接口添加ip
webservwe2:
[root@netmask ~]# nmcli connection reload
[root@netmask ~]# nmcli connection up eth0
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
[root@netmask ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@netmask ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@netmask ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@netmask ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@netmask ~]# ip a a 192.168.0.200/32 dev lo 给环回接口添加ip
最后结果:
四.LVS的调度算法 12种
1.静态调度算法
1、RR:roundrobin 轮询 RS分别被调度,当RS配置有差别时不推荐
2、WRR:Weighted RR,加权轮询根据RS的配置进行加权调度,性能差的RS被调度的次数少
3、SH:Source Hashing,实现session sticky,源IP地址hash;将来自于同一个IP地址的请求始终发往 第一次挑中的RS,从而实现会话绑定
4、DH:Destination Hashing;目标地址哈希,第一次轮询调度至RS,后续将发往同一个目标地址的请 求始终转发至第一次挑中的RS,典型使用场景是正向代理缓存场景中的负载均衡,如:宽带运营商
2.动态调度算法
1、LC:least connections(最少链接发)
适用于长连接应用Overhead(负载值)=activeconns(活动链接数) x 256+inactiveconns(非活 动链接数)
2、WLC:Weighted LC(权重最少链接) 默认调度方法Overhead=(activeconns x 256+inactiveconns)/weight 3、SED:Shortest Expection Delay,
初始连接高权重优先Overhead=(activeconns+1+inactiveconns) x 256/weight 但是,当node1的权重为1,node2的权重为10,经过运算前几次的调度都会被node2承接
4、NQ:Never Queue,第一轮均匀分配,后续SED
5、LBLC:Locality-Based LC,动态的DH算法,使用场景:根据负载状态实现正向代理
6、LBLCR:LBLC with Replication,带复制功能的LBLC,解决LBLC负载不均衡问题,从负载重的复制 到负载轻的RS
五.LVS火墙标记 解决轮询错误
第一步:在两台webserver中安装mod_ssl模块,让它支持httpd,并重启httpd服务
注意:要检查回环接口的IP还有没有,没有要配置
[root@netmask ~]# yum install mod_ssl -y
正在更新 Subscription Management 软件仓库。
无法读取客户身份
本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。
上次元数据过期检查:18:07:09 前,执行于 2024年08月06日 星期二 16时57分25秒。
软件包 mod_ssl-1:2.4.57-5.el9.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!
[root@netmask ~]# systemctl restart httpd 重启httpd服务
第二步:在lvs主机中添加443端口的策略,接着在lvs中为端口打标记
[root@lvs ~]# ipvsadm -C
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@lvs ~]# iptables -t mangle -A PREROUTING -d 192.168.0.200 -p tcp -m multiport --dports 80,443 -j MARK --set-mark 66
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@lvs ~]# ipvsadm -A -t 192.168.0.200:443 -s rr
[root@lvs ~]# ipvsadm -a -t 192.168.0.200:443 -r 192.168.0.10:443 -g
[root@lvs ~]# ipvsadm -a -t 192.168.0.200:443 -r 192.168.0.20:443 -g
[root@lvs ~]# ipvsadm -A -t 192.168.0.200:80 -s rr
[root@lvs ~]# ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.10:80 -g -w 1
[root@lvs ~]# ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.20:80 -g -w 2
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.200:80 rr
-> 192.168.0.10:80 Route 1 0 0
-> 192.168.0.20:80 Route 2 0 0
TCP 192.168.0.200:443 rr
-> 192.168.0.10:443 Route 1 0 0
-> 192.168.0.20:443 Route 1 0 0
为端口做标记:
[root@lvs ~]# iptables -t mangle -A PREROUTING -d 192.168.0.200 -p tcp -m multiport --dports 80,443 -j MARK --set-mark 66 为端口做标记
第三步:在client主机中测试
[root@client ~]# systemctl status firewalld
○ firewalld.service
Loaded: masked (Reason: Unit firewalld.service is masked.)
Active: inactive (dead)
[root@client ~]# systemctl stop firewalld 关闭防火墙
测试一下连通性 ping 192.268.0.10/20/50
[root@client ~]# curl 192.168.0.200;curl -k https://192.168.0.200
webserver1 - 192.168.0.10
webserver2 - 192.168.0.20
[root@client ~]# curl 192.168.0.200;curl -k https://192.168.0.200
webserver2 - 192.168.0.20
webserver1 - 192.168.0.10