LVS 负载均衡群集 ---------- DR(直连路由)模式

负载均衡 ---- DR模式

  • 本次主要介绍负载均衡中的DR模式
    直接路由(Direct Routing)简称 DR模式;采用半开放式的网络结构,与TUN模式的结构类似,但各节点并不是分散在各地,而是与调度器位于同一物理网络;负载调度器与各节点服务器通过本地网络连接,不需要建立专用的IP隧道。

在这里插入图片描述

网络环境部署

  • 本次将用 eNsp 拓扑图 + 5台虚拟机来进行搭建演示
    在这里插入图片描述

  • 节点服务器群集
    1、一台LVS调度器(vm1连接方式)
    更改 ip :192.168.100.21/24 网关:192.168.100.1 重启网卡;

    2、两台节点服务器(vm1连接方式)
    更改 ip :192.168.100.22/24 网关:192.168.100.1 重启网卡;
    更改 ip :192.168.100.23/24 网关:192.168.100.1 重启网卡;

    3、一台NFS共享存储器(vm1连接方式)
    更改 ip :192.168.100.24/24 网关:192.168.100.1 重启网卡;

  • 客户机
    1、一台 windows 系统的虚拟机(客户端)(vm2连接方式)
    更改网络适配器的网址 ip:192.168.200.100/24 网关:192.168.200.1

  • 真机
    1、真机更改 vm1 网卡(用于连接 CRT 的)
    更改 ip:192.168.100.2/24 网关:192.168.100.1

  • 关闭所有虚拟机的防火墙、核心防护、安装本地 yum源

开始配置 LVS — DR模式

配置 LVS调度器(192.168.100.21)

【1】配置虚拟IP地址(VIP)

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ll
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# vi ifcfg-ens33:0
 
NAME=ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.100
NETMASK=255.255.255.255

[root@localhost network-scripts]# ifup ens33:0
[root@localhost network-scripts]# ip addr                ####查看 lo:0 接口上设的IP是否出现(我这是最小化安装的)

【2】调整/proc响应参数 对于 DR 群集模式来说,由于 LVS 负载调度器和各节点需要共用 VIP 地址,应该关闭 Linux 内核的重定向参数响应服务器不是一台路由器,那么它不会发送重定向,所以可以关闭该功能(响应优化)

[root@localhost ~]# vi /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0

[root@localhost ~]# sysctl -p                   ####查看是否生效
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0

【3】配置负载分配策略

[root@localhost /]# ipvsadm -v
[root@localhost ~]# modprobe ip_vs
[root@localhost ~]# cat /proc/net/ip_vs
[root@localhost ~]# yum -y install ipvsadm
[root@localhost ~]# ipvsadm -A -t 192.168.100.100:80 -s rr                #### 可以 ipvsadm -ln 查看下
[root@localhost ~]# ipvsadm -a -t 192.168.100.100:80 -r 192.168.100.22:80 -g -w 1
[root@localhost ~]# ipvsadm -a -t 192.168.100.100:80 -r 192.168.100.23:80 -g -w 1
[root@localhost ~]# ipvsadm-save > /opt/ipvsadm  

[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  192.168.100.100:80 rr
  -> 192.168.100.22:80            Route   1      0          0         
  -> 192.168.100.23:80            Route   1      0          0 

配置 NFS共享存储(192.168.100.24)

rpm -q nfs-utils    ###如果没装,yum -y install nfs-utils
rpm -q rpcbind      ###如果没装,yum -y install rpcbind

[root@localhost ~]# mkdir /opt/51xue /opt/52xue
[root@localhost ~]# echo 'i am superman 51xue' > /opt/51xue/index.html
[root@localhost ~]# echo 'you are pig 52xue' > /opt/52xue/index.html
[root@localhost ~]# vi /etc/exports                          #####添加下面二行(共享出去)
/opt/51xit 192.168.100.0/24 (rw,sync)           
/opt/52xit 192.168.100.0/24 (rw,sync)

[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# showmount -e
Export list for localhost.localdomain:
/opt/52xue 192.168.100.0/24
/opt/51xue 192.168.100.0/24

配置 节点服务器(192.168.100.22)

【1】配置虚拟IP地址

[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
IPADDR=192.168.100.10
NETMASK=255.255.255.255
ONBOOT=yes

[root@localhost network-scripts]# ifup lo:0
[root@localhost network-scripts]# ip addr                   ####我是最小化安装
看看 lo:0 接口上设的IP是否出现

[root@localhost network-scripts]# vi /etc/rc.local 
/sbin/route add -host 192.168.100.100 dev lo:0                 ####在末尾添加

[root@localhost network-scripts]# route add -host 192.168.100.100 dev lo:0
( -bash: route: command not found     #### 出现这个报错就 yum -y install net-tools 安装这个工具)

【2】调整/proc响应参数

[root@localhost network-scripts]# 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

[root@localhost network-scripts]# sysctl -p                    ####查看是否生效
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

【3】安装httpd 挂载测试页

[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
[root@localhost ~]# yum -y install nfs-utils
[root@localhost ~]# mount 192.168.100.24:/opt/51xue /var/www/html/
[root@localhost ~]# df -Th                       ####查看挂载情况
[root@localhost ~]# vi /etc/fstab 
192.168.100.24:/opt/51xue /var/www/html nfs defaults,_netdev 0 0        ###开机自动挂载,注意格式对齐
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs

[root@localhost ~]# showmount -e 192.168.100.24        ####如果还没发布,请到存储服务器发布下,exportfs -rv(如果报错就是配置出错了)
Export list for 192.168.100.24:
/opt/52xue 192.168.100.0/24
/opt/51xue 192.168.100.0/24
  • 登录192.168.100.22测试网站是否正常
    真机测试:
    在这里插入图片描述
    客户机测试:
    在这里插入图片描述

配置 节点服务器(192.168.100.23)

【1】配置虚拟IP地址

[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
IPADDR=192.168.100.100
NETMASK=255.255.255.255
ONBOOT=yes

[root@localhost network-scripts]# ifup lo:0
[root@localhost network-scripts]# ip addr              ####我是最小化安装
看看 lo:0 接口上设的IP是否出现

[root@localhost network-scripts]# vi /etc/rc.local 
/sbin/route add -host 192.168.100.100 dev lo:0                ####在末尾添加

[root@localhost network-scripts]# route add -host 192.168.100.100 dev lo:0
( -bash: route: command not found     #### 出现这个报错就 yum -y install net-tools 安装这个工具)

【2】调整/proc响应参数

[root@localhost network-scripts]# 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

[root@localhost network-scripts]# sysctl -p                 ####查看是否生效

   ...............

【3】安装httpd 挂载测试页

[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
[root@localhost ~]# yum -y install nfs-utils
[root@localhost ~]# mount 192.168.100.24:/opt/52xue /var/www/html/
[root@localhost ~]# df -Th                       ####查看挂载情况
[root@localhost ~]# vi /etc/fstab 
192.168.100.24:/opt/52xue /var/www/html nfs defaults,_netdev 0 0        ###开机自动挂载,注意格式对齐
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs

[root@localhost ~]# showmount -e 192.168.100.24     
Export list for 192.168.100.24:
/opt/52xue 192.168.100.0/24
/opt/51xue 192.168.100.0/24
  • 登录192.168.100.23测试网站是否正常
    真机测试:
    在这里插入图片描述
    客户机测试:
    在这里插入图片描述

客户机测试 LVS 负载均衡

在这里插入图片描述
在这里插入图片描述

  • 真机测试
    在这里插入图片描述
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/XCsuperman/article/details/108744813