Bird
Raised Fist0
Kubernetesdevops~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between kubectl create and kubectl apply?
easy
A. kubectl create only creates new resources and fails if they exist; kubectl apply creates or updates resources.
B. kubectl create updates existing resources; kubectl apply deletes resources.
C. kubectl create deletes resources; kubectl apply only creates new resources.
D. kubectl create and kubectl apply do the same thing.

Solution

  1. Step 1: Understand kubectl create behavior

    kubectl create makes new resources but will fail if the resource already exists.
  2. Step 2: Understand kubectl apply behavior

    kubectl apply can create new resources or update existing ones safely without failing.
  3. Final Answer:

    kubectl create only creates new resources and fails if they exist; kubectl apply creates or updates resources. -> Option A
  4. Quick Check:

    create = new only, apply = create or update [OK]
Hint: Create fails if exists; apply updates or creates [OK]
Common Mistakes:
  • Thinking create updates existing resources
  • Believing apply deletes resources
  • Assuming both commands behave identically
2. Which of the following is the correct syntax to create a deployment using kubectl create?
easy
A. kubectl create deployment myapp --image=nginx
B. kubectl create pod myapp --image=nginx
C. kubectl apply deployment myapp --image=nginx
D. kubectl apply -f deployment.yaml

Solution

  1. Step 1: Identify kubectl create syntax for deployment

    The command kubectl create deployment myapp --image=nginx creates a deployment named myapp with the nginx image.
  2. Step 2: Differentiate from kubectl apply syntax

    kubectl apply uses the -f flag with a file, not direct deployment creation.
  3. Final Answer:

    kubectl create deployment myapp --image=nginx -> Option A
  4. Quick Check:

    create deployment uses direct command, apply uses files [OK]
Hint: Use 'create deployment' with image flag, apply uses files [OK]
Common Mistakes:
  • Using apply syntax with create command
  • Mixing file flag with create deployment
  • Confusing create and apply commands
3. Given the commands below, what will happen if the deployment myapp already exists?

kubectl create deployment myapp --image=nginx
kubectl apply -f deployment.yaml
medium
A. Both commands will fail because the deployment exists.
B. Both commands will update the deployment.
C. The create command will update; the apply command will fail.
D. The create command will fail; the apply command will update the deployment.

Solution

  1. Step 1: Behavior of kubectl create on existing resource

    kubectl create fails if the resource already exists, so the first command will fail.
  2. Step 2: Behavior of kubectl apply on existing resource

    kubectl apply updates the existing resource safely, so the second command updates the deployment.
  3. Final Answer:

    The create command will fail; the apply command will update the deployment. -> Option D
  4. Quick Check:

    create fails if exists; apply updates [OK]
Hint: Create fails if exists; apply updates existing resources [OK]
Common Mistakes:
  • Assuming create updates existing resources
  • Thinking apply fails if resource exists
  • Believing both commands behave the same
4. You ran kubectl create -f service.yaml but got an error saying the service already exists. What should you do to update the service instead?
medium
A. Use kubectl replace -f service.yaml to update the service.
B. Delete the service first, then run kubectl create -f service.yaml again.
C. Use kubectl apply -f service.yaml to update the existing service.
D. Run kubectl create -f service.yaml with a different name.

Solution

  1. Step 1: Understand the error cause

    kubectl create fails if the resource exists, so the error means the service is already present.
  2. Step 2: Choose the correct update method

    kubectl apply safely updates existing resources without deleting them, so use kubectl apply -f service.yaml.
  3. Final Answer:

    Use kubectl apply -f service.yaml to update the existing service. -> Option C
  4. Quick Check:

    apply updates existing resources safely [OK]
Hint: Use apply to update existing resources, not create [OK]
Common Mistakes:
  • Trying to create again without deleting
  • Deleting resources unnecessarily
  • Using replace without understanding its effect
5. You want to manage a deployment over time, updating its image and replicas safely without deleting it. Which command should you use and why?
hard
A. Use kubectl create deployment myapp --image=nginx repeatedly to update the deployment.
B. Use kubectl apply -f deployment.yaml because it creates or updates resources safely.
C. Use kubectl delete deployment myapp then kubectl create -f deployment.yaml to update.
D. Use kubectl replace -f deployment.yaml because it creates new resources.

Solution

  1. Step 1: Identify the need for safe updates over time

    Managing a deployment means updating it multiple times without deleting or failing.
  2. Step 2: Choose the command that supports create or update safely

    kubectl apply can create or update resources safely, preserving existing settings and avoiding errors.
  3. Step 3: Evaluate other options

    kubectl create fails if resource exists; deleting is disruptive; replace overwrites but is less safe than apply.
  4. Final Answer:

    Use kubectl apply -f deployment.yaml because it creates or updates resources safely. -> Option B
  5. Quick Check:

    Manage resources over time with apply [OK]
Hint: Apply manages updates safely; create is one-time only [OK]
Common Mistakes:
  • Using create repeatedly causing errors
  • Deleting resources unnecessarily
  • Confusing replace with apply
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