Logo ← PostgreSQL Blog

Solving the HAProxy Cannot assign requested address Error

If you are setting up a High Availability (HA) stack with HAProxy and Keepalived, you’ve likely hit a wall with a cryptic error message…

Solving the HAProxy Cannot assign requested address Error

If you are setting up a High Availability (HA) stack with HAProxy and Keepalived, you’ve likely hit a wall with a cryptic error message while starting the service. Your configuration looks perfect, your IPs are planned, but HAProxy simply refuses to stay active.

The error usually looks like this in your systemctl status haproxy output:

[ALERT] Starting proxy postgres: cannot bind socket (Cannot assign requested address) [**.**.**.40:5000]

In this post, we’ll dive into why this happens and how to fix it with a simple kernel-level adjustment.

The Root Cause: The Virtual IP (VIP) Paradox

In a typical HA setup, you use a Virtual IP (VIP) — in this case, **.**.**.40that floats between two or more servers. Keepalived manages this IP. If Server A is the MASTER, it holds the IP. If it fails, Server B becomes the MASTER and takes over the IP.

Here is the problem: When you try to start HAProxy on the BACKUP server, the VIP is not yet assigned to its network interface. By default, Linux prevents any service from binding to an IP address that doesn’t locally exist on the machine.

HAProxy tries to listen on **.***.**.40, sees it’s not there, throws the Cannot assign requested address error, and shuts down.

The Solution: Enabling Non-Local Binding

To fix this, we need to tell the Linux kernel to allow services to bind to IP addresses that are not currently present on the system. This is a standard procedure for any floating IP architecture.

1. Temporary Fix (Runtime)

To apply the fix immediately without rebooting, run:

sudo sysctl -w net.ipv4.ip_nonlocal_bind=1

2. Permanent Fix (Persistent)

To ensure this setting survives a system reboot, you must modify the sysctl configuration.

Open the configuration file:

sudo nano /etc/sysctl.conf

Add the following line to the bottom of the file:

net.ipv4.ip_nonlocal_bind = 1

Apply the changes:

sudo sysctl -p

Verification

Now, try starting your HAProxy service again:

sudo systemctl start haproxy
sudo systemctl status haproxy

You will notice that even if the server is in the BACKUP state (meaning it doesn’t own the VIP yet), HAProxy will start successfully and wait in a ready state. The moment Keepalived promotes the server to MASTER and assigns the VIP, HAProxy will immediately begin routing traffic without any manual intervention.

Conclusion

The ip_nonlocal_bind parameter is the secret sauce for smooth failovers in HA clusters. It allows your load balancer to be ready and waiting for an IP address before it even arrives.

Summary Checklist:

  • The Error: Cannot assign requested address.
  • The Reason: HAProxy trying to bind to a non-existent Floating IP.
  • The Fix: Setting net.ipv4.ip_nonlocal_bind to 1.