部署LVS/DR模式实验
实验说明: (不能使用云服务器,阿里云上有专门的产品负载均衡)
1.网络使用NAT模式
2.DR模式要求Director DIP 和 所有Real Server RIP必须在同一个网段及广播域
3.所有节点网关均指定真实网关
准备四台虚拟机,均关闭防火墙及selinux,且设置开机不自启
systemctl stop firewalld
systemctl disable firewalld
sed -ri ‘ /^SELINUX=/c SELINUX=disabled’ /etc/selinux/config
setenforce 0
DR服务器:
192.168.188.128
WEB服务器:
RS1:192.168.188.129
RS2:192.168.188.130
client服务器:
192.168.188.131
1、DR服务器操作
yum -y install ipvsadm
规则保存工具:/usr/sbin/ipvsadm —save > /path/to/file
配置文件:/etc/sysconfig/ipvsadm-config
配置VIP(virtual ip)
ip a a dev ens33 192.168.188.10/32 #添加ip需属于同一网段
systemctl start ipvsadm #启动ipvsadm
##若启动失败
Job for ipvsadm.service failed because the control process exited with error code. See “systemctl status ipvsadm.service” and “journalctl -xe” for details.
则ipvsadm —save >/etc/sysconfig/ipvsadm
2、定义LVS分发策略
-A:添加VIP
-t:用的是tcp协议
-a:添加的是lo的vip地址
-r:转发到realserverip
-s:算法
-L | -l –list #显示内核虚拟服务器表
—numeric, -n:#以数字形式输出地址和端口号
-g —gatewaying #指定LVS工作模式为直接路由器模式(也是LVS默认的模式)
-S -save #保存虚拟服务器规则到标准输出,输出为-R 选项可读的格式
rr:轮循
如果添加ip错了,删除命令如下:
# ip addr del 192.168.0.193 dev ens33
ipvsadm -C #清除内核虚拟服务器表中的所有记录
ipvsadm -A -t 192.168.188.10:80 -s rr
ipvsadm -a -t 192.168.188.10:80 -r 192.168.188.129 -g
ipvsadm -a -t 192.168.188.10:80 -r 192.168.188.130 -g
ipvsadm -S > /etc/sysconfig/ipvsadm #将部署的ip保存到/etc/sysconfig/ipvsadm 中
ipvsadm -L -n —stats #显示统计信息
1. Conns (connections scheduled) 已经转发过的连接数
2. InPkts (incoming packets) 入包个数
3. OutPkts (outgoing packets) 出包个数
4. InBytes (incoming bytes) 入流量(字节)
5. OutBytes (outgoing bytes) 出流量(字节)
ipvsadm -L -n —rate #显示速率
1. CPS (current connection rate) 每秒连接数
2. InPPS (current in packet rate) 每秒的入包个数
3. OutPPS (current out packet rate) 每秒的出包个数
4. InBPS (current in byte rate) 每秒入流量(字节)
5. OutBPS (current out byte rate) 每秒出流量(字节)
3、部署WEB服务器
<两台WEB服务器做同样操作>
1) yum -y install ipvsadm nginx
ip a a dev lo 192.168.188.10/32 #在lo接口上绑定DR服务器创建的VIP
echo “111” >/usr/share/nginx/html/index.html #192.168.188.129
echo “222” >/usr/share/nginx/html/index.html #192.168.188.130
2) echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore #忽略arp广播
echo 1 >/proc/sys/net/ipv4/ip_forword
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce #匹配精确地ip地址回包
3) systemctl start nginx
4) 使用client服务器访问测试(192.168.188.131)
curl 192.168.188.10 #多访问几次 观察结果
