📖 Scenario: You are working as a DevOps engineer managing containerized applications. You need to create a simple Pod in Kubernetes to run a basic web server container.
🎯 Goal: Learn how to create a Kubernetes Pod using kubectl commands and YAML configuration files step-by-step.
📋 What You'll Learn
Create a YAML file defining a Pod named web-pod running the nginx container
Add a label app: web to the Pod
Use kubectl to create the Pod from the YAML file
Verify the Pod is running using kubectl get pods
💡 Why This Matters
🌍 Real World
Creating Pods is a fundamental task in deploying containerized applications on Kubernetes clusters.
💼 Career
DevOps engineers and site reliability engineers often create and manage Pods to run application workloads reliably.
Progress0 / 4 steps
1
Create the Pod YAML file
Create a file named web-pod.yaml with the following content exactly: a Pod named web-pod running the nginx container and labeled with app: web.
Kubernetes
Hint
Use apiVersion: v1, kind: Pod, and define metadata with name and labels. Under spec, add one container named nginx with image nginx.
2
Create the Pod using kubectl
Run the kubectl create -f web-pod.yaml command to create the Pod from the YAML file you made.
Kubernetes
Hint
Use the kubectl create -f command followed by your YAML file name to create the Pod.
3
Check the Pod status
Use the command kubectl get pods to list all Pods and check that web-pod is created and running.
Kubernetes
Hint
Simply run kubectl get pods to see the list of Pods and their status.
4
Verify the Pod is running
Look at the output of kubectl get pods and confirm that the Pod named web-pod has the status Running. Write a print statement that outputs exactly: web-pod is Running if the Pod is running.
Kubernetes
Hint
Use print("web-pod is Running") to show the Pod status confirmation.
Practice
(1/5)
1. What does the command kubectl run mypod --image=nginx --restart=Never do?
easy
A. Creates a single Pod named 'mypod' running the nginx image
B. Creates a Deployment named 'mypod' running the nginx image
C. Deletes the Pod named 'mypod' if it exists
D. Updates the nginx image in all Pods
Solution
Step 1: Understand the kubectl run command
The command kubectl run creates a resource to run containers. By default, it creates a Deployment unless specified otherwise.
Step 2: Effect of --restart=Never
Adding --restart=Never tells Kubernetes to create a Pod directly, not a Deployment.
Final Answer:
Creates a single Pod named 'mypod' running the nginx image -> Option A
Quick Check:
kubectl run + --restart=Never = Pod creation [OK]
Hint: Use --restart=Never to create a Pod, not a Deployment [OK]
Common Mistakes:
Assuming kubectl run always creates a Deployment
Forgetting --restart=Never creates a Pod
Confusing Pod creation with deletion
2. Which of the following is the correct syntax to create a Pod named 'testpod' using the busybox image with kubectl?
easy
A. kubectl run testpod --image=busybox --restart=Never
B. kubectl create pod testpod --image=busybox
C. kubectl deploy testpod --image=busybox
D. kubectl pod create testpod --image=busybox
Solution
Step 1: Identify the correct kubectl command for Pod creation
The kubectl run command with --restart=Never creates a Pod directly.
Step 2: Check each option's syntax
kubectl create pod testpod --image=busybox uses kubectl create pod which is invalid. kubectl deploy testpod --image=busybox uses kubectl deploy which is not a kubectl command. kubectl pod create testpod --image=busybox uses kubectl pod create which is invalid. Only kubectl run testpod --image=busybox --restart=Never is correct syntax.
Final Answer:
kubectl run testpod --image=busybox --restart=Never -> Option A
Quick Check:
kubectl run + --restart=Never = Pod creation [OK]
Hint: Use 'kubectl run name --image=img --restart=Never' for Pod creation [OK]
Common Mistakes:
Using 'kubectl create pod' which is invalid
Confusing 'deploy' with 'run'
Omitting --restart=Never to create a Pod
3. What will be the output of the command kubectl get pods immediately after running kubectl run demo --image=nginx --restart=Never?
medium
A. A Deployment named 'demo' will be listed instead of a Pod
B. No Pods will be listed because the command creates a Deployment
C. A Pod named 'demo' will be listed with STATUS 'Running' or 'ContainerCreating'
D. An error message saying 'unknown command run'
Solution
Step 1: Understand the effect of the run command with --restart=Never
This command creates a Pod named 'demo' running the nginx image.
Step 2: Check what kubectl get pods shows
It lists all Pods. Since the Pod was just created, it will appear with status 'Running' or 'ContainerCreating'.
Final Answer:
A Pod named 'demo' will be listed with STATUS 'Running' or 'ContainerCreating' -> Option C
Quick Check:
kubectl run + --restart=Never creates Pod listed by get pods [OK]
Hint: Pods created with --restart=Never appear in 'kubectl get pods' [OK]
Common Mistakes:
Expecting a Deployment to appear in pod list
Thinking no pods appear immediately
Confusing command output with error
4. You run kubectl run app --image=nginx but no Pod named 'app' appears when you run kubectl get pods. What is the likely cause?
medium
A. The image name 'nginx' is invalid, so Pod creation failed
B. The command created a Deployment, not a Pod named 'app', so no Pod named 'app' appears
C. You need to add --restart=Always to create a Pod
D. The Pod was created but immediately deleted by Kubernetes
Solution
Step 1: Understand default behavior of kubectl run without --restart=Never
By default, kubectl run creates a Deployment, not a Pod.
Step 2: Check why 'kubectl get pods' shows no Pod named 'app'
Since a Deployment manages Pods, the Pod(s) are named like 'app-[hash]', not 'app'. The command kubectl get deployments would show the Deployment.
Final Answer:
The command created a Deployment, not a Pod named 'app', so no Pod named 'app' appears -> Option B
Quick Check:
kubectl run without --restart=Never creates Deployment [OK]
Hint: Add --restart=Never to create a Pod, else Deployment is created [OK]
Common Mistakes:
Assuming kubectl run always creates a Pod
Thinking image name caused failure without error
Confusing --restart=Always with Pod creation
5. You want to create a Pod named 'webserver' running the nginx image that does NOT restart automatically if it crashes. Which command should you use?
hard
A. kubectl run webserver --image=nginx
B. kubectl run webserver --image=nginx --restart=Always
C. kubectl create pod webserver --image=nginx --restart=Never
D. kubectl run webserver --image=nginx --restart=Never
Solution
Step 1: Understand restart policies in kubectl run
Using --restart=Never creates a Pod with restart policy 'Never', so it won't restart automatically.
Step 2: Evaluate each option
kubectl run webserver --image=nginx --restart=Never correctly uses --restart=Never. kubectl run webserver --image=nginx --restart=Always uses --restart=Always which creates a Deployment that restarts Pods. kubectl create pod webserver --image=nginx --restart=Never uses invalid syntax. kubectl run webserver --image=nginx creates a Deployment by default.
Final Answer:
kubectl run webserver --image=nginx --restart=Never -> Option D
Quick Check:
--restart=Never means Pod won't auto-restart [OK]
Hint: Use --restart=Never to prevent Pod auto-restart on failure [OK]
Common Mistakes:
Using default restart which creates Deployment
Confusing --restart=Always with no restart
Trying invalid commands like kubectl create pod
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.