How to force set pod as failed after specific time?

5/12/2020

I have written a configmap that have a script pinging on such ip address, but I want to force quit the pod and set is as failed after number of re-tries!

How I can set that inside the init container?

this is my configmap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: check-database
  namespace: devops
data:
  check_db.sh: |
    #!/bin/sh

    touch res.txt
    count=0
    filled=false
    until [ ! "$count" -le 12 ]; do
        echo $count
        if [[ -s res.txt ]]
        then
              filled=true
              break
        fi
        count=$((count+1))
        nc -w 5 mysql-svc 3306 > res.txt
        echo "Database is still running!"
        sleep 10
    done

    if [[ "$filled" == "true" ]]
    then
        echo "Database is up and running!"
    else
        echo "Session timeout!"
    fi
-- Noraa
configmap
contains
kubernetes
pod

1 Answer

5/13/2020

You should use rediness or liveness probes.

If liveness probe fails it will restart the pod.

If rediness probe fails it will mark pod as not ready.

Readiness and liveness probes can be used in parallel for the same container. Using both can ensure that traffic does not reach a container that is not ready for it, and that containers are restarted when they fail.

So you would need to choose if you wish to have pod being restarted all the time if ping fails, or mark pod as not ready so it doesn't accept traffic.

You can add few lines to your script that would create a file /tmp/ping if ping is OK. Then use the bellow probe to check of file with rediness probe:

readinessProbe:
  exec:
    command:
    - cat
    - /tmp/ping
  initialDelaySeconds: 5
  periodSeconds: 5

If you would like to use both liveness and readiness probes

Your pod might look like this:

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: mysql-test
  name: mysql-test-exec
spec:
  containers:
  - name: mysql-test
    image: mysql:5.6
    ports:
    - containerPort: 3306
    readinessProbe:
      tcpSocket:
        port: 3306
      initialDelaySeconds: 5
      periodSeconds: 10
    livenessProbe:
      tcpSocket:
        port: 3306
      initialDelaySeconds: 15
      periodSeconds: 20

This example uses both readiness and liveness probes. The kubelet will send the first readiness probe 5 seconds after the container starts. This will attempt to connect to the mysql-test container on port 3306. If the probe succeeds, the Pod will be marked as ready. The kubelet will continue to run this check every 10 seconds.

In addition to the readiness probe, this configuration includes a liveness probe. The kubelet will run the first liveness probe 15 seconds after the container starts. Just like the readiness probe, this will attempt to connect to the mysql-test container on port 3306. If the liveness probe fails, the container will be restarted.

You should also read Liveness and Readiness probes as it gives examples how to use which probe.

-- Crou
Source: StackOverflow