# Kubectl tips and tricks

**URL:** <https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192>\
**Category:** General Discussions\
**Created:** [May 11, 2018, 9:58pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192 "2018-05-11T21:58:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![suvi29](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/suvi29/32/502_2.png) [@suvi29](https://discuss.kubernetes.io/u/suvi29)\
**Post date:** [May 11, 2018, 9:58pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/1 "2018-05-11T21:58:45Z")

</div>

Hi Guys,  
As we know that **kubectl** is a powerfull tool which we use every day. It has broad functionality.

Which **kubectl** tip or tricks, you found out and said _ **wish I knew it before** _.

I will start with my experience wish I would have known earlier

**Tip:**  
If you want to check out the logs of the previously terminated container from pod POD\_NAME.  
You can use the following command with **-p flag**

`kubectl logs POD_NAME -c CONTAINER_NAME -p`

looking forward to seeing your tips and tricks  
Thanks

---

<div class="post-metadata">

**Author:** ![castrojo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/castrojo/32/8_2.png) [@castrojo](https://discuss.kubernetes.io/u/castrojo)\
**Post date:** [May 15, 2018, 12:13pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/2 "2018-05-15T12:13:54Z")

</div>

Just in case people didn’t know about `kubectl` auto complete:

- [https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion](https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion)

---

<div class="post-metadata">

**Author:** ![mrbobbytables](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/mrbobbytables/32/7_2.png) [@mrbobbytables](https://discuss.kubernetes.io/u/mrbobbytables)\
**Post date:** [May 15, 2018, 1:21pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/3 "2018-05-15T13:21:20Z")

</div>

Being able to get raw output quickly and easily with `--raw`. You can then pipe it to `jq` and filter it as much as you want.

```bash
kubectl get --raw=<api path>
kubectl get --raw=/apis/apps/v1/deployments
kubectl get --raw=/apis/apps/v1/deployments | jq

```

---

<div class="post-metadata">

**Author:** ![ValentinNC](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/valentinnc/32/203_2.png) [@ValentinNC](https://discuss.kubernetes.io/u/ValentinNC)\
**Post date:** [May 16, 2018, 10:15pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/4 "2018-05-16T22:15:26Z")

</div>

My small contribution :

Prompt info is pretty useful when switching clusters :

> **[jonmosco/kube-ps1](https://github.com/jonmosco/kube-ps1)**
>
> Kubernetes prompt info for bash and zsh. Contribute to jonmosco/kube-ps1 development by creating an account on GitHub.

  

> **[superbrothers/zsh-kubectl-prompt](https://github.com/superbrothers/zsh-kubectl-prompt)**
>
> Display information about the kubectl current context and namespace in zsh prompt. - superbrothers/zsh-kubectl-prompt

and Kubectx allow you to switch namespace & cluster :

> **[ahmetb/kubectx](https://github.com/ahmetb/kubectx)**
>
> Faster way to switch between clusters and namespaces in kubectl - ahmetb/kubectx

---

<div class="post-metadata">

**Author:** ![wstrange](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/wstrange/32/99_2.png) [@wstrange](https://discuss.kubernetes.io/u/wstrange)\
**Post date:** [May 17, 2018, 4:33pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/5 "2018-05-17T16:33:29Z")

</div>

+1 on shell auto complete.

It will save your sanity.

---

<div class="post-metadata">

**Author:** ![misterikkit](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/misterikkit/32/248_2.png) [@misterikkit](https://discuss.kubernetes.io/u/misterikkit)\
**Post date:** [May 18, 2018, 6:15am UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/6 "2018-05-18T06:15:39Z")

</div>

Does anyone `alias k=kubectl` and have shell autocompletion working?

---

<div class="post-metadata">

**Author:** ![ameukam](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/ameukam/32/672_2.png) [@ameukam](https://discuss.kubernetes.io/u/ameukam)\
**Post date:** [May 18, 2018, 9:37am UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/7 "2018-05-18T09:37:00Z")

</div>

Something i find useful when you don’t want type too much:

The Kubectl aliases :

> **[ahmetb/kubectl-aliases](https://github.com/ahmetb/kubectl-aliases)**
>
> Programmatically generated handy kubectl aliases. Contribute to ahmetb/kubectl-aliases development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![suvi29](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/suvi29/32/502_2.png) [@suvi29](https://discuss.kubernetes.io/u/suvi29)\
**Post date:** [May 18, 2018, 1:17pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/8 "2018-05-18T13:17:22Z")

</div>

another **awesome** and **helpful** command

This command analyzes two kubernetes resources and prints the lines that are different.

We can list out all of the functionality of it

`kubectl alpha diff --help`

---

<div class="post-metadata">

**Author:** ![luksa](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/luksa/32/243_2.png) [@luksa](https://discuss.kubernetes.io/u/luksa)\
**Post date:** [May 18, 2018, 2:17pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/9 "2018-05-18T14:17:56Z")

</div>

> Does anyone alias k=kubectl and have shell autocompletion working?

`source <(k completion bash | sed s/kubectl/k/g)`

Works on Linux; I remember someone reporting it doesn’t work on OSX, but that was a long time ago, so that may no longer be true.

---

<div class="post-metadata">

**Author:** ![danberg](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@danberg](https://discuss.kubernetes.io/u/danberg)\
**Post date:** [May 18, 2018, 4:20pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/10 "2018-05-18T16:20:32Z")

</div>

I do this all the time and auto-complete works fine with the alias.

---

<div class="post-metadata">

**Author:** ![superbrothers](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/superbrothers/32/91_2.png) [@superbrothers](https://discuss.kubernetes.io/u/superbrothers)\
**Post date:** [May 18, 2018, 5:07pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/11 "2018-05-18T17:07:01Z")

</div>

Autocompletion for alias commands was supported on v1.10! You can enable it with the following steps:

```
# For bash
source /path/to/bash_completion
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k

# For zsh
source <(kubectl completion zsh)
alias k=kubectl
complete -o default -F __start_kubectl k
```

---

<div class="post-metadata">

**Author:** ![InAnimaTe](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/inanimate/32/253_2.png) [@InAnimaTe](https://discuss.kubernetes.io/u/InAnimaTe)\
**Post date:** [May 18, 2018, 6:10pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/12 "2018-05-18T18:10:13Z")

</div>

I made my own zsh/bash function to more easily handle contexts and namespaces. I also run this every time with a kubectl alias to know which environment a certain command is being run against:

> <https://gist.github.com/InAnimaTe/eeeb4b01467d74c522b94f12ed009889>

---

<div class="post-metadata">

**Author:** ![errordeveloper](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/errordeveloper/32/118_2.png) [@errordeveloper](https://discuss.kubernetes.io/u/errordeveloper)\
**Post date:** [May 18, 2018, 7:41pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/13 "2018-05-18T19:41:46Z")

</div>

I think one big topic would be about using `--output='jsonpath=...'`, vs `--output='json' | jq ...`.

Personally, I find `jq` much more user-friendly, but `jsonpath` is so much more portable - just copy the script and no need to make sure you have `jq` installed… What do people think?

---

<div class="post-metadata">

**Author:** ![jgarr](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/jgarr/32/170_2.png) [@jgarr](https://discuss.kubernetes.io/u/jgarr)\
**Post date:** [May 18, 2018, 7:43pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/14 "2018-05-18T19:43:00Z")

</div>

Some good aliases can be found here too.

> **[kubermatic/fubectl](https://github.com/kubermatic/fubectl)**
>
> Reduces repetitive interactions with kubectl. Contribute to kubermatic/fubectl development by creating an account on GitHub.

I use a couple similar to `kcns` and `kex` fairly regularly.

I also have `alias kc='kubectl --namespace ${KUBE_NAMESPACE:-default} '` so I can export a namespace easily in a shell and not always have to type it. For me, `kcns` sets that variable

---

<div class="post-metadata">

**Author:** ![kris-nova](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/kris-nova/32/252_2.png) [@kris-nova](https://discuss.kubernetes.io/u/kris-nova)\
**Post date:** [May 18, 2018, 8:11pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/15 "2018-05-18T20:11:14Z")

</div>

Thanks for all the hardwork - we are loving these tips and tricks on TGIK!

---

<div class="post-metadata">

**Author:** ![proskehy](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@proskehy](https://discuss.kubernetes.io/u/proskehy)\
**Post date:** [May 18, 2018, 10:10pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/16 "2018-05-18T22:10:55Z")

</div>

This might be obvious for some, but I was very excited when I learned about `kubectl explain`. It explains resources and/or the fields of their configuration (e.g. `kubectl explain resource.field.field`).

---

<div class="post-metadata">

**Author:** ![Kolchap](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@Kolchap](https://discuss.kubernetes.io/u/Kolchap)\
**Post date:** [May 19, 2018, 5:46am UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/17 "2018-05-19T05:46:21Z")

</div>

I use autohot keys to shorten the commands in kubernetes

---

<div class="post-metadata">

**Author:** ![errordeveloper](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/errordeveloper/32/118_2.png) [@errordeveloper](https://discuss.kubernetes.io/u/errordeveloper)\
**Post date:** [May 19, 2018, 3:52pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/18 "2018-05-19T15:52:58Z")

</div>

I forgot to mention! Sometime last year I wrote `kubeplay`. It is intended for interactive scripting, like a REPL basically.

Personally, I find `kubectl` to tedious, and gluing it with shell scripts can be even more tedious.

Currently, `kubeplay` uses Ruby syntax (via mruby embedded in a Go program), but I am keen to explore other options. I am also not a fan of pseaudo-graphical interactive console apps, so this more of a pure REPL based on a proper language 🙂

> **[errordeveloper/kubeplay](https://github.com/errordeveloper/kubeplay)**
>
> kubeplay – a new way to interact with Kubernetes API from your terminal - errordeveloper/kubeplay

Take a look and let me know if you would be keen to use it, I would love to put more time into it!

---

<div class="post-metadata">

**Author:** ![Raffo](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@Raffo](https://discuss.kubernetes.io/u/Raffo)\
**Post date:** [May 19, 2018, 5:41pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/19 "2018-05-19T17:41:05Z")

</div>

Ok this might sounds very stupid, but here some alias I use for kubectl:

```
alias k='kubectl'
alias watch='watch '
alias pods='k get pods'
alias spods='k get pods -n kube-system'
alias deployments='k get deployments'
alias sdeployments='k get deployments -n kube-system'
alias ds='k get ds'
alias sds='k get ds -n kube-system'
alias logs='k logs'
alias slogs='k logs -n kube-system'

```

In general I think the best aliases are the one you build for yourself and that make your life easier every day… and I really can’t live without my “spods”.

EDIT: Also, hi!

---

<div class="post-metadata">

**Author:** ![st4nson](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/st4nson/32/287_2.png) [@st4nson](https://discuss.kubernetes.io/u/st4nson)\
**Post date:** [May 20, 2018, 8:12pm UTC](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192/20 "2018-05-20T20:12:29Z")

</div>

Hi all 🙂

I find this combo very helpful, a fuzzy finder [fzf](https://github.com/junegunn/fzf) + _kubectl_ + some bash/zsh function.

_Examples_:  
**exec into a pod**

```auto
kx () {
	local pod=($(kubectl get pods --all-namespaces -owide | fzf | awk '{print $1, $2}'))
	local cmd=${@:-"bash"}

	echo kubectl exec -it --namespace $pod[1] $pod[2] $cmd
	kubectl exec -it --namespace $pod[1] $pod[2] $cmd
}

```

**logs**

```auto
kl () {
	local pod=($(kubectl get pods --all-namespaces -owide | fzf | awk '{print $1, $2}'))
	local attr=${@:-""}

	echo kubectl logs -f $attr --namespace $pod[1] $pod[2]
	kubectl logs -f $attr --namespace $pod[1] $pod[2]
}

```

[Next page](https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192.md?page=2)
