📖 Scenario: You are managing a Kubernetes cluster. You want to deploy an application using YAML files. You need to understand the difference between kubectl create and kubectl apply commands to manage your resources effectively.
🎯 Goal: Learn how to create a Kubernetes deployment using kubectl create and then update it using kubectl apply. See how these commands behave differently when managing resource changes.
📋 What You'll Learn
Create a Kubernetes deployment YAML file named nginx-deployment.yaml with specified content
Use kubectl create to create the deployment from the YAML file
Modify the YAML file to change the number of replicas
Use kubectl apply to update the deployment with the new replica count
Observe the output of both commands
💡 Why This Matters
🌍 Real World
Kubernetes is widely used to deploy and manage containerized applications. Knowing how to create and update resources with kubectl commands is essential for daily cluster operations.
💼 Career
DevOps engineers and site reliability engineers use these commands to manage application deployments, updates, and rollbacks in production environments.
Progress0 / 4 steps
1
Create the initial deployment YAML file
Create a file named nginx-deployment.yaml with the exact content below:
Use a text editor to create nginx-deployment.yaml with the exact YAML content.
2
Create the deployment using kubectl create
Run the command kubectl create -f nginx-deployment.yaml to create the deployment in your Kubernetes cluster.
Kubernetes
Hint
Type exactly kubectl create -f nginx-deployment.yaml to create the deployment.
3
Modify the deployment YAML to change replicas
Edit the nginx-deployment.yaml file to change the replicas value from 2 to 3.
Kubernetes
Hint
Open the YAML file and change the line replicas: 2 to replicas: 3.
4
Update the deployment using kubectl apply
Run the command kubectl apply -f nginx-deployment.yaml to update the deployment with the new replica count. Then run kubectl get deployment nginx-deployment to see the updated replicas.
Kubernetes
Hint
Type exactly kubectl apply -f nginx-deployment.yaml and then kubectl get deployment nginx-deployment.
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
Step 1: Understand kubectl create behavior
kubectl create makes new resources but will fail if the resource already exists.
Step 2: Understand kubectl apply behavior
kubectl apply can create new resources or update existing ones safely without failing.
Final Answer:
kubectl create only creates new resources and fails if they exist; kubectl apply creates or updates resources. -> Option A
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
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.
Step 2: Differentiate from kubectl apply syntax
kubectl apply uses the -f flag with a file, not direct deployment creation.
Final Answer:
kubectl create deployment myapp --image=nginx -> Option A
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?