Search code examples
kuberneteskubernetes-helm

k8s check wait for pod existence


In K8s job file declared in yaml (with helm), I need that the job will run only whether the pod database exists and ready.

For the readiness, it works fine, since I added the following:

initContainers
    - name: wait-mysql-ready
      image: "bitnami//kubectl:latest"
      imagePullPolicy: IfNotPresent
      command:
        - kubectl
      args:
        - wait
        - pod/mysql-pod
        - --for=condition=ready
        - --timeout=120s

It works fine, but I need the job to run once (without duplications, and with long time period till it ends).

The job doesn't as usual run once, and it's name, when running kubectl get pods is <jobname-hash>

If it doesn't run once, at a the last attempt it will succeed (because the pod isn't created yet). Other attempt may failed.

So I added the following lines in main spec:

spec:
  parallelism: 1 
  completions: 1
  backoffLimit: 0

and in init container section (befor the previous one), I have added:

initContainers
  - name: wait-mysql-exist-pod
    image: "bitnami/kubectl:latest"
    imagePullPolicy: IfNotPresent
    command:
      - /bin/sh
    args:
      - -c
      - "while !(kubectl get pod mysql-pod); do echo 'Waiting for mysql pod to be existed...'; sleep 5; done"

(I could not find other good syntax for ! - for multiline string. I would be glade knowing how).

Also need another job to wait for the current job.

How can I create a job that run once, and check in the job that the pod exists before checking the ready state?


Solution

  • You can use this script as init-container to wait for other resources.