How to Set Up HAProxy with Keepalived for High Availability

When you have a load balancer, a common challenge comes in achieving availability, especially where continuous service availability is required. Even a load balancer can fail you if you have not incorporated a way to ensure its availability, and that’s where Keepalived comes into play.

It is an open-source framework that is designed for Linux systems to achieve reliability by managing the load balancing and IP failover. Keepalived is used with a load balancer such as HAProxy to ensure the continuity of service to achieve reliability. This post introduces Keepalived and goes through the steps to set it up with HAProxy.

What Is Keepalived?

A load balancer, such as HAProxy, is handy in handling the traffic to your site. However, you may get a case where availability is not guaranteed. Keepalived is an open-source software that ensures high availability by managing the IP failover with your load balancer and works seamlessly with HAProxy.

Some of the key features provided by Keepalived include the following:

1. Virtual IP Management

All servers are assigned with a virtual IP by Keepalived which allows them to operate as one virtual entity. Keepalived transfers the virtual IP to a backup server in case of a failover, avoiding downtime.

2. Health Checking

Keepalived is designed in a way that it can check the health of all services it manages. For instance, if a server is unresponsive, it immediately starts a failover and finds a backup server. With this health checking and failover feature, availability is guaranteed.

3. Load Balancing

Although its primary purpose is not being a load balancer, it’s possible to configure it to distribute the incoming traffic to different servers. This functionality is combined with a load balancer to enhance the system performance.

How to Set Up HAProxy with Keepalived

We already mentioned that Keepalived works with a load balancer. For this case, we set up HAProxy with Keepalived for high availability. Therefore, we must first install HAProxy and then set it up. Follow the given steps:

Step 1: Install HAProxy

Installing HAProxy is a straightforward process, especially since HAProxy is available from the default Linux repository. We are working with Ubuntu for this tutorial, but the steps are similar to those of the other distros.

Before installing HAProxy, quickly update the repository.

You can now use APT to install HAProxy.

$ sudo apt-get install haproxy

Step 2: Configure HAProxy

On your HAProxy, we must access the configuration file, define how you want to receive the incoming traffic from your client devices, and then define which servers to distribute the traffic.

Start by accessing the HAProxy config file.

$ sudo nano /etc/haproxy/haproxy.cfg

For this case, we specify that we will listen for all incoming connections via port 80. For the backend, we specify two servers using round robin to distribute the traffic. Ensure that you replace the server IPs to match your actual servers.

After you make the required edits to the config file, save it and exit. Lastly, restart HAProxy.

Step 3: Install Keepalived

Keepalived doesn’t come preinstalled in Linux. As such, we must install it from the default repository using APT. Run the following command:

$ sudo apt-get install keepalived

Confirm the installation by pressing “y” when prompted. Once the installation is complete, verify that Keepalived has been installed by checking its version.

Step 4: Configure Keepalived

To configure Keepalived, we must access its configuration file which is located at “/etc/keepalived/keepalived.conf”. Here, we must define how we want Keepalived to offer high availability to HAProxy.

We must define the virtual IP address, the priority for the available servers, and the script to check the health and status of HAProxy. In the following configuration file, we create the “chk_haproxy” script to asses HAProxy’s status every two seconds. We specify it using “enp0s3” as our network interface and create a VRRP instance named “VI_1”. Change the network interface to match your case.

Still, set a router ID that works for your interface and set what virtual IP address to use. You should also change the authentication password.

Step 5: Restart Keepalived

Once your Keepalived configuration is all set, save the file and close the text editor. You can then check the validity of the configuration file and restart the Keepalived service.

Lastly, ensure that HAProxy and Keepalived are started.

That’s it. You set up HAProxy with Keepalived for high availability. You can use the defined virtual IP to verify that the setup works as expected.


The setup that is defined in this post ensures that your load balancer is highly available to handle the incoming traffic by distributing it to the available servers. Moreover, Keepalived ensures that it handles failover and you won’t have cases of downtime with your load balancer. Follow through the steps and edit the configuration file to suit your preference.


Leave a Comment