Liveness Probe HTTPRequest is an element in Kubernetes, thanks to which you can control the state of life of a counter in Pods using the HTTP protocol. The component allows you to send queries to a given endpoint in the container and infer whether the application is working correctly.
In this article, I will list some common settings of Kubernetes Liveness Probe and httpGet options. Then, I’ll show you a working example of how you can set up Liveness Probe correctly in your code.
Liveness Probe restarts the container when the given endpoint returns a status higher than 399. This feature also has some useful settings such as:
httpGet options:
# YAML example # liveness-pod-example.yaml # apiVersion: v1 kind: Pod metadata: name: liveness-request spec: containers: - name: liveness image: nginx ports: - containerPort: 80 livenessProbe: httpGet: path: / port: 80 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-request
Restart Count: 0 . . . Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled <unknown> default-scheduler Successfully assigned example-dc/liveness-request to dcpoz-d-sou-k8swor3 Normal Pulling 4m45s kubelet, dcpoz-d-sou-k8swor3 Pulling image "nginx" Normal Pulled 4m42s kubelet, dcpoz-d-sou-k8swor3 Successfully pulled image "nginx" Normal Created 4m42s kubelet, dcpoz-d-sou-k8swor3 Created container liveness Normal Started 4m42s kubelet, dcpoz-d-sou-k8swor3 Started container liveness
Pod works great, no issue count.
To check if liveness probe works, remove index.html file from “liveness-request” pod.
kubectl exec -it liveness-request -- rm /usr/share/nginx/html/index.html
kubectl describe pod liveness-request
Restart Count: 1 . . . Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled <unknown> default-scheduler Successfully assigned example-dc/liveness-request to dcpoz-d-sou-k8swor3 Normal Pulling 9s (x2 over 11m) kubelet, dcpoz-d-sou-k8swor3 Pulling image "nginx" Warning Unhealthy 9s (x3 over 13s) kubelet, dcpoz-d-sou-k8swor3 Liveness probe failed: HTTP probe failed with statuscode: 403 Normal Killing 9s kubelet, dcpoz-d-sou-k8swor3 Container liveness failed liveness probe, will be restarted Normal Pulled 7s (x2 over 11m) kubelet, dcpoz-d-sou-k8swor3 Successfully pulled image "nginx" Normal Created 7s (x2 over 11m) kubelet, dcpoz-d-sou-k8swor3 Created container liveness Normal Started 7s (x2 over 11m) kubelet, dcpoz-d-sou-k8swor3 Started container liveness