Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing LVS Cluster with NAT Mode Using LVS, Apache, and NFS

Tech 1

Image


Introduction

LVS (Linux Virtual Server) is a high-performance, highly available server clustering system that achieves load balancing for network services through IP load-balancing technology. NAT mode is one of the most commonly used working modes in LVS.

In NAT mode, an LVS cluster consists of three types of nodes:

  1. Load Balancer (Director Server): A server running LVS software that receives client requests and forwards them to real servers based on scheduling algorithms.
  2. Real Servers: A group of servers providing actual services, which can include web servers, database servers, etc.
  3. Shared Storage Server (NFS Server): To enable session sharing, real servers store session data on shared storage servers, typically implemented using NFS (Network File System).

The workflow of NAT mode is as follows:

  1. Clients send requests to the virtual IP (VIP) address of the load balancer.
  2. The load balancer selects a real server based on the scheduling algorithm and forwards request to that server via Network Address Translation (NAT).
  3. The real server processes the request and sends the response directly back to the client.

Configuring LVS in NAT mode involves the following steps:

  1. Install the LVS software package and kernnel modules.
  2. Configure the virtual IP (VIP) and pool of real servers (RIP) on the load balancer.
  3. Set up NAT rules and scheduling algorithms on the load balancer.
  4. Configure shared storage (NFS) on the real servers and mount it.
  5. Configure the gateway on real servers to point to the load balancer.

Experimental Environment

Network Configuration

1. Load Balancer (LVS) Setup

# Navigate to network configuration directory
$ cd /etc/sysconfig/network-scripts/

# Copy interface configuration files
$ cp -p ifcfg-ens33 ifcfg-ens36

# Edit both interface configurations
$ vim ifcfg-ens33
$ vim ifcfg-ens36

Enable IP forwarding:

$ vim /etc/sysctl.conf

# Add or modify the line:
net.ipv4.ip_forward = 1

# Apply changes
$ sysctl -p

Restart network services and test connectivity:

$ systemctl restart network

Install required packages:

$ yum install -y ipvsadm

2. Web Server Configuration

Configure each web server's default gateway to point to the LVS server's ens33 interface IP address.

Install Apache HTTP Server on both web servers:

$ yum install -y httpd

3. Cluster Testing

To verify cluster functionality, manually create different index pages on each web server:

On first web server:

$ echo "Server One" > /var/www/html/index.html

On second web server:

$ echo "Server Two" > /var/www/html/index.html

Start/restart Apache service:

$ systemctl restart httpd
$ systemctl start httpd

Test acess from the LVS server to confirm basic connectivity before proceeding.

4. LVS Load Balancer Configuration

Load the IPVS kernel module and check current status:

$ modprobe ip_vs
$ cat /proc/net/ip_vs

Expected output:

IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port Forward Weight ActiveConn InActConn

Clear existing configurations and set up new ones:

$ ipvsadm -C

# Create virtual service with round-robin scheduler
$ ipvsadm -A -t 192.168.140.100:80 -s rr

# Add real servers using NAT mode (-m)
$ ipvsadm -a -t 192.168.140.100:80 -r 192.168.192.111:80 -m -w 1
$ ipvsadm -a -t 192.168.140.100:80 -r 192.168.192.112:80 -m -w 1

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

# Start IPVS service
$ systemctl start ipvsadm

Set client gateway to 192.168.140.100 (ens36 interface). Perform access tests to observe load distribution between the two backend servers.

5. NFS Server Configuration

Install NFS utilities:

$ yum install -y rpcbind nfs-utils

Create shared directory and configure exports:

$ mkdir /wwwroot
$ vim /etc/exports

# Add export rule
/wwwroot 192.168.192.0/24(rw,sync,no_root_squash)

Start NFS services and verify:

$ systemctl start nfs rpcbind
$ netstat -nultp | grep rpcbind
$ showmount -e

Expected output:

Export list for localhost.localdomain:
/wwwroot 192.168.192.0/24

6. Mounting NFS Shares on Web Servers

Perform these operations on both web servers:

$ systemctl start nfs

# Check exported directories from NFS server
$ showmount -e 192.168.192.113

# Expected output:
# Export list for 192.168.192.113:
# /wwwroot 192.168.192.0/24

# Mount remote share locally
$ mount 192.168.192.113:/wwwroot /var/www/html

# Verify mount
$ df -hT

Test client access to verify successful deployment. You should see alternating responses indicating proper load balancing across the two web servers.

Check connection statistics:

$ ipvsadm -Ln
Tags: LinuxLVS

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.