Liveness Probe HTTPRequest is an element in Kubernetes that allows you to control the liveness of a counter in pods using HTTP. The component allows to query a specific endpoint in the container and infer whether the application is working correctly.
In this article, I will list some common Kubernetes Liveness Probe and httpGet settings. Then I’ll show you a working example of how to properly configure Liveness Probe in your code.
Liveness Probe restarts the container when a 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
Utwórz pod:
kubectl create -f liveness-pod-example.yaml
Opisz 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 działa.
Żeby sprawdzić, czy działa liveness probe, usuń plik index.html z poda “liveness-request” poniższą komendą.
kubectl exec -it liveness-request -- rm /usr/share/nginx/html/index.html
Plik index.html został usunięty, a liveness probe próbował wysłać request do portu 80, ponieważ status odpowiedzi nie znajdował się w zakresie między 200 a 399. Kontener zostanie uruchomiony ponownie. Rezultat zobaczysz poniżej:
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