Kubernetes Kubectl Alias Commands CheatSheet|Aliasing kubectl

Posted April 26, 2024
Kubernetes Kubectl Alias Commands CheatSheet|Aliasing kubectl

This guide provides cheat sheet tips for creating Kubernetes Kubectl Alias Commands. Aliases create shortcuts for commands This way, you let Kubectl use shorter names for frequently used commands.

Related: Install and Run Kubectl on Linux|Ubuntu 20.04|22.04|18.04

This cheat sheet will create a quick reference for Aliasing common kubectl commands for productivity. Ready? Dive into this cheat sheet for aliasing kubectl commands.

Creating Kubectl Alias Commands CheatSheet

Take examples of common Kubernetes kubectl commands such as get pods, kubectl describe, and kubectl get deployments. Now to create aliasing for these commands you will need to run an Alias command pointing to your command for example kubectl get pods command will be aliased as kp. You command to Alias this command will be:

alias kp='kubectl get pods'

Kubernetes Kubectl Alias Commands CheatSheet|Aliasing kubectl

Aliasing Common Kubernetes Kubectl Commands with Terminal

Now that you know to alias these kubectl commands, the following list shows the common command and the associated Aliases.

  • The Kubectl Apply command can be aliased as ka. The fill command as an alias will be:
alias ka='kubectl apply -f'

Note: I will add this alias as I can. You can adjust these aliases according to your preferences and workflows.

  • CheatSheet to aliasing Kubectl Namespaces commands:
alias kns='kubectl get namespaces'
alias kdns='kubectl describe namespace'
  • Using Alias with Kubectl deployments and services commands:
# Deployments
alias kd='kubectl get deployments'
alias kdd='kubectl describe deployment'

# Services
alias ks='kubectl get services'
alias kds='kubectl describe service'
  • Aliasing Kubectl pod commands:
alias kp='kubectl get pods'
alias kpd='kubectl describe pod'
alias kl='kubectl logs'
  • How to alias your Nodes, Configurations and Contexts
# nodes
alias kn='kubectl get nodes'
alias kdn='kubectl describe node'
# contexts
alias kcfg='kubectl config get-contexts'
alias kctx='kubectl config use context
alias kcc='kubectl config current-context'
alias ksc='kubectl config set-context'

Once you understand how these aliases are added, you can even add more Kubectl aliases to Pod Exec, Port Forwarding, or a command of your choice:

alias kexec='kubectl exec -it'
alias kexec='kubectl exec -it'

Related: Install Single Node K3s on Ubuntu 20.04|22.04 Step-by-Step

Setting up Kubectl Aliases with Shell Configuration File

Another to add these aliases is to use your shell configuration file. For example  

  • On Bash, open ~/.bashrc or ~/.bash_profile in a text editor and paste the alias commands there.
  • Save the file and reload your shell.
  • Run source ~/.bashrc to apply the changes.

Make sure you determine which shell configuration file your shell uses.

  • Bash: ~/.bashrc or ~/.bash_profile
  • Zsh: ~/.zshrc
  • Fish: ~/.config/fish/config.fish

Now open the shell configuration file:

nano ~/.bashrc

Next, add alias commands for kubectl commands to the configuration file:

alias kp='kubectl get pods'
alias kpd='kubectl describe pod'
alias kl='kubectl logs'
alias kd='kubectl get deployments'
alias kdd='kubectl describe deployment'
alias ks='kubectl get services'
alias kds='kubectl describe service'
alias kn='kubectl get nodes'
alias kdn='kubectl describe node'
alias kns='kubectl get namespaces'
alias kdns='kubectl describe namespace'
alias kcfg='kubectl config get-contexts'
alias kctx='kubectl config use-context'
alias kcc='kubectl config current-context'
alias ksc='kubectl config set-context'
alias kexec='kubectl exec -it'
alias kpf='kubectl port-forward'
alias ka='kubectl apply -f'

Finally, Save and Exit. Remember to Reload your shell to apply the changes made:

# Bash
source ~/.bashrc
# Zsh
source ~/.zshrc
# Fish
source ~/.config/fish/config.fish

How to use the Added Kubectl Aliases

Once the Alias are ready, you only need to type the alias in your terminal. For example, you have:

alias kp='kubectl get pods'

This command gets all pods in your default namespace. This way you only need to run the alias:

kp

Kubernetes Kubectl Alias Commands CheatSheet|Aliasing kubectl

Also, you can add the Parameters you want to pass to the kubectl command on the alias. Let’s say you have another namespace, your Kubectl alias command can be:

kp -n <namespace>
## or kubectl describe deployment
kdd <deployment-name>

Check another example here kns alias to run kubectl get namespaces:

Kubernetes Kubectl Alias Commands CheatSheet|Aliasing kubectl

Here is an expanded list of how to use the Kubectl alias you have created:

kp
kpd <pod-name>
kl <pod-name>
kd
kdd <deployment-name>
ks
kds <service-name>
kn
kdn <node-name>
kns
kdns <namespace-name>
kcfg
kctx <context-name>
kcc
ksc <context-name>
kexec <pod-name> <command>
kpf <pod-name> <local-port>

Conclusion

You now have a successfully set up aliases for your kubectl commands. I hope you can improve your Kubernetes productivity.

Kubernetes Kubectl Alias Commands CheatSheet|Aliasing kubectl

Written By:

Joseph Chege