Kubernetes CLI (kubectl) tips you didn't know about
Tags: kubernetes, beginners, discuss, productivity
Published: 2022-06-28 16:45:55

---
title: Kubernetes CLI (kubectl) tips you didn't know about
published: true
description: Kubernetes CLI (kubectl) tips you didn't know about
tags: kubernetes, beginners, discuss, productivity
canonical_url: https://www.learncloudnative.com/blog/2022-05-10-kubectl-tips 
//cover_image: https://direct_url_to_image.jpg
---

[AhmetB](https://twitter.com/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.

{% embed https://twitter.com/ahmetb/status/1523741824141041664 %}

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`:

```shell
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](https://twitter.com/learn_cnative) 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](https://twitter.com/pixie_run)

```shell
kubectl autoscale deployment foo --min=2 --max=10
```

## 2. Create a new job from a cronjob

By [@mauilion](https://twitter.com/mauilion)

```shell
kubectl create job --from=cronjob/<name of cronjob> <name of this run>
```

## 3. Enumerate permissions for a given service account

By [@mauilion](https://twitter.com/mauilion)

```shell
kubectl -n <namespace> auth can-i --list --as system:serviceaccount:<namespace>:<service account name>
```

## 4. Annotate resources

By [@hikvar](https://twitter.com/hikhvar)

```shell
# 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](https://twitter.com/ivnrcki)

```shell
kubectl get ep -A
```

## 6. Get a list of events sorted by lastTimestamp

By [RewssPozzi](https://twitter.com/ReesPozzi)

```shell
kubectl get events --sort-by=".lastTimestamp"
```

## 7. Watch all warnings across the namespaces

By [@LachlanEvenson](https://twitter.com/LachlanEvenson) and [@jpetazzo](https://twitter.com/jpetazzo)

```shell
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](https://twitter.com/jpetazzo)

```shell
kubectl get pods --watch --output-watch-events
```

## 9. Get raw JSON for the various APIs

By [cheddarmint](https://twitter.com/cheddarmint) and [@mauilion](https://twitter.com/mauilion)

```shell
kubectl get --raw /apis/apps/v1

# Get metrics
kubectl get --raw /metrics
```

## 10. Wait for specific pods to be ready

By [@csanchez](https://twitter.com/csanchez)

```shell
kubectl wait --for=condition=ready pod -l foo=bar
```

## 11. Explain the various resources

By [@pratikbin](https://twitter.com/pratikbin)

```shell
kubectl explain pod.spec
```

## 12. Get all resources that match a selector

By [@jpetazzo](https://twitter.com/jpetazzo)

```shell
kubectl get deployments,replicasets,pods,services --selector=hello=yourecute
```

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

By [@victortrac](https://twitter.com/victortrac)

```shell
kubectl port-forward svc/<service-name> <local-port>:<remote-port>
```

<ArticleSignupForm />

## 14. List the environment variables for a resource

By [@oldmanuk](https://twitter.com/oldmanuk)

```shell
kubectl set env <resource>/<resource-name> --list
```

## 15. Get a list of pods and the node they run on

By [@damnlamb](https://twitter.com/damnlamb)

```shell
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](https://twitter.com/ll_Cool__Josh)

```shell
kubectl create deploy nginx-deployment --image=nginx --dry-run=client -o yaml
```

## 17. Get a list of pods sorted by memory usage

By [@NehmiHungry](https://twitter.com/NehmiHungry)

```shell
kubectl top pods -A --sort-by='memory'
```

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

By [@shrayk](https://twitter.com/shrayk)

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

## 19. Get the pod logs before the last restart

By [@_elvir_kuric_](https://twitter.com/_elvir_kuric_)

```shell
kubectl logs <pod-name> --previous
```

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

By [@oshi1136](https://twitter.com/oshi1136)

```shell
kubectl cp <namespace>/<pod>:<file_path> <local_file_path>
```

## 21. Delete a pod immediately

By [@Rodrigo_Loza_L](https://twitter.com/Rodrigo_Loza_L)

```shell
kubectl delete pod <pod-name> --now
```

## 22. Show the logs from pods with specific labels

By [@oldmanuk](https://twitter.com/oldmanuk)

```shell
kubectl logs -l app=xyz
```

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

By [@hrvojekov](https://twitter.com/hrvojekov)

```shell
kubectl get <resource> -o wide
```

## 24. Output and apply the patch

By [@gipszlyakab](https://twitter.com/gipszlyakab)

```shell
# 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>
```