Bird
Raised Fist0
Kubernetesdevops~10 mins

Creating Pods with kubectl in Kubernetes - Visual Walkthrough

Choose your learning style10 modes available
Process Flow - Creating Pods with kubectl
Write Pod YAML file
↓
Run kubectl apply -f pod.yaml
↓
kubectl sends request to API Server
↓
API Server validates and stores Pod spec
↓
Scheduler assigns Pod to a Node
↓
Kubelet on Node creates container
↓
Pod status changes to Running
↓
kubectl get pods shows Pod status
This flow shows how writing a Pod definition, applying it with kubectl, and Kubernetes components work together to create and run a Pod.
Execution Sample
Kubernetes
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: nginx
This YAML defines a Pod named 'my-pod' running an nginx container.
Process Table
StepActionCommand/OperationResult/Output
1Write Pod YAML fileCreate pod.yaml with Pod specFile pod.yaml created with Pod definition
2Apply Pod YAMLkubectl apply -f pod.yamlPod 'my-pod' created in Kubernetes cluster
3API Server receives requestAPI Server validates Pod specPod object stored in etcd
4Scheduler assigns PodScheduler selects a NodePod assigned to Node 'node-1'
5Kubelet creates containerKubelet pulls nginx image and starts containerContainer running on Node
6Pod status updatePod status changes to Runningkubectl get pods shows STATUS=Running
7Verify Podkubectl get podsNAME READY STATUS RESTARTS AGE my-pod 1/1 Running 0 1m
💡 Pod is running successfully; kubectl get pods confirms the Pod status as Running.
Status Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
Pod ObjectNot createdCreated in clusterAssigned to Node 'node-1'Container runningPod Running
Pod StatusNonePendingPendingRunningRunning
Key Moments - 3 Insights
Why does the Pod status show 'Pending' before it becomes 'Running'?
After applying the Pod YAML (Step 2), the Pod object exists but the scheduler has not assigned it to a Node yet (Step 4). During this time, the status is 'Pending' as Kubernetes prepares to run it.
What does 'kubectl apply -f pod.yaml' actually do?
It sends the Pod definition to the Kubernetes API Server which validates and stores it (Step 3). This command does not create the container directly but triggers the cluster to create the Pod.
How do we know the Pod is successfully running?
The final 'kubectl get pods' command (Step 7) shows the Pod with STATUS=Running and READY=1/1, meaning the container inside the Pod is up and running.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the Pod get assigned to a Node?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Check the 'Action' column for 'Scheduler assigns Pod' in the execution table.
According to the variable tracker, what is the Pod status after Step 2?
ASucceeded
BRunning
CPending
DFailed
💡 Hint
Look at the 'Pod Status' row under 'After Step 2' in the variable tracker.
If the container image was incorrect, which step would most likely fail?
AStep 5 - Kubelet creates container
BStep 3 - API Server validation
CStep 2 - Apply Pod YAML
DStep 7 - kubectl get pods
💡 Hint
Refer to the 'Kubelet creates container' action in Step 5 where the image is pulled and container started.
Concept Snapshot
Creating Pods with kubectl:
- Write Pod YAML file defining apiVersion, kind, metadata, spec
- Run 'kubectl apply -f pod.yaml' to send Pod spec to cluster
- API Server validates and stores Pod object
- Scheduler assigns Pod to a Node
- Kubelet on Node pulls image and starts container
- Pod status changes from Pending to Running
- Use 'kubectl get pods' to check Pod status
Full Transcript
To create a Pod in Kubernetes, you first write a YAML file describing the Pod's details like its name and container image. Then you run 'kubectl apply -f pod.yaml' which sends this definition to the Kubernetes API Server. The API Server checks the Pod spec and stores it. Next, the Scheduler picks a Node to run the Pod. The Kubelet on that Node pulls the container image and starts the container. The Pod status changes from Pending to Running once the container is up. You can verify the Pod is running by using 'kubectl get pods' which shows the Pod's status and readiness.

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

  1. 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.
  2. Step 2: Effect of --restart=Never

    Adding --restart=Never tells Kubernetes to create a Pod directly, not a Deployment.
  3. Final Answer:

    Creates a single Pod named 'mypod' running the nginx image -> Option A
  4. 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

  1. Step 1: Identify the correct kubectl command for Pod creation

    The kubectl run command with --restart=Never creates a Pod directly.
  2. 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.
  3. Final Answer:

    kubectl run testpod --image=busybox --restart=Never -> Option A
  4. 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

  1. Step 1: Understand the effect of the run command with --restart=Never

    This command creates a Pod named 'demo' running the nginx image.
  2. 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'.
  3. Final Answer:

    A Pod named 'demo' will be listed with STATUS 'Running' or 'ContainerCreating' -> Option C
  4. 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

  1. Step 1: Understand default behavior of kubectl run without --restart=Never

    By default, kubectl run creates a Deployment, not a Pod.
  2. 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.
  3. Final Answer:

    The command created a Deployment, not a Pod named 'app', so no Pod named 'app' appears -> Option B
  4. 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

  1. Step 1: Understand restart policies in kubectl run

    Using --restart=Never creates a Pod with restart policy 'Never', so it won't restart automatically.
  2. 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.
  3. Final Answer:

    kubectl run webserver --image=nginx --restart=Never -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard