Menu

Poland

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

Sweden

Drottninggatan 86
111 36 Stockholm
+46 762 041 514
info@grandmetric.com

UK

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

US Region

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

  • en
  • pl
  • se
  • Kubernetes liveness probe – Command Exec

    Design & Configure

    Kubernetes liveness probe – Command Exec

    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.

    Command Exec settings

    Liveness Probe restarts the container when the command returns a failure code. 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 restart 

    exec options: 

    1. command – list of commands (depend on image distribution) to do by liveness probe
    # 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

     

    Liveness Probe – example

    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 
    Index.html file was removed, and the liveness probe tried to check (by cat) this file. File does not exist; hence, the container will restart. You can see the result below.

     

    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.

    Author: Wojciech Tokarski
     
    Grandmetric