
Kubernetes is a powerful orchestration platform, and kubectl is its command-line interface (CLI). Mastering kubectl commands enables efficient troubleshooting, resource management, and automation.
In this guide, we’ll cover:
- Essential and advanced
kubectlcommands - Efficient querying and filtering of Kubernetes resources
- Debugging and troubleshooting with
kubectl - Bulk operations and automation
- FAQs and best practices
Prerequisites
Before using advanced kubectl commands, ensure you have:
- kubectl installed (Install Guide)
- A configured Kubernetes cluster
- Basic understanding of Kubernetes resources (Pods, Deployments, Services, etc.)
Step 1: Essential kubectl Commands Refresher
1. Viewing Cluster Resources
kubectl get nodes
kubectl get pods --all-namespaces
kubectl get deployments -n default
2. Describing Resources in Detail
kubectl describe pod my-pod -n my-namespace
kubectl describe deployment my-deployment
3. Viewing Pod Logs
kubectl logs my-pod
kubectl logs -f my-pod -c my-container # Following logs of a specific container
Step 2: Querying and Filtering Kubernetes Resources
1. Using Labels to Filter Resources
kubectl get pods -l app=my-app
kubectl get services -l env=production
2. Customizing Output Formats
kubectl get pods -o wide
kubectl get pods -o json
kubectl get pods -o yaml
3. Sorting and Formatting Output
kubectl get pods --sort-by=.status.startTime
kubectl get pods -o custom-columns="NAME:.metadata.name,STATUS:.status.phase"
Step 3: Debugging and Troubleshooting with kubectl
1. Checking Pod Events
kubectl get events --sort-by=.metadata.creationTimestamp
2. Debugging a Running Pod with an Interactive Shell
kubectl exec -it my-pod -- /bin/sh
kubectl exec -it my-pod -c my-container -- /bin/bash
3. Port Forwarding for Local Access
kubectl port-forward svc/my-service 8080:80
4. Troubleshooting Network Connectivity
kubectl run curl-test --image=curlimages/curl -it --rm -- sh
curl http://my-service
Step 4: Bulk Operations and Automation
1. Deleting Resources in Bulk
kubectl delete pods --all -n my-namespace
kubectl delete deployments -l app=my-app
2. Applying Configuration Changes Efficiently
kubectl apply -f deployment.yaml
kubectl apply -k ./kustomize-dir/
3. Restarting Pods Without Deleting
kubectl rollout restart deployment my-deployment
4. Scaling Deployments on the Fly
kubectl scale deployment my-deployment --replicas=5
FAQs:
Q1: How do I view resource usage for a pod?
A: Use kubectl top pod to check CPU and memory usage.
kubectl top pod my-pod
Q2: How can I switch between different Kubernetes contexts?
A: Use kubectl config use-context.
kubectl config get-contexts
kubectl config use-context my-context
Q3: How do I get detailed YAML configuration of a resource?
A: Use kubectl get with -o yaml.
kubectl get pod my-pod -o yaml
Q4: How can I rollback a deployment to a previous version?
A: Use kubectl rollout undo.
kubectl rollout undo deployment my-deployment
Q5: How do I apply multiple YAML files at once?
A: Use kubectl apply -f with a directory.
kubectl apply -f ./manifests/
Conclusion
Mastering advanced kubectl commands improves efficiency in managing Kubernetes clusters.
Key Takeaways:
✅ Query and filter Kubernetes resources effectively.✅ Debug and troubleshoot Pods using logs, exec, and events.✅ Perform bulk operations and automate deployments.✅ Use port forwarding, scaling, and rollouts efficiently.
If you found this guide helpful, follow me for more Kubernetes and DevOps tutorials! 🚀
📢 Have questions or feedback? Drop a comment below or connect with me on Twitter/X@spysood!
Originally published on Medium.