Docker Keepalived: check if a HAProxy (in another container) is healthy

851 Views Asked by At

I have two Docker containers:

1 for Keepalived 1 for Haproxy

The problem is that my track_script killall -0 haproxy (to check if haproxy is still alive) can't reach the process of haproxy (because it is in another container..)

What should I do ?

Thanks!

3

There are 3 best solutions below

1
On

Nothing. If the main process in a container exits, the container will exit too; Docker does this automatically, and you shouldn't (and for the most part can't) check on an individual process in another container.

You can use a restart policy to have Docker automatically restart a container, and its contained process, if it happens to exit:

docker run -d --restart on-failure ... haproxy
0
On

Finally I mount bind the docker socket to run docker exec othercontainer killall -0 haproxy in my container.

Not the best method but.. it works

0
On

I had same scenario, but my keepalived was not in container. My scenario:

  1. HAProxy container
  2. Keepalived service (not container-based)

In order to monitor haproxy using track_script in keepalived, unfortunately I could not deal with docker command, so I used this track_script:

#!/bin/bash

output=$(netstat -tlpn | grep <YOUR_PORT_NUMBER>) 

if [[ -n "$output" ]]; then
  exit 0
else
  exit 1
fi

This track_script returns 0 when the port is used by haproxy container.