Skip to content
ducktorsPublic

About

Fastify ping responder for Kubernetes Liveness and Readiness Probes

Topics

Resources

Code of conduct

Stars

33 stars

Watchers

2 watching

Forks

Repository files navigation


ARECIBO


arecibo_telescope_sending_a_signal_into_the_space_during__b5c6335a-ae52-4f9d-9350-fd7953fb5013

CI pnpm@12.23.0 NPM version NPM downloads code style: Biome Known Vulnerabilities

Arecibo is a Fastify plugin that adds two routes for the Kubernetes probes: /arecibo/readiness and /arecibo/liveness. By default, each route replies with status 200. To tell Kubernetes that the application is not ready, add readiness checks or a custom callback.

Quick start

This section also gives AI coding agents the data that they need to add Arecibo to a Fastify application.

Requirements: Node.js 22 or later, and Fastify 4 or 5. Only the latest major version of Arecibo is maintained. For Fastify 2 or 3, refer to the older releases.

npm install arecibo # or: pnpm add arecibo, yarn add arecibo
const fastify = require('fastify')({ logger: true })
const arecibo = require('arecibo') // ES modules: import arecibo from 'arecibo'

fastify.register(arecibo, { logLevel: 'warn' })

// Listen on 0.0.0.0, because the kubelet connects to the Pod IP address.
fastify.listen({ port: 3000, host: '0.0.0.0' })

The plugin adds GET /arecibo/readiness and GET /arecibo/liveness. By default, each route replies with status 200 and the Arecibo message. For the probe settings, refer to Kubernetes probes.

Options

All options are optional.

Option Type Default Description
message string the Arecibo message The response body (text/html) of a route that has no callback.
readinessURL string '/arecibo/readiness' The path of the readiness route.
livenessURL string '/arecibo/liveness' The path of the liveness route.
readinessCallback RouteHandler replies 200 with message The handler of the readiness route.
livenessCallback RouteHandler replies 200 with message The handler of the liveness route.
readinessChecks Record<string, () => unknown> none The checks of the readiness route. See Readiness checks.
checkTimeout number 500 The time limit of each check, in milliseconds. Use a number from 1 to 2147483647.
checkInterval number none If set, the checks run in the background at this interval, in milliseconds. See Checks in the background.
shutdownDelay number none If set, the plugin starts a graceful shutdown with this delay, in milliseconds, on SIGTERM. See Graceful shutdown.
logLevel 'fatal', 'error', 'warn', 'info', 'debug', 'trace', or 'silent' 'info' The log level of the two routes.

Custom callbacks

A callback is a Fastify route handler. It can send a string or an object. Fastify serializes an object as JSON. Reply with status 503 to tell Kubernetes that the application is not ready.

const fastify = require('fastify')()
const arecibo = require('arecibo')

let ready = false // Set this to true when your application can accept traffic.

fastify.register(arecibo, {
  readinessCallback: (request, reply) => {
    if (!ready) {
      return reply.code(503).send({ status: 'not ready' })
    }
    return reply.send({ status: 'ready' })
  },
})

fastify.listen({ port: 3000, host: '0.0.0.0' })

Do not check external dependencies in livenessCallback. For example, do not check a database, a cache, or a different service. If a dependency fails, Kubernetes restarts your containers, and a restart does not repair the dependency. Put these checks in readinessCallback.

Readiness checks

Use readinessChecks to let the plugin check your dependencies on each readiness request.

fastify.register(arecibo, {
  readinessChecks: {
    database: () => db.query('SELECT 1'),
    queue: () => queue.isConnected(), // false means that the check failed
  },
  checkTimeout: 500, // optional, in milliseconds, defaults to 500
})
  • The checks run in parallel.
  • A check fails if it throws, rejects, returns or resolves to false, or does not settle in checkTimeout milliseconds.
  • The timeout does not stop the work of a check. A check that blocks the event loop also blocks the timeout.
  • If all checks pass, the route replies with status 200 and message.
  • If a check fails, the route replies with status 503 and the body Service Unavailable. The plugin logs the name and the error with request.log.error. The logLevel option 'silent' hides this log.
  • Do not use readinessChecks together with readinessCallback. The registration fails.
  • readinessChecks must be a plain object with at least one check, and each check must be a function. If not, the registration fails.
  • Set timeoutSeconds of the Kubernetes readiness probe to a value larger than checkTimeout.
  • The checks do not change the liveness route.

Checks in the background

Without checkInterval, the checks run on each readiness request. Each Pod then sends a query to your dependencies on each probe. Use checkInterval to run the checks at a fixed interval:

fastify.register(arecibo, {
  readinessChecks: { database: () => db.query('SELECT 1') },
  checkInterval: 5000, // in milliseconds
})
  • The first run starts when Fastify is ready. fastify.ready() waits for the first run.
  • The readiness route replies with the result of the last run. It does not run the checks.
  • The next run starts checkInterval milliseconds after the previous run ends. Thus two runs never overlap.
  • A result can be old by up to checkInterval plus checkTimeout milliseconds.
  • The plugin logs a failed check with fastify.log.error only when the check starts to fail. It logs with fastify.log.info when the check passes again. These logs use the logLevel option.
  • The runs stop when Fastify closes.
  • If you use checkInterval without readinessChecks, or if the value is not a number from 1 to 2147483647, the registration fails.

Readiness state

The plugin adds fastify.arecibo. Use it to tell Kubernetes when your application can accept traffic, for example after a cache warm-up.

await fastify.register(arecibo)
fastify.arecibo.setReady(false)

await warmUpCache()
fastify.arecibo.setReady(true)
  • setReady(false) makes the readiness route reply with status 503 and the body Service Unavailable. Only true makes the application ready again.
  • The state applies before the readiness handler. Thus the plugin does not run readinessCallback, or the checks of a request, while the application is not ready. With checkInterval, the checks continue to run in the background.
  • isReady() returns the current state. The initial state is ready.
  • The state does not change the liveness route.
  • If you register the plugin more than one time in the same context, or in a child context, all registrations share one state. Registrations in two sibling contexts have two different states.

Graceful shutdown

When Kubernetes stops a Pod, it sends SIGTERM. The cluster can still send new traffic to the Pod for some seconds. Use shutdownDelay to close Fastify only after this time.

fastify.register(arecibo, {
  shutdownDelay: 5000, // in milliseconds
})

On SIGTERM, the plugin does these steps:

  1. It sets the readiness state to not ready. The readiness route replies with status 503.
  2. It waits shutdownDelay milliseconds. All other routes, and the liveness route, continue to reply as usual.
  3. It calls fastify.close().
  • Without shutdownDelay, the plugin does not listen to SIGTERM.
  • Do not add a SIGTERM handler that calls fastify.close() directly. If Fastify closes during the delay, the delay stops.
  • The process exits after fastify.close() only if no other timer, connection, or handler keeps it open.
  • If you handle the signals yourself, call fastify.arecibo.shutdown(delay). It does the same steps and returns a promise. The default delay is 5000 ms. A second call returns the same promise.

Behaviour notes

  • The plugin ignores the prefix option of register, because it uses fastify-plugin. To change the paths, use readinessURL and livenessURL.
  • Global hooks also apply to the probe routes. For example, an authentication onRequest hook can reject the probes. Make such a hook skip the probe URLs.
  • The plugin ignores message for a route that has a callback.
  • The routes also answer HEAD requests.
  • With the default logLevel ('info'), an enabled Fastify logger logs each probe request. Use 'warn' to stop these request logs.
  • If a callback throws an error, the default Fastify error handler replies with status 500. The response body contains the error message. Do not put secrets in error messages.

TypeScript

import arecibo from 'arecibo'

This import needs esModuleInterop, allowSyntheticDefaultImports, or "module": "nodenext". For CommonJS output without these settings, use import arecibo = require('arecibo'). Do not use import * as arecibo from 'arecibo'. With esModuleInterop or nodenext, register does not accept that value.

Kubernetes probes

Add these fields to the container in your Kubernetes manifest:

ports:
  - name: http
    containerPort: 3000
startupProbe:
  httpGet:
    path: /arecibo/liveness # the startup probe and the liveness probe use the same route
    port: http
  periodSeconds: 5
  failureThreshold: 30
livenessProbe:
  httpGet:
    path: /arecibo/liveness # do not check external dependencies here
    port: http
  periodSeconds: 15
readinessProbe:
  httpGet:
    path: /arecibo/readiness
    port: http
  periodSeconds: 5
  • In this example, the application has 150 seconds (30 x 5) to start. Kubernetes does not run the other probes until the startup probe succeeds.
  • To give the cluster time to stop new traffic to the Pod, use the shutdownDelay option. Then you do not need a preStop hook.
  • Set terminationGracePeriodSeconds of the Pod to a value longer than shutdownDelay plus the shutdown time of your application. The default value is 30 seconds.

How to commit

This repo uses Semantic Release with Conventional Commits. The type of the commit message sets the type of the release:

feat: new feature ---> 1.x.0
fix: fix a bug ---> 1.0.x

To mark a breaking change, add a ! after the type (for example, feat!:) or add a BREAKING CHANGE: footer to the commit message. Each one starts a major release.

feat: remove an option

BREAKING CHANGE: the option no longer exists.

Reference

Fun fact: where does the name come from?

The name is inspired by the Arecibo message, a 1974 interstellar radio message carrying basic information about humanity and Earth sent to globular star cluster M13 in the hope that extraterrestrial intelligence might receive and decipher it. The message was broadcast into space a single time via frequency modulated radio waves at a ceremony to mark the remodelling of the Arecibo radio telescope in Puerto Rico on 16 November 1974.

About

Fastify ping responder for Kubernetes Liveness and Readiness Probes

Topics

Resources

Code of conduct

Stars

33 stars

Watchers

2 watching

Forks

Releases

Used by

Contributors

Languages