Karthik Rao V
Karthik Rao V

Reputation: 61

How do I run a command while starting a Pod in Kubernetes?

I want to execute a command during the creation of the pod. I see two options available :

kubectl run busybox --image=busybox --restart=Never -- sleep 3600

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

What is the difference between the above two commands ?

Upvotes: 0

Views: 21055

Answers (1)

confused genius
confused genius

Reputation: 3284

  • In short no difference in the outcome if you want to run "sleep 3600". Both perform the same operation.

  • To understand the behaviour of those options add dry-run option to it

  • First one passes "sleep" & "3600" as arguments to Entrypoint of busybox image which is "/bin/sh"

kubectl run busybox --image=busybox --restart=Never --dry-run=client -o yaml -- sleep 3600
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: busybox
  name: busybox
spec:
  containers:
  - args:
    - sleep
    - "3600"
    image: busybox
    name: busybox
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
  • second one passes "/bin/sh -c" , "sleep" & "3600" as arguments to Entrypoint of busybox image which is "/bin/sh" . So it will open a new shell to run "sleep 3600" inside the container.
kubectl run busybox --image=busybox --restart=Never --dry-run=client -o yaml -- /bin/sh -c "sleep 3600"
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: busybox
  name: busybox
spec:
  containers:
  - args:
    - /bin/sh
    - -c
    - sleep 3600
    image: busybox
    name: busybox
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}
  • As mentioned in the beginning it does not make any difference in the outcome of "sleep 3600" But this method is useful when you want to run multiple commands by container for example "sleep 3600" & "echo boo". so the syntax would be
kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "sleep 3600;echo boo"

Upvotes: 2

Related Questions