AhmetB started an excellent thread yesterday where he asked people to share a Kubernetes CLI (kubectl) tip that they think a lot of users don't know about.
There are so many excellent tips I decided to collect the most interesting ones in a single post. The first thing that I typically do when I am on a new machine is to set the alias for kubectl
:
alias k=kubectl
If you're working with Kubernetes, you'll be typing kubectl
a lot, so why not make it shorter.
Enjoy the tips below and let us know if you have any other tips you want to share. Here are all the tips in no particular order.
1. Set up load-based horizontal pod autoscaling on your Kubernetes resources
By @pixie_run
kubectl autoscale deployment foo --min=2 --max=10
2. Create a new job from a cronjob
By @mauilion
kubectl create job --from=cronjob/<name of cronjob> <name of this run>
3. Enumerate permissions for a given service account
By @mauilion
kubectl -n <namespace> auth can-i --list --as system:serviceaccount:<namespace>:<service account name>
4. Annotate resources
By @hikvar
# To add annotation
kubectl annotate <resource-type>/<resource-name> foo=bar
# To remove annotation
kubectl annotate <resource-type>/<resource-name> foo-
5. Get a list of endpoints across all namespaces
By ivnrcki
kubectl get ep -A
6. Get a list of events sorted by lastTimestamp
By RewssPozzi
kubectl get events --sort-by=".lastTimestamp"
7. Watch all warnings across the namespaces
By @LachlanEvenson and @jpetazzo
kubectl get events -w --field-selector=type=Warning -A
8. Add the EVENT
column to the list of watched pods
kubectl get pods --watch --output-watch-events
9. Get raw JSON for the various APIs
By cheddarmint and @mauilion
kubectl get --raw /apis/apps/v1
# Get metrics
kubectl get --raw /metrics
10. Wait for specific pods to be ready
By @csanchez
kubectl wait --for=condition=ready pod -l foo=bar
11. Explain the various resources
By @pratikbin
kubectl explain pod.spec
12. Get all resources that match a selector
By @jpetazzo
kubectl get deployments,replicasets,pods,services --selector=hello=yourecute
13. Forward a port from a service to a local port
By @victortrac
kubectl port-forward svc/<service-name> <local-port>:<remote-port>
14. List the environment variables for a resource
By @oldmanuk
kubectl set env <resource>/<resource-name> --list
15. Get a list of pods and the node they run on
By @damnlamb
kubectl get po -o=custom-columns=NODE:.spec.nodeName,NAME:.metadata.name
16. Create a starter YAML manifest for deployment (also works for other resources)
kubectl create deploy nginx-deployment --image=nginx --dry-run=client -o yaml
17. Get a list of pods sorted by memory usage
By @NehmiHungry
kubectl top pods -A --sort-by='memory'
18. Get a list of pods that have a specific label value set
By @shrayk
kubectl get pods -l 'app in (foo,bar)'
19. Get the pod logs before the last restart
kubectl logs <pod-name> --previous
20. Copy a file from a pod to a local file
By @oshi1136
kubectl cp <namespace>/<pod>:<file_path> <local_file_path>
21. Delete a pod immediately
kubectl delete pod <pod-name> --now
22. Show the logs from pods with specific labels
By @oldmanuk
kubectl logs -l app=xyz
23. Get more information about the resources (aka wide-view)
By @hrvojekov
kubectl get <resource> -o wide
24. Output and apply the patch
By @gipszlyakab
# Edit a resource and get the patch
kubectl edit <resource>/<name> --output-patch
# Use the output from the command above to apply the patch
kubectl patch --patch=<output_from_previous_command>
Top comments (0)