What if you could update your apps without fear of breaking them every time?
kubectl apply vs create in Kubernetes - When to Use Which
Imagine you have a list of instructions to set up a garden. You plant seeds one by one, but if you want to change the layout later, you have to dig everything up and start over.
Manually creating resources with kubectl create means you must remember exactly what you created. If you want to update something, you often have to delete and recreate it, which is slow and risky. Mistakes can cause downtime or lost data.
kubectl apply lets you tell Kubernetes the desired state of your resources. It compares what exists with what you want and makes only the needed changes. This saves time, reduces errors, and keeps your system stable.
kubectl create -f deployment.yaml
kubectl apply -f deployment.yaml
You can safely update and manage your Kubernetes resources repeatedly without fear of breaking things.
A developer updates a web app's configuration. Using kubectl apply, they push changes smoothly without downtime, unlike recreating the app which would cause interruptions.
kubectl create is for one-time resource creation.
kubectl apply manages resources by applying changes safely.
Using apply helps keep your system stable and easy to update.
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
