Mastering LVS: From Basics to MySQL High Availability with Keepalived
This article provides a comprehensive guide to Linux Virtual Server (LVS), covering its history, four operating modes, scheduling algorithms, installation steps, MySQL load‑balancing configuration, ARP suppression techniques, and high‑availability setup using keepalived, all illustrated with diagrams and code snippets.
LVS Overview
LVS (Linux Virtual Server) is a kernel‑level load‑balancing solution that implements IPVS. It originated as a kernel patch in Linux 2.2 and became part of the mainline kernel from version 2.4.23 onward.
IPVS History and Layers
IPVS (IP Virtual Server) is the kernel module that provides the actual scheduling. In modern operations (2022) LVS is usually placed in front of an Nginx 7‑layer proxy to handle massive TCP traffic, while Nginx handles HTTP/HTTPS.
Four LVS Working Modes
Network Address Translation (NAT)
IP Tunneling (TUN)
Direct Routing (DR)
Full NAT (FULLNAT)
Mode Principles
NAT : The load balancer rewrites the destination address of incoming packets, forwards them to real servers, and rewrites the source address on the reply so that the client always sees the virtual IP.
TUN : Uses IP‑in‑IP tunnels to forward packets; real servers can be in a different VLAN but must support IPIP.
DR : Only the destination MAC address is changed; the real server sends the reply directly to the client. DR requires the load balancer and all real servers to share the same physical network segment.
FULLNAT : Extends NAT by performing both DNAT and SNAT, allowing real servers to reside in a different VLAN while the client still sees the virtual IP. Performance loss is <10 % compared with pure NAT.
Scheduling Algorithms
LVS supports two families of algorithms. Fixed algorithms: rr, wrr, dh, sh. Dynamic algorithms: wlc, lc, lblc, lblcr, SED, NQ. The most commonly used are rr, wrr, and wlc.
Installation and Basic Commands
yum install ipvsadm -y
rpm -qa | grep ipvsadm # ipvsadm-1.27-8.el7.x86_64
modprobe ip_vs # load ip_vs module
lsmod | grep ip_vs # verify
uname -r # kernel version
ln -s /usr/src/kernels/$(uname -r) /usr/src/linux # for kernel headersMySQL Load‑Balancing Example
1. Configure a virtual IP (VIP) on the LVS director.
ifconfig eth1:18 172.16.1.18/24 up
# optional route add -host 172.16.1.18 dev eth12. Add real servers with ipvsadm using the DR mode.
ipvsadm -C
ipvsadm --set 30 5 60
ipvsadm -A -t 172.16.1.18:3306 -s wrr -p 20
ipvsadm -a -t 172.16.1.18:3306 -r 172.16.1.7:3306 -g -w 1 # DR
ipvsadm -a -t 172.16.1.18:3306 -r 172.16.1.51:3306 -g -w 1
ipvsadm -Ln # view table3. Suppress ARP on the real servers so they do not answer for the VIP.
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announceARP Suppression Script
#!/bin/bash
VIP=(172.16.1.18)
for ip in "${VIP[@]}"; do
ifconfig lo:${ip##*.} $ip netmask 255.255.255.255 up
done
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
# ... similar for all interfacesHigh Availability with keepalived
Two LVS directors (lb4‑01 and lb4‑02) run keepalived in VRRP mode. The master holds the virtual IP 172.16.1.18/24 and runs a virtual_server block for MySQL port 3306 using the DR kind and wrr scheduler. The backup takes over when the master fails.
global_defs {
router_id lb01
}
vrrp_instance VI_2 {
state MASTER
interface eth1
virtual_router_id 52
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.1.18/24 dev eth1 label eth1:18
}
}
virtual_server 172.16.1.18 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 20
protocol TCP
real_server 172.16.1.7 3306 { weight 1 TCP_CHECK { connect_timeout 5 delay_before_retry 3 connect_port 3306 } }
real_server 172.16.1.51 3306 { weight 1 TCP_CHECK { connect_timeout 5 delay_before_retry 3 connect_port 3306 } }
}Web Layer (L4+L7) Example
Two Nginx instances provide HTTP load balancing on VIP 10.0.0.17 while the LVS directors handle TCP traffic on 172.16.1.18. The configuration mirrors the MySQL example but uses port 80 and the DR kind.
Key Takeaways
LVS is a kernel‑level load balancer that works well with Nginx for L4/L7 hybrid deployments.
Choosing the proper mode (NAT, TUN, DR, FULLNAT) depends on network topology and performance requirements.
ARP suppression is essential for DR/FULLNAT to avoid IP conflicts.
keepalived provides simple VRRP‑based high availability for both the virtual IP and the LVS configuration.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
