Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing LVS Direct Routing Mode with Configuration Examples

Tech 2

LVS (Linux Virtual Server) operates on a three-tier architecture: the load balancer, server pool, and shared storage. The load balancer directs client requests to servers in the pool, presenting a single virtual IP (VIP) address. Servers in the pool handle actual requests, while shared storage ensures consistent data across servers, typically using solutions like NFS or distributed file systems such as AFS or GFS.

For Direct Routing (DR) mode, confgiure three machines: a load balancer (e.g., 192.168.1.191) and two servers in the pool (e.g., 192.168.1.189 and 192.168.1.213). The VIP is set to 192.168.1.192.

On the load balancer, install ipvsadm and create a configuration script. Enable IP forwarding and set up the VIP on an alias enterface:

#!/bin/bash
sysctl -w net.ipv4.ip_forward=1
ipvs_tool=/sbin/ipvsadm
vip_addr=192.168.1.192
server1=192.168.1.213
server2=192.168.1.189

ifconfig eth0:0 down
ifconfig eth0:0 $vip_addr broadcast $vip_addr netmask 255.255.255.255 up
route add -host $vip_addr dev eth0:0

$ipvs_tool -C
$ipvs_tool -A -t $vip_addr:80 -s rr
$ipvs_tool -a -t $vip_addr:80 -r $server1:80 -g -w 3
$ipvs_tool -a -t $vip_addr:80 -r $server2:80 -g -w 1

$ipvs_tool -A -t $vip_addr:25 -s rr
$ipvs_tool -a -t $vip_addr:25 -r $server1:25 -g -w 3
$ipvs_tool -a -t $vip_addr:25 -r $server2:25 -g -w 1

This script configures the VIP, clears existing rules, and sets up load balancing for ports 80 and 25 using round-robin (rr) scheduling. Adjust weights (-w) as needed; for services requiring session persistence, avoid rr mode.

Key ipvsadm commands include:

  • -A: Add a virtual server.
  • -a: Add a real server to a virtual server.
  • -t: Specify TCP service with VIP:port.
  • -s: Set scheduling algorithm (e.g., rr, wrr, lc).
  • -g: Use DR mode (default).
  • -w: Assign weight to a real server.

Common scheduling algorithms:

  • rr: Round-robin, cycles through servers.
  • wrr: Weighted round-robin, uses weight values.
  • lc: Least-connection, selects server with fewest active connections.
  • wlc: Weighted least-connection, divides connections by weight.
  • sh: Source hashing, routes same client IP to same server.

On each server in the pool, configure ARP settings to prevent them from responding to ARP requests for the VIP. Use a script to set up a loopback alias and adjust kernel parameters:

#!/bin/bash
vip_addr=192.168.1.192
ifconfig lo:0 $vip_addr broadcast $vip_addr netmask 255.255.255.255 up
route add -host $vip_addr lo:0

sysctl -w net.ipv4.conf.lo.arp_ignore=1
sysctl -w net.ipv4.conf.lo.arp_announce=2
sysctl -w net.ipv4.conf.all.arp_ignore=1
sysctl -w net.ipv4.conf.all.arp_announce=2

ARP parameters exlpained:

  • arp_ignore=1: Respond only to ARP queries for IPs on the recieving interface.
  • arp_announce=2: Use the most appropriate local address for ARP replies, avoiding advertisement of additional IPs.

After configuration, test connectivity to the VIP on ports like 80 and 25. Ensure servers provide the entended services; for example, if one server lacks a service on port 25, requests may fail. Use tools like telnet to verify that client requests show the original source IP when handled by servers, confirming DR mode functionality.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.