How to Run Multiple Commands in a Kubernetes Pod Using BusyBox
The DevOps team is testing a Kubernetes workload that requires a container to perform a series of initialization commands and then remain running for further inspection.
A Pod needs to be configured so that its container executes multiple shell commands in sequence.
Your task is to create a Pod named env-pod using the busybox image.
Configure the Pod so that the container:
- Prints
Hello, Kubernetes world. - Executes
printenvto display the container's environment variables. - Continues running after the commands complete.
- Uses a restart policy of
Never.
Once the Pod has been created, verify that it reaches the Running state and inspect the container logs. Save the Pod logs to /tmp/env.log
Finally, verify that the saved log file contains the expected command output.
Generate the Pod Manifest
Use kubectl run to generate a basic Pod manifest:
$ kubectl run env-pod \
--image=busybox \
--restart=Never \
--dry-run=client \
-o yaml > env-pod.yamlThe --dry-run=client option generates the YAML without creating the Pod.
The -o yaml option outputs the Pod definition in YAML format.
Configure the Command and Arguments
Open env-pod.yaml and configure the container to use /bin/sh -c as its command. Provide the commands that should be executed through the args field.
The final manifest should look like:
Understanding command and args
This is an important CKA concept. The command starts the shell using /bin/sh -c, while the args provide the commands that the shell executes. In this case, the container prints Hello, Kubernetes world, displays the environment variables with printenv, and then runs sleep infinity to keep the container running.
Conceptually, Kubernetes executes:
/bin/sh -c 'echo "Hello, Kubernetes world"; printenv; sleep infinity'
Apply the Pod
Deploy the Pod using the manifest:
$ kubectl apply -f env-pod.yaml
pod/env-pod createdVerify the Pod Status
Check the Pod:
$ kubectl get pod env-pod
NAME READY STATUS RESTARTS AGE
env-pod 1/1 Running 0 3The Pod should show READY: 1/1 and STATUS: Running, confirming that the container is running successfully.
Save the Logs to /tmp/env.log
Redirect the env-pod logs to the required file. This creates /tmp/env.log and stores the container's output, including the echo message and environment variables.
kubectl logs env-pod > /tmp/env.logVerify the Log File
Check that /tmp/env.log was created successfully, then display its contents. You should see Hello, Kubernetes world followed by the environment variables printed by printenv.
$ cat /tmp/env.log
Hello, Kubernetes worldVerify the Container Is Still Running
Because the final command is sleep infinity, the container continues running after executing the previous commands. Verify the Pod status using kubectl get pod env-pod.
$ kubectl get pod env-pod
NAME READY STATUS RESTARTS AGE
env-pod 1/1 Running 0 10The expected result is READY 1/1 and STATUS Running, confirming that the container is still running successfully.
Why Does printenv Appear in the Logs?
The printenv command writes the container's environment variables to standard output. Kubernetes captures the container's standard output and standard error, which allows kubectl logs env-pod to display the output generated by the container.
In this example, echo prints the message, printenv displays the environment variables, and sleep infinity keeps the container running. The output can then be redirected from kubectl logs to /tmp/env.log.
Why Use sleep infinity?
Commands such as echo and printenv finish immediately. If they were the only commands, the container would exit after completing them.
The final command, sleep infinity, keeps running indefinitely, allowing the container and Pod to remain in the Running state.
CKA Exam Tips
These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.
Tip 1: command Overrides the Image Entrypoint
The command field overrides the container image's default entrypoint. In this scenario, command: ["/bin/sh", "-c"] tells the container to start the shell and execute the commands provided through the args field.
Tip 2: args Provides Arguments to the Command
The commands echo "Hello, Kubernetes world", printenv, and sleep infinity are provided through the args field. These commands are then executed by the shell specified in the command field.
Tip 3: Use | for Multiline Commands
Using | in the args field allows multiple shell commands to be written cleanly across multiple lines. The shell executes each command in sequence.
args:
- |
echo "Hello, Kubernetes world";
printenv;
sleep infinity;