Kubernetes Cheat Sheet: kubectl Commands Explained with Real-World Examples

Kubernetes commands are easy to copy from the internet.

Knowing which command to use during an incident, deployment, or debugging session is what actually matters in real environments.

This post explains the most commonly used kubectl commands, grouped by use case, with real-world DevOps examples — not theory.

Reference: kubectl quick reference (official docs)


Cluster & Context Management

Check current cluster and context

kubectl config current-context

Real-world use case:
You manage multiple clusters (dev, staging, prod). Before running any command, you verify you’re not on production by mistake.


List all available contexts

kubectl config get-contexts

Why it matters:
Prevents accidental deletes or restarts in the wrong environment.


Switch context

kubectl config use-context prod-cluster

Production tip:
Always run this explicitly, never assume.


Resource Discovery (What’s Running?)

List nodes

kubectl get nodes

Real-world use case:
Check node readiness during:

  • Node upgrades
  • Spot instance termination
  • Cluster scale issues

List pods

kubectl get pods

Common variations:

kubectl get pods -n kube-system
kubectl get pods -o wide

Real-world use case:
You immediately see:

  • CrashLoopBackOff pods
  • Pending pods due to resource issues
  • Node where the pod is running

Get all resources in a namespace

kubectl get all -n my-namespace

Why this is powerful:
Gives a quick mental map of what exists: pods, services, deployments, replicasets.


Describing & Inspecting Resources (Deep Debugging)

Describe a pod

kubectl describe pod my-pod

This is one of the MOST important commands.

Real-world use case:
When a pod:

  • Is stuck in Pending
  • Keeps restarting
  • Fails to mount volumes
  • Can’t pull image

Events at the bottom usually tell the real story.


Describe deployment

kubectl describe deployment my-app

Use this to check:

  • Rolling update status
  • Replica availability
  • Image versions deployed

Logs & Runtime Debugging

View pod logs

kubectl logs my-pod

For multi-container pods:

kubectl logs my-pod -c app-container

Real-world use case:
First place to look during:

  • Application crashes
  • Startup failures
  • Runtime exceptions

Stream logs (tail -f equivalent)

kubectl logs -f my-pod

Production tip:
Avoid tailing logs on production for long durations — use centralized logging.

Exec into a container

kubectl exec -it my-pod -- /bin/sh

or

kubectl exec -it my-pod -- /bin/bash

Real-world use case:

  • Validate config files
  • Check environment variables
  • Run quick curl or DNS checks

⚠️ Use sparingly in production.


Deployments & Rollouts (Day-to-Day DevOps)

Apply configuration

kubectl apply -f deployment.yaml

Why apply?
It supports declarative management and diff-based updates.


Check rollout status

kubectl rollout status deployment my-app

Real-world use case:
Used in CI/CD pipelines to:

  • Block pipeline until deployment is healthy
  • Fail fast if rollout stalls

Rollback deployment

kubectl rollout undo deployment my-app

Important truth:
App rollbacks are easy.
Database rollbacks are not.

Always ensure DB changes are backward compatible.


Scaling & Resource Control

Scale deployment

kubectl scale deployment my-app --replicas=5

Real-world use case:
Temporary scale-up during:

  • Traffic spikes
  • Marketing campaigns
  • Incident mitigation

Check resource usage

kubectl top pods
kubectl top nodes

Used for:

  • Capacity planning
  • Identifying noisy neighbors
  • Debugging CPU/memory throttling

Namespace Management

List namespaces

kubectl get ns

Set default namespace

kubectl config set-context --current --namespace=my-namespace

Why this matters:
Avoid typing -n every time and reduce mistakes.


ConfigMaps & Secrets

View ConfigMaps

kubectl get configmap

Describe a ConfigMap

kubectl describe configmap app-config

Real-world use case:
Validate runtime configs without redeploying images.


Secrets (base64 encoded)

kubectl get secret my-secret -o yaml

⚠️ Never commit decoded secrets.


Troubleshooting & Cleanup

Delete a pod

kubectl delete pod my-pod

Reality:
Used more often than people admit.

Kubernetes will recreate the pod if managed by a controller.


Force delete (last resort)

kubectl delete pod my-pod --grace-period=0 --force

Use only when:

  • Pod is stuck terminating
  • Node is gone

Useful kubectl Shortcuts & Tips

Use aliases

alias k=kubectl

Dry run before applying

kubectl apply -f deployment.yaml --dry-run=client

Output formats

kubectl get pods -o yaml
kubectl get pods -o json

Great for scripting and automation.


Final Thoughts

kubectl is not about memorizing commands.

It’s about:

  • Knowing where to look first
  • Understanding what’s safe in production
  • Debugging under pressure

If you master these commands with context, Kubernetes becomes predictable — not scary.

🤞 Don’t miss the posts!

We don’t spam! Read more in our privacy policy

🤞 Don’t miss the posts!

We don’t spam! Read more in our privacy policy

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top