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! 🛠️💡💻⚙️

cluster Article's
30 articles in total
Favicon
Configure MariaDB Galera Cluster in ubuntu 24.04 LTS
Favicon
Kubenetes Cluster & Nodes related issues
Favicon
Scaling Node.js: Handling 1 Million Requests Like a Pro
Favicon
Guia de Comandos PM2
Favicon
Usage of Node.js Cluster vs Worker
Favicon
🔋⚡ Ensuring High Availability with Two-Server Setup Using Keepalived
Favicon
Monitor Apache Ignite in 5 Minutes: Fix Cluster Issues Fast!
Favicon
Patroni, ETCD ve HAProxy kullanarak Cluster Kurulumu ve Yapılandırması
Favicon
Boost Kubernetes Efficiency: Upgrade to v1.14 in 11 Easy Steps!
Favicon
native people's contribution to ML
Favicon
Create an EKS Cluster Using Terraform
Favicon
Kubernetes Cluster Setup Guide 2024
Favicon
Using Phoenix.PubSub as a message-bus for Elixir cluster
Favicon
Differences between primary, core and task nodes in Amazon EMR cluster
Favicon
What is the purpose of the "cluster" module in Node.js?
Favicon
What is the purpose of the "cluster" module in Node.js?
Favicon
Oracle Cluster File System(OCFS2) setup for shared Block Volume
Favicon
How to Setup a Kubernetes Cluster with Minikube
Favicon
Deploying a WildFly 30.0.1.Final cluster using Ansible
Favicon
Deploy NGINX Application on AWS ECS
Favicon
Kubeadm ile kubernetes cluster kurulumu
Favicon
Security Considerations in Kubernetes
Favicon
Demystifying Kubernetes Manifests
Favicon
Deleting a cluster in Big Animal
Favicon
Highly scalable Minecraft cluster
Favicon
Cluster Architecture
Favicon
Kafka Multi-Cluster Deployment on Kubernetes - simplified !
Favicon
Creating a distributed high-availability cluster
Favicon
Understanding the Difference Between Cluster and Worker in Node.js
Favicon
Different types of cluster in EDB

Featured ones: