IP分配:
调度器:172.16.16.173
Web服务器1:172.16.16.177
Web服务器2:172.16.16.178
NFS服务器:172.16.16.250
客户端:172.16.16.200
IP分配:
调度器:内网网卡:192.168.1.10 外网网卡:192.168.2.10
Web服务器1:192.168.1.11 网关:192.168.1.10(调度器内网网卡的IP)
Web服务器2:192.168.1.12 网关:192.168.1.10(调度器内网网卡的IP)
NFS服务器:192.168.1.13 网关:192.168.1.10(调度器内网网卡的IP)
客户端:192.168.2.11 网关:192.168.2.10
2:配置负载调度器
(1)配置虚拟IP地址VIP
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0
[root@localhost network-scripts]# vi ifcfg-eth0:0
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0:0
DEVICE=eth0:0
ONBOOT=yes
IPADDR=172.16.16.172
NETMASK=255.255.255.0
[root@localhost network-scripts]# systemctl restart network
(2)调整内核参数
[root@localhost ~]# vi /etc/sysctl.conf
在末尾添加:
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
[root@localhost ~]# sysctl -p
(3)配置负载分配策略
[root@localhost ~]#yum -y install ipvsadm
[root@localhost ~]#ipvsadm -v \\查看版本
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]#ipvsadm -C
[root@localhost ~]# ipvsadm -A -t 172.16.16.172:80 -s rr
[root@localhost ~]# ipvsadm -a -t 172.16.16.172:80 -r 172.16.16.177 -g -w 1
[root@localhost ~]# ipvsadm -a -t 172.16.16.172:80 -r 172.16.16.178 -g -w 1
[root@localhost ~]# ipvsadm-save
-A -t localhost.localdomain:http -s rr
-a -t localhost.localdomain:http -r 172.16.16.177:http -g -w 1
-a -t localhost.localdomain:http -r 172.16.16.178:http -g -w 1
[root@localhost ~]# systemctl enable ipvsadm
3:NFS共享
1.
mkdir -p /opt/aaa #创建aaa,bbb两个待会用来共享的目录
mkdir -p /opt/bbb
2.
vim /etc/exports #修改NFS配置文件
添加以下内容:
/opt/aaa 172.16.16.0/24(rw,sync,no_root_squash)
/opt/bbb 172.16.16.0/24(rw,sync,no_root_squash)
3.
systemctl start rpcbind #启动并查询NFS
systemctl start nfs
netstat -anpt | grep rpc
4.
showmount -e #查看本机发布的NFS共享目录
4:配置web节点服务器
(1)配置虚拟ip地址VIP
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]#cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vi ifcfg-lo:0
修改
DEVICE=lo:0
NETMASK=255.255.255.255 \\子网掩码必须为255.255.255.255
IPADDR=172.16.16.172
ONBOOT=yes
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ifconfig
[root@localhost network-scripts]# cd
[root@localhost ~]# vi /etc/rc.local
添加
/sbin/route add -host 172.16.16.172 dev lo:0
[root@localhost ~]# route add -host 172.16.16.172 dev lo:0
(2)调整内核参数
[root@localhost ~]# vi /etc/sysctl.conf
添加:
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
注释:
arp_ignore =1
当收到arp请求时,只有ARP请求数据包所请求的IP地址属于当前网卡的IP地址,才会回应ARP响应数据包。
arp_announce = 2
当发送arp请求时,忽略IP数据包的源IP地址,总是选择网卡所配置的首选IP地址作为ARP请求数据包的发送方IP地址
目的:因为lo:0的ip地址和调度器的eth0:0的ip地址是一样的,为了不造成ip冲突,要禁止更新VIP的mac地址。
[root@localhost ~]# sysctl -p
(3)安装httpd创建测试网页
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# mount 172.16.16.250:/opt/wwwroot /var/www/html
[root@localhost ~]#vi /var/www/html/index.html
<h1>LVS负载均衡群集--测试页面01</h1>
(4)启用httpd服务
[root@localhost ~]# systemctl start httpd
(5)配置第二台web节点
配置方法与第一台完全相同
注意:在实验环境中,为了验证负载过程,第二台web服务器的网站可以可第一个不一样,所以这两个web服务器就没有必要使用NFS,直接在web服务器本地创建测试网页文件即可
5:在client上测试网站
测试时频繁刷新页面
也可以用脚本测试:
[root@localhost ~]# for i in $(seq 10); do curl http://172.16.16.172 ;done
6:在LVS调度器上查看LVS策略
[root@localhost ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.16.172:80 rr
-> 172.16.16.177:80 Route 1 0 0
-> 172.16.16.178:80 Route 1 0 0
[root@localhost ~]# ipvsadm -lncsss
7:验证不了就
不成功就重新添加
[root@localhost ~]#ipvsadm -C
[root@localhost ~]# ipvsadm -A -t 172.16.16.172:80 -s rr
[root@localhost ~]# ipvsadm -a -t 172.16.16.172:80 -r 172.16.16.177 -g -w 1
[root@localhost ~]# ipvsadm -a -t 172.16.16.172:80 -r 172.16.16.178 -g -w 1
systemctl restart ipvsadm
ipvsadm -lnc