Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubectl run --command vs -- arguments

I was a little confused with below command:

kubectl run busybox --image=busybox --restart=Never -o yaml --dry-run -- /bin/sh -c 'echo hello;sleep 3600'

YAML:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: busybox
  name: busybox
spec:
  containers:
  - args:
    - /bin/sh
    - -c
    - echo hello;sleep 3600
    image: busybox
    name: busybox
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

I know if we don't specify parameter --command, then parameters after -- will be treated as arguments.

But I wanted to know, how /bin/sh -c "echo hello;sleep 3600" was working in case of arguments? According to Kubernetes documentation(https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#notes), If we specify only arguments in a POD then docker EntryPoint will be used as EntryPoint command. So the resultant command which will be executed in the docker image will be Docker EntryPoint + kubectl arguments.

As Busybox DockerFile does not contain any EntryPoint(https://github.com/docker-library/busybox/blob/master/musl/Dockerfile), so arguments specified in the kubectl command will only be used, so the command will look like:

/bin/sh -c 'echo hello;sleep 3600'

And if we specify --command, then according to Kubernetes documentation, DockerFile arguments(CMD) and command(EntryPoint) both will be overridden with command specified in the kubectl command, so it will look similar to above:

/bin/sh -c 'echo hello;sleep 3600'

So it would be same in the end.

like image 711
Amit Thakkar Avatar asked Dec 09 '19 11:12

Amit Thakkar


People also ask

What is the difference between command and args in Kubernetes?

If you supply a command but no args for a Container, only the supplied command is used. The default EntryPoint and the default Cmd defined in the Docker image are ignored. If you supply only args for a Container, the default Entrypoint defined in the Docker image is run with the args that you supplied.

What does kubectl Run command do?

kubectl run − Run command has the capability to run an image on the Kubernetes cluster. kubectl scale − It will scale the size of Kubernetes Deployments, ReplicaSet, Replication Controller, or job. kubectl set image − It updates the image of a pod template.

What is the difference between kubectl run and create?

The key difference between kubectl apply and create is that apply creates Kubernetes objects through a declarative syntax, while the create command is imperative. The command set kubectl apply is used at a terminal's command-line window to create or modify Kubernetes resources defined in a manifest file.

What is the use of kubectl rollout and Run command?

kubectl rollout − It is capable of managing the rollout of deployment. kubectl run − Run command has the capability to run an image on the Kubernetes cluster. kubectl scale − It will scale the size of Kubernetes Deployments, ReplicaSet, Replication Controller, or job.

What is kubectl Exec in Kubernetes?

This allows to directly edit a resource which one can receive via the command line tool. kubectl exec − This helps to execute a command in the container. kubectl expose − This is used to expose the Kubernetes objects such as pod, replication controller, and service as a new Kubernetes service.

How to override dockerfile arguments in Kubernetes?

And if we specify --command, then according to Kubernetes documentation, DockerFile arguments (CMD) and command (EntryPoint) both will be overridden with command specified in the kubectl command, so it will look similar to above: So it would be same in the end. Show activity on this post.

Why does the kubectl--resource-version command fail?

If --resource-version is specified and does not match the current resource version on the server the command will fail. Use "kubectl api-resources" for a complete list of supported resources. $ kubectl annotate [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]


Video Answer


2 Answers

When working with containers in Kubernetes, you should be careful not to mix up Kubenetes command and Docker Cmd.

  • the command field in Kubernetes corresponds to the EntryPoint field in Docker
  • the args field in Kubernetes corresponds to the Cmd field in Docker

From Kubernets documentation:

When you override the default Entrypoint and Cmd, these rules apply:

  • If you do not supply command or args for a Container, the defaults defined in the Docker image are used.

  • If you supply a command but no args for a Container, only the supplied command is used. The default EntryPoint and the default Cmd defined in the Docker image are ignored.

  • If you supply only args for a Container, the default Entrypoint defined in the Docker image is run with the args that you supplied.

  • If you supply a command and args, the default Entrypoint and the default Cmd defined in the Docker image are ignored. Your command is run with your args.

like image 193
Chris H. Avatar answered Oct 18 '22 19:10

Chris H.


In this example, yes both are same. Lets say if an entry point (command) is set as sleep 1000 but if your args are set as sleep 3000 then container command is ignored and sleep 3000 is executed.

Args takes precedence over command, and overrides command values if args exists

like image 29
ARU Avatar answered Oct 18 '22 21:10

ARU