DEV Community

Cover image for K8s Hands-On: Probes
Akshay Rao
Akshay Rao

Posted on

K8s Hands-On: Probes

Introduction
Hi, I am Akshay Rao. This series will help you in hands on experience with Kubernetes.

Pre-requisites

  • Understand of deployment and pods.
  • minikube running.

Agenda
In this blog we will understand about the probes, what are they, why are they used.

Let's Start
What and Why are probes in k8s?
We need to check the application containers are running properly before exposing it to the real world. These check are called probes.

readiness probe
When an application might need to load large data or configuration files during startup, or depend on external services after startup then readiness probe is used.
Let's understand through hands on

  • create a pod.
apiVersion: v1
kind: Pod
metadata:
  name: pod1 
spec:
  containers:
  - name: probes
    image: nginx
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5   # delay of 5 sec for 1st probe 
      periodSeconds: 5.      # every 5 sec the probe will run 
Enter fullscreen mode Exit fullscreen mode
  • deploy it
[ k8s-ckad (⎈|minikube:hands-on)]$ k create -f pod1.yaml 
pod/pod1 created
[ k8s-ckad (⎈|minikube:hands-on)]$ k get po
NAME   READY   STATUS              RESTARTS   AGE
pod1   0/1     ContainerCreating   0          3s
[ k8s-ckad (⎈|minikube:hands-on)]$ k get po
NAME   READY   STATUS    RESTARTS   AGE
pod1   0/1     Running   0          7s
Enter fullscreen mode Exit fullscreen mode
  • you can see that the pods is not ready(0/1) that can verify it by describing the events.
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  98s                default-scheduler  Successfully assigned hands-on/pod1 to minikube
  Normal   Pulling    97s                kubelet            Pulling image "nginx"
  Normal   Pulled     95s                kubelet            Successfully pulled image "nginx" in 1.895668441s
  Normal   Created    95s                kubelet            Created container probes
  Normal   Started    95s                kubelet            Started container probes
  Warning  Unhealthy  3s (x19 over 88s)  kubelet            Readiness probe failed: cat: /tmp/healthy: No such file or directory
Enter fullscreen mode Exit fullscreen mode
  • it says that the healthy directory doesn’t exist. now get in the pod and make a directory called healthy
[ k8s-ckad (⎈|minikube:hands-on)]$ kubectl exec -it pod1 bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@pod1:/# cd /tmp
root@pod1:/tmp# ls
root@pod1:/tmp# touch healthy
root@pod1:/tmp# ls
healthy
root@pod1:/tmp# exit
Enter fullscreen mode Exit fullscreen mode
  • now use kubectl get po and describe it.
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   False 
  PodScheduled      True
[k8s-ckad (⎈|minikube:hands-on)]$ k get po
NAME   READY   STATUS    RESTARTS   AGE
pod1   1/1     Running   0          6m26s
Enter fullscreen mode Exit fullscreen mode

liveness probe
Liveness probes detects a deadlock, in which a program is operating but unable to advance. Despite issues, restarting a container in this condition can assist to make the program more available.
Let's understand through hands on

apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
  - name: liveness
    image: nginx
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 600
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5
Enter fullscreen mode Exit fullscreen mode
  • deploy the pod, and describe it
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-hbk44:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  9s    default-scheduler  Successfully assigned hands-on/pod1 to minikube
  Normal  Pulling    8s    kubelet            Pulling image "nginx"
  Normal  Pulled     6s    kubelet            Successfully pulled image "nginx" in 1.917415303s
  Normal  Created    6s    kubelet            Created container liveness
  Normal  Started    6s    kubelet            Started container liveness
Enter fullscreen mode Exit fullscreen mode
  • wait for 30s and again describe the pod
Events:
  Type     Reason     Age                 From               Message
  ----     ------     ----                ----               -------
  Normal   Scheduled  2m1s                default-scheduler  Successfully assigned hands-on/pod1 to minikube
  Normal   Pulled     118s                kubelet            Successfully pulled image "nginx" in 1.917415303s
  Normal   Pulling    45s (x2 over 2m)    kubelet            Pulling image "nginx"
  Normal   Created    43s (x2 over 118s)  kubelet            Created container liveness
  Normal   Started    43s (x2 over 118s)  kubelet            Started container liveness
  Normal   Pulled     43s                 kubelet            Successfully pulled image "nginx" in 1.993915629s
  Warning  Unhealthy  0s (x6 over 85s)    kubelet            Liveness probe failed: cat: /tmp/healthy: No such file or directory
  Normal   Killing    0s (x2 over 75s)    kubelet            Container liveness failed liveness probe, will be restarted
Enter fullscreen mode Exit fullscreen mode
  • now the probe are failed, and it will try to restart the container
[k8s-ckad (⎈|minikube:hands-on)]$ k get po
NAME   READY   STATUS    RESTARTS      AGE
pod1   1/1     Running   4 (23s ago)   5m24s
Enter fullscreen mode Exit fullscreen mode
  • hope that this has clarified you with probes.

Thank you

Top comments (0)