0
0
Kubernetesdevops~30 mins

kubectl apply vs create in Kubernetes - Hands-On Comparison

Choose your learning style9 modes available
Understanding kubectl apply vs create
📖 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:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
Kubernetes
Need a hint?

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
Need a 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
Need a 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
Need a hint?

Type exactly kubectl apply -f nginx-deployment.yaml and then kubectl get deployment nginx-deployment.