Open plinto
Docs›Guides

Use a load balancer of your own

Guide·4 min read

When plinto restarts your service on a host, what your load balancer sends there meanwhile fails. If it's a load balancer of your own, such as HAProxy, two scripts of your release take the host out first and put it back once it answers. An image's are the same scripts, run in its container by its entrypoint, with what the image has: socat or curl come with a Dockerfile base.

1. What the scripts get

Your release's scripts run on the host, in the release's folder, with the environment's configuration, APP_HOME, and what they need of the host:

Variable What it is Example
PLINTO_HOST the host's name in plinto web-2
PLINTO_ADDRESS its address, as plinto reaches it web-2.internal
PLINTO_PORT the app's port: the first of the environment's ports, else its health-url's; empty without either 8080
PLINTO_HEALTH_URL the environment's health-url; empty without one http://localhost:8080/health

plinto writes them with each release, so the scripts of a restart or a reboot get them too, not only a deployment's.

2. Out before it stops

scripts/pre-stop.sh runs as the service is asked to stop, before it's told to: the release before, at a deployment, and any other stop. Here it drains the host in HAProxy, through its runtime API, until its last request ends:

scripts/pre-stop.sh
# Out of HAProxy before the service stops: drained, until its last session ends, 30 s at most.
api() { echo "$1" | socat stdio "tcp:$HAPROXY_API"; }
sessions() { api "show stat" | awk -F, -v h="$PLINTO_HOST" '$1 == "billing" && $2 == h { print $5 }'; }
api "set server billing/$PLINTO_HOST state drain"
for i in $(seq 30); do
  [ "$(sessions)" = 0 ] && break
  sleep 1
done

One that fails is a warning, and the stop goes on.

3. Back in once it answers

scripts/post-start.sh runs once the service has started, at every start, before plinto checks its health:

scripts/post-start.sh
# Back in HAProxy once the service answers, as plinto's health check would see it.
api() { echo "$1" | socat stdio "tcp:$HAPROXY_API"; }
if [ -n "$PLINTO_HEALTH_URL" ]; then
  until curl -fsS -o /dev/null --max-time 5 "$PLINTO_HEALTH_URL"; do sleep 1; done
fi
api "set server billing/$PLINTO_HOST state ready"

One that fails fails the start: the deployment puts the release before back, whose own post-start.sh puts the host back in. A host that never answers fails after the environment's health-timeout.

4. Set it up

In HAProxy, name each server as the host is named in plinto, and open the runtime API to your hosts alone, on an address of your private network:

haproxy.cfg
global
  stats socket ipv4@lb.internal:9999 level admin

backend billing
  server web-1 web-1.internal:8080
  server web-2 web-2.internal:8080