public

Kubernetes Health Checks: Demystified

When all of the containers inside of a pod starts Kubernetes will start sending traffic, no questions asked. What happens when you have an application that takes several seconds, or

Latest Post Speed kills software engineering. by Matthew Davis public

When all of the containers inside of a pod starts Kubernetes will start sending traffic, no questions asked. What happens when you have an application that takes several seconds, or minutes, to “warm up” before it is actually able to deliver any value?

Enter the readiness & liveliness probes. These are crude methods of asking your application for it’s current state.

Types of Probes

There are two types of probes built in:

1. Readiness Probe

The Readiness Probe is used to determine when your pod(s) should begin to receive traffic.

2. Liveliness Probe

The Liveliness Probe is an ongoing check used to determine if your application is still alive.

Probe Methods

Command

The command check will execute an arbitrary command within the context of your container. If the command executed returns with an exit code of 0 then it will be assumed healthy, otherwise any other exit code will be deemed unhealthy.

An example of a simple command check could be one that checks if a lock file exists:

...
      containers:
        - name:  k8-byexamples-spring-rest
          image: gcr.io/matthewdavis-byexamples/k8-byexamples-spring-rest:1d4c1401c9485ef61322d9f2bb33157951eb351f
          ports:
            - containerPort: 8080
              name: http
          livenessProbe:
            exec:
              command:
                - stat
                - /myapp/somethingcool.lock
            initialDelaySeconds: 5
            periodSeconds: 5
...

TCP

The TCP check will establish a connection with a TCP socket and can be customized with the port field.'

...
      containers:
        - name:  k8-byexamples-spring-rest
          image: gcr.io/matthewdavis-byexamples/k8-byexamples-spring-rest:1d4c1401c9485ef61322d9f2bb33157951eb351f
          ports:
            - containerPort: 8080
              name: http
          readinessProbe:
            tcpSocket:
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 5
          livenessProbe:
            tcpSocket:
              port: 8080
            initialDelaySeconds: 15
            periodSeconds: 15
...

httpCheck

An httpCheck will perform an http GET request and can be customized with the following fields:

...
      containers:
        - name:  k8-byexamples-spring-rest
          image: gcr.io/matthewdavis-byexamples/k8-byexamples-spring-rest:1d4c1401c9485ef61322d9f2bb33157951eb351f
          ports:
            - containerPort: 8080
              name: http
          readinessProbe:
            httpGet:
              path: /test/is_ready
              scheme: http
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 5
          livenessProbe:
            httpGet:
              path: /test/is_alive
              scheme: http
              port: 8080
            initialDelaySeconds: 15
            periodSeconds: 15
...

Example: https://github.com/mateothegreat/k8-byexamples-spring-rest/blob/master/manifests/deployment.yaml

Matthew Davis

Published 4 years ago