How to Create a Kubernetes Pod with a Specific Image, Label, and Restart Policy
You are working on a Kubernetes cluster where a web application requires a dedicated Pod.
Your task is to create a Pod named with the following configuration:
- Pod name:
webserver - Container name:
webserver-container - Image:
nginx:1.25.4 - Restart policy:
OnFailure - Label:
type=web-application
kubectl run with --dry-run=client and -o yaml to generate a basic Pod manifest if needed.pods, refer to the official Kubernetes documentation.Generate the Pod Manifest
Use kubectl run to generate the basic Pod YAML:
$ kubectl run webserver \
--image=nginx:1.25.4 \
--restart=OnFailure \
--labels=type=web-application \
--dry-run=client -o yaml > webserver.yamlThe --dry-run=client option generates the manifest without creating the Pod.
The -o yaml option outputs the Pod configuration in YAML format.
Edit the Container Name
Open webserver.yaml in a text editor and verify that it contains the required image, label, and restart policy. Update the container name to webserver-container.
The final manifest should look like this:
The generated YAML may contain additional fields such as creationTimestamp: null, resources: {}, dnsPolicy, or status: {}. These are not required for this task and can be removed to keep the manifest cleaner.
Apply the Manifest
Deploy the Pod:
$ kubectl apply -f webserver.yaml
pod/webserver createdVerify the Pod Status
Check the Pod:
$ kubectl get pod webserver
NAME READY STATUS RESTARTS AGE
webserver 1/1 Running 0 70sThe important value is STATUS: Running. Since the Pod uses the nginx:1.25.4 image and the nginx process continues running, the expected Pod status is Running.
Important: Do not expectCompletedfor this task.Completedis normally associated with a container that has finished its command successfully. The nginx container continues running, so the expected status here isRunning.
Verify the Pod Label
Check the label with the following command:
$ kubectl get pod webserver --show-labels
NAME READY STATUS RESTARTS AGE LABELS
webserver 1/1 Running 0 5m55s type=web-applicationYou should see:
type=web-applicationYou can also use the label selector:
kubectl get pods -l type=web-applicationThe webserver Pod should be listed.
Verify the Container Image
Execute the command below:
kubectl get pod webserver -o jsonpath='{.spec.containers[0].image}'The expected output is nginx:1.25.4, confirming that the Pod is using the required image.
Verify the Restart Policy
Execute the command below:
kubectl get pod webserver -o jsonpath='{.spec.restartPolicy}'Run the command to verify the Pod's restart policy. The expected output is OnFailure, confirming that the Pod is configured with the required restart policy.
CKA Exam Tips
These tips will help you quickly identify key Kubernetes concepts and avoid common mistakes during the CKA exam and hands-on practice
Tip 1: Use kubectl run to Generate Pod YAML
Instead of writing a Pod manifest from scratch, you can quickly generate one:
kubectl run webserver --image=nginx:1.25.4 --dry-run=client -o yaml
This can save time during the CKA exam.
Tip 2: Know the --dry-run=client Option
This command:
kubectl run webserver --image=nginx:1.25.4 --dry-run=client -o yaml
generates the YAML without creating the Pod.
You can redirect the output to a file:
kubectl run webserver \
--image=nginx:1.25.4 \
--dry-run=client -o yaml > webserver.yaml
Then edit the manifest before applying it.
Tip 3: Know the Restart Policies
Kubernetes Pods support these restart policies:
Always
OnFailure
Never
Tip 4: Use jsonpath for Specific Values
When you need to quickly verify a particular field, jsonpath is useful.
For the image:
kubectl get pod webserver -o jsonpath='{.spec.containers[0].image}'
For the restart policy:
kubectl get pod webserver -o jsonpath='{.spec.restartPolicy}'