← All network notes
KubernetesLinux

Stop typing kubectl – bash alias and completion

I use my k8s-admin container (ubuntu with kubectl, flux, helm and kustomize) for all kubernetes work. One thing makes a huge difference: a k alias with full tab-completion, so I never have to type kubectl in full.

It just goes in /root/.bashrc in the container:

alias k='kubectl'

source /etc/bash_completion
source <(kubectl completion bash)
complete -o default -F __start_kubectl k

Now things like k get po<TAB> and k -n kube-system get deploy <TAB> just work. Note the last line: it gives the same completion to k as to kubectl, because the completion function is called __start_kubectl.

Important: the bash-completion package must be installed (I added it to my Containerfile). And since a container is ephemeral, the snippet lives as a file (bash-aliases) next to the Containerfile and is copied in at build — otherwise it vanishes on the next rebuild:

COPY bash-aliases /tmp/bash-aliases
RUN cat /tmp/bash-aliases >> /root/.bashrc && rm /tmp/bash-aliases