📖 Scenario: You are a new DevOps engineer starting to manage applications on Kubernetes clusters. You need to understand how to use kubectl, the command-line tool for Kubernetes, to interact with your cluster resources effectively.
🎯 Goal: Learn the basics of kubectl commands by creating a simple pod, checking its status, and listing pods in the cluster. This will help you gain confidence in managing Kubernetes resources.
📋 What You'll Learn
Create a YAML file defining a simple pod named my-pod running the nginx container
Use kubectl to create the pod from the YAML file
Use kubectl to check the status of my-pod
Use kubectl to list all pods in the default namespace
💡 Why This Matters
🌍 Real World
In real work, DevOps engineers use <code>kubectl</code> daily to deploy, monitor, and troubleshoot applications running on Kubernetes clusters.
💼 Career
Mastering <code>kubectl</code> commands is essential for roles like Kubernetes administrator, DevOps engineer, and cloud engineer to manage containerized applications effectively.
Progress0 / 4 steps
1
Create a pod YAML file
Create a file named my-pod.yaml with the exact content below to define a pod named my-pod running the nginx container:
Make sure the file is named exactly my-pod.yaml and the indentation matches YAML format.
2
Create the pod using kubectl
Run the exact command kubectl apply -f my-pod.yaml to create the pod in your Kubernetes cluster.
Kubernetes
Hint
Use kubectl apply -f followed by the YAML file name to create resources.
3
Check the pod status
Run the exact command kubectl get pod my-pod to see the status of the pod named my-pod.
Kubernetes
Hint
Use kubectl get pod followed by the pod name to check its status.
4
List all pods in the default namespace
Run the exact command kubectl get pods to list all pods running in the default namespace.
Kubernetes
Hint
Use kubectl get pods to see all pods. The output should show my-pod with status Running.
Practice
(1/5)
1. Why is mastering kubectl important when working with Kubernetes?
easy
A. It is only used for installing Kubernetes.
B. It allows you to quickly view and manage your Kubernetes resources.
C. It replaces the need to understand Kubernetes concepts.
D. It automatically fixes all errors in your Kubernetes cluster.
Solution
Step 1: Understand the role of kubectl
kubectl is the main command-line tool to interact with Kubernetes clusters.
Step 2: Recognize its practical use
It helps you view, update, and manage your apps and resources quickly and efficiently.
Final Answer:
It allows you to quickly view and manage your Kubernetes resources. -> Option B
Quick Check:
kubectl = manage Kubernetes resources fast [OK]
Hint: kubectl is your hands-on tool to control Kubernetes [OK]
Common Mistakes:
Thinking kubectl fixes errors automatically
Believing kubectl replaces Kubernetes knowledge
Assuming kubectl is only for installation
2. Which of the following kubectl commands is correctly written to list all pods in the default namespace?
easy
A. kubectl get pods
B. kubectl list pods
C. kubectl show pods
D. kubectl pods get
Solution
Step 1: Recall the correct command syntax
The correct command to list pods is kubectl get pods.
Step 2: Identify incorrect options
Commands like list, show, or reversed order are invalid in kubectl.
Final Answer:
kubectl get pods -> Option A
Quick Check:
List pods = kubectl get pods [OK]
Hint: Use 'kubectl get' to list resources [OK]
Common Mistakes:
Using 'list' or 'show' instead of 'get'
Reversing command order
Omitting 'get' keyword
3. What will be the output of the command kubectl get pods myapp-pod -o wide if the pod exists?
medium
A. Only the pod name will be displayed.
B. An error saying pod not found.
C. Detailed information about the pod including node and IP address.
D. The pod will be deleted.
Solution
Step 1: Understand the command options
-o wide shows extra details like node and IP for the pod.
Step 2: Confirm pod existence effect
If the pod exists, kubectl displays its detailed info; no error or deletion occurs.
Final Answer:
Detailed information about the pod including node and IP address. -> Option C
Quick Check:
kubectl get pod -o wide = detailed pod info [OK]
Hint: Use '-o wide' for extra pod details [OK]
Common Mistakes:
Expecting deletion from get command
Thinking only pod name shows
Assuming error if pod exists
4. You run kubectl apply -f deployment.yaml but get an error: "error: unable to recognize \"deployment.yaml\": no matches for kind \"Deploymnt\"". What is the likely cause?
medium
A. kubectl is not installed.
B. The file path is incorrect.
C. The cluster is down.
D. The kind field in the YAML file is misspelled.
Solution
Step 1: Analyze the error message
The error says "no matches for kind \"Deploymnt\"", indicating a typo in the resource kind.
Step 2: Understand kubectl behavior
kubectl cannot recognize the misspelled kind, so it fails to apply the file.
Final Answer:
The kind field in the YAML file is misspelled. -> Option D
Quick Check:
Typo in kind causes apply error [OK]
Hint: Check spelling of 'kind' in YAML files [OK]
Common Mistakes:
Assuming file path error without checking content
Thinking kubectl installation causes this error
Blaming cluster status without error details
5. You want to update a running deployment named webapp to use a new image version webapp:v2 without downtime. Which kubectl command should you use?
hard
A. kubectl set image deployment/webapp webapp=webapp:v2