HAProxy community

Disable Haproxy when all backend servers are down

Hi
I am looking for setting in haproxy config file (/etc/haproxy/haproxy.cfg) by which haproxy server gets disabled as soon as all its backend servers are DOWN automatically . Similarly , HA Proxy server should get enabled automatically as soon as even 1 backend server is UP.

My Config file below:

global
log 127.0.0.1:514 local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats timeout 30s
user haproxy
group haproxy
daemon

defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000

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

#round robin balancing backend http
#-----------------------------------
backend http_back
balance roundrobin
#balance leastconn
mode http
#server webserver1 10.10.31.179:8000 check
server webserver2 10.10.31.171:8080 check
server webserver3 10.10.31.172:8080 check

Can you elaborate what you mean by “disabled”?

Haproxy cannot close the listening socket, because it would not be able to open it again with the downgraded privileges.

I am using keep alive with haproxy load balancer.
Case: When all backend servers are down but HA Proxy load balancer remains UP

When HA Proxy load balancer goes down , keepalive is directing request automatically to DR machine BUT when all backend servers are down but HA Proxy load balancer remains UP , request is not diverting automatically to DR and just hangs out as backend webservers are down.

I need to divert request to DR in above case also . To achieve this , If i can stop haproxy and keep alive services on the fly( Ideally, using settings in ha proxy) in load balancer , Keep alive will automatically divert traffic to DR

Please tell me how can i achieve this?

You cannot stop haproxy automatically for the reason I mentioned above.

I suggest you configure HTTP level health checks on keepalived.

I am not sure this will help because keepalive is installed on HA Proxy load balancer machine which is still UP and running( healthy state). Is there way to do below:

  1. HA Proxy can tell keep keepalive that all my backend servers are down

Also , can i implement any open source monitoring service on load balancer that can stop HA proxy and keepalive when all backend servers are down AND start haproxy and keepalived services as soon as one one of the backend web servers are UP again

I can only help you with haproxy specific question unfortunately.

If you implement a specific “health check endpoint” in HAproxy that returns 200 OK when backends are up and something else when things are down you can then implement a HTTP based vrrp check script in keepalived as @lukastribus mentioned.

Something like this in the HAProxy config:

listen vrrp_check
    bind 127.0.0.1:8080
    monitor-uri /healthcheck.htm
    monitor fail if { nbsrv(backend_name) lt 1 }

Then a check script something like the following that is set up as a vrrp_script in your keepalived config:

#!/bin/sh
printf 'GET /healthcheck.htm\r\nHost: localhost\r\nConnection: close\r\n\r\n' \
    | nc localhost 8080 \
    | head -1 \
    | grep -q -e "^HTTP/.* 200" || exit 1

End result of the above is that /healthcheck.htm will return a “200” HTTP response when everything is up on the backend (ie nbsrv >= 1), so the script called by keepalived will finish OK. When things are all down, /healthcheck.htm will return a 503 so the script will exit 1, which will trigger keepalived to go into FAIL or BACKUP state depending on your config.

1 Like

I am NOT using keepalive now.

I want to implement a monitoring service in HA Proxy load balancer which can shut down haproxy server when all its backend servers are down and automatically start HA Proxy server(load balancer) when even one of its web servers are up again.

Below is my HA Proxy configuration file:
global
log 127.0.0.1:514 local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats timeout 30s
user haproxy
group haproxy
daemon

defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000

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

#round robin balancing backend http
#-----------------------------------
backend http_back
balance roundrobin
mode http
server webserver1 10.10.31.171:8080 check # ip_address_of_2nd_centos_webserver (RLV4WB03)
server webserver2 10.10.31.172:8080 check # ip_address_of_1st_centos_webserver (RLV4WB04)

That is not possible and out of scope of haproxy.