Technology Sharing

LVS-DR Cluster

2024-07-08

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

LVS-DR Cluster

LVS-DR (Linux Virtual Server DIrector Server) working mode is the most commonly used working mode in production environment.

Working Principle of LVS-DR

In LVS-DR mode, Director Server is used as the access entrance of the cluster, not as a gateway. Director Server and Real Server nodes need to be in the same network, and data returned to the client does not need to pass through Director Server. In order to respond to access to the entire cluster, Director Server and Real Server need to be configured with VIP addresses.

LVS-DR Packet Flow Analysis

How DR mode works

  1. The client sends a request to the Director Server, and the requested datagram (source IP is CIP, target IP is VIP) arrives in the kernel space
  2. Director Server and Real Server are in the same network, and data is transmitted through the Layer 2 data link layer.
  3. The kernel space determines that the target IP of the data packet is the local VIP. At this time, IPVS compares the service requested by the data packet to see if it is a cluster service. If it is a cluster service, the data packet is repackaged. The source MAC address is changed to the MAC address of the Director Server, and the target MAC address is changed to the MAC address of the Real Server. The source IP address and the target IP address remain unchanged, and then the data packet is sent to the Real Server.
  4. The MAC address of the request message that reaches the Real Server is its own MAC address, so it receives the message, re-encapsulates the data packet (source IP address is VIP, destination IP is CIP), transmits the response message to the physical network card through the lo interface, and then sends it out.
  5. The Real Server directly sends the response message to the client

Features of LVS-DR mode

  • Director Server and Real Server must be in the same physical network
  • The Real Server can use a private address or a public address. If a public address is used, RIP can be directly accessed through the Internet.
  • All request messages go through the Director Server, but the reply response messages cannot go through the Director Server
  • The gateway of the Real Server is not allowed to point to the Director Server IP, that is, the data packet is not allowed to pass through the Director Server
  • Configure the IP address of the VIP on the lo interface of the Real Server

LVS direct routing mode example

#配置负载调度器
#配置虚拟IP地址VIP
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# vi ifcfg-ens33:0
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.10.172
NETMASK=255.255.255.0
[root@localhost network-scripts]# systemctl restart network
#调整内核参数,禁止转发重定向报文由于 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
#配置负载分配策略
[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 192.168.10.172:80 -s wrr
[root@localhost ~]# ipvsadm -a -t 192.168.10.172:80 -r 192.168.10.102 -g -w 1
[root@localhost ~]# ipvsadm -a -t 192.168.10.172:80 -r 192.168.10.103 -g -w 1
[root@localhost ~]# ipvsadm-save
-A -t localhost.localdomain:http -s rr
-a -t localhost.localdomain:http -r 192.168.10.102:http -g -w 1
-a -t localhost.localdomain:http -r 192.168.10.103:http -g -w 2
[root@localhost ~]# systemctl enable ipvsadm
-g:直接路由模式
#配置web节点服务器
#配置虚拟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=192.168.10.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 192.168.10.172 dev lo:0
[root@localhost ~]# route add -host 192.168.10.172 dev lo:0
#调整内核参数
[root@localhost ~]# vi /etc/sysctl.conf
#添加
net.ipv4.conf.all.arp_ignore = 1 #忽略arp请求
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
[root@localhost ~]# sysctl -p
#安装httpd创建测试网页
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# yum install nfs-utils
[root@localhost ~]# mount 192.168.10.104:/opt/wwwroot /var/www/html
[root@localhost ~]#vi /var/www/html/index.html