Search code examples
kuberneteskubernetes-helm

Serialize creation of Pods in a deployment manifest using Helm charts


So I have a helm chart that deploys a pod, so the next task is to create another pod once the first pod is running.

So I created a simple pod.yaml in chart/templates which creates a simple pod-b, so next step to only create pod-b after pod-a is running.

So was only at helm hooks but don't think they care about pod status.

Another idea is to use Init container like below but not sure how to write command to lookup a pod is running?

spec:
  containers:
  - name: myapp-container
    image: busybox
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox
    command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']

Another idea is a simple script to check pod status something like:

y=`kubectl get po -l app=am -o 'jsonpath={.items[0].status.phase}'`
   while [ $i -le 5 ]
   do
    if [[ "$y" == "Running" ]]; then
      break
    fi
    sleep 5
   done

Any advice would be great.


Solution

  • If you want your post-install/post-upgrade chart hooks to work, you should add readiness probes to your first pod and use --wait flag.

    helm upgrade --install -n test --wait mychart .

    pod.yaml

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

    hook.yaml

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: "post-deploy"
      annotations:
        "helm.sh/hook": post-upgrade,post-install
        "helm.sh/hook-delete-policy": before-hook-creation
    spec:
      backoffLimit: 1
      template:
        metadata:
          name: "post-deploy"
        spec:
          restartPolicy: Never
          containers:
            - name: post-deploy
              image: k8s.gcr.io/busybox
              args:
              - /bin/sh
              - -c
              - echo "executed only after previous pod is ready"