Essential kubectl Commands Every Test Engineer Needs for Kubernetes Debugging
This guide compiles the most frequently used kubectl commands for automated testing in Kubernetes, covering context management, service status checks, log retrieval, port forwarding, and practical tips, enabling test engineers to quickly verify deployments, troubleshoot failures, and integrate checks into CI/CD pipelines.
1. Environment and Context Management
List all available cluster contexts with kubectl config get-contexts and verify you are operating in the correct test or pre‑production environment. Switch the default namespace to avoid repeatedly specifying -n staging using
kubectl config set-context --current --namespace=staging.
2. Service Status Verification
Show pods in a namespace: kubectl get pods -n staging. Filter for a specific service, e.g.,
kubectl get pods -n staging | grep order-service, and inspect the STATUS column (Running, CrashLoopBackOff, Pending). Check deployments with kubectl get deploy -n staging to ensure the Desired/Ready replica counts match. Verify the image version of a deployment using
kubectl describe deploy order-service -n staging | grep Image.
3. Log Retrieval and Fault Diagnosis
View the last 100 lines of a pod’s logs:
kubectl logs order-service-7d5b8c9f4-xk2l9 -n staging --tail=100. Follow logs in real time with
kubectl logs -f order-service-7d5b8c9f4-xk2l9 -n staging. Access logs from a previous crashed container using
kubectl logs order-service-7d5b8c9f4-xk2l9 -n staging --previous. Retrieve logs from all pods matching a label:
kubectl logs -l app=order-service -n staging --tail=50.
4. Debugging and Local Validation
Port‑forward a service to the local machine:
kubectl port-forward svc/order-service 8080:80 -n staging, then access it via localhost:8080. Execute commands inside a container:
kubectl exec -it order-service-7d5b8c9f4-xk2l9 -n staging -- /bin/sh. If the container lacks a shell, run a single command directly, e.g.,
kubectl exec order-service-7d5b8c9f4-xk2l9 -n staging -- curl http://user-service:8080/health.
5. Network and Service Discovery
List services to verify ClusterIP and ports: kubectl get svc -n staging. Inspect Ingress resources to obtain external hostnames: kubectl get ingress -n staging.
6. Practical Tips for Efficiency
Use kubectl short names (e.g., po for pods, svc for services, deploy for deployments, ns for namespaces) to reduce typing. Combine commands to extract key data, such as retrieving pod IPs:
kubectl get po -n staging -o wide | grep order | awk '{print $6}', or listing abnormal pods:
kubectl get po -n staging | grep -E 'Crash|Error|Pending'.
7. Recommended Testing Scenarios
Integrate kubectl get pods checks into CI/CD pipelines to abort the build if any pod is not Running.
Perform pre‑test health checks with kubectl describe or log inspection before executing automated test cases.
Archive relevant pod logs alongside test reports for future traceability.
Grant test accounts read‑only permissions (e.g., get, list, logs) to minimize accidental changes.
Conclusion
Proficiency with basic Kubernetes commands is now a core competency for modern automated test engineers; mastering these commands empowers teams to actively participate in quality assurance, locate issues precisely, and validate services efficiently in cloud‑native environments.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
