Logo

dev-resources.site

for different kinds of informations.

πŸ”‹βš‘ Ensuring High Availability with Two-Server Setup Using Keepalived

Published at
11/28/2024
Categories
devops
linux
vagrant
cluster
Author
dorinandreidragan
Categories
4 categories in total
devops
open
linux
open
vagrant
open
cluster
open
Author
17 person written this
dorinandreidragan
open
πŸ”‹βš‘ Ensuring High Availability with Two-Server Setup Using Keepalived

Ensuring High Availability with Two-Server Setup Using Keepalived

Ensuring high availability with limited resources can be challenging. I recently wanted to prove you can do it using Keepalived and just two servers πŸ’ͺ✨. To prove it, I used Vagrant. Here's a quick rundown of my journey! πŸš€

Step 1: Creating the Vagrantfile

First, I created a Vagrantfile to define my virtual machines. This file specified the configuration for two Ubuntu 22.04 VMs, each with a unique MAC address and a static IP address in the same subnet.

Vagrant.configure("2") do |config|
  # Array of predefined MAC addresses
  mac_addresses = ["080027000001", "080027000002"]

  # Array of static IP addresses for the VMs
  ip_addresses = ["192.168.56.11", "192.168.56.12"]

  mac_addresses.each_with_index do |mac, index|
    config.vm.define "vm#{index + 1}" do |vm|
      vm.vm.box = "ubuntu/jammy64"
      vm.vm.network "private_network", ip: ip_addresses[index], mac: mac
      vm.vm.provider "virtualbox" do |vb|
        vb.name = "vm#{index + 1}"
        vb.memory = "512"
        vb.cpus = 1
      end

      # Provision Keepalived and Nginx
      vm.vm.provision "shell", inline: <<-SHELL
        sudo apt-get update
        sudo apt-get install -y keepalived nginx
        # Example Keepalived configuration
        sudo bash -c 'cat > /etc/keepalived/keepalived.conf <<EOF
vrrp_instance VI_1 {
    state #{index == 0 ? 'MASTER' : 'BACKUP'}
    interface enp0s8
    virtual_router_id 51
    priority #{index == 0 ? 100 : 90}
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.56.10
    }
}
EOF'
        sudo systemctl restart keepalived

        # Configure a simple web page to show role (MASTER or BACKUP)
        sudo bash -c 'echo "<html><body><h1>Server Role: #{index == 0 ? 'MASTER' : 'BACKUP'}</h1></body></html>" > /var/www/html/index.html'
        sudo systemctl restart nginx
      SHELL
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

Step 2: Spinning Up the VMs

With the Vagrantfile ready, I used Vagrant commands to create and start the virtual machines.

vagrant up
Enter fullscreen mode Exit fullscreen mode

This command created and configured the VMs as specified in the Vagrantfile. Once the VMs were up and running, I could SSH into them to verify the setup.

vagrant ssh vm1
vagrant ssh vm2
Enter fullscreen mode Exit fullscreen mode

Step 3: Configuring Keepalived

The heart of this setup was the Keepalived configuration. The Vagrantfile already included provisioning scripts to install and configure Keepalived on both VMs. Here’s a recap of the configuration:

  • On the MASTER server:
vrrp_instance VI_1 {
    state MASTER
    interface enp0s8
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.56.10
    }
}
Enter fullscreen mode Exit fullscreen mode
  • On the BACKUP server:
vrrp_instance VI_1 {
    state BACKUP
    interface enp0s8
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.56.10
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Testing the Setup

With everything configured, it was time to test the setup. I started by verifying that the virtual IP (VIP) was correctly assigned to the MASTER server.

ip a show enp0s8
Enter fullscreen mode Exit fullscreen mode

I then accessed the web server using the VIP to ensure it was reachable.

curl http://192.168.56.10
Enter fullscreen mode Exit fullscreen mode

If the setup is correct, you should see the page that indicates which server (MASTER or BACKUP) is responding.

Step 5: Simulating Failover

To test the failover functionality, I stopped the Keepalived service on the MASTER server.

sudo systemctl stop keepalived
Enter fullscreen mode Exit fullscreen mode

I then checked the BACKUP server to see if it had taken over the VIP.

ip a show enp0s8
Enter fullscreen mode Exit fullscreen mode

Sure enough, the BACKUP server had taken over, and the web server was still accessible via the VIP.

Lessons Learned

This experience proved that high availability can be achieved with just two servers using Keepalived. Here are some key takeaways:

  1. Simplicity and Power: Keepalived is a powerful tool that is relatively simple to set up and configure.
  2. High Availability on a Budget: Even with just two servers, you can achieve a high level of availability.
  3. Practical Testing: Always test your setup thoroughly to ensure that failover works as expected.

Conclusion

When it comes to ensuring high availability with limited resources, Keepalived is a great tool to have in your arsenal. I hope this article has inspired you to explore high availability setups further.

Feel free to share your own experiences or ask questions in the comments below. Use your resources wisely and keep your systems running smoothly! πŸ› οΈπŸ’‘πŸ’»βš™οΈ

vagrant Article's
30 articles in total
Favicon
Comandos BΓ‘sicos de Vagrant
Favicon
πŸ”‹βš‘ Ensuring High Availability with Two-Server Setup Using Keepalived
Favicon
Using Vagrant to Provision a VM and Multiple VMs
Favicon
How to Create and Configure a Multi-VM Vagrant Setup for Web and Database Servers
Favicon
Error : Loading Vagrant Up
Favicon
[pt-BR] Como utilizei o Vagrant e o Virtual Box para desenvolvimento local
Favicon
Deploying a Spring Boot Banking Application with Docker
Favicon
Resizing the disk on a Vagrant virtual machine
Favicon
πŸš€ Creating a Kubernetes Cluster with Vagrant: A Step-by-Step Guide πŸš€
Favicon
Setup a Docker Swarm cluster using Vagrant
Favicon
Solve "[email protected]: Permission denied (publickey)" error
Favicon
Vagrant ssh alternative
Favicon
Vagrant Setup for macOS with Apple Chips
Favicon
EXPLORING DOCKER CONTAINER USING VAGRANT
Favicon
Automate Your local VM workflow
Favicon
Day 3: Exploring Vagrant and Linux Server Setup
Favicon
Vagrant: A Comprehensive Guide to Managing Virtual Environments
Favicon
Vagrant and VMWare Fusion in Mac M1
Favicon
Windows GitHub Self Hosted Runner inside a Linux Container
Favicon
How to Enable a Virtual Machine on Your Windows Laptop With Vagrant and Git Bash
Favicon
Vagrant CLI Commands Cheat Sheet
Favicon
Install Virtual Machine With Vagrant & Remotely Access it
Favicon
Run Vagrant VMs in a M1/M2/M3 Apple Sillicon Chip
Favicon
Exploring Vagrant: Simplifying Development Environments
Favicon
Do M1 Mac Dream of x86 Linux?
Favicon
Vagrant - configurando VM's de forma performΓ‘tica
Favicon
Getting Started with Vagrant for Local Development Environments
Favicon
Vagrant For Beginners: Box Basics in Vagrant.
Favicon
Vagrant For Beginners: Creating Your First Virtual Environment.
Favicon
Getting started with Vagrant: a beginner's Guide

Featured ones: