Kubectl Tricks: Boost Your Kubernetes Productivity

kubectl is the primary command-line tool for interacting with Kubernetes clusters. While the basics are widely known, mastering some lesser-known tricks can significantly enhance your daily workflow.

1. Quickly Switch Contexts

kubectl config use-context my-cluster
kubectl config get-contexts

Use kubectx for even faster context switching.

2. View Resources with Custom Columns

kubectl get pods -o custom-columns="NAME:.metadata.name,STATUS:.status.phase"

3. Watch Resource Changes in Real-Time

kubectl get pods --watch

4. Debug a Running Pod

kubectl exec -it my-pod -- /bin/sh

5. Apply YAML from stdin

cat pod.yaml | kubectl apply -f -

6. Port Forward a Pod to Localhost

kubectl port-forward svc/my-service 8080:80

7. Set a Temporary Alias

alias k='kubectl'

8. Output as YAML/JSON

kubectl get pod mypod -o yaml
kubectl get pod mypod -o json

Conclusion

These kubectl tricks are just a starting point. Efficient CLI usage can drastically reduce time spent managing Kubernetes clusters.