Post Image

Kubernetes Helpful Command Alias's

It wont be very long after you start interacting with a Kubernetes cluster that you realize the commands can be rediculously long. In the example of a microk8s cluster an example is "microk8s kubectl describe deployment nginx" and lets be real 5 words is way too long. To solve this problem you can use kubectl command line remotely and remove the "microk8s" but still were at a 4 word command. The best way I have found is to alias a set of commonly used commands, In this post I share various alias's that increase the speed in which I can manage my cluster.

 

What is an Alias

An alias is just a word that points to a separate command on a system. For instance on a linux system, the command "ipconfig" does not exist, so you could create an alias named "ipconfig" that is an alias to the "ifconfig" command on a Linux system so that when you enter "ipconfig" in actually executes "ifconfig".

It is also important to note that any arguments that follow the alias when entering it on the command line will be passed to the command that its an alias for.

In this case we are not trying to make commands we are familiar with appear as if they exist, we are trying to shorten commands which is the same concept.

 

Caution

Before creating any alias on your system make sure a command with the alias name does not already exist. For instance if I aliased the ifconfig command to "cat" every time I tried to cat a file it would show me my interface configuration and we do not want that. 

The easiest way to see if a command exists is to execute it on the command line and see what error it returns, if it returns "Command not found" or something to that effect you are safe to alias it at this time. However keep in mind if you install something in the future that conflicts with an existing alias you will run into some confusion.

 

Alias kubectl

I find it useful to alias the kubectl command to 'kc' to reduce the number of characters I need to type. In this example I alias on a microk8s cluster node, if you just are aliasing the kubectl command line you can omit "microk8s".

alias kc='microk8s kubectl'

 

Alias kubectl get 

alias get='microk8s kubectl get'

Usage

$ get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-778d9549bb-6s6cl   1/1     Running   0          25m
$

 

Alias kubectl describe

alias describe='microk8s kubectl describe'

Usage

$ describe pod nginx-778d9549bb-6s6cl
Name:             nginx-778d9549bb-6s6cl
Namespace:        default
Priority:         0
Service Account:  default
~ Output Truncated ~
$

 

Alias kubectl apply

alias apply='microk8s kubectl apply'

Usage

$ apply -f mydeployment.yaml

 

Alias kubectl scale

alias scale='microk8s kubectl scale'

Usage

$ scale deployment nginx --replicas=10

 

 

Maintain Persistence with Alias's

You will quickly find that issuing the command 'alias' to make an alias for a command does not persist after you log out. In order to maintain this for each login on a linux system in your current users home directory you can make a .bash_profile file and put your alias commands in there. Every time you launch a bash shell it will export those alias's and you can use them right away. Below is the contents of my .bash_profile that you can copy and paste if you wish.

# ~/.bash_profile
alias kc='microk8s kubectl'
alias get='microk8s kubectl get'
alias describe='microk8s kubectl describe'
alias apply='microk8s kubectl apply'
alias scale='microk8s kubectl scale'

 

 

 



Comments (0)
Leave a Comment