Pod with Command and Args
Why use command and args?
A container image has a default command that runs when the container starts. Sometimes we want the container to run a different command, or run the same command with different arguments.
In a Pod manifest, command replaces the image's default command, and args supplies arguments to that command. If you set only args, Kubernetes uses the image's default command with those arguments.
Create a BusyBox Pod
BusyBox is a small image that includes common command-line tools. It is useful for simple examples and troubleshooting.
Imperative command
kubectl run busybox-pod --image=busybox:1.36 --command -- sh -c 'echo "Hello from busybox"; sleep 300'
Manifest Create a file named
2-pod-with-cmd.yamlwith this content:
apiVersion: v1
kind: Pod
metadata:
name: busybox-pod
spec:
containers:
- name: busybox-container
image: busybox:1.36
command: ["sh", "-c"]
args: ["echo 'Hello from BusyBox'; sleep 300"]
Here, command starts a shell, and args tells the shell to print a message and then wait for 5 minutes. The wait keeps the container running so that we can inspect it.
Create the Pod:
kubectl apply -f 2-pod-with-cmd.yaml
kubectl apply -f 2-pod-with-cmd.yaml pod/busybox-pod created
Check its status:
kubectl get pods
kubectl get pods NAME READY STATUS RESTARTS AGE busybox-pod 1/1 Running 0 10s
Check the message printed by the command:
kubectl logs busybox-pod
kubectl logs busybox-pod Hello from BusyBox