Automating health checks using Kubernetes liveness and readiness probes
TypeScriptTo automate health checks in Kubernetes, liveness and readiness probes are used as mechanisms to manage the state of containers within a Pod. When you define a Pod in Kubernetes, you can specify liveness and readiness probes for each container:
-
Liveness Probes: They check if your application is alive. If it fails, the kubelet kills the container, and the container is subjected to its restart policy. It's useful to catch cases where an application is running but is unable to make progress.
-
Readiness Probes: They determine if your application is ready to service requests. If it fails, the kubelet removes the Pod's IP address from the service's endpoints, which has the effect of not sending traffic to the Pod until it passes the readiness check.
Here is how you do it using Pulumi with TypeScript. We’ll create a Deployment with defined liveness and readiness probes that check the availability of an HTTP endpoint
/healthz
.import * as k8s from "@pulumi/kubernetes"; const appName = "myapp"; const appLabels = { app: appName }; // Define a Deployment with health checks. const appDeployment = new k8s.apps.v1.Deployment(appName, { metadata: { labels: appLabels }, spec: { replicas: 1, selector: { matchLabels: appLabels }, template: { metadata: { labels: appLabels }, spec: { containers: [{ name: appName, image: "myimage", // replace with your application image ports: [{ name: "http", containerPort: 80 }], livenessProbe: { httpGet: { path: "/healthz", port: "http" }, initialDelaySeconds: 5, timeoutSeconds: 2, }, readinessProbe: { httpGet: { path: "/healthz", port: "http" }, initialDelaySeconds: 5, timeoutSeconds: 2, }, }], }, }, }, }); // Export the Deployment name export const deploymentName = appDeployment.metadata.name;
Explanation of the code above:
- A new instance of
k8s.apps.v1.Deployment
is created with:appName
as its name and label selector.- A single replica for the sake of this example.
- A template that describes the Pod with one container named after
appName
. - The image should be replaced with the one you are deploying; for example,
'nginx'
if you are deploying an nginx server. - The container exposes port 80 named "http". The liveness and readiness probes use HTTP GET requests to check the
/healthz
endpoint on that port. initialDelaySeconds
specifies that the kubelet should wait 5 seconds before the first probe is initiated.timeoutSeconds
tells the kubelet that the probe should time out after 2 seconds.
You should modify the
image
field with the exact image you want to deploy. The/healthz
endpoint is often used by convention in many applications, but in your actual deployment, you should provide the path to the specific health check endpoint defined in your application.In a real-world scenario, you may also want to adjust the
initialDelaySeconds
,timeoutSeconds
, as well as other probe parameters such asperiodSeconds
andfailureThreshold
according to your specific application needs and behaviors.-