How to put healthcheck in a deploy manifest?

7/17/2019

I'm still learning kubernetes and, from pods, I'm moving to deploy configuration. On pods I like to put healthchecks, here's an example using spring boot's actuator:

livenessProbe:
      httpGet:
        path: /actuator/health
        port: 8080
    initialDelaySeconds: 60
    timeoutSeconds: 1
    periodSeconds: 30
    failureThreshold: 3

Problem is that the above configuration only works for pods. How can I use them in my deploy?

-- Phate
kubernetes

1 Answer

7/17/2019

The Deployment will create a ReplicaSet and the ReplicaSet will maintain your Pods

Liveness and readiness probes are configured at a container level and a Pod is considered to be ready when all of its containers are ready.

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-exec
spec:
  containers:
  - name: liveness
    image: k8s.gcr.io/busybox
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5

The Spring actuator health check API is part of your application that is bundled in a container.

Kubernetes will check the liveness and readiness probes of each container in a Pod, if any of these probes fail to return successfully after a certain amount of time and attempts it will kill the pod and start a new one.

Setting a probe at a deployment level wouldn't make sense since you can potentially have multiple pods running under the same deployment and you wouldn't want to kill healthy pods if one of your pods is not healthy.

A deployment descriptor using the same pod configuration would be something like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: liveness-deployment
  labels:
    app: liveness
spec:
  replicas: 3
  selector:
    matchLabels:
      app: liveness
  template:
    metadata:
      labels:
        app: liveness
    spec:
      containers:
      - name: liveness
        image: k8s.gcr.io/busybox
        args:
        - /bin/sh
        - -c
        - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
        livenessProbe:
          exec:
            command:
            - cat
            - /tmp/healthy
          initialDelaySeconds: 5
          periodSeconds: 5
-- jlmayorga
Source: StackOverflow