Linux HA

Creating a High Availability Nginx System on Linux with Keepalived

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.

HA System

Table of Contents

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!

gabriel-heinzer-4Mw7nkQDByk-unsplash

What are the 100 most common linux commands and its use?

Here are 100 commonly used Linux commands and a brief explanation of their usage:

ls – lists the contents of a directory
cd – changes the current working directory
pwd – displays the current working directory
mkdir – creates a new directory
rmdir – removes a directory
touch – creates a new empty file
rm – removes a file or directory
cp – copies files or directories
mv – moves or renames files or directories
cat – displays the contents of a file
less – displays the contents of a file one page at a time
grep – searches for a pattern in a file
find – searches for files in a directory hierarchy
chmod – changes the permissions of a file or directory
chown – changes the ownership of a file or directory
ps – displays information about running processes
top – displays real-time information about system performance
kill – sends a signal to terminate a process
tar – creates or extracts compressed archives
gzip – compresses or decompresses files
gunzip – decompresses gzip files
ping – tests network connectivity
ifconfig – displays network interface configuration
netstat – displays network connections and statistics
route – displays or modifies network routing tables
ssh – connects to a remote system using the Secure Shell protocol
scp – copies files between systems using the Secure Copy protocol
rsync – synchronizes files between systems
wget – downloads files from the web
curl – transfers data from or to a server
uname – displays information about the system
date – displays or sets the system date and time
cal – displays a calendar
whoami – displays the current user
su – switches to the root user or another user account
sudo – executes a command with elevated privileges
passwd – changes the user password
history – displays the command history
alias – creates a shortcut for a command
echo – displays text on the screen
tee – reads from standard input and writes to standard output and files
wc – displays the number of lines, words, and characters in a file
sort – sorts lines in a file
uniq – removes duplicate lines from a file
cut – extracts fields from a file
sed – performs text transformations on a file
awk – processes and manipulates text files
diff – compares two files or directories
patch – applies a patch to a file
tar – creates or extracts compressed archives
gzip – compresses or decompresses files
gunzip – decompresses gzip files
zcat – displays the contents of a compressed file
tail – displays the last lines of a file
head – displays the first lines of a file
tr – translates characters in a file
xargs – reads items from standard input and executes a command with them
cut – extracts fields from a file
paste – combines lines from multiple files
df – displays disk usage statistics
du – displays disk usage for a file or directory
mount – mounts a file system
umount – unmounts a file system
free – displays memory usage statistics

top – displays real-time information about system performance

ps – displays information about running processes