VMware : Tips of the week #4
We want to share with you some concepts, helpful commands, and gotchas that we have come across during the first few days within the Kubernetes world.
Kubernetes uses a command-line tool called kubectl for running commands against clusters, you may find yourself frustrated by the time spent typing repetitive commands.
This article will highlight tips used to simplify the usage of kubectl and save you time.
Shell aliases can be used to shorten all shell commands and not just those related to kubectl. Begin by creating a file in your $HOME directory specific for kubectl aliases, and add all helpful aliases for kubectl such as :
Here top 10 shell aliases for 𝐤𝐮𝐛𝐞𝐜𝐭𝐥:
- alias k=’kubectl’
- alias kgns=’kubectl get namespaces’
- alias kgc=’kubectl config get-contexts’
- alias kuc=’kubectl config use-context’
- alias kg=’kubectl get’
- alias kgn=’kubectl get nodes’
- alias kgp=’kubectl get pod’
- alias kgpall=”kubectl get pods –all-namespaces”
- alias krm=”kubectl delete’
- alias kgsvc=’kubectl get service’
And then edit your .bashrc file with the following:
1 |
<span style="font-family: poppins; font-size: 16px;"><code>$ <samp>[ -f ~/.kubectl_alias ] && source ~/.kube_alias</samp></code></span> |
If you need to list all your aliases just run:
1 |
<span style="font-family: poppins; font-size: 16px;"><code>$ alias</code></span> |
kubectl shell autocompletion is another way to save time. To configure shell autocompletion on centos just run:
1 2 3 |
<span style="font-family: poppins; font-size: 16px;"><code><span class="code-line">$ yum install -y bash-completion</span> <span class="code-line"> $ source <span class="token tag"><span class="token punctuation"><</span>(kubectl <span class="token attr-name">completion</span> <span class="token attr-name">bash)</span> </span> <span class="token attr-name"> $</span> <span class="token attr-name">echo</span> <span class="token attr-name">"source</span> <span class="token attr-name"><(kubectl</span> <span class="token attr-name">completion</span> <span class="token attr-name">bash)"</span> <span class="token punctuation">></span>> ~/.bashrc</span></code><i class="fa fa-clone t-copy"></i></span> |
If you get a message with the error below:
1 2 |
<span style="font-family: poppins; font-size: 16px;"><code>$ kubectl bash: <span style="color: #ff0000;"><strong>_get_comp_words_by_ref: command not found </strong></span></code><i class="fa fa-clone t-copy"></i></span> |
You can fix this with these commands:
1 2 |
<span style="font-size: 16px;"><code><span style="font-family: poppins;"><span class="code-line">$ source /etc/profile.d/bash_completion.sh</span> </span><span class="code-line"><span style="font-family: poppins;"> $ s</span>ource <(kubectl completion bash)*</span></code></span> |
If you are not a CLI champion, one of the tool you can use is Octant from VMware.
Octant is a web interface for Kubernetes that gives you the visibility of your Kubernetes cluster and applications running on it.
Enjoy!
Maybe someone will appreciate even more aliases and get new ideas: https://github.com/Aracki/configs/blob/master/.aliases#L47-L87