Liveness Probe Command Exec is an element in Kubernetes, thanks to which you can control the state of life of a counter in Pods using command inside containers. This option allows us to check, for example, the content of files, the existence of files and other options (available from the command level) that can give us information about the correct work of our container.
In this article, I will show you an example of using Kubernetes Liveness Probe Command Exec and available options.
Liveness Probe restarts the container when the command returns a failure code. This feature also has some useful settings such as:
exec options:
# YAML example # liveness-pod-example.yaml # apiVersion: v1 kind: Pod metadata: name: liveness-command-exec spec: containers: - name: liveness image: nginx ports: - containerPort: 80 livenessProbe: exec: command: - cat - /usr/share/nginx/html/index.html initialDelaySeconds: 2 #Default 0 periodSeconds: 2 #Default 10 timeoutSeconds: 1 #Default 1 successThreshold: 1 #Default 1 failureThreshold: 3 #Default 3
Create pod:
kubectl create -f liveness-pod-example.yaml
Describe the pod:
kubectl describe pod liveness-command-exec
Restart Count: 0 . . . Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled <unknown> default-scheduler Successfully assigned jenkins/liveness-command-exec to dcpoz-d-sou-k8swor2 Normal Pulling 100s kubelet, dcpoz-d-sou-k8swor2 Pulling image "nginx" Normal Pulled 97s kubelet, dcpoz-d-sou-k8swor2 Successfully pulled image "nginx" Normal Created 97s kubelet, dcpoz-d-sou-k8swor2 Created container liveness Normal Started 97s kubelet, dcpoz-d-sou-k8swor2 Started container liveness Warning Unhealthy 2s (x2 over 4s) kubelet, dcpoz-d-sou-k8swor2 Liveness probe failed: cat: /usr/share/nginx/html/index.html: No such file or directory
Pod works great, no issue count.
To check if the probe works, remove index.html file from “liveness-command-exec” pod.
kubectl exec -it liveness-command-exec -- rm /usr/share/nginx/html/index.html
kubectl describe pod liveness-command-exec
Restart Count: 1
.
.
.
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled <unknown> default-scheduler Successfully assigned jenkins/liveness-command-exec to dcpoz-d-sou-k8swor2
Warning Unhealthy 5s (x3 over 9s) kubelet, dcpoz-d-sou-k8swor2 Liveness probe failed: cat: /usr/share/nginx/html/index.html: No such file or directory
Normal Killing 5s kubelet, dcpoz-d-sou-k8swor2 Container liveness failed liveness probe, will be restarted
Normal Pulling 4s (x2 over 105s) kubelet, dcpoz-d-sou-k8swor2 Pulling image "nginx"
Normal Pulled 2s (x2 over 102s) kubelet, dcpoz-d-sou-k8swor2 Successfully pulled image "nginx"
Normal Created 2s (x2 over 102s) kubelet, dcpoz-d-sou-k8swor2 Created container liveness
Normal Started 2s (x2 over 102s) kubelet, dcpoz-d-sou-k8swor2 Started container liveness
Curious to see what else you can test with Kubernetes probes? Check this HTTPRequest config.