Problem:
In order to save time and make your life a lot easier when you want to execute multiple Kubernetes commands in one session use command aliases in Linux.
An alias lets you create a shortcut name for a command, file name, or any shell text. By using aliases, you save a lot of time when doing tasks you do frequently.
Useful Kubernetes command aliases:
- alias k='kubectl'
- alias kc='k config view --minify | grep name'
- alias kdp='kubectl describe pod'
- alias c='clear'
- alias kd='kubectl describe pod'
- alias ke='kubectl explain'
- alias kf='kubectl create -f'
- alias kg='kubectl get pods --show-labels'
- alias kr='kubectl replace -f'
- alias ks='kubectl get namespaces'
- alias l='ls -lrt'
- alias kga='k get pod --all-namespaces'
- alias kgaa='kubectl get all --show-labels'
Create ~/.bash_aliases file
By default when you create aliases in bash session via command line, they get removed after the session is closed or the machine rebooted. In order to make aliases permanent on Ubuntu 20.04 or other Linux based distribution with Bash shell follow the underlying instructions:
Please note that ~/.bash_aliases
file only works if the following line presents in the ~/.bashrc
file:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
If above lines missing just append them at the end of ~/.bashrc
file.
- To see a list of default aliases defined for your user execute:
alias
- To keep aliases between sessions, you can save them in your user’s shell configuration profile file. For bash shell it is:
~/.bashrc
file. However, the best practice is to keep your bash shell aliases in a separate file~/.bash_aliases
instead of adding them directly into~/.bashrc
file. - Execute the following command:
vim ~/.bash_aliases
- Add the following content:
# To set permanent bash aliases change this file ~/.bash_aliases alias k='kubectl' alias kc='k config view --minify | grep name' alias kdp='kubectl describe pod' alias c='clear' alias kd='kubectl describe pod' alias ke='kubectl explain' alias kf='kubectl create -f' alias kg='kubectl get pods --show-labels' alias kr='kubectl replace -f' alias ks='kubectl get namespaces' alias l='ls -lrt' alias kga='k get pod --all-namespaces' alias kgaa='kubectl get all --show-labels'
- Save and close the file.
- Activate alias by typing:
source ~/.bash_aliases
- Close the shell session and start a new one. Type one of the specified aliases in the command line to check if they are working.