kubectl apply vs create in Kubernetes - Performance Comparison
We want to understand how the time to run kubectl apply and kubectl create changes as the number of Kubernetes resources grows.
How does the command's work grow when we manage more resources?
Analyze the time complexity of these commands managing multiple resources.
kubectl create -f resources.yaml
kubectl apply -f resources.yaml
kubectl create makes new resources, while kubectl apply creates or updates resources by checking their current state.
Look at what repeats when these commands run on many resources.
- Primary operation: For each resource,
kubectl createsends a creation request once;kubectl applyfirst fetches the current state, then sends create or update requests. - How many times: Once per resource for both commands, but
applydoes extra checks per resource.
As the number of resources grows, the work grows roughly in a straight line.
| Input Size (n) | Approx. Operations for create | Approx. Operations for apply |
|---|---|---|
| 10 | 10 create requests | 10 fetch + 10 create/update requests |
| 100 | 100 create requests | 100 fetch + 100 create/update requests |
| 1000 | 1000 create requests | 1000 fetch + 1000 create/update requests |
Pattern observation: Both commands scale linearly, but apply does about twice the work per resource.
Time Complexity: O(n)
This means the time grows directly with the number of resources you manage.
[X] Wrong: "kubectl apply is always faster than kubectl create because it updates only changed resources."
[OK] Correct: apply always checks the current state first, so it does extra work per resource, making it slower when many resources are involved.
Understanding how commands scale with resource count helps you design efficient deployment processes and shows you think about real-world system behavior.
What if we changed kubectl apply to use server-side apply? How would the time complexity change?
Practice
kubectl create and kubectl apply?Solution
Step 1: Understand
kubectl createbehaviorkubectl createmakes new resources but will fail if the resource already exists.Step 2: Understand
kubectl applybehaviorkubectl applycan create new resources or update existing ones safely without failing.Final Answer:
kubectl createonly creates new resources and fails if they exist;kubectl applycreates or updates resources. -> Option AQuick Check:
create= new only,apply= create or update [OK]
- Thinking create updates existing resources
- Believing apply deletes resources
- Assuming both commands behave identically
kubectl create?Solution
Step 1: Identify
The commandkubectl createsyntax for deploymentkubectl create deployment myapp --image=nginxcreates a deployment named myapp with the nginx image.Step 2: Differentiate from
kubectl applysyntaxkubectl applyuses the-fflag with a file, not direct deployment creation.Final Answer:
kubectl create deployment myapp --image=nginx -> Option AQuick Check:
create deploymentuses direct command,applyuses files [OK]
- Using apply syntax with create command
- Mixing file flag with create deployment
- Confusing create and apply commands
myapp already exists?kubectl create deployment myapp --image=nginxkubectl apply -f deployment.yamlSolution
Step 1: Behavior of
kubectl createon existing resourcekubectl createfails if the resource already exists, so the first command will fail.Step 2: Behavior of
kubectl applyon existing resourcekubectl applyupdates the existing resource safely, so the second command updates the deployment.Final Answer:
The create command will fail; the apply command will update the deployment. -> Option DQuick Check:
createfails if exists;applyupdates [OK]
- Assuming create updates existing resources
- Thinking apply fails if resource exists
- Believing both commands behave the same
kubectl create -f service.yaml but got an error saying the service already exists. What should you do to update the service instead?Solution
Step 1: Understand the error cause
kubectl createfails if the resource exists, so the error means the service is already present.Step 2: Choose the correct update method
kubectl applysafely updates existing resources without deleting them, so usekubectl apply -f service.yaml.Final Answer:
Usekubectl apply -f service.yamlto update the existing service. -> Option CQuick Check:
applyupdates existing resources safely [OK]
- Trying to create again without deleting
- Deleting resources unnecessarily
- Using replace without understanding its effect
Solution
Step 1: Identify the need for safe updates over time
Managing a deployment means updating it multiple times without deleting or failing.Step 2: Choose the command that supports create or update safely
kubectl applycan create or update resources safely, preserving existing settings and avoiding errors.Step 3: Evaluate other options
kubectl createfails if resource exists; deleting is disruptive; replace overwrites but is less safe than apply.Final Answer:
Usekubectl apply -f deployment.yamlbecause it creates or updates resources safely. -> Option BQuick Check:
Manage resources over time withapply[OK]
- Using create repeatedly causing errors
- Deleting resources unnecessarily
- Confusing replace with apply
Ready to prove it?
Pass the test and this pattern moves unknown
