Step-by-Step Guide to Building an LVS+Keepalived High Availability Cluster with Nginx
This tutorial walks through preparing four CentOS 7 servers, installing and configuring IPVS, Keepalived, and Nginx, creating a virtual IP script, starting the services, and verifying the load‑balancing setup with ipvsadm, providing a complete high‑availability solution.
This article provides a detailed, step‑by‑step tutorial for building a high‑availability load‑balancing cluster using LVS (IPVS) and Keepalived on four CentOS 7 servers, with Nginx as the web server.
Preparation : Four servers with IPs 192.168.210.85, 192.168.210.177, 192.168.210.8, and 192.168.210.195 are designated. System services include LVS and Keepalived; the web server runs Nginx, and the cluster uses the LVS DR mode.
Installation of required packages is performed with yum:
yum install ipvsadm -y yum install keepalived -yAfter installation, the Keepalived configuration file is edited (e.g., /etc/keepalived/keepalived.conf ) with the following content:
global_defs {
notification_email {
[email protected]
}
lvs_id hahashen1
}
vrrp_instance LVS {
state MASTER
interface ens33
virtual_router_id 51
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.210.11
}
}
virtual_server 192.168.210.11 80 {
delay_loop 1
lb_algo rr
lb_kind DR
persistence_time 0
protocol TCP
real_server 192.168.210.84 80 {
weight 1
TCP_CHECK {
connection_timeout 10
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.210.195 80 {
weight 1
TCP_CHECK {
connection_timeout 10
nb_get_retry 3
delay_before_retry 3
}
}
}The Keepalived service is then started:
systemctl start keepalivedNginx installation on the two web servers (192.168.210.84 and 192.168.210.195) is done with:
yum install nginx -yA helper script ( lvsnginx.sh ) configures the virtual IP and necessary kernel parameters:
#!/bin/bash
ifconfig lo:0 192.168.210.11 netmask 255.255.255.255 broadcast 192.168.210.11 up
route add -host 192.168.210.11 dev lo:0
echo "0" > /proc/sys/net/ipv4/ip_forward
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_announce
exit 0After deploying the script and starting Keepalived, the setup is verified using ipvsadm -ln , which should display the virtual server 192.168.210.11:80 with two real servers (192.168.210.84 and 192.168.210.195) in the routing table.
This concludes the configuration, providing a functional LVS+Keepalived load‑balancing cluster with Nginx.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
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.