0

I am looking for a Kubectl wait command for init containers. Basically I need to wait until pod initialization of init container before proceeding for next step in my script.

I can see a wait option for pods but not specific to init containers.

Any clue how to achieve this

Please suggest any alternative ways to wait in script

magic
  • 254
  • 2
  • 10
  • 19

1 Answers1

1

You can run multiple commands in the init container or multiple init containers to do the trick.


  • Multiple commands
command: ["/bin/sh","-c"]
args: ["command one; command two && command three"]

Refer: https://stackoverflow.com/a/33888424/3514300


* Multiple init containers

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', "until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]

Refer: https://kubernetes.io/docs/concepts/workloads/pods/init-containers/#init-containers-in-use

Tummala Dhanvi
  • 3,007
  • 2
  • 19
  • 35
  • Thanks for the suggestions... if command1 fails then I should immediately display the logs using Kubectl logs -f podname -c initcontainer... How do I achieve this... kindly note I cannot execute manually.. I need to automate this – magic May 13 '20 at 19:55
  • The simplest solution would be to show the logs always not just when the init container failure – Tummala Dhanvi May 13 '20 at 20:11
  • If you want to show the logs only when init container fails you might have to write a wrapper script around the status of init pod https://kubernetes.io/docs/tasks/debug-application-cluster/debug-init-containers/#getting-details-about-init-containers – Tummala Dhanvi May 13 '20 at 20:22
  • Yes, I can display the logs with the help of a script, but the challenge here I don't know when my init container succeeded or failed. That is the main reason I am looking for a wait command for Init containers – magic May 15 '20 at 10:41
  • you can get the status of the init container when you describe pod, Refer: https://kubernetes.io/docs/tasks/debug-application-cluster/debug-init-containers/#getting-details-about-init-containers – Tummala Dhanvi May 15 '20 at 11:41