Defining command & argument - Pod yaml

7/26/2021

As per the syntax mentioned, below is the Pod yaml using args:

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-exec
spec:
  containers:
  - name: liveness
    image: k8s.gcr.io/busybox
    resources:
      limits:
        memory: "64Mi" #64 MB
        cpu: "50m" #50 millicpu (.05 cpu or 5% of the cpu)
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5

Documentation says: If you define args, but do not define a command, the default command is used with your new arguments.

As per the documentation, What is the default command for the arguments(in above yaml)?

-- overexchange
dockerfile
kubernetes
kubernetes-pod
linux

2 Answers

7/26/2021

"Default command" references the command set in your container image. In case of your image - k8s.gcr.io/busybox - this appears to be /bin/sh:

$ docker pull k8s.gcr.io/busybox
Using default tag: latest
latest: Pulling from busybox
a3ed95caeb02: Pull complete 
138cfc514ce4: Pull complete 
Digest: sha256:d8d3bc2c183ed2f9f10e7258f84971202325ee6011ba137112e01e30f206de67
Status: Downloaded newer image for k8s.gcr.io/busybox:latest
k8s.gcr.io/busybox:latest
$ docker image inspect k8s.gcr.io/busybox | jq '.[0] | .Config.Cmd'
[
  "/bin/sh"
]

So, by explicitly setting a pod.spec.containers.command, you are effectively overriding that value.

See also:

$ kubectl explain pod.spec.containers.command
KIND:     Pod
VERSION:  v1

FIELD:    command <[]string>

DESCRIPTION:
     Entrypoint array. Not executed within a shell. The docker image's
     ENTRYPOINT is used if this is not provided. Variable references $(VAR_NAME)
     are expanded using the container's environment. If a variable cannot be
     resolved, the reference in the input string will be unchanged. The
     $(VAR_NAME) syntax can be escaped with a double $, ie: $(VAR_NAME).
     Escaped references will never be expanded, regardless of whether the
     variable exists or not. Cannot be updated. More info:
     https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell

Read more here.

-- Emile Pels
Source: StackOverflow

7/26/2021

To visualize this, you can run the following command:

kubectl  run busybox-default --image busybox
pod/busybox-default created
kubectl  run busybox-command --image busybox --command sleep 10000
pod/busybox-command created

check the output of docker ps and look for COMMAND column. You may use --no-trunc flag for complete output.

Output for the container running without command option:

docker ps |grep -E 'busybox-default'
CONTAINER ID        IMAGE                  COMMAND                  CREATED              STATUS              PORTS               NAMES
e01428c98071        k8s.gcr.io/pause:3.2   "/pause"                 3 minutes ago       Up 3 minutes                            k8s_POD_busybox-default_default_3449d2bc-a731-4441-9d78-648a7fa730dd_0

Output for the container running with command option:

docker ps |grep -E 'busybox-command'
CONTAINER ID        IMAGE                  COMMAND                  CREATED              STATUS              PORTS               NAMES
557578fc60ea        busybox                "sleep 10000"            5 minutes ago       Up 5 minutes                            k8s_busybox-comand_busybox-command_default_57f6d09c-2ed1-4b73-b3f9-c2b612c19a16_0
7c6f1240ab07        k8s.gcr.io/pause:3.2   "/pause"                 5 minutes ago       Up 5 minutes                            k8s_POD_busybox-comand_default_57f6d09c-2ed1-4b73-b3f9-c2b612c19a16_0
-- P....
Source: StackOverflow