Kube Task of the Day, Aliases


Userlevel 7
Badge +22

To make our life in the command line easier we can setup aliases. So instead of typing kubectl all the time we could simply change that to k.

Working in windows you need to install the Windows Subsystem for Linux so that you can take advantage of the immense power that the linux command line provides. On Windows 11 this is very simple. Open a cmd box and type 

wsl --install -d Ubuntu

Now you could choose a different distribution like debian etc but ubuntu seems to be the most popular. 

 

You will need to reboot then set a password but after that you will be all set.

 

In your cmd or windows terminal type 

bash

and you will be greeted by the your bash shell

 

Now type cd 

cd

so that you end up in your user directory.

First of all lets make sure autocompletion is enabled for kubectl, type the following in the bash command line:

echo "source <(kubectl completion bash)" >> ~/.bashrc

Next we are going to leverage vim to edit our .bashrc file. Vim is an enhance text editor derived from vi and is well worth learning since it is installed by default on every *nix type of system. 

vi .bashrc

here will will go to the end of the file by pressing the esc key then shfit and g then to start editing type shift A and press return to start on a new line. Copy or type the following:

alias kubectl='kubectl.exe'
alias helm='helm.exe'
alias k=kubectl
complete -o default -F __start_kubectl k

then press the esc key and type : then w q (which writes to the file and exists).

 

Now you can either type exit then bash again to re-enter the bash shell or type 

exec bash

and the changes will be implemented.

 

Now type 

 

k get nodes

 

and you should see something like this:

 

 

That is it for our short task today.


9 comments

Userlevel 7
Badge +20

First one small correction - the :we for editing the file should be :wq --- write then quit.

Also when running this I get the following after editing the file trying to execute bash then the other command -

chris@SAGER-LAPTOP:~$ exec bash
kubectl: command not found
chris@SAGER-LAPTOP:~$ k get nodes
kubectl: command not found
chris@SAGER-LAPTOP:~$


So something is either off with me or the details.  Let me know. 😋😂

Userlevel 7
Badge +20

And yes I unpaused Minikube as well from the last instructions where we paused it. 😉

Userlevel 7
Badge +22

First one small correction - the :we for editing the file should be :wq --- write then quit.

Also when running this I get the following after editing the file trying to execute bash then the other command -

chris@SAGER-LAPTOP:~$ exec bash
kubectl: command not found
chris@SAGER-LAPTOP:~$ k get nodes
kubectl: command not found
chris@SAGER-LAPTOP:~$


So something is either off with me or the details.  Let me know. 😋😂

Thanks, early morning typo :) e should be q :)

I think I know what it is.. we need to add the alias for the windows exe files. Good catch Chris, I will add this now to the article.

Userlevel 7
Badge +22

So try now with this additional alias

alias kubectl='kubectl.exe'

Userlevel 7
Badge +20

Seems to work now but the - exec bash still does not ---

chris@SAGER-LAPTOP:~$ exec bash
kubectl: command not found
chris@SAGER-LAPTOP:~$ k get nodes
NAME       STATUS   ROLES           AGE   VERSION
minikube   Ready    control-plane   3d    v1.24.3

Userlevel 7
Badge +14

@Chris.Childerhose I think I’ve found the problem. You need to define the kubectl alias before the “source <(kubectl completion bash)”.

Here’s how the alias is created in Powershell:

new-alias -name k -Value kubectl

k get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane 8m18s v1.24.3
Userlevel 7
Badge +20

@Chris.ChilderhoseI think I’ve found the problem. You need to define the kubectl alias before the “source <(kubectl completion bash)”.

Here’s how the alias is created in Powershell:

new-alias -name k -Value kubectl

k get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane 8m18s v1.24.3

Yeah we got that figured out if you look at Geoff’s update to his post.  So all good but thanks though. 😁

Userlevel 7
Badge +14

I was referring to your last post with this error:

Seems to work now but the - exec bash still does not ---

chris@SAGER-LAPTOP:~$ exec bash
kubectl: command not found

If you add the aliases before the other command, then everything works.

Userlevel 7
Badge +20

I was referring to your last post with this error:

Seems to work now but the - exec bash still does not ---

chris@SAGER-LAPTOP:~$ exec bash
kubectl: command not found

If you add the aliases before the other command, then everything works.

Ah ok.  Let me give that a try. 👍🏼

Comment