Complexity: [MEDIUM] - Essential commands to master
Time to Complete: 40-45 minutes
Prerequisites: Module 1 (First Cluster running)
Imagine a pager going off at 3 AM. Your company’s primary checkout service is down, and thousands of dollars are evaporating every minute. You have a terminal and one tool: kubectl. If you know exactly how to query the cluster, pull the logs, and find the failing pod, you are the hero. If you are fumbling through documentation trying to remember the flag for namespace filtering, the outage drags on. kubectl is not just a CLI tool; it is your central nervous system for communicating with Kubernetes.
kubectl is your primary interface to Kubernetes. Every interaction—creating resources, debugging problems, checking status—goes through kubectl. Mastering it is essential for both daily work and certification exams.
The “Wrong Context” Outage (A Real War Story)
In 2019, a prominent FinTech startup (anonymized) experienced a devastating 45-minute outage costing an estimated $120,000 in dropped transactions. The root cause was not a complex network failure or a zero-day exploit. A senior engineer, intending to clean up resources in a staging environment, executed kubectl delete namespace payments. Unfortunately, their kubectl context was still set to production. Because kubectl executes exactly what you tell it against the currently active context without a safety net, the entire production payments routing layer was instantly wiped out. This is why mastering kubectl context management, dry-runs, and careful execution is not just about speed—it is about survival.
Pause and predict: What will happen if you run kubectl apply -f . in a directory containing both valid Kubernetes YAML files and a plain text README.md file? (It will attempt to apply everything, fail with an error on the README, but still successfully apply the valid YAMLs).
You just got paged that the payment-processor pod is crash-looping in the finance namespace. You need to see the logs from the previous, crashed instance of the container to understand why it died. What command do you run?
Why? The --previous flag (or -p) is critical for debugging CrashLoopBackOff errors. By default, kubectl logs only shows the logs of the currently running container. When a container crashes and restarts, the current logs might be empty or only show the startup sequence. Using --previous pulls the logs from the dead container just before it exited, revealing the actual exception or error that caused the crash.
You are writing a script to monitor the IP addresses of all running pods in the cluster, but you only want the raw IP addresses without headers or extra columns. How do you extract just this specific field?
Answer
`kubectl get pods -o jsonpath='{.items[*].status.podIP}'`
Why? While -o wide shows the IP address, it includes headers and other columns that are hard to parse in a script. jsonpath allows you to navigate the JSON structure of the Kubernetes API response and extract exactly the data you need. This is a crucial skill not only for automation and bash scripting but also for passing the CKA/CKAD certification exams where extracting specific data is heavily tested.
A developer asks you to update a deployment to use a new image tag (v2.1.0). They do not have the original YAML file, and you need to do this quickly without risking typos in a manual kubectl edit session. What is the safest imperative command?
Answer
`kubectl set image deployment/myapp myapp=nginx:v2.1.0`
Why? Using kubectl set image is safer and faster than kubectl edit because it performs a targeted, atomic update to the specific field without opening a text editor. Opening a live resource in vi/nano introduces the risk of accidentally deleting or modifying other lines, which can break the deployment. This imperative command triggers a standard rolling update immediately, ensuring you do not accidentally introduce syntax errors.
You have created a YAML file app.yaml and applied it, but the pod is not behaving correctly and is stuck in a Pending state. You want to see the detailed events associated with this specific pod to understand why. What do you do?
Answer
`kubectl describe pod `
Why? The kubectl describe command aggregates data from multiple API endpoints, most importantly the cluster Events associated with that specific resource. When a pod is stuck in Pending or ImagePullBackOff, the get command will not tell you why. The describe command will show you exactly what the scheduler or kubelet is complaining about at the bottom of its output, making it your first stop for troubleshooting.
Your team is moving to a declarative GitOps workflow. You need to create a complex Deployment but want to avoid writing the YAML from scratch to prevent indentation errors. How can you trick kubectl into writing the skeleton for you?
Why? The combination of --dry-run=client and -o yaml is the ultimate cheat code in Kubernetes. It tells the kubectl client to process the command and format the intended API request as YAML, but stops before actually sending the POST request to the API server. By redirecting this output to a file, you get a syntactically perfect, valid template that you can commit to version control and modify later.
You are investigating a performance issue and need to execute a network diagnostic tool (curl) from inside an existing, running pod named api-backend. How do you drop into an interactive shell inside that pod?
Answer
`kubectl exec -it api-backend -- sh`
Why? The kubectl exec command works very similarly to docker exec, allowing you to spawn a new process inside the namespace of a running container. The -i (interactive) and -t (tty) flags allocate a terminal session so you can type commands and see the output in real-time. We use sh (or bash) as the command to run, giving us a fully interactive shell environment to run our diagnostics from the pod’s perspective.
You are working on two different clusters: dev-cluster and prod-cluster. You want to verify which cluster your kubectl commands are currently pointing to before you accidentally delete a critical resource. How do you check this?
Answer
`kubectl config current-context`
Why? Your ~/.kube/config file can contain connection details for dozens of clusters. The “context” determines which cluster, user, and default namespace kubectl will communicate with. Running current-context is a crucial safety check that should become muscle memory before running destructive commands, ensuring you do not repeat the classic mistake of applying dev changes to the production environment.
You notice a pod named cache-worker is completely unresponsive and stuck in a Terminating state for over 30 minutes. Normal deletion commands just hang. How do you forcefully remove it from the API server?
Answer
`kubectl delete pod cache-worker --force --grace-period=0`
Why? Sometimes the kubelet on a node loses communication with the API server, or a container runtime gets deadlocked, leaving a pod permanently stuck in Terminating. Setting --grace-period=0 tells Kubernetes not to wait for the container to shut down gracefully. Adding --force tells the API server to immediately remove the pod object from its datastore, even if the node has not confirmed the deletion.