一、DR模式
二、实验环境
实验目的:利用lVS实现网站的高并发架构
三台CentOS-7.5虚拟机
lVS负载均衡器:10.0.11.205 VIP: 10.0.11.210
web-1(real_server1):10.0.11.203
web-2(real_server2):10.0.11.204
VIP(虚拟IP): 10.0.11.210
三台服务器均关闭:防火墙+selinux
三台服务器均可以上外网
Client为自己的笔记本电脑:10.0.11.53
Web类型:nginx
三、lVS-DR实战
3.1 lVS准备VIP和路由
1)添加VIP
[[email protected] ~]#
[[email protected] ~]# ip addr show #查看LVS服务器的真实IP
LVS虚拟网卡设置,因为是临时设置IP,重启网卡设置将会丢失
说明:因为Centos 7 不安装net-lools 工具是不能使用ifconfig 命令的,建议先安装yum install net-tools工具
[[email protected] ~]#
[[email protected] ~]# yum install net-tools #安装net-tools工具
[[email protected] ~]# rpm -q net-tools
net-tools-2.0-0.25.20131004git.el7.x86_64
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# ifconfig ens32:1 10.0.11.210 broadcast 10.0.11.255 netmask 255.255.255.0 up #使用ifconfig命令临时添加虚IP:10.0.11.210
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# ifconfig #查看当前网卡状况
2)配置路由
[[email protected] ~]#
[[email protected] ~]# route add -host 10.0.11.210 dev ens32:1
[[email protected] ~]#
3)配置路由转发功能
[[email protected] ~]#
[[email protected] ~]# vim /etc/sysctl.conf
#加入以下四行内容即可
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.ens32.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
[[email protected] ~]#
[[email protected] ~]# cat /etc/sysctl.conf #查看修改好的配置文件
3.2 LVS设置路由条目
设置apvsadm
1)安装LVS软件包
[[email protected] ~]#
[[email protected] ~]# yum install ipvsadm 安装LVS
[[email protected] ~]# rpm -qa ipvsadm
ipvsadm-1.27-7.el7.x86_64
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# ipvsadm -C #清除记录表,这个命令一般不用
[[email protected] ~]# ipvsadm -L #查看LVS情况
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[[email protected] ~]#
2)设置apvsadm
[[email protected] ~]#
[[email protected] ~]# ipvsadm -A -t 10.0.11.210:80 -s rr #添加虚拟服务器10.0.11.210,80端口,指定调度策略为rr,协议为tcp
[[email protected] ~]#
[[email protected] ~]# ipvsadm -a -t 10.0.11.210:80 -r 10.0.11.203:80 -g #其中-r表示真实服务器,-g表示全局
[[email protected] ~]# ipvsadm -a -t 10.0.11.210:80 -r 10.0.11.204:80 -g
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# ipvsadm -L #再次查看LVS策略
3)设置“ipvsadm配置”永久生效
[[email protected] ~]#
[[email protected] ~]# ipvsadm-save > /etc/sysconfig/ipvsadm #让配置永久生效
[[email protected] ~]#
[[email protected] ~]# systemctl enable ipvsadm #设置LVS程序开机启动
3.3 准备两个web
安装web服务
real_server-1端:
[[email protected] ~]#
[[email protected] ~]# yum install nginx #安装nginx网站服务
[[email protected] ~]# rpm -q nginx
nginx-1.16.1-1.el7.x86_64
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# > /usr/share/nginx/html/index.html #清空原配置内容
[[email protected] ~]#
[[email protected] ~]# vim /usr/share/nginx/html/index.html #写入自定义配置
[[email protected] ~]# cat /usr/share/nginx/html/index.html #查看写入的内容
real_server-1:10.0.11.203
[[email protected] ~]#
[[email protected] ~]# systemctl start nginx #启动nginx服务
[[email protected] ~]# systemctl enable nginx #设置nginx开机自启动
real_server-2端:
[[email protected] ~]#
[[email protected] ~]# yum install nginx #安装nginx网站服务
[[email protected] ~]# vim /usr/share/nginx/html/index.html #写入自定义配置
[[email protected] ~]# cat /usr/share/nginx/html/index.html #查看写入的内容
real_server-2:10.0.11.204
[[email protected] ~]#
[[email protected] ~]# systemctl start nginx #启动nginx服务
[[email protected] ~]# systemctl enable nginx #设置nginx开机自启动
3.4 给两台web服务器的lo网卡设置子网掩码为32位VIP
real_server-1端:
[[email protected] ~]#
[[email protected] ~]# ifconfig lo:1 10.0.11.210/32
[[email protected] ~]#
[[email protected] ~]# ifconfig
real_server-2端:
[[email protected] ~]#
[[email protected] ~]# ifconfig lo:1 10.0.11.210/32
[[email protected] ~]#
[[email protected] ~]# ifconfig
3.5 给两个web服务器设置内核参数
real_server-1端:
[[email protected] ~]#
[[email protected] ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore #忽略arp响应,不允许收
[[email protected] ~]#
[[email protected] ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce #为了让VIP发包出去,但允许发
[[email protected] ~]#
real_server-2端:
[[email protected] ~]#
[[email protected] ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[[email protected] ~]#
[[email protected] ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[[email protected] ~]#
3.6 客户端测试
1)访问网站
2)ipvsadm状态测试
[[email protected] ~]#
[[email protected] ~]# ipvsadm –Lnc #查看状态
结论:利用LVS-DR成功实现了两台WEB服务器之间的负载均衡
原文地址:https://blog.51cto.com/14783377/2486135