Can someone explain me this please? `kubectl run busybox --image=busybox --comm . . .

Ravi Vijaykumar:
Can someone explain me this please?

kubectl run busybox --image=busybox --command --restart=Never -it -- env

  1. in what scenarios should we be using --restart? (I did not see this being added when we take declarative approach)
  2. in what scenarios should we use --command
  3. the last part is strange where it has just one - and followed by – and then space and env. (Anyone if you could add a line about this particular thing, would be helpful

Shwetha Shenoy V:
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#run
Explains it nicely

Bryan Tanoue:
usually it is --command env so that the env command is run.

Bryan Tanoue:
I remember -it is for an interactive shell, but most use something like:

Bryan Tanoue:
kubectl run busybox -it --image=busybox --command /bin/sh

Bryan Tanoue:
That usually drops you in a command line terminal in the shell. Pretty tired right now so someone double check that.

Sohaib Ahmed:
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#run

kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...]

For question number 2 you have added --command

at the end you have give – env (this is the command)

when u want to use different command then u have to provide

–command – COMMAND (in your case – env) you do not have any argument.

please check above link and correct me as well if I am wrong

Sohaib Ahmed:
when u provide --command you are overriding default command

Shwetha Shenoy V:
kubectl run busybox --image=busybox --restart=Never -it -- env in short, you are asking the pod to output the environment variables to the terminal. After the env variables are output, the task of the pod is complete and won’t restart (will be in completed state).
Do try the various options locally to grasp them better.

Sohaib Ahmed:

kubectl run busybox --image=busybox --restart=Never -- bin/sh -c "sleep 3600; ls"

In above kubectl command we are using default command and we are providing arguments only so when u see yaml file of above kubectl command will look like this

containers:

  • args:
    • bin/sh
    • -c
    • ls; sleep 3600
      image: busybox
      name: busybox

Sohaib Ahmed:

kubectl run busybox --image=busybox --restart=Never --command -- /bin/sh -c "while true; do echo 'Hi I am from command container' >> /var/log/index.html; sleep 5;done"

Sohaib Ahmed:
In above kubectl command we are overriding default using --command and our command is – /bin/sh

so when u see yaml file of above kubectl command will look like this

  • command:
    • /bin/sh
    • -c
    • while true; do echo ‘Hi I am from Main container’ >> /var/log/index.html; sleep
      5;done

Sohaib Ahmed:
https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/

Sohaib Ahmed:
plz read this as well

Ravi Vijaykumar:
holy!!! so many amazing people one question and so much help!!! Thanks @Sohaib Ahmed @Shwetha Shenoy V @Bryan Tanoue!!!

Bryan Tanoue:
Sure thing dude. We got ya back!

Bryan Tanoue:
@Sohaib Ahmed I know if you use the command with the - sleep 3600 that works fine as the argument. However, if you just have a number it has to be in quotes I think.

Anyway, my question is below

- command:
  - /bin/sh
  - -c
  - sleep 3600

Bryan Tanoue:

- command:
  - /bin/sh
  - -c
  - sleep
  - "3600"

Bryan Tanoue:
Both of those do the same thing right? I never had a good handle on why sometimes I see it in the top form and others the bottom.

Bryan Tanoue:
Also, I never understood why you couldn’t write