lvs模式配置
- lvs简介
LVS(Linux Virtual Server)即Linux虚拟服务器,是由章文嵩博士主导的开源负载均衡项目,目前LVS已经被集成到Linux内核模块中。该项目在Linux内核中实现了基于IP的数据请求负载均衡调度方案,其体系结构如图1所示,终端互联网用户从外部访问公司的外部负载均衡服务器,终端用户的Web请求会发送给LVS调度器,调度器根据自己预设的算法决定将该请求发送给后端的某台Web服务器,比如,轮询算法可以将外部的请求平均分发给后端的所有服务器,终端用户访问LVS调度器虽然会被转发到后端真实的服务器,但如果真实服务器连接的是相同的存储,提供的服务也是相同的服务,最终用户不管是访问哪台真实服务器,得到的服务内容都是一样的,整个集群对用户而言都是透明的。最后根据LVS工作模式的不同,真实服务器会选择不同的方式将用户需要的数据发送到终端用户,LVS工作模式分为NAT模式、TUN模式、以及DR模式。
1.1 Ivs: Linux Virtual Server
四层交换,四层路由:
根据请求报文的目标IP和PORT将其转发至后端主机集群中的某-台主机(根据挑选算法)
netfilter
- PREROUTING --> INPUT
- PREROUTING --> FORWARD --> POSTROUTING
- OUTPUT --> POSTROUTING
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:
- 调度器: director, dispatcher, balancer
- RS: Real Server
- Client IP: CIP
- Director Virutal IP: VIP
- Director IP: DIP
- Real Server IP: RIP
配置NAT模式
- 准备工作
服务器名称 | IP | 版本 |
---|---|---|
lvs | 192.168.78.134 | centos8/redhat8 |
RS1 | 192.168.78.135 | centos8/redhat8 |
RS2 | 192.168.78.136 | centos8/redhat8 |
配置RS1主机
//修改主机名
[root@localhost ~]# hostnamectl set-hostname RS1
[root@localhost ~]# bash
[root@RS1 ~]#
//关闭防火墙和selinux
[root@RS1 ~]# systemctl disable --now firewalld
[root@RS1 ~]# vim /etc/selinux/config
SELINUX=disabled
//查看ip
[root@RS1 ~]# ip addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:d2:6d:a2 brd ff:ff:ff:ff:ff:ff
inet 192.168.78.135/24 brd 192.168.78.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed2:6da2/64 scope link noprefixroute
valid_lft forever preferred_lft forever
//安装httpd配置
[root@RS1 ~]# yum install httpd -y
[root@RS1 ~]# systemctl enable --now httpd
[root@RS1 ~]# echo 'RS1' > /var/www/html/index.html
[root@RS1 ~]# cat /var/www/html/index.html
RS1
//配置网卡
[root@RS1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.78.135
PREFIX=24
GATEWAY=192.168.78.134 //调度器的主机IP
DNS1=114.114.114.114
[root@RS1 ~]# systemctl restart NetworkManager
访问网页
配置RS2主机
//修改主机名
[root@localhost ~]# hostnamectl set-hostname RS2
[root@localhost ~]# bash
[root@RS2 ~]#
//关闭防火墙和selinux
[root@RS2 ~]# systemctl disable --now firewalld
[root@RS2 ~]# vim /etc/selinux/config
SELINUX=disabled
//查看ip
[root@RS2 ~]# ip a s ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:db:88:69 brd ff:ff:ff:ff:ff:ff
inet 192.168.78.136/24 brd 192.168.78.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.78.136/24 brd 192.168.78.255 scope global secondary dynamic noprefixroute ens33
valid_lft 1781sec preferred_lft 1781sec
inet6 fe80::20c:29ff:fedb:8869/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@RS2 ~]#
//安装apache配置
[root@RS2 ~]# yum install httpd -y
[root@RS2 ~]# systemctl enable --now httpd
[root@RS2 ~]# echo 'RS2' > /var/www/html/index.html
[root@RS2 ~]# cat /var/www/html/index.html
RS2
//配置网卡
[root@RS2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.78.136
PREFIX=24
GATEWAY=192.168.78.134 //调度器的主机IP
DNS1=114.114.114.114
[root@RS2 ~]# systemctl restart NetworkManager
访问网页
配置LVS主机
//设置主机名
[root@localhost ~]# hostnamectl set-hostname LVS
[root@localhost ~]# bash
[root@LVS ~]#
//关闭防火墙和selinux
[root@LVS ~]# systemctl disable --now firewalld.service
[root@LVS ~]# vim /etc/selinux/config
SELINUX=disabled
[root@LVS ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1 //加入这一个
//让其生效
[root@LVS ~]# sysctl -p
net.ipv4.ip_forward = 1
//安装配置ipvsadm
[root@LVS ~]# yum install ipvsadm -y
[root@LVS ~]# ipvsadm -A -t 192.168.78.134:80 -s rr
[root@LVS ~]# ipvsadm -a -t 192.168.78.134:80 -r 192.168.220.135:80 -m
[root@LVS ~]# ipvsadm -a -t 192.168.78.134:80 -r 192.168.220.136:80 -m
[root@LVS ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
[root@LVS ~]# cat /etc/sysconfig/ipvsadm
-A -t 192.168.78.134:80 -s rr
-a -t 192.168.78.134:80 -r 192.168.220.135:80 -m -w 1
-a -t 192.168.78.134:80 -r 192.168.220.136: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 192.168.78.134:80 rr
-> 192.168.78.135:80 Masq 1 0 0
-> 192.168.78.136:80 Masq 1 0 0
[root@LVS ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.78.134
PREFIX=24
GATEWAY=192.168.78.2
[root@LVS ~]# systemctl restart NetworkManager
//访问
[root@LVS ~]# curl 192.168.78.134
RS2
[root@LVS ~]# curl 192.168.78.134
RS1
配置DR模式
- 准备工作
服务器名称 | IP | 版本 |
---|---|---|
lvs | 192.168.78.134 | centos8/redhat8 |
RS1 | 192.168.78.135 | centos8/redhat8 |
RS2 | 192.168.78.136 | centos8/redhat8 |
RS1主机
记得恢复快照
//修改主机名
[root@localhost ~]# hostnamectl set-hostname RS1
[root@localhost ~]# bash
[root@RS1 ~]#
//查看ip
[root@RS1 ~]# ip addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:d2:6d:a2 brd ff:ff:ff:ff:ff:ff
inet 192.168.78.135/24 brd 192.168.78.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed2:6da2/64 scope link noprefixroute
valid_lft forever preferred_lft forever
//关闭防火墙和selinux
[root@RS1 ~]# systemctl disable --now firewalld
[root@RS1 ~]# vim /etc/selinux/config
SELINUX=disabled
//安装httpd配置网页
[root@RS1 ~]# yum install httpd -y
[root@RS1 ~]# systemctl enable --now httpd
[root@RS1 ~]# echo 'RS1' > /var/www/html/index.html
[root@RS1 ~]# cat /var/www/html/index.html
RS1
RS2主机
//修改主机名
[root@localhost ~]# hostnamectl set-hostname RS2
[root@localhost ~]# bash
[root@RS2 ~]#
//ip
[root@RS2 ~]# ip a s ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:db:88:69 brd ff:ff:ff:ff:ff:ff
inet 192.168.78.136/24 brd 192.168.78.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.78.136/24 brd 192.168.78.255 scope global secondary dynamic noprefixroute ens33
valid_lft 1781sec preferred_lft 1781sec
inet6 fe80::20c:29ff:fedb:8869/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@RS2 ~]#
//关闭防火墙和selinux
[root@RS2 ~]# systemctl disable --now firewalld
[root@RS2 ~]# vim /etc/selinux/config
SELINUX=disabled
//安装apache配置
[root@RS2 ~]# yum install httpd -y
[root@RS2 ~]# systemctl enable --now httpd
[root@RS2 ~]# echo 'RS2' > /var/www/html/index.html
[root@RS2 ~]# cat /var/www/html/index.html
RS2
配置LVS主机
//修改主机名
[root@localhost ~]# hostnamectl set-hostname LVS
[root@localhost ~]# bash
[root@LVS ~]#
//关闭防火墙和selinux
[root@LVS ~]# systemctl disable --now firewalld.service
[root@LVS ~]# vim /etc/selinux/config
SELINUX=disabled
[root@LVS ~]# yum install net-tools -y //安装软件包
//配置dip
[root@LVS ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.78.134
PREFIX=24
GATEWAY=192.168.78.2
DNS1=114.114.114.114
//配置vip
[root@LVS ~]# ifconfig ens33:0 192.168.78.254/32 broadcast 192.168.78.254 up //配置vip为192.168.78.254 广播地址为192.168.78.254
//查看网卡
[root@LVS ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.78.134 netmask 255.255.255.0 broadcast 192.168.78.255
inet6 fe80::20c:29ff:fe8c:8ed5 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:8c:8e:d5 txqueuelen 1000 (Ethernet)
RX packets 31174 bytes 18679875 (17.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 30364 bytes 5067821 (4.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 //vip
inet 192.168.78.254 netmask 0.0.0.0 broadcast 192.168.78.254
ether 00:0c:29:8c:8e:d5 txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
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
配置RS1的rip
注意:此处必须先修改网卡内核参数然后再配置vip,因为如果先配vip,vip配好后就会立马通告给别人,而修改内核参数就是为了不通告
[root@RS1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.78.135
PREFIX=24
GATEWAY=192.168.78.2
DNS1=114.114.114.114
//末尾加入下面两行
[root@RS1 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
//-P 读取
[root@RS1 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
//配置vip
[root@RS1 ~]# ifconfig lo:0 192.168.78.254/32 broadcast 192.168.78.254 up
[root@RS1 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.78.135 netmask 255.255.255.0 broadcast 192.168.78.255
inet6 fe80::20c:29ff:fed2:6da2 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d2:6d:a2 txqueuelen 1000 (Ethernet)
RX packets 37460 bytes 2876407 (2.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 56204 bytes 10689362 (10.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
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<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.78.254 netmask 0.0.0.0
loop txqueuelen 1000 (Local Loopback)
配置RS1的rip
[root@RS2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.78.136
PREFIX=24
GATEWAY=192.168.78.2
DNS1=114.114.114.114
//末尾加入下面两行
[root@RS2 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
//-p 读取
[root@RS2 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
//配置vip
[root@RS2 ~]# ifconfig lo:0 192.168.78.254/32 broadcast 192.168.78.254 up
[root@RS2 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.78.136 netmask 255.255.255.0 broadcast 192.168.78.255
inet6 fe80::20c:29ff:fedb:8869 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:db:88:69 txqueuelen 1000 (Ethernet)
RX packets 39942 bytes 3059152 (2.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 59431 bytes 11212720 (10.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
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<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.78.254 netmask 0.0.0.0
loop txqueuelen 1000 (Local Loopback)
调度器配置
//分别在三台主机上配置路由
[root@RS1 ~]# route add -host 192.168.78.254 dev lo:0
[root@RS2 ~]# route add -host 192.168.78.254 dev lo:0
[root@LVS ~]# route add -host 192.168.78.254 dev ens33:0
//调度器配置
[root@LVS ~]# yum install ipvsadm -y
[root@LVS ~]# ipvsadm -A -t 192.168.78.254:80 -s wrr
//-g表示dr模式
[root@LVS ~]# ipvsadm -a -t 192.168.78.254:80 -r 192.168.78.135:80 -g
[root@LVS ~]# ipvsadm -a -t 192.168.78.254:80 -r 192.168.78.136:80 -g
[root@LVS ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
//真机cmd命令行测试
C:\Users\wg>curl 192.168.78.254
RS1
C:\Users\wg>curl 192.168.78.254
RS2
C:\Users\wg>curl 192.168.78.254
RS1
C:\Users\wg>curl 192.168.78.254
RS2
[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.78.254:80 wrr
-> 192.168.78.135:80 Route 1 0 2//表示访问次数
-> 192.168.78.136:80 Route 1 0 2