Kubernetes Liveness Check
When deploying software with Kubernetes, you need to expose a liveness HTTP request in the application.
The Kubernetes default liveness HTTP endpoint is /healthz
, which seems to be a Google convention, z-pages.
A lot of Kubernetes deployments won’t rely on the defaults.
Here is an example Kubernetes pod configuration for a liveness check at <ip>:8080/health
:
apiVersion: v1kind: Podmetadata: name: liveness-httpspec: containers: - name: liveness image: k8s.gcr.io/liveness args: - /server livenessProbe: httpGet: path: '/health' port: 8080 initialDelaySeconds: 3 periodSeconds: 3
When setting up a new app to be deployed on Kubernetes, ideally, the liveness endpoint is defined in a service scaffold (this is company and framework dependent), but in the case it isn’t, you just need to add a simple HTTP handler for the route configured in the yaml file. In an express app, it could look something like this:
app.get('/health', (req, res) => { res.status(200).send('OK');});
Notably, the default express scaffold doesn’t come with this route. You either need to remember to add this route or create a custom scaffold that includes it following your Kubernetes liveness check convention.