Init Containers bypassing the command

1/8/2019

I am following documentation example at https://kubernetes.io/docs/concepts/workloads/pods/init-containers/#init-containers-in-use

I created following pod:

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  initContainers:
    - name: init-myservice
      image: busybox
      command:
        [
          "sh",
          "-c",
          "until nslookup myservice; do echo waiting for myservice; sleep 2; done;",
        ]
    - name: init-mydb
      image: busybox
      command:
        [
          "sh",
          "-c",
          "until nslookup mydb; do echo waiting for mydb; sleep 2; done;",
        ]
  containers:
    - name: myapp-container
      image: busybox
      command: ["sh", "-c", "echo The app is running! && sleep 3600"]

but I did not create the services yet (myservice, mydb).

My expectation is for deployment to hold until I create services, but it just continues with deployment and creates the pod called "myapp-pod".

Am I missing something on this run?
Why it does not hold until I create the services?

-- Teoman shipahi
kubernetes

1 Answer

1/8/2019

This happens because you are using ash inside busybox and it has different behavior (not same as bash). So your script actually ends there.

You can try it inside busybox yourself:

kubectl run -i --tty busybox --image=busybox --restart=Never -- sh

And then use your command:

until nslookup myservice; do echo waiting for myservice; sleep 2; done;

To fix this issue you can try something different, for example alpine.

kubectl run -i --tty alpine --image=alpine --restart=Never -- sh
-- Slava Bacherikov
Source: StackOverflow