20 cool Kubernetes commands to level up your skills
While many developers and DevOps engineers are familiar with basic kubectl
commands, there are some incredibly powerful commands that can make your life easier when managing Kubernetes clusters.
In this post, we’ll explore 20 cool Kubernetes commands that can elevate your Kubernetes game. Whether you’re debugging, optimising workflows, or simply looking to impress your peers, these commands will come in handy.
1. Debug a Running Pod Using kubectl debug
Want to troubleshoot a running pod without disrupting the existing containers? Use kubectl debug
to attach a temporary debugging container. This container will share the network namespace with the target pod.
kubectl debug <pod-name> -n <namespace> --image=busybox --target=<container-name> -- /bin/sh
Why it’s cool: You can debug live issues without having to restart or modify your pods.
2. Copy a File or Directory from a Pod to Local
Need to pull logs or files from a running pod? kubectl cp
makes it easy to copy files from inside a pod to your local machine.
kubectl cp <namespace>/<pod-name>:/path/to/file /local/path
Why it’s cool: This is perfect for extracting logs, config files, or data without exposing the pod’s storage externally.
3. Run a Pod Temporarily and Auto-Delete It
Sometimes you need to spin up a pod just for a quick test. This command runs a pod in interactive mode and auto-deletes it after you’re done.
kubectl run --rm -it <pod-name> --image=<image> -- /bin/sh
Why it’s cool: Great for one-off tasks, leaving no resource clutter afterward.
4. Patch a Resource Inline
Need to tweak a configuration without editing a YAML file? Use kubectl patch
to modify resources directly from the command line.
kubectl patch <resource> <resource-name> --patch '{"spec": {"replicas": 3}}'
Why it’s cool: Inline patching is a fast, efficient way to make changes without the need for YAML files.
5. Get Pod IPs Only
Ever wanted a clean list of pod IPs? This command fetches just the IPs without extra information.
kubectl get pods -o custom-columns="IP:.status.podIP"
Why it’s cool: Simplifies IP collection, making it perfect for network diagnostics.
6. Roll Back a Deployment to a Previous Version
If a recent deployment breaks things, you can easily roll back to the last working state.
kubectl rollout undo deployment/<deployment-name>
Why it’s cool: This command saves you from headaches during deployment issues.
7. Run a Pod and Attach to It Like SSH
This spins up a temporary pod and immediately attaches a terminal to it, making it a great SSH-like experience for quick tasks.
kubectl run -it --rm --image=<image> --generator=run-pod/v1 <pod-name> -- bash
Why it’s cool: Super useful for debugging or testing connectivity without leaving pods hanging around.
8. Patch Resources with Strategic Merging
Instead of the basic kubectl patch
, use strategic merge patching to intelligently update fields in your Kubernetes objects without overwriting unrelated fields. This comes in handy when you're working in environments where you need to tweak just one aspect of a resource without affecting others.
For example, to add a label to a deployment:
kubectl patch deployment <deployment-name> --type='strategic' -p '{"metadata":{"labels":{"new-label":"value"}}}'
Why it’s cool: This method ensures you’re only updating the necessary parts of your configuration without disrupting the rest.
9. List Pods by Their Node Affinity
Sort all pods by the node they are running on, useful for diagnosing node resource usage or balancing issues.
kubectl get pods -o wide --sort-by='.spec.nodeName'
Why it’s cool: Quickly view which nodes are hosting which pods and spot imbalances or affinity issues.
10. Apply a Resource with Server-side Validation
The --server-side
option ensures changes are applied via the server's merge strategy, rather than client-side.
kubectl apply -f <file.yaml> --server-side
Why it’s cool: Enables better control in GitOps workflows where different parts of the YAML may be owned by different processes.
11. Create a Temporary Port Forward to a Service
If you need to debug or interact with an internal service, port forwarding can help without exposing it to the public.
kubectl port-forward svc/<service-name> <local-port>:<service-port>
Why it’s cool: Quickly access services running inside your cluster, perfect for debugging.
12. Expose a Deployment as a Service Temporarily
Need to expose your deployment for temporary testing? kubectl expose
makes it easy.
kubectl expose deployment <deployment-name> --port=<external-port> --target-port=<internal-port> --type=ClusterIP
Why it’s cool: Instant service exposure without a permanent Service manifest.
13. View Pod’s Image Without Extra Information
This command outputs only the images used by running pods, useful for quick audits.
kubectl get pods -o jsonpath="{.items[*].spec.containers[*].image}"
Why it’s cool: Fast and concise way to check which images are deployed in your cluster.
14. Impersonate a User for RBAC Testing
Use this command to test what a specific user can or can’t do in the cluster.
kubectl auth can-i <verb> <resource> --as=<user>
Why it’s cool: Handy for troubleshooting and testing RBAC permissions without logging in as that user.
15. Generate Resource Manifests Without Applying
Sometimes, you just need the YAML output without applying the resource. This command creates deployment manifests in YAML form.
kubectl create deployment <deployment-name> --image=<image> --dry-run=client -o yaml
Why it’s cool: Get boilerplate YAML quickly for customisation and reusability.
16. Execute Rolling Restarts with Zero Downtime
Instead of deleting pods manually or tweaking deployment configurations to trigger restarts, you can easily perform a rolling restart of your application to apply config changes or refresh your pods without impacting availability.
kubectl rollout restart deployment <deployment-name>
Why it’s cool: This ensures a graceful restart, allowing Kubernetes to handle the restarts gradually without downtime.
17. Check Which ServiceAccount a Pod Uses
Need to know which ServiceAccount a pod is using? This command fetches that detail.
kubectl get pod <pod-name> -o jsonpath='{.spec.serviceAccountName}'
Why it’s cool: Vital for understanding what permissions a pod has within the cluster.
18. Check for Open TCP Ports in Pods
Sometimes you need to troubleshoot networking at the container level. This command shows open ports inside a pod.
kubectl exec <pod-name> -- netstat -tuln
Why it’s cool: Great for verifying port exposure and network connectivity within containers.
19. Identify Events Causing Pod Issues
View events happening in your cluster, sorted by time, to help troubleshoot pod issues.
kubectl get events --sort-by='.metadata.creationTimestamp'
Why it’s cool: It gives you a real-time view of what’s happening in your cluster and can help pinpoint problems.
20. Use kubectl run
as a Ping Test
Need to verify network connectivity between services? Use this trick to run a quick ping test.
kubectl run tmp-shell --rm -i --tty --image busybox -- /bin/sh -c "ping <service-name>"
Why it’s cool: You can quickly confirm network connectivity between pods and services without complex tooling.
Conclusion:
While the basic commands are sufficient for most tasks, these advanced kubectl
commands will make you more effective, whether you’re debugging, optimising workflows, or scaling your applications.
Next time you’re managing your cluster, try these out and see how they can make your life easier!
Have more Kubernetes tips or commands? Share them in the comments below, and let’s level up our Kubernetes skills together!