roeland

roeland

We built this at Pandosearch to replace an older library of ours, traffic_drain_plug, after a review turned up a handful of real bugs in it. We also realised it was ~260 lines wrapping graceful_stop to reimplement machinery the BEAM already ships for free. More detail on exactly what was wrong and why is in the traffic_drain_plug README - we kept that repo (deprecated) around specifically as a writeup of what was wrong.

kubernetes_probes is the replacement, built directly on OTP’s native shutdown sequence instead of a custom signal handler and mutable hook list. Two pieces:

  • KubernetesProbes.Plug — first plug in your endpoint. Answers /probe/liveness (200 while up) and /probe/readiness (200 while ready, 503 while draining or not ready).
  • KubernetesProbes.Drainer — a GenServer to be added as the last child of your application supervisor. On SIGTERM, OTP tears down children in reverse start order, so this terminates first. Its terminate/2 flips readiness to 503 via :persistent_term and sleeps for the drain window then OTP terminates your Endpoint, Repo, etc.

Usage

# mix.exs
{:kubernetes_probes, "~> 0.1"}

# lib/my_app/application.ex
children = [
  MyApp.Repo,
  MyAppWeb.Endpoint,
  # Must be last - terminates first on shutdown
  {KubernetesProbes.Drainer, wait: 20_000}
]

# lib/my_app_web/endpoint.ex
plug KubernetesProbes.Plug

# optional: gate readiness on something real, e.g. DB connectivity
plug KubernetesProbes.Plug, ready?: &MyApp.repos_ready?/0

Point your Deployment’s probes at /probe/liveness and /probe/readiness (paths can be configured) and set terminationGracePeriodSeconds to the drain window plus a few seconds of slack (30s covers the 20s default).

We have been running this in production for a while and are happy with the result, apart from one bug we found where an internal client kept the connection open (hence the 0.1.1 release).

Currently at 0.1.1 - MIT-0, on Hex and GitLab. Happy to hear if this is useful for anyone else’s setup. Feedback, bug reports or MRs more than welcome.

Where Next? Top

Trending in Announcing Top

wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New
pferriby
Introductory paragraph I’ll be looking for a keen junior or someone that has a couple of years experience in the real world (so you’ve be...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews