1.准备3台虚拟机node20,www1,www2
2.配置3台虚拟机的网络
eth0在同一网段,DIP,RIP在一个网段。
#我的环境简介
DR负载调度器:10.206.49.60
VIP(虚拟回环):10.206.49.100
真实服务器1:10.206.49.64
真实服务器2:10.206.49.65
3.配置LVS的VIP
在调度服务器node20上配置一个虚拟服务器ip称为vip
在配置虚拟服务器ip之前,先查看当前的网卡
[root@node20 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.206.49.60 netmask 255.255.255.0 broadcast 10.206.49.255
inet6 fe80::5054:ff:fe54:e971 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:54:e9:71 txqueuelen 1000 (Ethernet)
RX packets 96116 bytes 34524498 (32.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 40272 bytes 81511214 (77.7 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 53313 bytes 1133428652 (1.0 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 53313 bytes 1133428652 (1.0 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:6d:f5:cd txqueuelen 1000 (Ethernet)
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
配置虚拟服务器ip
[root@node20 ~]# ifconfig eth0:2 10.206.49.100 netmask 255.255.255.0
[root@node20 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.206.49.60 netmask 255.255.255.0 broadcast 10.206.49.255
inet6 fe80::5054:ff:fe54:e971 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:54:e9:71 txqueuelen 1000 (Ethernet)
RX packets 96433 bytes 34553913 (32.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 40471 bytes 81532872 (77.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.206.49.100 netmask 255.255.255.0 broadcast 10.206.49.255
ether 52:54:00:54:e9:71 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 53353 bytes 1133431524 (1.0 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 53353 bytes 1133431524 (1.0 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:6d:f5:cd txqueuelen 1000 (Ethernet)
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
[root@node20 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
说明:网卡还是同一个,只是在里面虚拟了一个ip
4.调整RS的响应与通告级别(每一台RS都配,这里仅展示一台)
arp_ignore:定义接收到ARP请求时响应级别
0:只要本地配置的有相应地址,就给予响应
1:仅在请求的目标(MAC)地址配置请求到达的接口上的时候,才给予响应
arp_announce:定义将自己地址向外通告时的通告级别
0:将本地任何接口上的任何地址向外通告
1:试图仅向目标网络通告与其网络匹配的地址
2:仅向与本地接口上地址匹配的网络进行通告
[root@www1 ~]# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
[root@www1 ~]# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
[root@www1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@www1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
5.配置RS的VIP(每一台RS都配,这里仅展示一台)
[root@www1 all]# ifconfig lo:8 10.206.49.100 netmask 255.255.255.255
[root@www1 all]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.206.49.64 netmask 255.255.255.0 broadcast 10.206.49.255
inet6 fe80::5054:ff:fea5:67b8 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:a5:67:b8 txqueuelen 1000 (Ethernet)
RX packets 34638 bytes 2787534 (2.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1063 bytes 114312 (111.6 KiB)
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 1616 bytes 129616 (126.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1616 bytes 129616 (126.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:8: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 10.206.49.100 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
6.启动RS上的httpd
[root@www1 all]# yum install httpd -y
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.sjtu.edu.cn
* extras: ftp.sjtu.edu.cn
* updates: mirror.lzu.edu.cn
base | 3.6 kB 00:00
extras | 2.9 kB 00:00
updates | 2.9 kB 00:00
正在解决依赖关系
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-97.el7.centos.5 将被 安装
--> 正在处理依赖关系 httpd-tools = 2.4.6-97.el7.centos.5,它被软件包 httpd-2.4.6 -97.el7.centos.5.x86_64 需要
--> 正在处理依赖关系 /etc/mime.types,它被软件包 httpd-2.4.6-97.el7.centos.5.x86 _64 需要
--> 正在处理依赖关系 libaprutil-1.so.0()(64bit),它被软件包 httpd-2.4.6-97.el7.c entos.5.x86_64 需要
--> 正在处理依赖关系 libapr-1.so.0()(64bit),它被软件包 httpd-2.4.6-97.el7.cento s.5.x86_64 需要
--> 正在检查事务
---> 软件包 apr.x86_64.0.1.4.8-7.el7 将被 安装
---> 软件包 apr-util.x86_64.0.1.5.2-6.el7 将被 安装
---> 软件包 httpd-tools.x86_64.0.2.4.6-97.el7.centos.5 将被 安装
---> 软件包 mailcap.noarch.0.2.1.41-2.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
================================================================================
Package 架构 版本 源 大小
================================================================================
正在安装:
httpd x86_64 2.4.6-97.el7.centos.5 updates 2.7 M
为依赖而安装:
apr x86_64 1.4.8-7.el7 base 104 k
apr-util x86_64 1.5.2-6.el7 base 92 k
httpd-tools x86_64 2.4.6-97.el7.centos.5 updates 94 k
mailcap noarch 2.1.41-2.el7 base 31 k
事务概要
================================================================================
安装 1 软件包 (+4 依赖软件包)
总下载量:3.0 M
安装大小:10 M
Downloading packages:
(1/5): apr-1.4.8-7.el7.x86_64.rpm | 104 kB 00:00
(2/5): mailcap-2.1.41-2.el7.noarch.rpm | 31 kB 00:00
(3/5): httpd-tools-2.4.6-97.el7.centos.5.x86_64.rpm | 94 kB 00:00
(4/5): apr-util-1.5.2-6.el7.x86_64.rpm | 92 kB 00:00
(5/5): httpd-2.4.6-97.el7.centos.5.x86_64.rpm | 2.7 MB 00:05
--------------------------------------------------------------------------------
总计 552 kB/s | 3.0 MB 00:05
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : apr-1.4.8-7.el7.x86_64 1/5
正在安装 : apr-util-1.5.2-6.el7.x86_64 2/5
正在安装 : httpd-tools-2.4.6-97.el7.centos.5.x86_64 3/5
正在安装 : mailcap-2.1.41-2.el7.noarch 4/5
正在安装 : httpd-2.4.6-97.el7.centos.5.x86_64 5/5
验证中 : apr-1.4.8-7.el7.x86_64 1/5
验证中 : mailcap-2.1.41-2.el7.noarch 2/5
验证中 : httpd-tools-2.4.6-97.el7.centos.5.x86_64 3/5
验证中 : apr-util-1.5.2-6.el7.x86_64 4/5
验证中 : httpd-2.4.6-97.el7.centos.5.x86_64 5/5
已安装:
httpd.x86_64 0:2.4.6-97.el7.centos.5
作为依赖被安装:
apr.x86_64 0:1.4.8-7.el7 apr-util.x86_64 0:1.5.2-6.el7
httpd-tools.x86_64 0:2.4.6-97.el7.centos.5 mailcap.noarch 0:2.1.41-2.el7
完毕!
[root@www1 all]# cd /var/www/html/
[root@www1 html]# ll
总用量 0
[root@www1 html]# vi index.html
[root@www1 html]# cat index.html
from 10.206.49.64
[root@www1 html]# service httpd start
Redirecting to /bin/systemctl start httpd.service
[root@www1 html]# systemctl start httpd
[root@www1 html]# netstat -antlp | grep 80
tcp6 0 0 :::80 :::* LISTEN 13845/httpd
[root@www1 html]# vi index.html
[root@www1 html]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.206.49.1 0.0.0.0 UG 100 0 0 eth0
10.206.49.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
客户端验证:RIP:80能显示;VIP:80不能显示
7.LVS——ipvsadm
[root@node20 etc]# yum install -y ipvsadm
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 8.5 kB 00:00:00
* base: ftp.sjtu.edu.cn
* epel: ftp.iij.ad.jp
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/4): base/7/x86_64/group_gz | 153 kB 00:00:00
(2/4): extras/7/x86_64/primary_db | 247 kB 00:00:04
(3/4): base/7/x86_64/primary_db | 6.1 MB 00:00:13
(4/4): updates/7/x86_64/primary_db | 17 MB 00:00:22
正在解决依赖关系
--> 正在检查事务
---> 软件包 ipvsadm.x86_64.0.1.27-8.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
============================================================================================================================================================================
Package 架构 版本 源 大小
============================================================================================================================================================================
正在安装:
ipvsadm x86_64 1.27-8.el7 base 45 k
事务概要
============================================================================================================================================================================
安装 1 软件包
总下载量:45 k
安装大小:75 k
Downloading packages:
ipvsadm-1.27-8.el7.x86_64.rpm | 45 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : ipvsadm-1.27-8.el7.x86_64 1/1
验证中 : ipvsadm-1.27-8.el7.x86_64 1/1
已安装:
ipvsadm.x86_64 0:1.27-8.el7
完毕!
配置负载均衡策略
[root@node20 ~]# ipvsadm -A -t 10.206.49.100:80 -s rr
[root@node20 ~]# ipvsadm -a -t 10.206.49.100:80 -r 10.206.49.64:80 -g
[root@node20 ~]# ipvsadm -a -t 10.206.49.100:80 -r 10.206.49.65:80 -g
[root@node20 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.206.49.100:80 rr
-> 10.206.49.64:80 Route 1 0 0
-> 10.206.49.65:80 Route 1 0 0
8.测试
浏览器刷新: 访问vip:10.206.49.100不断刷新会发现在64,65间转换
ipvsadm -lnc
netstat -natp