Search code examples
kuberneteskubernetes-poddocker-desktop

Usage of command variable in Kubernetes Pod object


I'm trying to understand what is the correct usage of command in Pods. Taking below example of my yaml. This is a working YAML. My doubts are

1> the sleep command is issued for 3600 seconds, but my pod busybox2 is still running after few hours when I see pods via 'k get pods'. My current understanding is, the sleep should execute for 3600 seconds and the pod is supposed to die out after that, as there is no process running my Pod (like httpd, nginx etc). Not sure why this is

apiVersion: v1
kind: Pod
metadata:
  name: busybox2
  namespace: default
spec:
  containers:
  - name: busy
    image: busybox
    command:
      - sleep
      - "3600" 

2> When checked on k8s docs, the usage shows a different way to write it. I understand that cmd and the args are separate things.. but can I not simply use both ways for all scenarios? like writing command: ["sleep", "3600"] as first example, and command: - printenv \ - HOSTNAME as another way to write second yaml command section. Can someone elaborate a bit.

apiVersion: v1
kind: Pod
metadata:
  name: command-demo
  labels:
    purpose: demonstrate-command
spec:
  containers:
  - name: command-demo-container
    image: debian
    command: ["printenv"]
    args: ["HOSTNAME", "KUBERNETES_PORT"]
  restartPolicy: OnFailure

Solution

  • ...but my pod busybox2 is still running after few hours...

    This is because the default value for restartPolicy is Always. Means after an hour, your pod actually restarted.

    apiVersion: v1
    kind: Pod
    metadata:
      name: busybox2
      namespace: default
    spec:
      restartPolicy: OnFailure  # <-- Add this line and it will enter "Completed" status.
      containers:
      - name: busy
        image: busybox
        command:
          - sleep
          - "10"  # <-- 10 seconds will do to see the effect.
    

    See here for how K8s treats entrypoint, command, args and CMD.