Elegant way of checking other Container status

6/14/2019

Since Kubernetes does not implement a dependency between Containers, I was wondering whether there is an elegant way of checking whether another Container in the same Pod is ready.

I would assume the Downward API is necessary. Maybe it could be done by embedding kubectl inside the container - but is there a easier way?

-- abergmeier
kubernetes
kubernetes-pod

2 Answers

6/15/2019

Waiting container from other pod to be ready is easily done by using init container that will do a check (typically a curl on the health endpoint or anything else) until receiving an acceptable answer. On a container within the same pod, this solution will not work, but you can use the command part of the container specification do achieve a something quite similar.

For an HTTP service :

    command:
    - '/bin/sh'
    - '-c'
    - >
      set -ex;
      until curl --fail --connect-timeout 5 http://localhost:8080/login; do sleep 2; done
      && <start command>

You can easily achieve the same for a postgres database :

    command:
    - '/bin/bash'
    - >
      until pg_isready --host localhost -p 5432; do sleep 2; done
      && bash /sql/00-postgres-configuration.sh

Those are juste example, you must determine the best way to detect of your other container is up or not.

Have a look at https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/ to see how you can specify command to a pod.

-- Alexandre Cartapanis
Source: StackOverflow

6/15/2019

For now I ended up using a simple file existence check:

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
      ...
      - name: former
        readinessProbe:
          exec:
            command:
            - /bin/sh
            - "-c"
            - /bin/sh /check_readiness.sh && touch /foo/ready
        volumeMounts:
        - name: shared-data
          mountPath: /foo
        ...
      - name: latter
        command:
        - /bin/sh
        - "-c"
        - while [ ! -f /foo/ready ]; do sleep 1; done; /bar.sh
        volumeMounts:
        - name: shared-data
          mountPath: /foo
          readOnly: true
      ...
      volumes:
      - name: shared-data
        emptyDir: {}
-- abergmeier
Source: StackOverflow