Menu

US Region

Grandmetric LLC
Lewes DE 19958
16192 Coastal Hwy USA
EIN: 98-1615498
+1 302 691 94 10
info@grandmetric.com

EMEA Region

GRANDMETRIC Sp. z o.o.
ul. Metalowa 5, 60-118 Poznań, Poland
NIP 7792433527
+48 61 271 04 43
info@grandmetric.com

UK

Grandmetric LTD
Office 584b
182-184 High Street North
London
E6 2JA
+44 20 3321 5276
info@grandmetric.com

  • en
  • pl
  • Kubernetes Liveness Probe – HTTPRequest

    Design & Configure

    Kubernetes Liveness Probe – HTTPRequest

    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 settings

    Liveness Probe restarts the container when the given endpoint returns a status higher than 399. This feature also has some useful settings such as: 

    1. initialDelaySeconds – time after which Liveness Probe should start polling the endpoint 
    2. periodSeconds – the frequency of polling the endpoint 
    3. timeoutSeconds – time after which the timeout will expire. 
    4. successThreshold – the minimum number of successful attempts after which the probe will determine the correct operation of the container 
    5. failureThreshold – number of failed attempts after which the container will be restarted 

    httpGet options: 

    1. host – Host name to connect to
    2. scheme – Protocol type HTTP or HTTPS
    3. path – Path to access on the HTTP/HTTPS server
    4. httpHeaders– Custom headers to set in the request 
    5. port – Port number to access on the container 

     

    # 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 
          periodSeconds2 #Default 10 
          timeoutSeconds: 1 #Default 1 
          successThreshold: 1 #Default 1 
          failureThreshold: 3 #Default 3 

     

    Liveness Probe – example

    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 
    Index.html file was removed and the liveness probe tried to send a request to port 80, because the response status was not in the range from 200 to 399. Hence, the container will be restarted. You can see the result below.

     

    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 
    Author: Wojciech Tokarski
     
    Grandmetric