In today’s digital landscape, ensuring that your web services are always available is crucial. High Availability (HA) setups are designed to minimize downtime and ensure that services remain accessible even when some components fail. In this blog post, we’ll walk you through setting up a High Availability Nginx system on Linux using Keepalived. This setup ensures that if your primary server goes down, a backup server can take over seamlessly, providing uninterrupted service to your users.
Table of Contents
- Prerequisites
- Installing Nginx and Keepalived
- Configuring Nginx
- Configuring Keepalived
- Starting and Enabling Keepalived
- Testing the Setup
- Verifying the Virtual IP
- Conclusion
Prerequisites
Before we begin, make sure you have:
- Two or more Linux servers (e.g., Ubuntu)
- Nginx installed on each server
- Keepalived installed on each server
Installing Nginx and Keepalived
First, we need to install Nginx and Keepalived on both the primary and secondary servers. You can do this by running the following commands on each server:
sudo apt-get update
sudo apt-get install nginx keepalived
Configuring Nginx
Set up your Nginx configuration on both servers. Ensure that both configurations are identical or synced to serve the same content.
Here’s a basic Nginx configuration example:
sudo nano /etc/nginx/nginx.conf
Add the necessary configurations for your web application. Ensure that the web content is the same on both servers. You can use tools like rsync
to sync content between servers.
Configuring Keepalived
Next, we’ll configure Keepalived to manage the failover process. Keepalived uses the Virtual Router Redundancy Protocol (VRRP) to create a virtual IP address that can float between the primary and secondary servers.
Configuring the Primary Server
Edit the Keepalived configuration file on the primary server:
sudo nano /etc/keepalived/keepalived.conf
Add the following configuration:
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 42
}
virtual_ipaddress {
192.168.1.100
}
track_script {
chk_nginx
}
}
vrrp_script chk_nginx {
script "pidof nginx"
interval 2
weight 2
}
Configuring the Secondary Server
Edit the Keepalived configuration file on the secondary server:
sudo nano /etc/keepalived/keepalived.conf
Add the following configuration
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 42
}
virtual_ipaddress {
192.168.1.100
}
track_script {
chk_nginx
}
}
vrrp_script chk_nginx {
script "pidof nginx"
interval 2
weight 2
}
Notes:
- Replace
eth0
with the correct network interface for your setup. - The primary server should have a higher priority than the secondary server.
- Replace
192.168.1.100
with the virtual IP address you want to use for HA.
Starting and Enabling Keepalived
After configuring Keepalived, start and enable the service on both servers:
sudo systemctl start keepalived
sudo systemctl enable keepalived
Testing the Setup
It’s crucial to test your HA setup to ensure that it works as expected.
Testing Failover
Primary Server Failover Test:
Stop the Nginx service on the primary server:
sudo systemctl stop nginx
Check if the secondary server takes over the virtual IP:
ip addr show
You should see the virtual IP (192.168.1.100
) assigned to the secondary server.
Secondary Server Failover Test:
Restart the Nginx service on the primary server:
sudo systemctl start nginx
Ensure that the virtual IP reverts back to the primary server:
ip addr show
Verifying the Virtual IP
From a client machine, ping the virtual IP address to ensure it’s reachable:
ping 192.168.1.100
You can also access the Nginx service using the virtual IP to confirm that it serves the content correctly.
Conclusion
By following these steps, you’ve set up a robust High Availability Nginx system using Keepalived. This setup ensures that if the primary server fails, the secondary server will take over seamlessly, providing continuous service to your users. This kind of HA configuration is essential for maintaining uptime and ensuring a reliable web service.
Keepalived’s simplicity and effectiveness make it an excellent choice for managing IP failover in an HA setup. With Keepalived and Nginx, you can provide a highly available web service that minimizes downtime and maximizes reliability.
Feel free to share your experiences or ask questions in the comments below. Happy hosting!