Alta disponibilidad en máquinas virtuales
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.
- Install
keepalived
on all nodes.
sudo dnf install -y keepalived
- 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:
|
- Create
check_nginx.sh
script.
sudo vim /bin/check_nginx.sh
#!/bin/sh
if [ -z `pidof nginx` ]; then
exit 1
else
exit 0
fi
- Specify permissions to
check_nginx.sh
script.
sudo chmod +x /bin/check_nginx.sh
- Enable
keepalived
service on all nodes.
sudo systemctl start keepalived
sudo systemctl enable keepalived
- Check the ip address on all nodes and you will see that only one node has the ip address
192.168.100.167
.`