DEV Community

Peter Jausovec
Peter Jausovec

Posted on • Originally published at learncloudnative.com

Kubernetes CLI (kubectl) tips you didn't know about

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

2. Create a new job from a cronjob

By @mauilion

kubectl create job --from=cronjob/<name of cronjob> <name of this run>
Enter fullscreen mode Exit fullscreen mode

3. Enumerate permissions for a given service account

By @mauilion

kubectl -n <namespace> auth can-i --list --as system:serviceaccount:<namespace>:<service account name>
Enter fullscreen mode Exit fullscreen mode

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-
Enter fullscreen mode Exit fullscreen mode

5. Get a list of endpoints across all namespaces

By ivnrcki

kubectl get ep -A
Enter fullscreen mode Exit fullscreen mode

6. Get a list of events sorted by lastTimestamp

By RewssPozzi

kubectl get events --sort-by=".lastTimestamp"
Enter fullscreen mode Exit fullscreen mode

7. Watch all warnings across the namespaces

By @LachlanEvenson and @jpetazzo

kubectl get events -w --field-selector=type=Warning -A
Enter fullscreen mode Exit fullscreen mode

8. Add the EVENT column to the list of watched pods

By Jérôme Petazzoni

kubectl get pods --watch --output-watch-events
Enter fullscreen mode Exit fullscreen mode

9. Get raw JSON for the various APIs

By cheddarmint and @mauilion

kubectl get --raw /apis/apps/v1

# Get metrics
kubectl get --raw /metrics
Enter fullscreen mode Exit fullscreen mode

10. Wait for specific pods to be ready

By @csanchez

kubectl wait --for=condition=ready pod -l foo=bar
Enter fullscreen mode Exit fullscreen mode

11. Explain the various resources

By @pratikbin

kubectl explain pod.spec
Enter fullscreen mode Exit fullscreen mode

12. Get all resources that match a selector

By @jpetazzo

kubectl get deployments,replicasets,pods,services --selector=hello=yourecute
Enter fullscreen mode Exit fullscreen mode

13. Forward a port from a service to a local port

By @victortrac

kubectl port-forward svc/<service-name> <local-port>:<remote-port>
Enter fullscreen mode Exit fullscreen mode

14. List the environment variables for a resource

By @oldmanuk

kubectl set env <resource>/<resource-name> --list
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

16. Create a starter YAML manifest for deployment (also works for other resources)

By @ll_Cool__Josh

kubectl create deploy nginx-deployment --image=nginx --dry-run=client -o yaml
Enter fullscreen mode Exit fullscreen mode

17. Get a list of pods sorted by memory usage

By @NehmiHungry

kubectl top pods -A --sort-by='memory'
Enter fullscreen mode Exit fullscreen mode

18. Get a list of pods that have a specific label value set

By @shrayk

kubectl get pods -l 'app in (foo,bar)'
Enter fullscreen mode Exit fullscreen mode

19. Get the pod logs before the last restart

By @_elvir_kuric_

kubectl logs <pod-name> --previous
Enter fullscreen mode Exit fullscreen mode

20. Copy a file from a pod to a local file

By @oshi1136

kubectl cp <namespace>/<pod>:<file_path> <local_file_path>
Enter fullscreen mode Exit fullscreen mode

21. Delete a pod immediately

By @Rodrigo_Loza_L

kubectl delete pod <pod-name> --now
Enter fullscreen mode Exit fullscreen mode

22. Show the logs from pods with specific labels

By @oldmanuk

kubectl logs -l app=xyz
Enter fullscreen mode Exit fullscreen mode

23. Get more information about the resources (aka wide-view)

By @hrvojekov

kubectl get <resource> -o wide
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)