How to Build a High‑Availability Web Service on CentOS 7 with Keepalived & LVS
This guide walks you through setting up a highly available web service on CentOS 7 by using Vagrant to create four virtual machines, installing Keepalived and OpenResty, configuring VRRP and LVS for load balancing, binding a virtual IP, and testing failover to ensure continuous service delivery.
In this tutorial we show how to achieve high availability and load balancing for a web service on CentOS 7 using Keepalived and LVS.
Preparation
If you prefer a quick start, clone the mylxsw/keepalived-example repository and run make create to provision the entire demo environment automatically.
Create test VMs with Vagrant
Four virtual machines are defined in a Vagrantfile. Replace the placeholder IP addresses in the configuration files as follows:
keepalived (master) – 192.168.88.8 – Load‑balancer master
keepalived‑backup – 192.168.88.9 – Load‑balancer backup
node‑1 – 192.168.88.10 – Web server
node‑2 – 192.168.88.11 – Web server
client – 192.168.88.2 – Test client (any IP can be used)
The virtual IP (VIP) used for the service is 192.168.88.100, and the client accesses the service via this address.
Start Vagrant environment
vagrant upLog into each VM with: vagrant ssh All commands require root privileges; you can switch to root with su root (password: vagrant) or prefix commands with sudo.
Set hostnames
# On 192.168.88.8
hostnamectl set-hostname keepalived
# On 192.168.88.9
hostnamectl set-hostname keepalived-backup
# On 192.168.88.10
hostnamectl set-hostname node-1
# On 192.168.88.11
hostnamectl set-hostname node-2Install Keepalived
yum install -y keepalived ipvsadmEnable it to start on boot:
systemctl enable keepalivedInstall web service (OpenResty/Nginx)
yum install -y yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
yum install -y openrestyModify the default page to display the server’s IP address:
ip addr show eth1 | grep '192.168.88.' | awk '{print $2}' > /usr/local/openresty/nginx/html/index.htmlEnable and start the web server:
systemctl enable openresty
systemctl start openrestyConfigure Keepalived for high availability
The following is the core of /etc/keepalived/keepalived.conf (both keepalived nodes use the same file except for the priority value).
global_defs {
router_id LVS_8808
}
vrrp_instance HA_WebServer {
state MASTER
interface eth1
virtual_router_id 18
garp_master_refresh 10
garp_master_refresh_repeat 2
priority 100 # set to 99 on the backup node
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.88.100/24
}
}
virtual_server 192.168.88.100 80 {
delay_loop 6
lb_algo wlc
lb_kind DR
protocol TCP
nat_mask 255.255.255.0
real_server 192.168.88.10 80 {
weight 3
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
}
}
real_server 192.168.88.11 80 {
weight 3
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
}
}
}After editing, restart Keepalived on both nodes:
systemctl restart keepalivedBind the VIP on the web servers (DSR mode)
Because we use Direct Server Return (DR) mode, each web server must bind the VIP to its loopback interface: ip addr add 192.168.88.100/32 dev lo To make the binding persistent, create /etc/sysconfig/network-scripts/ifcfg-lo:0 with the following content:
DEVICE=lo:0
IPADDR=192.168.88.100
NETMASK=255.255.255.255
ONBOOT=yes
NAME=loopbackRestart the network service:
systemctl restart networkTesting failover
Access the service via the VIP (e.g., http://192.168.88.100) and you will see responses from both web servers. Stop Keepalived on the master node: systemctl stop keepalived The backup node automatically takes over, and the VIP moves to it. Verify the change with ipvsadm -Ln and by checking the logs in /var/log/message. Restart Keepalived on the original master to see the VIP migrate back.
Conclusion
Following this guide you now have a functional high‑availability web service using Keepalived and LVS. The same approach can be applied to other critical services such as MySQL, Redis, or RabbitMQ.
References
24 Hours 365 Days Continuous Service: Core Technologies of Server Infrastructure
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.
