[Linux] LVS负载均衡群集——DR模式

一、 DR模式的特点

        直接路由: 在LVS_DR模式下,负载均衡器不修改数据包的IP地址,只修改目的MAC地址。这使得数据包可以直接路由到后端实际服务器上,而不需要返回到负载均衡器。

        高性能: 由于数据包在传输过程中不需要回到负载均衡器,LVS_DR模式具有较低的延迟,提供了高性能的负载均衡。

        负载均衡器无状态: 在LVS_DR模式下,负载均衡器是无状态的,因为它不处理响应流量。这降低了负载均衡器的负担,使其更容易扩展和维护。

        后端服务器真实IP: 客户端直接与后端实际服务器通信,因此客户端看到的是后端服务器的真实IP地址,而不是负载均衡器的IP地址。

        ARP协议处理: 在LVS_DR模式下,负载均衡器需要处理ARP(地址解析协议)请求,以便将数据包正确路由到后端实际服务器。通常,负载均衡器会使用VIP(Virtual IP)来响应ARP请求。

二、LVS-DR模式工作过程

1. 客户端通过VIP将访问请求报文(源IP为客户端IP,目的IP为VIP)发送到调度器


2. 调度器通过调度算法选择最适合的节点服务器,并重新封装数据报文(将源MAC改为调度器的       MAC,目的MAC改为节点服务器的MAC地址),再通过交换机转发请求给节点服务器


3. 节点服务器收到请求报文后,确认目的MAC和目的IP无误后解包并送到应用层进行处理


4. 节点服务器在返回响应报文前,会先重新封装报文(源IP为VIP,目的为客户端IP),再将响应报文先通过lo接口传送给物理网卡,再通过物理网卡发送给客户端

三、LVS-DR模式部署过程

第一步:部署共享服务(主机IP:192.168.136.110)
 95  systemctl stop firewalld.service
   96  systemctl disable firewalld.service
   97  setenforce 0
   98  yum install nfs-utils rpcbind -y
   99  systemctl start rpcbind.service
  100  systemctl start nfs.service
  101  systemctl enable nfs.service
  102  systemctl enable rpcbind.service
  103  mkdir -p /var/www/html/{cxk,xhz}
  104  cd /var/www/html/
  105  ls
  106  echo 'this is cxk_web01!' >cxk/test.html
  107  echo 'this is xhz_web02!' >xhz/test.html
  108  cat cxk/test.html 
  109  cat xhz/test.html 
  110  vim /etc/exports
  111  systemctl enable --now rpcbind nfs
  112  showmount -e
  113  systemctl stop firewalld
  114  setenforce 0
  115  showmount -e
  116  systemctl enable --now rpcbind nfs
  117  showmount -e
  118  vim /etc/exports
  119  systemctl status rpcbind.service 
  120  systemctl status nfs
  121  showmount -e
  122  exportfs -a
  123  showmount -e

第二步:配置节点服务器(192.168.136.120 192.168.136.130) 

两台web节点服务器相同的操作:

   77  systemctl restart network.service
   78  systemctl disable --now firewalld
   79  setenforce 0
   80  showmount -e 192.168.136.110
   81  cd /etc/yum.repos.d/
   82  ls
   83  mkdir bak
   84  mv *.repo bak
   85  rz -E
   86  ls
   87  yum -y install nginx
   88  vim /etc/fstab 
   89  mount -a
   90  df -h
   91  ls
   92  ls /usr/share/nginx/html/
   93  cat te
   94  ls /usr/share/nginx/html/
   95  cat /usr/share/nginx/html/test.html 
   96  vim /etc/nginx/nginx.conf 
   97  systemctl restart nginx
   98  vim /etc/sysconfig/network-scripts/ifcfg-ens33
   99  systemctl restart network nginx
   

  web2节点服务器的后续操作:

第三步:配置虚拟 IP 地址(192.168.136.200)

  两台web节点服务器相同的操作:

   68  vim nginx.conf 
   69  cat /etc/sysconfig/network-scripts/ifcfg-ens33 
   70  cd /etc/sysconfig/network-scripts/
   71  ls
   72  vim ifcfg-ens33 
   73  cat /proc/net/ip_vs
   74  systemctl restart network
   75  vim /etc/sysctl.conf 
   76  cp ifcfg-lo ifcfg-lo:0
   77  ls
   78  vim ifcfg-lo:0
   79  cp ifcfg-ens33 ifcfg-lo:0
   80  vim ifcfg-lo:0
   81  route add -host 192.168.136.200 dev lo:0
   82  cat ifcfg-lo:0
   83  systemctl restart network
   84  ip a
   85  ifconfig 
   86  route add -host 192.168.136.200 dev lo:0
   87  route -n
   88  history

web1网卡设置:

web2网卡设置:

第四步:调整内核的 ARP 响应参数以阻止更新 VIP 的 MAC 地址,避免发生冲突
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.conf.lo.arp_ignore = 1     #系统只响应目的IP为本地IP的ARP请求
net.ipv4.conf.lo.arp_announce = 2   #系统不使用IP包的源地址来设置ARP请求的源地址,而选择发送接口的IP地址
net.ipv4.conf.all.arp_ignore = 1 
net.ipv4.conf.all.arp_announce = 2
sysctl -p

两台web服务器同样操作 :

 第五步:配置负载调度器(192。168.136.100)
  102  ifconfig 
  103  cd /etc/sysconfig/network-scripts
  104  ls
  105  cp ifcfg-ens33 ifcfg-ens33:0
  106  vim ifcfg-ens33:0  #配置虚拟 IP 地址
  107  systemctl restart network
  108  vim /etc/sysctl.conf 
  109  yum -y install ipvsadm.x86_64  #安装服务
  110  ipvsadm-save > /etc/sysconfig/ipvsadm #不做这一项服务启动不了
  111  systemctl restart ipvsadm.service 
  112  modprobe ip_vs  #启动ip_vs模块
  113  cat /proc/net/ip_vs  #查看启动结果/ip_vs版本
     #配置负载分配策略
  114  ipvsadm -ln  #查看节点状态,Route代表 DR模式
  115  ipvsadm -C  #清空规则
  116  ipvsadm -ln
  117  ipvsadm -A -t 192.168.136.200:80 -s rr
  118  ipvsadm -a -t 192.168.136.200:80 -r 192.168.136.120:80 -g  #若隧道模式,-g替换为-i
  119  ipvsadm -a -t 192.168.136.200:80 -r 192.168.136.130:80 -g
  120  ipvsadm
  121  ipvsadm -ln
  122  systemctl disable --now firewalld
  123  setenforce 0
  124  vim /etc/sysctl.conf  #调整 proc 响应参数
  125  history

 

第六步:测试

 

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2023-12-16 15:42:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-16 15:42:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-16 15:42:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-16 15:42:05       20 阅读

热门阅读

  1. Linux学习教程(第十二章 Linux系统管理)二

    2023-12-16 15:42:05       24 阅读
  2. python 如何快速注释几行代码

    2023-12-16 15:42:05       38 阅读
  3. (洛谷)P8835 [传智杯 #3 决赛] 子串

    2023-12-16 15:42:05       31 阅读
  4. HAProxy 根据报文解析来定义不同的acl

    2023-12-16 15:42:05       29 阅读
  5. 【LeetCode】15. 三数之和(Set用法)

    2023-12-16 15:42:05       42 阅读
  6. C语言编程题-单身狗

    2023-12-16 15:42:05       36 阅读
  7. 小程序人脸识别—检测人脸图片获取图片

    2023-12-16 15:42:05       27 阅读
  8. 贪心算法总结

    2023-12-16 15:42:05       40 阅读
  9. SpringCloud 和 Linux 八股文第二期五问五答

    2023-12-16 15:42:05       44 阅读
  10. opencv 安装

    2023-12-16 15:42:05       27 阅读
  11. PHP获取数组中最大最小值和下标

    2023-12-16 15:42:05       31 阅读
  12. C语言——const函数

    2023-12-16 15:42:05       39 阅读
  13. centos网卡重命名方法

    2023-12-16 15:42:05       39 阅读