Whether you're wrangling microservices in production or just tired of Googling the same five kubectl commands, this blog is for you.
We will go beyond the copy & paste to give you real command-line hands-on examples and some lighter dives to understand why things work the way they do.
Let’s level up your K8s game. 🚀
Quick Refresher: What Kubernetes Is
Kubernetes is a container orchestration system that helps you manage applications across clusters of machines. It handles their scheduling, scaling, networking, and rollouts. You tell it what you want, and it figures out how to get there!
Before we jump into commands, it helps to know how kubectl is structured—it’ll make everything click faster, especially as you start scripting or working with multiple clusters - Please do read the other parts of this series to get the grasp of the underlying architecture.
Enough of theory!
Most kubectl commands follow this pattern:
kubectl [operation] [resource] [name] [flags]
For example:
kubectl get pods -n dev
Here’s what’s happening:
get is the operation
pods is the resource type
-n dev tells kubectl to only look in the dev namespace
Now, here's where flags come into play:
-n
-A (short for --all-namespaces) will show results across every namespace.
Compare these two:
kubectl get pods -n dev # Just pods in the 'dev' namespace
kubectl get pods -A # All pods in all namespaces
Why this matters: Many kubectl commands default to the current namespace (often default), so if you don’t specify -n or set your namespace context, you might think things are missing.
Bonus Tip: To avoid typing -n all the time, you can set your namespace context like this:
kubectl config set-context --current --namespace=dev
Now all kubectl commands will assume dev unless you override it.
Try out the kubectx and kubens tools after checking support to your platform - they make life much easier for context and namespace switching and many more!
Hey wait, Context??
Relax! We will get the context in few minutes!!
Cool? Cool. Now let’s hit the terminal.
Essentials
These are the bread-and-butter commands for interacting with your K8s cluster.
kubectl get all # Get pods, services, deployments, etc.
kubectl get pods # List all pods in the current namespace
kubectl describe pod # Detailed info about a pod
kubectl delete pod # Delete a pod
Now, keep playing with permutation and combination for other resource types with these operations!
Multi-Cluster Management
Working across multiple clusters?
Here's where you will need kubectl config commands the most. These commands help you manage different contexts, namespaces, and clusters seamlessly.
If you’re using Azure Kubernetes Service (AKS), you’ll need to configure your kubectl to authenticate and connect to the correct AKS cluster.
A Kubernetes context is like a shortcut or profile that tells kubectl where to send commands (your cluster) and how to authenticate (user creds).
Get a quick local setup for your projects -
az login # Login to Azure
az account set -s "" # Set subscription context
az aks get-credentials --name --resource-group # Get credentials for AKS
kubectl config
Commands
To manage multiple clusters, here are some useful commands:
# List all available clusters
kubectl config get-contexts
# Get the current AKS cluster connected with kubectl (e.g., dev/uat/prod)
kubectl config current-context
# Switch between clusters (dev/uat/prod)
kubectl config use-context
Deployments and Scaling
Managing deployments and scaling is where Kubernetes really shines. Use the following commands to control your apps in the cluster.
kubectl create deployment --image=nginx
kubectl expose deployment --port=80 --type=NodePort
kubectl scale deployment --replicas=3
kubectl rollout status deployment/
kubectl rollout undo deployment/
Logs, Exec, and Debugging
When things go wrong, you need to dig deep. Here are some useful ways to get the logs, interact with your containers, and troubleshoot effectively.
kubectl logs --tail # Shows specific number of lines of the log
kubectl logs | findstr # Shows all logs matching the search string
kubectl logs --timestamps=true # Logs of a specific pod with timestamps
kubectl logs --since=1h # Logs for a specific duration (1 hour here)
kubectl logs --follow # Continuously shows the logs (Ctrl + C to exit)
kubectl logs --previous # Logs for a previous instantiation of a container
kubectl logs > # Write logs to a file
kubectl logs -c # Logs from a specific container in a multi-container pod
kubectl logs -l app= # Logs from all pods having a common label
When debugging, --follow
for real-time monitoring, and --previous
helps track down issues after a pod has been restarted subject to error situation.
Getting Inside a Pod
Need to jump into a running pod? Here's the command:
kubectl exec -it cmd.exe
For Windows containers use cmd.exe
, otherwise use /bin/bash
or sh
for Linux containers based on your preference.
Metrics & Resource Monitoring
Stay on top of your cluster’s health with kubectl top to see resource usage!
kubectl top pod --containers
kubectl top pod --sort-by=cpu
kubectl top node
ConfigMaps & Secrets
Handle sensitive data with kubectl commands for ConfigMaps and Secrets:
kubectl create configmap my-config --from-literal=env=prod
kubectl get configmaps
kubectl describe configmap my-config
kubectl get secrets
kubectl describe secret my-secret
Final Thoughts
Kubernetes can feel like magic—until it breaks. Then it’s all about knowing the right commands, fast!
This cheat sheet is aimed to bridge you from just struggling to get it working by surfing to really understanding how the pieces fit together. The more you use these commands, the more comfortable and handier it becomes!
Got a favorite kubectl command or trick to share? Drop it in the comments...
Happy learning!