0
votes

I am preparing for the CKAD exam and I am doing practice with the questions provide here

I have a doubt over this two different way of executing commands. Here the example provided is with a job but I think the question might be more general and extendable to all containers.

Under the job exercises there are two requests:

Create a job named pi with image perl that runs the command with arguments "perl -Mbignum=bpi -wle 'print bpi(2000)'"

kubectl create job pi  --image=perl -- perl -Mbignum=bpi -wle 'print bpi(2000)'

Create a job with the image busybox that executes the command 'echo hello;sleep 30;echo world

kubectl create job busybox --image=busybox -- /bin/sh -c 'echo hello;sleep 30;echo world'

Why in the second command I need to provide /bin/sh -c as well?

How could I understand when to use it and when not?

1
You are using ; which isn't an argument to echo but a delimiter parsed by the shell, so you need a shell to parse and execute it. Otherwise, you'd be running echo with arguments hello;sleep and 30;echo and world which is likely not what you want. - CherryDT
Thank you very much for the answer, would you like to add it as answer so that I can accept it? - Cr4zyTun4

1 Answers

2
votes

Because in first example

kubectl create job pi  --image=perl -- perl -Mbignum=bpi -wle 'print bpi(2000)'

you call a perl interpreter, perl -Mbignum=bpi -wle 'print bpi(2000)'

and in second example you call a bash shell command, echo hello;sleep 30;echo world therefore /bin/sh -c

kubectl create job busybox --image=busybox -- /bin/sh -c 'echo hello;sleep 30;echo world'

The -- is saying after this it is a command is executed inside the container.