Mastering LVS and Keepalived: Build a High‑Performance Load‑Balancing Cluster
This article explains the fundamentals of load balancing, introduces Linux Virtual Server (LVS) and its architecture, and provides a step‑by‑step guide to set up a DR‑mode LVS cluster with Keepalived on CentOS, including configuration, testing, and failover verification.
1.1 Find More Cows to Pull the Cart
Most internet systems use server‑cluster technology, deploying identical services on multiple machines to form a cluster that provides services externally. Clusters can be web‑application servers, database servers, distributed cache servers, and so on.
Ancient saying: When one ox cannot pull the cart, don’t look for a stronger ox; use two oxen.
In practice, a load‑balancer sits in front of a web‑server cluster, selects the most suitable server, and forwards client requests, achieving transparent forwarding. Modern cloud computing and distributed architectures essentially expose a backend cluster as a single virtually unlimited server.
1.2 Types of Load Balancing
Load balancing can be implemented with hardware devices (e.g., F5) or with software.
Commercial hardware load balancers are costly, so software load balancing is preferred when conditions allow.
Software solutions address two core problems—selection and forwarding—with the most famous being LVS (Linux Virtual Server).
2.1 What Is LVS?
LVS stands for Linux Virtual Server, a free software project initiated by Dr. Zhang Wensong. It is now part of the standard Linux kernel. Since Linux 2.4, LVS modules are built‑in and require no kernel patches.
2.2 What Does LVS Do?
LVS provides network‑layer load balancing for server clusters, offering high performance and high availability. It can combine many low‑performance servers into a “super server”, is easy to configure, supports multiple balancing methods, and remains stable even if a node fails.
LVS has been used to build scalable services such as WWW, cache, DNS, FTP, mail, video/audio streaming, etc., by sites like linux.com, real.com, sourceforge.net.
2.3 LVS Architecture
An LVS‑based cluster consists of three layers:
Load balancer layer (Load Balancer)
Server array layer (Server Array)
Shared storage layer (Shared Storage)
From the user’s perspective, all internal services are transparent, appearing as a single virtual server.
2.4 LVS Load‑Balancing Mechanisms
(1) LVS operates at OSI layer 4 (transport), supporting TCP/UDP load balancing, which is more efficient than higher‑layer solutions.
(2) Forwarding is achieved by IP address modification (NAT mode, with SNAT and DNAT) or MAC address modification (DR mode).
NAT mode: Network Address Translation maps external and internal addresses. LVS acts as a gateway for real servers (RS). Incoming packets are DNAT‑ed to the RS IP; responses are SNAT‑ed back to the virtual IP (VIP), making the client unaware of the RS.
DR mode: Direct Routing binds the same VIP to both LVS and RS. LVS only changes the destination MAC address, forwarding the packet to the chosen RS, which replies directly to the client without passing through LVS again, eliminating the bandwidth bottleneck.
DR mode does not modify IP addresses, only MAC addresses, allowing real servers to send responses directly to clients, which improves performance and avoids the load‑balancer’s network‑card becoming a bottleneck.
3. Practical Build: LVS + Keepalived Load Balancing
3.1 Experiment Overview
Four CentOS 6.4 VMs are set up: two load‑balancer nodes (master and backup) and two real web servers. The DR mode is used with a virtual IP 192.168.80.200. Load‑balancer IPs are 192.168.80.100/101; web servers are 192.168.80.102/103.
3.2 Basic Preparation
On all servers:
Configure a static IP (setup command or GUI, then service network restart).
Set the hostname: temporary hostname xxxx, permanent edit /etc/sysconfig/network and reboot.
Add host entries in /etc/hosts for master, slave, and the two web servers.
Disable the firewall: service iptables stop and chkconfig iptables off.
3.3 Configure Web Servers
On each web server:
Start the HTTP service: service httpd start and enable it at boot with chkconfig httpd on.
Upload a custom static page (e.g., showing its IP) via FTP.
Edit the /etc/init.d/realserver script to bind the virtual IP to the loopback interface (example shown below).
Make the script executable: chmod 755 realserver and start it: service realserver start.
3.4 Configure Primary Load‑Balancer
Install Keepalived: yum install -y keepalived Edit /etc/keepalived/keepalived.conf (example configuration):
global_defs {
notification_email { [email protected] }
notification_email_from [email protected]
smtp_server 192.168.80.1
smtp_connection_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { 192.168.80.200 }
}
virtual_server 192.168.80.200 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
real_server 192.168.80.102 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.80.103 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}Start Keepalived: service keepalived start.
3.5 Configure Backup Load‑Balancer
Same as the primary configuration, but change state to BACKUP and priority to 99 in the VRRP instance.
3.6 Validation Tests
(1) Requests are alternately forwarded to both web servers, confirming load distribution.
(2) When one web server fails (e.g., service httpd stop on 192.168.80.102), traffic is served only by the remaining server.
(3) When the primary load‑balancer fails (e.g., service keepalived stop on 192.168.80.100), the backup immediately assumes the master role and continues serving traffic.
Learning Summary
LVS is a widely adopted software load‑balancing solution used in large‑scale enterprise and internet systems. Operating at layer 4 gives it a performance advantage over layer‑7 solutions such as Nginx. The author plans to explore running ASP.NET MVC projects on Linux with Jexus in the future.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
