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
2
Answers
For your second point, if you are not familiar with Docker
ENTRYPOINT
andCMD
, I suggest you read the documentation about them because it is directly related to Kubernetes’scommand
andargs
.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 whileargs
, as the name suggests, should be used for its arguments. In your case it can look like: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.
...but my pod busybox2 is still running after few hours...
This is because the default value for
restartPolicy
isAlways
. Means after an hour, your pod actually restarted.See here for how K8s treats entrypoint, command, args and CMD.