Alta disponibilidad en máquinas virtuales

cloudinfrastructurelinuxkeepalived

In a production environment, when your applications are receiving a lot requests to a single node and you see that node isn’t able to handle all requests, you need to scale your application.

One way to do this is by adding more nodes to your application. But, how can you distribute the requests between all nodes? You can use a load balancer to distribute the requests between all nodes, but what happens if the load balancer fails? You need to have a backup load balancer to avoid downtime. This is where high availability comes in.

In this post, we will see how to configure high availability using Keepalived on virtual machines to avoid downtime.

What is Keepalived?

Is a tool that provides a heartbeat between two or more nodes. It’s used to monitor the health of your nodes and distribute the requests between all nodes.

The actual process

I am using 3 nodes running Alma Linux for this post, and each node has a NGINX server running on it. The idea is to have a high availability NGINX cluster that if one node fails, the other node will take the requests.

  1. Install keepalived on all nodes.
sudo dnf install -y keepalived
  1. Configure keepalived on all nodes.
sudo vim /etc/keepalived/keepalived.conf

global_defs {
  router_id nginx
}

vrrp_script check_nginx {
  script "/bin/check_nginx.sh"
  interval 2
  weight 50
  script_security script
}

vrrp_instance VI_01 {
  state BACKUP
  interface enp0s3
  virtual_router_id 50
  priority 90

  virtual_ipaddress {
    192.168.100.167/24
  }

  track_script {
    check_nginx
  }

  authentication {
    auth_type AH
    auth_pass secret
  }
} 
Variable Description
global_defs This section is used to define global variables. In this case, we are defining the router_id which is used to identify the cluster. This must be the same on all nodes.
vrrp_script This section is used to define the script that will be used to check if the node is the primary. In this case, we are using the check_nginx.sh script that we are going to create later.
vrrp_instance This section is used to define the instance of the cluster. In this case, we are defining the following variables:
  • state: This variable is used to define the state of the node. In this case, we are defining the state as BACKUP because we want to have a primary node and two backup nodes.
  • interface: This variable is used to define the interface that will be used to communicate with the other nodes.
  • virtual_router_id: This variable is used to define the id of the cluster. This must be the same on all nodes.
  • priority: This variable is used to define the priority of the node. The node with the highest priority will be the primary node.
  • virtual_ipaddress: This variable is used to define the virtual ip address of the cluster. This must be the same on all nodes.
  • track_script: This variable is used to define the script that will be used to check if the node is the primary. In this case, we are using the check_mysql script that we defined in an above variable.
  • authentication: This variable is used to define the authentication type and password. In this case, we are using the AH authentication type and the secret password.
  1. Create check_nginx.sh script.
sudo vim /bin/check_nginx.sh
#!/bin/sh

if [ -z `pidof nginx` ]; then
  exit 1
else
  exit 0
fi
  1. Specify permissions to check_nginx.sh script.
sudo chmod +x /bin/check_nginx.sh
  1. Enable keepalived service on all nodes.
sudo systemctl start keepalived
sudo systemctl enable keepalived
  1. Check the ip address on all nodes and you will see that only one node has the ip address 192.168.100.167.`