Written by

,

Kubernetes is rarely forgiving. Consequently, wasting time on syntax errors is not an option for a serious professional. Furthermore, reliance on a GUI often slows you down during a critical outage. Therefore, you must master the CLI to remain effective. Specifically, this guide cuts through the noise. It focuses strictly on the commands that actually solve problems.

Know Your Environment

First, you must verify your location. Accidentally destroying a production cluster because you thought you were in staging is a rookie mistake. Therefore, always check your context before acting.

Check current context:

PowerShell

kubectl config current-context

Switch contexts quickly:

PowerShell

kubectl config use-context <context-name>

List all available contexts: Additionally, you might need to see what clusters are available in your config file.

PowerShell

kubectl config get-contexts

Inspecting Workloads

Next, you need visibility into your running applications. However, the default output is often too sparse. Thus, use flags to get the details you actually need immediately.

List pods with extra info: Standard output hides IP addresses and node assignments. Therefore, always append -o wide.

PowerShell

kubectl get pods -o wide

Watch pods in real-time: Waiting for a pod to start is tedious. Instead, watch the status change live.

PowerShell

kubectl get pods --watch

Filter logs efficiently: Debugging requires logs. However, scrolling through thousands of lines is inefficient. Consequently, use the --tail flag to see only recent events. Since you are on Windows, avoid grep; use Select-String if you must filter further.

PowerShell

# Get the last 20 lines
kubectl logs <pod-name> --tail=20

# Filter for errors using PowerShell
kubectl logs <pod-name> | Select-String "Error"

Debugging and Interaction

Sometimes, observation is insufficient. Consequently, you must interact directly with the container to diagnose complex issues.

Execute a shell: Specifically, this command drops you directly into the container’s shell.

PowerShell

kubectl exec -it <pod-name> -- /bin/sh

Port forwarding: Security policies often block direct access to services. Therefore, map a local port to the cluster to bypass this restriction temporarily.

PowerShell

kubectl port-forward <pod-name> 8080:80

Resource Cleanup

Finally, leave no trace. Abandoned resources consume money and compute power. Thus, delete them decisively when they are no longer required.

Force delete a stuck pod: Occasionally, a pod refuses to terminate. Therefore, force the deletion immediately.

PowerShell

kubectl delete pod <pod-name> --grace-period=0 --force

Delete all resources in a file: Furthermore, you can reverse your deployment in one shot.

PowerShell

kubectl delete -f deployment.yaml