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

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.

Share a kubectl tip below that you think a lot of users don’t know about. 👇🏼

There are so many excellent tips I decide 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

By Jérôme Petazzoni

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>

Join more than a thousand happy subscribers receiving Learn Cloud Native newsletter. An occasional email with technical blog posts, videos, and tutorials from the cloud-native world.

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

Bt @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)

By @ll_Cool__Josh

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 specific label value set

By @shrayk

kubectl get pods -l 'app in (foo,bar)'

19. Get the pod logs before the last restart

By @elvir_kuric

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

By @Rodrigo_Loza_L

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>
Peter Jausovec
Peter Jausovec
Peter Jausovec is a software and content engineer at Tetrate.io. He has more than a decade of experience in the field of software development and tech, in various roles such as QA (test), software engineering and leading tech teams. He's been working in the cloud-native space for the past couple of years, and delivering talks and workshops around the world. He authored and co-authored a couple of books, latest being Cloud Native: Using Containers, Functions, and Data to Build Next-Generation Applications.

Sign up for cloud-native newsletter

Receive an occasional email with blog posts, videos, tutorials, and other content from the cloud-native world.

No spam ever.