11.12.2024

How to make your website disaster-proof with NGINX and Keepalived

What is keepalived

Keepalived is service, designed for crashless infrastructure making and support. It is not all-sufficient entity and need balancer service to supply access to the "target" content. In this article I'll show how to setup a bundle "keepalived + haproxy + webserver" on Ubuntu 20.04 LTS.

Preparing procedures

Before installation we need to prepare our infrastructure:

Lets start to prepare:

<backend_server_one_IP> content1 content1.your.domain
<backend_server_two_IP> content2 content2.your.domain
<balancer_server_one_IP> haproxy1 haproxy1.your.domain
<balancer_server_two_IP> haproxy2 haproxy2.your.domain
<1st_floating_IP_for_keepalived> your.domain
<2nd_floating_IP_for_keepalived> your.domain

Bundle "webserver, balancer and watch daemon" setup

apt-get update; apt install -y nginx

echo $(hostname) > /var/www/html/index.nginx-debian.html  # path may be different, it is website working directory and index file

apt -y install haproxy

#frontend
#---------
frontend my_haproxy
bind *:80
stats uri /haproxy?stats
default_backend my_haproxy

# backend
#---------
backend my_haproxy
balance roundrobin
mode http
server content1 <1st_backend_server_private_IP>:80 check
server content2 <2nd_backend_server_private_IP>:80 check

service haproxy restart

while sleep 3; do curl http://<1st_balancer_public_IP>; done

while sleep 3; do curl http://<2nd_balancer_public_IP>; done

If you see interleaved "answers" in both terminal windows - haproxies are working, go to the next step;

apt install -y keepalived
ip a | grep <server_public_IP>

nano /etc/keepalived/keepalived.conf

 

vrrp_instance MY_KEEPALIVED1 {
state MASTER
interface ens192
virtual_router_id 1
priority 11

virtual_ipaddress {
45.14.48.134/29 dev ens192 label ens192:1
}
}

Parameters description:

vrrp_instance - any name without spaces,

state - server role,

interface - network interface name, you knew it upon tle previous step,

virtual_router_id - instance identifier, should be identical on the all balancers,

priority - using order, master-server should have higher priority ,

virtual_ipaddress - one of unused IPs in the subnet, your domain should be "pinned" to this address.

vrrp_instance MY_KEEPALIVED1 {
state SLAVE
interface ens192
virtual_router_id 1
priority 10

virtual_ipaddress {
45.14.48.134/29 dev ens192 label ens192:1
}
}

Pay attention to the config text - "priority" and "state" parameters are "swapped", all other points are stay identical.

systemctl enable haproxy && service haproxy restart; systemctl enable keepalived && service keepalived start

systemctl status haproxy && service keepalived status

Checking and disaster simulation

So, time to check our work.

while sleep 5; do curl http://<your_domain>; done

Both servers are working and in-turn answering;

systemctl stop haproxy && service keepalived stop

As we see, backup-balancer turns to master, no сontent delivering interrupts;

service nginx stop

As we see, just one package was dropped, then our project continue to deliver content from second node;

systemctl start haproxy && service keepalived start

Conclusion

In this tutorial I explained what is keepalive daemon and how to make crashesproof infrastructure with nginx, haproxy and keepalived packages on Ubuntu 20 LTS.