LVS Load Balancing: NAT and DR Mode Implementation

  1. Introduction to LVS

LVS (Linux Virtual Server) is an open-source project initiated by Wensong Zhang, primarily designed to achieve load balancing at the Linux kernel level. It enables the creation of a high-availability, high-performance server cluster by distributing client requests among backend servers according to load balancing algorithms.

1.1 Cluster Types

  • LB (Load Balancing): Distributes incoming requests across multiple servers.
  • HA (High Availability): Ensures system uptime by implementing redundancy and fault tolerance.
  • HPC (High Performance Computing): Optimized for applicasions requiring high throughput and low latency.

1.2 Reliability Metrics

  • MTBF (Mean Time Between Failures): Average time between system failures.
  • MTTR (Mean Time to Repair): Average time required to repair a system after failure.
  • Availability A = MTBF / (MTBF + MTTR): A value close to 1 indicates high system availability.

1.3 LVS Terminology

  • VIP (Virtual IP): The public-facing IP address clients use to access the cluster.
  • DIP (Director IP): IP address used by the load baalncer to communicate with real servers.
  • RIP (Real IP): IP address of backend servers.
  • CIP (Client IP): IP address of the client making the request.
  1. LVS Operation Modes

LVS supports three main modes:

  • NAT: All traffic passes through the load balancer.
  • DR (Direct Routing): Backend servers respond directly to clients.
  • TUN (Tunneling): Encapsulates packets for long-distance transmission.

2.1 NAT Mode

In NAT mode, the load balancer modifies the destination IP and port of incoming packets to forward them to backend servers. Responses also go through the balancer, which rewrites the source IP before sending the response to the client.

Advantages

  • Simple configuration
  • Transparent to clients

Disadvantages

  • Performance bottleneck due to double translation
  • Single point of failure if the balancer fails

Configuration Example


# Load IPVS module
modprobe ip_vs

# Install management tool
yum -y install ipvsadm

# Clear existing rules
ipvsadm -C

# Add virtual server
ipvsadm -A -t 12.0.0.1:80 -s rr

# Add real servers
ipvsadm -a -t 12.0.0.1:80 -r 192.168.10.41:80 -m
ipvsadm -a -t 12.0.0.1:80 -r 192.168.10.42:80 -m

# Save configuration
ipvsadm-save > /etc/sysconfig/ipvsadm

2.2 DR Mode

In DR mode, the load balancer modifies the destination MAC address of packets, allowing backend servers to respond directly to clients without passing through the balancer again.

Key Requirements

  • All real servers must be in the same subnet as the balancer.
  • Each real server must have the VIP configured on its loopback interface.
  • ARP settings must be adjusted to prevent conflicts.

Configuration Example


# Add VIP to balancer
ip addr add 192.168.10.100 dev ens33:0

# Configure sysctl
echo "net.ipv4.ip_forward=0" >> /etc/sysctl.conf
echo "net.ipv4.conf.all.send_redirects=0" >> /etc/sysctl.conf
sysctl -p

# Configure IPVS rules
ipvsadm -C
ipvsadm -A -t 192.168.10.100:80 -s rr
ipvsadm -a -t 192.168.10.100:80 -r 192.168.10.41:80 -g
ipvsadm -a -t 192.168.10.100:80 -r 192.168.10.42:80 -g

Real Server Setup


# Configure loopback VIP
ip addr add 192.168.10.100 dev lo:0
route add -host 192.168.10.100 dev lo:0

# Adjust ARP settings
echo "net.ipv4.conf.lo.arp_ignore=1" >> /etc/sysctl.conf
echo "net.ipv4.conf.lo.arp_announce=2" >> /etc/sysctl.conf
echo "net.ipv4.conf.all.arp_ignore=1" >> /etc/sysctl.conf
echo "net.ipv4.conf.all.arp_announce=2" >> /etc/sysctl.conf
sysctl -p

  1. Mode Comparison

NAT DR TUN
Pros Simple setup, transparent Best performance Supports geographically distant servers
Cons Single point of failure, performance bottleneck Must be in same subnet Requires dediacted tunneling (e.g., VPN)
RS Requirements No special setup Must disable ARP responses on non-physical interfaces Must support tunneling
Max RS Count 10-20 ~100 ~100

Tags: Linux LVS Load Balancing NAT DR

Posted on Mon, 20 Jul 2026 17:40:42 +0000 by soulroll