13.1. Use a Liveness Probe#

In order to best maintain your pods, Kubernetes needs to know if a pod is alive or not. A liveness probe is an action that the Kubernetes system takes periodically to determine if a pod is working.

Tip

You can see examples of liveness probes in the Kubernetes documentation.

Since Django is a web application we can use a simple form of liveness probe: Having Kubernetes fetch a URL in our application. The configuration fragment shows how to configure the probe.

Add this to the container definition in deployment/deployment.yaml in the pod specification under the spec.containers[0] key:

livenessProbe:
  httpGet:
    path: /
    port: 8000
    httpHeaders:
    - name: host
      value: .localhost

After you redeploy your app you should see a steady stream of connections. These are the liveness probes:

$ kubectl logs -f deployment.apps/mysite-deployment
Watching for file changes with StatReloader
[12/Jan/2024 00:11:14] "GET / HTTP/1.1" 200 487
[12/Jan/2024 00:11:24] "GET / HTTP/1.1" 200 488
[12/Jan/2024 00:11:34] "GET / HTTP/1.1" 200 488
[12/Jan/2024 00:11:44] "GET / HTTP/1.1" 200 488
[12/Jan/2024 00:11:54] "GET / HTTP/1.1" 200 487
...