Kubernetes: Command and Arguments in Pod
The
command
andargs
fields in Kubernetes Pod YAML files specify the command to be run in the container and any additional arguments to be passed to the command, respectively. Thecommand
field corresponds to theENTRYPOINT
instruction in a Dockerfile, while theargs
field corresponds to theCMD
instruction. By specifying these fields, you can customize the command and arguments that are executed in the container, giving you flexibility to run any command in the container.
command and args in Pod YAML
To specify the command and arguments that should be executed within a Pod, you can define them in the Pod’s YAML file using the command
and args
fields.
The command
field is used to specify the command to be run in the container. The command can be specified as an array of strings, where each string in the array represents a part of the command.
The args
field is used to specify any additional arguments to be passed to the command specified in the command
field. The arguments can also be specified as an array of strings.
pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
command: ["echo", "Hello"]
args: ["world"]
In this example, the command
field specifies that the command to be run in the container should be echo "Hello"
. The args
field specifies that the argument to be passed to the command is world
.
So, when this Pod is started, it will run the command echo "Hello" world
, which will output Hello world
in the container.
You can also specify command
and args
using multiple lines :
command:
- "echo"
- "Hello"
args:
- "world"
Dockerfile vs. Pod YAML
ENTRYPOINT
and CMD
instruction in a Dockerfile corresponds to command
and args
field in a Pod YAML file, respectively.
ENTRYPOINT(Docker) vs. command(Pod YAML)
In a Dockerfile, the ENTRYPOINT
instruction specifies the command that will be run when a container starts.
In a Kubernetes Pod YAML file, the command
field specifies the command to be run in the container.
CMD(Docker) vs. args(Pod YAML)
In a Dockerfile, the CMD
instruction specifies the default arguments that will be passed to the ENTRYPOINT
command if no arguments are specified when the container is started.
In a Kubernetes Pod YAML file, the args
field specifies the arguments to be passed to the command specified in the command
field.
Example
Dockerfile
FROM alpine
ENTRYPOINT ["echo", "Hello"]
CMD ["world"]
pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
command: ["echo", "Hello"]
args: ["world"]
In this example, the ENTRYPOINT
instruction in the Dockerfile sets the command to be echo "Hello"
, while the CMD
instruction specifies that the default argument should be world
.
In the Pod YAML file, the command
field specifies the command to be echo "Hello"
, while the args
field specifies that the argument should be world
. When this Pod is started, it will run the command echo "Hello" world
, which will output Hello world
in the container.
These are my personal notes for CKA exam preparation on Kubernetes. Please feel free to correct me if you notice any errors. 😊
Related Story
Reference: