赞
踩
LVS(Linux Virtual Server)即Linux虚拟服务器,是由章文嵩博士主导的开源负载均衡项目,目前LVS已经被集成到Linux内核模块中。该项目在Linux内核中实现了基于IP的数据请求负载均衡调度方案,其体系结构如图1所示,终端互联网用户从外部访问公司的外部负载均衡服务器,终端用户的Web请求会发送给LVS调度器,调度器根据自己预设的算法决定将该请求发送给后端的某台Web服务器,比如,轮询算法可以将外部的请求平均分发给后端的所有服务器,终端用户访问LVS调度器虽然会被转发到后端真实的服务器,但如果真实服务器连接的是相同的存储,提供的服务也是相同的服务,最终用户不管是访问哪台真实服务器,得到的服务内容都是一样的,整个集群对用户而言都是透明的。最后根据LVS工作模式的不同,真实服务器会选择不同的方式将用户需要的数据发送到终端用户,LVS工作模式分为NAT模式、TUN模式、以及DR模式。
四层交换,四层路由:
根据请求报文的目标IP和PORT将其转发至后端主机集群中的某-台主机(根据挑选算法)
netfilter :
Ivs:
ipvsadm/ipvs
ipvsadm:用户空间的命令行工具,用于管理集群服务
ipvs:工作于内核中netfilter INPUT钩子.上
支持TCP,UDP,AH, EST, AH_ EST, SCTP等诸多协议
grep -i -A 2 "ipvs’ /boot/config-2.6.32-504.el6.x86_ 64
查看系统对ipvs的支持情况,包括算法
Ivs arch:
lvs,4种工作模式
一,LVS_NAT
二,LVS_DR
三,LVS_TUN
静态方法:仅根据算法本身进行调度
服务器名称 | IP | 版本 |
---|---|---|
lvs | 192.168.106.20 | centos8/redhat8 |
RS1 | 192.168.106.16 | centos8/redhat8 |
RS2 | 192.168.106.17 | centos8/redhat8 |
为lvs这台虚拟机添加一块网卡
配置IP地址信息
director (dip vip )
RS (rip)
//多了一个ens224网卡 [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 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:9c:36:92 brd ff:ff:ff:ff:ff:ff inet 192.168.106.20/24 brd 192.168.106.255 scope global noprefixroute ens160 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe9c:3692/64 scope link valid_lft forever preferred_lft forever 3: ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:9c:36:9c brd ff:ff:ff:ff:ff:ff inet 192.168.127.130/24 brd 192.168.127.255 scope global dynamic noprefixroute ens224 valid_lft 1676sec preferred_lft 1676sec inet6 fe80::a7ee:c9be:508c:e9b6/64 scope link noprefixroute valid_lft forever preferred_lft forever //配置ens160网卡 [root@lvs ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens160 TYPE=Ethernet BOOTPROTO=static NAME=ens160 DEVICE=ens160 ONBOOT=yes PREFIX=24 IPADDR=192.168.106.20 GATEWAY=192.168.106.2 DNS1=8.8.8.8 //配置ens224网卡 [root@lvs ~]# cd /etc/sysconfig/network-scripts/ [root@lvs network-scripts]# cp ifcfg-ens160 ifcfg-ens224 [root@lvs network-scripts]# ls ifcfg-ens160 ifcfg-ens224 [root@lvs network-scripts]# cat ifcfg-ens224 //修改如下内容 TYPE=Ethernet BOOTPROTO=static NAME=ens160 DEVICE=ens160 ONBOOT=yes PREFIX=24 IPADDR=192.168.127.130 //在虚拟环境下只能有一个网关,所以这里就没有设置网关 //重启一下 [root@lvs network-scripts]# reboot //ping一下 [root@lvs ~]# ping www.baidu.com PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data. 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=128 time=33.1 ms 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=128 time=29.9 ms 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=128 time=30.4 ms 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=4 ttl=128 time=29.4 ms ^C --- www.a.shifen.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3007ms rtt min/avg/max/mdev = 29.382/30.695/33.050/1.418 ms //是可以访问到外网的 //配置RS1网卡 [root@RS1 ~]# yum -y install httpd //先安装httpd [root@RS1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens160 TYPE=Ethernet BOOTPROTO=static NAME=ens160 DEVICE=ens160 ONBOOT=yes IPADDR=192.168.106.16 PREFIX=24 GATEWAY=192.168.106.20 //这里的网关要指向lvs的IP DNS1=8.8.8.8 //配置RS2的网卡 [root@RS2 ~]# yum -y install httpd //先安装httpd [root@RS2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens160 TYPE=Ethernet BOOTPROTO=static NAME=ens160 DEVICE=ens160 ONBOOT=yes IPADDR=192.168.106.17 PREFIX=24 GATEWAY=192.168.106.20 //这里的网关要指向lvs的IP DNS1=8.8.8.8 //开启director的IP转发功能 [root@lvs ~]# vim /etc/sysctl.conf [root@lvs ~]# 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@lvs ~]# sysctl -p net.ipv4.ip_forward = 1 [root@lvs ~]# dnf -y install ipvsadm //安装这个命令 //在director上添加并保存规则 [root@lvs ~]# ipvsadm -A -t 192.168.127.130:80 -s rr [root@lvs ~]# ipvsadm -a -t 192.168.127.130:80 -r 192.168.106.16 -m [root@lvs ~]# ipvsadm -a -t 192.168.127.130:80 -r 192.168.106.17 -m [root@lvs ~]# ipvsadm -l IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP lvs:http rr -> 192.168.106.16:http Masq 1 0 0 -> 192.168.106.17:http Masq 1 0 0 [root@lvs ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm //写到这文件中 [root@lvs ~]# cat /etc/sysconfig/ipvsadm -A -t 192.168.127.130:80 -s rr -a -t 192.168.127.130:80 -r 192.168.106.16:80 -m -w 1 -a -t 192.168.127.130:80 -r 192.168.106.17:80 -m -w 1 //关闭sellinux和防火墙 //RS1 [root@RS1 html]# 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 html]# setenforce 0 [root@RS1 ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled //修改为disabled # SELINUXTYPE= can take one of these three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted //RS2 [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@RS2 ~]# vim /etc/selinux/config [root@RS2 ~]# setenforce 0 [root@RS2 ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled //修改为disabled # SELINUXTYPE= can take one of these three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted //lvs [root@lvs ~]# systemctl disable --now firewalld [root@lvs ~]# vim /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled //修改为disabled # SELINUXTYPE= can take one of these three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted [root@lvs ~]# setenforce 0 setenforce: SELinux is disabled //RS1和RS2分别放一个不同的网站。过程不做演示 //在虚拟机中测试 [root@lvs ~]# curl 192.168.127.130 hello world [root@lvs ~]# curl 192.168.127.130 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>在线考试试卷</title> </head> <body> <p><h1>HTML在线考试试题</h1></p> </body> <ol type="1"> <li>.HTML中,换行使用的标签是()。</li> <ol type="A"> <li><br/></li> <li><p></li> <li><hr/></li> <li><img/></li> </ol> <li>.<img/>标签的()属性用于指定图像的地址。</li> <ol type="A"> <li>alt</li> <li>href</li> <li>src</li> <li>addr</li> </ol> <li>.创建一个超级链接使用的是()标签。</li> <ol type="A"> <li><a></li> <li><ol></li> <li><img/></li> <li><hr/></li> </ol> <li>.<img/>标签的()属性用来设置图片与旁边内容的水平距离。</li> <ol type="A"> <li>hspace</li> <li>vspace</li> <li>border</li> <li>alt</li> </ol> <li>.下面HTML结构中,不属于列表结构的是()。</li> <ol type="A"> <li>ul-li</li> <li>ol-li</li> <li>dl-dt-dd</li> <li>p-br</li> </ol> </ol> </html> [root@lvs ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens224 [root@lvs ~]# curl 192.168.127.130 hello world [root@lvs ~]# curl 192.168.127.130 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>在线考试试卷</title> </head> <body> <p><h1>HTML在线考试试题</h1></p> </body> <ol type="1"> <li>.HTML中,换行使用的标签是()。</li> <ol type="A"> <li><br/></li> <li><p></li> <li><hr/></li> <li><img/></li> </ol> <li>.<img/>标签的()属性用于指定图像的地址。</li> <ol type="A"> <li>alt</li> <li>href</li> <li>src</li> <li>addr</li> </ol> <li>.创建一个超级链接使用的是()标签。</li> <ol type="A"> <li><a></li> <li><ol></li> <li><img/></li> <li><hr/></li> </ol> <li>.<img/>标签的()属性用来设置图片与旁边内容的水平距离。</li> <ol type="A"> <li>hspace</li> <li>vspace</li> <li>border</li> <li>alt</li> </ol> <li>.下面HTML结构中,不属于列表结构的是()。</li> <ol type="A"> <li>ul-li</li> <li>ol-li</li> <li>dl-dt-dd</li> <li>p-br</li> </ol> </ol> </html> //可以看到访问的是同一个IP地址却是不同的网页。证明已经成功
Ivs-dr配置: director只需要 一块网卡,vip配 置在Io接口中即可,此处假设dip与rip在同-网段
配置director的ip地址信息(dip, vip)
配置dip (编辑物理网卡配置文件/etc/sysconfig/network-scripts/ifcfg-eth0)
配置vip: .
ifconfig eth0:0 vip/32 broadcast vip up
配置RS的ip地址信息: .
配置rip (编辑物理网卡配置文件/etc/sysconfig/network-scripts/ifcfg-eth0)
修改网卡内核参数:编辑/etc/sysctl.conf文件, 添加如下内容:
net.ipv4.conf. alarp ignore = 1
net.ipv4.conf. alarp _announce = 2
配置vip:
ifconfig lo:0 vip/32 broadcast vip up
注意:此处必须先修改网卡内核参数然后再配置vip,因为如果先配vip,vip配好 后就会立马通告给别人,而修改馁核参数就是为了不通告
配置路由信息:在director和所有RS 上进行如下配置:
route add -host vip dev interface:0
先把lvs虚拟机恢复恢复快照
//修改名字 [root@localhost ~]# hostnamectl set-hostname lvs [root@localhost ~]# bash [root@lvs ~]# //关闭防火墙和selinux [root@lvs ~]# systemctl disable --now firewalld [root@lvs ~]# setenforce 0 setenforce: SELinux is disabled [root@lvs ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled //改为disabled # SELINUXTYPE= can take one of these three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted //开启httpd并RS1和RS2的网站不同 //RS2 [root@RS2 ~]# systemctl enable --now httpd Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service. [root@RS2 ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:111 0.0.0.0:* LISTEN 0 32 192.168.122.1:53 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 5 127.0.0.1:631 0.0.0.0:* LISTEN 0 128 [::]:111 [::]:* LISTEN 0 128 *:80 *:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 5 [::1]:631 [::]:* [root@RS2 ~]# echo "hello RS2" > /var/www/html/index.html //RS1 [root@RS1 ~]# systemctl enable --now httpd Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service. [root@RS1 ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:111 0.0.0.0:* LISTEN 0 32 192.168.122.1:53 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 5 127.0.0.1:631 0.0.0.0:* LISTEN 0 128 [::]:111 [::]:* LISTEN 0 128 *:80 *:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 5 [::1]:631 [::]:* [root@RS1 ~]# echo "hello RS1" > /var/www/html/index.html [root@lvs ~]# dnf -y install net-tools //安装这个包 //配置dip [root@lvs network-scripts]# cat ifcfg-ens160 TYPE=Ethernet BOOTPROTO=static NAME=ens160 DEVICE=ens160 ONBOOT=yes PREFIX=24 GATEWAY=192.168.106.2 IPADDR=192.168.106.20 DNS1=8.8.8.8 //让网卡生效 [root@lvs network-scripts]# ifdown ens160;ifup ens160 Connection 'ens160' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1) Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2) //配置vip [root@lvs ~]# ifconfig ens160:0 192.168.106.250/32 broadcast 192.168.106.250 up //配置RS的ip地址信息 [root@RS1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens160 TYPE=Ethernet BOOTPROTO=static NAME=ens160 DEVICE=ens160 ONBOOT=yes IPADDR=192.168.106.16 PREFIX=24 GATEWAY=192.168.106.2 DNS1=8.8.8.8 //重置网卡 [root@RS1 ~]# ifdown ens160;ifup ens160 成功停用连接 "ens160"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/5) 连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/6) //修改RS2网卡 [root@RS2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens160 TYPE=Ethernet BOOTPROTO=static NAME=ens160 DEVICE=ens160 ONBOOT=yes IPADDR=192.168.106.17 PREFIX=24 GATEWAY=192.168.106.2 DNS1=8.8.8.8 //重置RS2网卡 [root@RS2 ~]# ifdown ens160;ifup ens160 成功停用连接 "ens160"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4) 连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/5) //修改网卡内核参数:编辑/etc/sysctl.conf文件, 添加如下内容: //配置RS2 [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 ~]# 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 //配置vip //RS1 [root@RS1 network-scripts]# ifconfig lo:0 192.168.106.250/32 broadcast 192.168.106.250 up [root@RS1 network-scripts]# ifconfig //能看到下面内容就说明成功 lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 192.168.106.250 netmask 0.0.0.0 loop txqueuelen 1000 (Local Loopback) //RS2 [root@RS2 ~]# ifconfig lo:0 192.168.106.250/32 broadcast 192.168.106.250 up [root@RS2 ~]# ifconfig lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 192.168.106.250 netmask 0.0.0.0 loop txqueuelen 1000 (Local Loopback) //配置路由信息:在director和所有RS上进行如下配置: [root@lvs ~]# route add -host 192.168.106.250 dev ens160:0 [root@RS1 ~]# route add -host 192.168.106.250 dev lo:0 [root@RS2 ~]# route add -host 192.168.106.250 dev lo:0 //在director上添加并保存规则 [root@lvs ~]# dnf -y install ipvsadm //下载这个包 [root@lvs ~]# ipvsadm -A -t 192.168.106.250:80 -s wrr [root@lvs ~]# ipvsadm -a -t 192.168.106.250:80 -r 192.168.106.16 -g [root@lvs ~]# ipvsadm -a -t 192.168.106.250:80 -r 192.168.106.17 -g [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.106.250:80 wrr -> 192.168.106.16:80 Route 1 0 0 -> 192.168.106.17:80 Route 1 0 0 [root@lvs ~]# ipvsadm -S > /etc/sysconfig/ipvsadm [root@lvs ~]# cat /etc/sysconfig/ipvsadm -A -t lvs:http -s wrr -a -t lvs:http -r 192.168.106.16:http -g -w 1 -a -t lvs:http -r 192.168.106.17:http -g -w 1
在网站上填写你设置的vip
第一次访问到RS1
刷新一下就会访问到RS2,
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。