Kubectl tips and tricks

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

20 Likes

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

12 Likes

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.

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

My small contribution :

Prompt info is pretty useful when switching clusters :


and Kubectx allow you to switch namespace & cluster :

8 Likes

+1 on shell auto complete.

It will save your sanity.

1 Like

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

5 Likes

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

The Kubectl aliases :

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

9 Likes

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.

6 Likes

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

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
7 Likes

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:

4 Likes

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?

4 Likes

Some good aliases can be found here too.

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

3 Likes

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

4 Likes

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).

8 Likes

I use autohot keys to shorten the commands in kubernetes

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

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

3 Likes

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!

2 Likes

Hi all :slight_smile:

I find this combo very helpful, a fuzzy finder fzf + kubectl + some bash/zsh function.

Examples:
exec into a pod

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

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]
}
7 Likes