Liveness probes are used by the kubelet (kubernetes agent) to identify when to restart a container. The kubelet can identify a pod crash and restart the pod in such an event. However, at times, it may happen that the pod is alive but the application is in a state where it cannot serve requests, and restarting the application makes sense.
For example, your application might have gone into a deadlock, and a restart may allow it to recover and start serving the requests again.
Liveness is configured in a pod container spec. You can see an example below.
apiVersion: v1kind: Podmetadata:labels:test: livenessname: live-execspec:containers:- name: livenessimage: k8s.gcr.io/busyboxargs:- /bin/sh- -c- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600livenessProbe:exec:command:- cat- /tmp/healthyinitialDelaySeconds: 5periodSeconds: 5
In the code snippet above, the pod
has a single container
. Let’s look at what each line does in some detail.
Line 15 defines the liveness probe.
We are using the exec
to indicate that the liveness probe is a command.
Line 18 and 19 indicate that the liveness probe will do a cat /tmp/healthy
and if the command return zero, we exit the code, and the liveness probe is considered successful.
Line 20 gives the value of initialDelaySeconds
as 5
, which tells Kubernetes to start the liveness probe after 5
seconds elapse from the pod start.
Line 21 gives the value of periodSeconds
as 5
, which tells Kubernetes to do a liveness check every 5
seconds after the first one.
Let’s look at an example of using an HTTP request for a liveness probe.
apiVersion: v1kind: Podmetadata:labels:test: livenessname: live-httpspec:containers:- name: livenessimage: k8s.gcr.io/livenessargs:- /serverlivenessProbe:httpGet:path: /healthzport: 8080httpHeaders:- name: Custom-Headervalue: AwesomeinitialDelaySeconds: 3periodSeconds: 3
In the code snippet above, the pod
has a single container
. Let’s look at what each line does in some detail.
httpGet
on line 14 to indicate that the liveness probe is an HTTP get request.200
.initialDelaySeconds
and periodSeconds
, both of which have been explained in the previous section.Let’s look at an example of using TCP for a liveness probe.
apiVersion: v1kind: Podmetadata:name: goproxylabels:app: goproxyspec:containers:- name: goproxyimage: k8s.gcr.io/goproxy:0.1ports:- containerPort: 8080livenessProbe:tcpSocket:port: 8080initialDelaySeconds: 15periodSeconds: 20
In the code snippet above, the pod
has a single container
. Let’s look at what each line does in some detail.
tcpSocket
on line 14 to indicate that the liveness probe is a TCP request.8080
. The liveness probe will succeed if the TCP socket connection to port 8080
succeeds.initialDelaySeconds
and periodSeconds
, both of which have been explained in the previous section.Liveness probes help the kubelet know when to restart your container. There are multiple ways to configure a liveness probe, and the application owner can choose what is best for the application.