LVS(Linux Virtual Server)的工作原理可以简单概括为以下几个步骤:
通过以上步骤,LVS 实现了负载均衡和高可用性。它将客户端的请求分发到多个后端服务器上,以提高系统的性能和可扩展性,并确保服务的高可用性,即使其中一台后端服务器发生故障,其他正常运行的服务器仍然可以继续提供服务。
NAT(network address translation) 网络地址转换,其主要原理是修改数据报头,使得位于企业内部的私有ip地址可以访问外王,以及外部用户可以访问位于公司内部的私有的ip主机。
LVS(NAT)模式的集群环境中,所有数据包的请求的回应的哦欧需要经过调度器处理,但是在TUN模式中,将NAT模式中的问题有所解决。因为数据包的请求包往往远远小于响应数据包的大小。因为响应数据包中有包含客户需要的具体数据所以,TUN的原理就是将请求与响应数据分离。,让调度器仅仅处理数据请求,让真实的服务器响应数据包直接返回给客户。
在DR模式下,LVS负载均衡器作为网关插入到后端服务器和客户端之间。客户端请求到达LVS负载均衡器后,LVS负载均衡器会将请求直接转发给后端服务器,而响应流量则直接返回给客户端,绕过LVS负载均衡器。这样可以避免了请求流量经过LVS负载均衡器的转发,提高了性能。
环境说明:
服务器类型 | IP地址 | 系统版本 |
---|---|---|
DR | 192.168.195.133 | centos 8 |
RS1 | 192.168.195.134 | centos 8 |
RS2 | 192.168.195.135 | centos 8 |
在DR上配置
//关闭防火墙和selinux
[root@DR ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@DR ~]# setenforce 0
//设置yum源并安装所需软件包
[root@DR ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@DR ~]# yum -y install epel-release
[root@DR ~]# yum makecache
[root@DR ~]# yum -y install net-tools ipvsadm
在RS1上配置
//关闭防火墙和selinux
[root@RS1 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS1 ~]# setenforce 0
//设置yum源并安装所需软件包
[root@RS1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@RS1 ~]# yum -y install epel-release
[root@RS1 ~]# yum makecache
[root@RS1 ~]# yum -y install net-tools
在RS2上配置
//关闭防火墙和selinux
[root@RS2 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS3 ~]# setenforce 0
//设置yum源并安装所需软件包
[root@RS2 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@RS2 ~]# yum -y install epel-release
[root@RS2 ~]# yum makecache
[root@RS2 ~]# yum -y install net-tools
1.lvs-dr配置:director只需要一块网卡,vip配置在lo接口中即可,此处假设dip与rip在同一网段
配置director的ip地址信息(dip,vip)
配置dip(编辑物理网卡配置文件/etc/sysconfig/network-scripts/ifcfg-eth160)
配置vip:
ifconfig eth0:0 vip/32 broadcast vip up
由于我们的虚拟机本身是配置的DHCP自动下发ip,所以我们的dip不用再次设置,需要我们操作的就是添加一个vip
在DR上配置vip:
//此处我们设置的vip为192.168.195.100,需要与我们的RS处于同一网段
[root@DR ~]# ifconfig ifcfg-ens160:0 192.168.195.100/32 broadcast 192.168.195.100 up
[root@DR ~]# ifconfig
ens160: flags=4163,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.195.133 netmask 255.255.255.0 broadcast 192.168.195.255
inet6 fe80::411e:cef7:14ab:7e28 prefixlen 64 scopeid 0x20
ether 00:0c:29:1f:2f:75 txqueuelen 1000 (Ethernet)
RX packets 1307 bytes 435094 (424.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 913 bytes 94925 (92.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens160:0: flags=4163,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.195.100 netmask 0.0.0.0 broadcast 192.168.195.100
ether 00:0c:29:1f:2f:75 txqueuelen 1000 (Ethernet)
//此处我们可以查看到我们添加的vip
lo: flags=73,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
配置rip(编辑物理网卡配置文件/etc/sysconfig/network-scripts/ifcfg-eth160)
同样,我们目前所演示的三台虚拟机都是以DHCP的方式下发ip地址,所以同样不用再次配置rip
[root@RS1 ~]# ip a
1: lo: ,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.195.100/0 brd 192.168.195.100 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: ,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:8b:9b:af brd ff:ff:ff:ff:ff:ff
inet 192.168.195.134/24 brd 192.168.195.255 scope global dynamic noprefixroute ens160
valid_lft 1684sec preferred_lft 1684sec
inet6 fe80::3aa0:b2e5:ecf1:7bd1/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@RS2 ~]# ip a
1: lo: ,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.195.100/0 brd 192.168.195.100 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: ,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:bd:6c:95 brd ff:ff:ff:ff:ff:ff
inet 192.168.195.135/24 brd 192.168.195.255 scope global dynamic noprefixroute ens160
valid_lft 1688sec preferred_lft 1688sec
inet6 fe80::bccb:8e1a:5b03:dd61/64 scope link noprefixroute
valid_lft forever preferred_lft forever
在RS1上配置:
[root@RS1 ~]# vim /etc/sysctl.conf
[root@RS1 ~]# 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.conf.all.arp_ignore = 1 //添加这两行内容
net.ipv4.conf.all.arp_announce = 2
[root@RS1 ~]# sysctl -p //重新读配置
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
在RS2上配置:
[root@RS2 ~]# vim /etc/sysctl.conf
[root@RS2 ~]# 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.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@RS2 ~]# sysctl -p //重新读配置
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
在RS1上配置:
[root@RS1 ~]# ifconfig lo:0 192.168.195.100/32 broadcast 192.168.195.100 up
[root@RS1 ~]# ifconfig
ens160: flags=4163,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.195.134 netmask 255.255.255.0 broadcast 192.168.195.255
inet6 fe80::3aa0:b2e5:ecf1:7bd1 prefixlen 64 scopeid 0x20
ether 00:0c:29:8b:9b:af txqueuelen 1000 (Ethernet)
RX packets 887 bytes 74380 (72.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 576 bytes 63180 (61.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:0: flags=73,LOOPBACK,RUNNING> mtu 65536
inet 192.168.195.100 netmask 0.0.0.0 //添加成功
loop txqueuelen 1000 (Local Loopback)
在RS2上配置:
[root@RS2 ~]# ifconfig lo:0 192.168.195.100/32 broadcast 192.168.195.100 up
[root@RS2 ~]# ifconfig
ens160: flags=4163,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.195.135 netmask 255.255.255.0 broadcast 192.168.195.255
inet6 fe80::bccb:8e1a:5b03:dd61 prefixlen 64 scopeid 0x20
ether 00:0c:29:bd:6c:95 txqueuelen 1000 (Ethernet)
RX packets 372 bytes 32447 (31.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 286 bytes 32632 (31.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:0: flags=73,LOOPBACK,RUNNING> mtu 65536
inet 192.168.195.100 netmask 0.0.0.0 //添加成功
loop txqueuelen 1000 (Local Loopback)
注:此处必须先修改网卡内核参数然后再配置vip,因为如果先配vip,vip配好后就会立马通告给别人,而修改内核参数就是为了不通告
route add -host vip dev interface:0
在DR上配置:
[root@DR ~]# route add -host 192.168.195.100 dev ens160:0
[root@DR ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.195.2 0.0.0.0 UG 100 0 0 ens160
192.168.195.0 0.0.0.0 255.255.255.0 U 100 0 0 ens160
192.168.195.100 0.0.0.0 255.255.255.255 UH 0 0 0 ens160
在RS1上配置:
[root@RS1 ~]# route add -host 192.168.195.100 dev lo:0
[root@RS1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.195.2 0.0.0.0 UG 100 0 0 ens160
192.168.195.0 0.0.0.0 255.255.255.0 U 100 0 0 ens160
192.168.195.100 0.0.0.0 255.255.255.255 UH 0 0 0 lo
在RS上配置:
[root@RS2 ~]# route add -host 192.168.195.100 dev lo:0
[root@RS2 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.195.2 0.0.0.0 UG 100 0 0 ens160
192.168.195.0 0.0.0.0 255.255.255.0 U 100 0 0 ens160
192.168.195.100 0.0.0.0 255.255.255.255 UH 0 0 0 lo
[root@DR ~]# ipvsadm -A -t 192.168.195.100:80 -s wrr
[root@DR ~]# ipvsadm -a -t 192.168.195.100:80 -r 192.168.195.134:80 -g
[root@DR ~]# ipvsadm -a -t 192.168.195.100:80 -r 192.168.195.135:80 -g
[root@DR ~]# 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.195.100:80 wrr
-> 192.168.195.134:80 Route 1 0 0
-> 192.168.195.135:80 Route 1 0 0
保存配置:
[root@DR ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
[root@DR ~]# cat /etc/sysconfig/ipvsadm
-A -t 192.168.195.100:80 -s wrr
-a -t 192.168.195.100:80 -r 192.168.195.134:80 -g -w 1
-a -t 192.168.195.100:80 -r 192.168.195.135:80 -g -w 1
总的来说,IPVS是一种强大的集群服务技术,能够提供高性能、高可用的服务,并提供灵活的配置选项满足不同应用的需求。它在构建大规模、高并发的网络应用中得到广泛应用。