Creating Pods with kubectl in Kubernetes - Performance & Efficiency
When creating pods using kubectl, it is important to understand how the time to complete the operation changes as you create more pods.
We want to know how the work grows when we ask Kubernetes to create many pods at once.
Analyze the time complexity of the following kubectl commands.
kubectl create -f pod1.yaml
kubectl create -f pod2.yaml
kubectl create -f pod3.yaml
...
kubectl create -f podN.yaml
This code creates pods one by one by applying separate YAML files for each pod.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sending a create request to the Kubernetes API for each pod.
- How many times: Once per pod, repeated N times for N pods.
Each pod creation requires a separate request, so the total time grows as you add more pods.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 create requests |
| 100 | 100 create requests |
| 1000 | 1000 create requests |
Pattern observation: The work grows directly in proportion to the number of pods you create.
Time Complexity: O(n)
This means the time to create pods grows linearly as you add more pods.
[X] Wrong: "Creating multiple pods with separate commands happens instantly regardless of number."
[OK] Correct: Each pod creation sends a separate request and takes time, so more pods mean more total time.
Understanding how operations scale with input size helps you explain system behavior clearly and shows you can think about efficiency in real-world tasks.
"What if we created all pods using a single YAML file with multiple pod definitions? How would the time complexity change?"
Practice
kubectl run mypod --image=nginx --restart=Never do?Solution
Step 1: Understand the kubectl run command
The commandkubectl runcreates a resource to run containers. By default, it creates a Deployment unless specified otherwise.Step 2: Effect of --restart=Never
Adding--restart=Nevertells Kubernetes to create a Pod directly, not a Deployment.Final Answer:
Creates a single Pod named 'mypod' running the nginx image -> Option AQuick Check:
kubectl run + --restart=Never = Pod creation [OK]
- Assuming kubectl run always creates a Deployment
- Forgetting --restart=Never creates a Pod
- Confusing Pod creation with deletion
Solution
Step 1: Identify the correct kubectl command for Pod creation
Thekubectl runcommand with--restart=Nevercreates a Pod directly.Step 2: Check each option's syntax
kubectl create pod testpod --image=busybox useskubectl create podwhich is invalid. kubectl deploy testpod --image=busybox useskubectl deploywhich is not a kubectl command. kubectl pod create testpod --image=busybox useskubectl pod createwhich is invalid. Only kubectl run testpod --image=busybox --restart=Never is correct syntax.Final Answer:
kubectl run testpod --image=busybox --restart=Never -> Option AQuick Check:
kubectl run + --restart=Never = Pod creation [OK]
- Using 'kubectl create pod' which is invalid
- Confusing 'deploy' with 'run'
- Omitting --restart=Never to create a Pod
kubectl get pods immediately after running kubectl run demo --image=nginx --restart=Never?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 CQuick Check:
kubectl run + --restart=Never creates Pod listed by get pods [OK]
- Expecting a Deployment to appear in pod list
- Thinking no pods appear immediately
- Confusing command output with error
kubectl run app --image=nginx but no Pod named 'app' appears when you run kubectl get pods. What is the likely cause?Solution
Step 1: Understand default behavior of kubectl run without --restart=Never
By default,kubectl runcreates 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 commandkubectl get deploymentswould show the Deployment.Final Answer:
The command created a Deployment, not a Pod named 'app', so no Pod named 'app' appears -> Option BQuick Check:
kubectl run without --restart=Never creates Deployment [OK]
- Assuming kubectl run always creates a Pod
- Thinking image name caused failure without error
- Confusing --restart=Always with Pod creation
Solution
Step 1: Understand restart policies in kubectl run
Using--restart=Nevercreates 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=Alwayswhich 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 DQuick Check:
--restart=Never means Pod won't auto-restart [OK]
- Using default restart which creates Deployment
- Confusing --restart=Always with no restart
- Trying invalid commands like kubectl create pod
Ready to prove it?
Pass the test and this pattern moves unknown
