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
  • K8S Liveness Probe HTTP Request

    Design & Configure

    K8S Liveness Probe HTTP Request

    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 Settings

    Liveness Probe restarts the container when a 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 – Endpoint polling frequency
    3. timeoutSeconds – time after which the timeout will expire.
    4. successThreshold – the minimum number of successful attempts after which the liveness probe will confirm that the container is operating correctly
    5. failThreshold – the number of failed attempts after which the container will be restarted

    httpGet options:

    1. host – name of the host to connect to
    2. scheme – HTTP or HTTPS protocol type
    3. path – access path on the HTTP/HTTPS server
    4. httpHeaders – Custom headers to set in the request
    5. port – The port number that can be accessed in 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 HTTP Request – przykłady

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