Kubectl tips and tricks


#1

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


Subscribing to topics and tags
#2

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


#3

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

#4

My small contribution :

Prompt info is pretty useful when switching clusters :


and Kubectx allow you to switch namespace & cluster :


#5

+1 on shell auto complete.

It will save your sanity.


#6

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


#7

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

The Kubectl aliases :


#8

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

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.


#10

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


#11

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

#12

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:


#13

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?


#14

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


#15

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


#16

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


#17

I use autohot keys to shorten the commands in kubernetes


#18

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!


#19

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!


#20

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