Home > Software design >  Usage of command variable in Kubernetes Pod object
Usage of command variable in Kubernetes Pod object

Time:10-29

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

CodePudding user response:

For your second point, if you are not familiar with Docker ENTRYPOINT and CMD, I suggest you read the documentation about them because it is directly related to Kubernetes's command and args.

In fact they are so related that they actually are the same thing. That means command should be used to set the "core" of the command while args, as the name suggests, should be used for its arguments. In your case it can look like:

command: ["sleep"]
args: ["3600"]

Now for the first point, it is because a Pod is not a Job, so it will continue to run unless there is a fatal error, the liveness probe fails or you delete it manually.

CodePudding user response:

...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.

  • Related