Auto-Completion for Kubectl allows you to set up an alias for kubectl on bash cli.
Auto-completion can be very useful to avoid having to reference a cheat sheet constantly when using kubectl commands to interact with your Kubernetes cluster.
Benefits:
- Efficiency – reduce the amount of typing required, especially in very long commands
- Minimize errors – by suggesting command completions, you minimize the chances for mistyping, thus reducing overall frustration
- Enhanced learning – this is beneficial, especially for people just starting out with K8s, as auto-completion can serve as an education tool by showing available commands and options users may not be familiar with
- Increased productivity – streamline workflows by allowing users to execute commands more quickly and accurately
Ubuntu / Raspberry Pi OS
apt install bash-completion -y
Red Hat / CentOS / Rocky Linux
dnf install bash-completion -y
Install latest release of kubens from https://github.com/ahmetb/kubectx/releases
vi /root/.bashrc
OR vi /root/.bash_profile
source <(kubectl completion bash)
alias k="kubectl"
alias kg="kubectl get -o wide"
alias kd="kubectl describe"
alias kl="kubectl logs"
alias klf="kubectl logs -f"
alias kgn="kubectl get nodes"
alias kgns="kubectl get ns"
alias ke="kubectl get events --sort-by=.metadata.creationTimestamp"
complete -o default -F __start_kubectl k
# Changing of namespace with kubens
kns() {
input=$1
num_of_namespaces=`kubens | wc -l`
if [[ ${input} != "" ]]
then
# Switch to namespace specified
if [ -n "${input}" ] && [ "${input}" -eq "${input}" ] 2>/dev/null
then
# If argument is a number
# Check for valid range
if ((${input} >= 1 && ${input} <= ${num_of_namespaces}))
then
kubens `kubens | sed -n $1p`
else
echo "Usage: kns <1...${num_of_namespaces}>"
fi
else
# If argument is a namespace
kubens ${input}
fi
else
# List all namespaces in numbered
echo -e "${ENDCOLOR}Currently in \033[7m${BLUE}`kubens -c`${ENDCOLOR}\033[0m namespace."
kubens_output=`kubens`
count=1
for i in `echo ${kubens_output}`
do
echo "${count}) ${i}"
((count=count+1))
done
fi
}
source /root/.bashrc
OR source /root/.bash_profile