0
0
Kubernetesdevops~10 mins

kubectl apply vs create in Kubernetes - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Process Flow - kubectl apply vs create
Write YAML file
kubectl create
Create resource if not exists
Error if exists
kubectl apply
Create resource if not exists
Update resource if exists
This flow shows how 'kubectl create' only makes new resources and errors if they exist, while 'kubectl apply' creates or updates resources based on YAML files.
Execution Sample
Kubernetes
kubectl create -f pod.yaml
kubectl apply -f pod.yaml
Creates a pod resource with 'create', then updates or creates it with 'apply'.
Process Table
StepCommandResource Exists?Action TakenOutput/Result
1kubectl create -f pod.yamlNoCreate resourcepod/my-pod created
2kubectl create -f pod.yamlYesErrorError from server (AlreadyExists): pods "my-pod" already exists
3kubectl apply -f pod.yamlNoCreate resourcepod/my-pod created
4kubectl apply -f pod.yamlYesUpdate resourcepod/my-pod configured
💡 kubectl create errors if resource exists; kubectl apply creates or updates resource.
Status Tracker
Resource StateStartAfter Step 1After Step 2After Step 3After Step 4
pod/my-podDoes not existCreatedNo change (error)CreatedUpdated
Key Moments - 2 Insights
Why does 'kubectl create' fail on the second attempt?
'kubectl create' fails because the resource already exists, as shown in step 2 of the execution table where it returns an error.
How does 'kubectl apply' handle existing resources differently?
'kubectl apply' updates the existing resource instead of erroring, as seen in step 4 where it says 'pod/my-pod configured'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens when you run 'kubectl create' on an existing resource?
AIt updates the resource
BIt deletes the resource
CIt returns an error
DIt ignores the command
💡 Hint
Check step 2 in the execution table where 'kubectl create' is run on an existing resource.
At which step does 'kubectl apply' update an existing resource?
AStep 4
BStep 2
CStep 1
DStep 3
💡 Hint
Look at the 'Action Taken' column for 'kubectl apply' in the execution table.
If the resource does not exist, what do both 'kubectl create' and 'kubectl apply' do?
ABoth return an error
BBoth create the resource
C'create' errors, 'apply' creates
D'apply' errors, 'create' creates
💡 Hint
Check steps 1 and 3 in the execution table for commands run when resource does not exist.
Concept Snapshot
kubectl create -f file.yaml
  - Creates resource only if it does not exist
  - Errors if resource exists

kubectl apply -f file.yaml
  - Creates resource if missing
  - Updates resource if exists

Use 'apply' for managing config changes safely.
Full Transcript
This visual shows the difference between 'kubectl create' and 'kubectl apply'. 'kubectl create' makes a resource only if it does not exist and errors if it does. 'kubectl apply' creates the resource if missing or updates it if it exists. The execution table traces commands on a pod resource, showing creation, error on duplicate create, and update with apply. Variables track the pod's existence state. Key moments clarify why create errors on duplicates and how apply updates. The quiz tests understanding of these behaviors. Remember, use 'kubectl apply' to safely manage resource changes.