Starting a container with a specific command fails

8/31/2018

I am trying this example from the Kubernetes docs to define a command to run when starting the container.

I've directly copied the .yaml in the given example which is like below:

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

Then I've created the Pod with the kubectl apply -f command-demo.yaml

After that when I execute kubectl get pods, Pod is never starting:

NAME                                 READY     STATUS             RESTARTS   AGE
command-demo                         0/1       Completed          0          2m

Also when I execute kubectl logs command-demo related environment variables gets printed. Ex:

command-demo
tcp://10.80.2.3:443

It seems like when I use command in a yaml file, my pod is never starting but command is executed. Is there anyone got have any idea about this situation? Thanks in advance.

-- Cemal Unal
kubernetes

1 Answer

8/31/2018

This is exactly the behavior described in the example. The pod starts and runs a command which finished directly and exits. When the command exits the pod is stopped. The lifecycle of a Pod depends on the process with the PID 1 which is the command defined in the yaml or in the used Docker file specified by image.

Because of the restartPolicy: OnFailure defined the pod will not be restarted and gets the Status Completed. If you remove the policy the default is Always and your pod will restart over and over again until you delete the pod manually.

You can check some more details with kubectl descripe pod command-demo.

Problem is the example is only for learning and did not represent a real life example. Normally you run commands which do not stop and should be running the whole time like web server. But these are normally managed by deployments. Or you run a command which should be executed once and finish but for these a Job is used.

So just follow the other tutorials from Kubernetes. In the Run Applications section will be an example with a webserver nginx which will start a pod and keeps running.

-- mszalbach
Source: StackOverflow