0
0
Kubernetesdevops~5 mins

Why kubectl mastery matters in Kubernetes - Why It Works

Choose your learning style9 modes available
Introduction
Managing applications on Kubernetes requires interacting with the cluster efficiently. Mastering kubectl, the command-line tool, helps you control and troubleshoot your apps quickly without confusion.
When you need to check the status of your application pods to ensure they are running.
When you want to update or change your app configuration without downtime.
When you need to debug errors by viewing logs or describing resources.
When you want to scale your app up or down based on demand.
When you need to clean up unused resources to save cluster space.
Commands
This command lists all pods in the current namespace so you can see which apps are running and their status.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-app-pod-1 1/1 Running 0 10m my-app-pod-2 1/1 Running 0 10m
-n - Specify namespace to list pods from
-o wide - Show extra details like node and IP
Shows detailed information about the pod, including events and container status, useful for troubleshooting.
Terminal
kubectl describe pod my-app-pod-1
Expected OutputExpected
Name: my-app-pod-1 Namespace: default Status: Running Containers: my-app: Container ID: docker://abc123 Image: my-app-image:latest State: Running Ready: True Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 12m default-scheduler Successfully assigned default/my-app-pod-1 to node-1
Fetches the logs from the pod's main container to help understand what the app is doing or why it might be failing.
Terminal
kubectl logs my-app-pod-1
Expected OutputExpected
Starting application... Application running on port 8080 Received request from 10.0.0.5
-f - Follow logs in real time
Changes the number of pod replicas for the deployment to handle more or less traffic.
Terminal
kubectl scale deployment my-app --replicas=3
Expected OutputExpected
deployment.apps/my-app scaled
Deletes a specific pod, useful for forcing a restart or cleaning up.
Terminal
kubectl delete pod my-app-pod-2
Expected OutputExpected
pod "my-app-pod-2" deleted
Key Concept

If you remember nothing else from this pattern, remember: kubectl is your direct control panel to manage and fix your Kubernetes apps quickly.

Common Mistakes
Not specifying the correct namespace when running kubectl commands.
You might see no resources or the wrong resources because kubectl defaults to the 'default' namespace.
Always use the -n flag with the right namespace or set the default namespace context.
Trying to delete or scale resources without checking their current status first.
You might accidentally remove or scale the wrong resource causing downtime.
Use kubectl get and describe commands to verify resource names and status before making changes.
Ignoring logs and events when troubleshooting pod issues.
You miss important clues about why your app is failing or behaving unexpectedly.
Always check logs with kubectl logs and events with kubectl describe to understand problems.
Summary
Use kubectl get pods to see running app containers and their status.
Use kubectl describe pod to get detailed info and events for troubleshooting.
Use kubectl logs to read app output and debug issues.
Use kubectl scale to adjust the number of app instances.
Use kubectl delete to remove unwanted or stuck pods.