Pages

Friday, May 18, 2018

Kubernetes - Health Checks

Health Checks are exactly what they sound like , a way of checking the health of the resource.
In the case of a container , a health check is used to determine the health of a running
container.

When a health check is specified, it gives us a way to test if the container is working or
not,basically to test if the services are up or not. With no health check specified, it can be complex to find if the service running inside a container is active or not.

Kubernetes provides 2 layers of health checking Http checks or Tcp checks.

Http or Tcp - In this type of checking K8 can attempt to connect to the particular endpoint that is
provided in the check and gives a status of health on the successfully connection.

Application Specific - In this type of health check , application health is identified by using
command line scripts.

Http Check : In this we are going to create a http url hit and see if the pod endpoint can be connected or not

Create a basic health check pod as,
[root@manja17-I13330 kubenetes-config]# cat health-httpGet-pod.yml
apiVersion: v1
kind: Pod
metadata:
 name: testing-service
spec:
 containers:
   - name: test-ser
     image: docker.io/jagadesh1982/testing-service
     ports:
     - containerPort: 9876
     livenessProbe:
       initialDelaySeconds: 2
       periodSeconds: 5
       httpGet:
         path: /info
         port: 9876

In the above yaml file, we have created a health check to let k8 connect to the endpoint “/info”.
The important thing is the livenessProbe element. Under this we can specify either
httpGet,tcpSocket or exec. K8 will check the path and port specified and restart the pod if it
does not successfully return. Generally a status code between 200 to 399 are all considered as
healthy. The “initialDelaySeconds” lets us to wait until the pod finished initializing so that K8 can probe a health check.

Create the pod and confirm ,
[root@manja17-I13330 kubenetes-config]# kubectl get pods
kNAME              READY STATUS RESTARTS   AGE
testing-service   1/1 Running  0 27s

Now describe the pod using,
[root@manja17-I13330 kubenetes-config]# kubectl describe pod testing-service
Name:         testing-service
Namespace:    default
Node:         work-node1/10.0.2.15
Start Time:   Fri, 18 May 2018 03:47:03 -0400
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.44.0.1
Containers:
   Liveness:       http-get http://:9876/info delay=2s timeout=1s period=5s #success=1 #failure=3
 ************

Health Check - Exec : In this type of health check we will use k8 application specific or a command to identify the health of a container.

Create a exec health check as,
[root@manja17-I13330 kubenetes-config]# cat health-exec-pod.yml
apiVersion: v1
kind: Pod
metadata:
 name: testing-service
spec:
 containers:
   - name: test-ser
     image: docker.io/jagadesh1982/testing-service
     ports:
     - containerPort: 9876
     livenessProbe:
       initialDelaySeconds: 2
       periodSeconds: 5
       exec:
        command:
         - cat
         - /testing_service.py

The period seconds field specifies that the k8 should perform the liveness probe every 5 seconds. initialDelaySeconds let the container complete initializing before the liveness test is done.

The exec probe executes the command “cat /testing_service.py” in the container. If the file is
available and command is successfull which mean returns 0 then health check is success.
If the command returns non-zero , k8 will kill the containers and restart.

Health Check - Tcp Sockets: A health check for a TCP Port check or validation can also be done in the Pod as below,

A Tcp Health check can be configured using,
[root@manja17-I13330 kubenetes-config]# cat health-tcpSocket-pod.yml
apiVersion: v1
kind: Pod
metadata:
 name: testing-service
spec:
 containers:
   - name: test-ser
     image: docker.io/jagadesh1982/testing-service
     ports:
     - containerPort: 9876
     livenessProbe:
       initialDelaySeconds: 2
       periodSeconds: 5
       tcpSocket:
         port: 9876

This will check the port 9876, it that is up and used.

More to Come , happy learning :-)

No comments :

Post a Comment