Kubernetes – supercharge your user experience with kubectl command aliases

Kubernetes – supercharge your user experience with kubectl command aliases

Typing more is good if you are trying to explain something in detail ( e.g. this post ). However when it comes to getting more with less typing for kubernetes, you will feel great improvements in your efficiency by using aliases for kubernetes commands which otherwise take lot of time to type and quite difficult to remember.

In order to make this aliases available for a linux shell (e.g. bash), you can first need to executing following commands –

vi ~/.bash_aliases

# add one or more aliases to the bottom of this file

source ~/.bash_aliases

Remember you will need to execute last command only if you have added a new alias and you want it to reflect immediately without logging out and logging in. Any alias added to .bash_aliases file will be available automatically during next session without executing the last command.

1. Switching namespace

If you use a kubectl command without -n flag then it will use the default namespace which is defined in the kubeconfig file. You can update default namespace by using following command –

kubectl config set-context --current --namespace=<target-namespace>

However memorizing such a long command is not something which everyone is comfortable with unless you have a sharp memory like Viktor Farcic ( a devops legend ). Other option is to use -n flag with every kubectl command which supports this flag. A good user experience with be to switch namespace with a simple command as following –

setns <namespace>

you can achieve this by creating following alias –

alias setns='function _setns(){ export K8_NAMESPACE=$1;  };_setns'

This command will define an environment variable K8_NAMESPACE whose value will be set to the first argument to setns command.

Now you need to define another alias so that one can execute kubectl commands against a target namespace without using -n flag every time. This can be achieved by creating following alias –

alias kubectl1='function _kubectl1(){ kubectl -n $K8_NAMESPACE $@;  };_kubectl1'

Once these two aliases are in place, you can execute multiple commands against a namespace without either using a long command to switch the namespace or using -n flag. E.g. you can list all the pods in a namespace using following command –

kubectl1 get pods

2. Tailing pod logs

This is the most common command which a developer uses while developing/troubleshooting a service in an environment. In order to tail the logs of a pod, we first get the name of the pod by executing kubectl get pods command and then execute kubectl logs -f <pod-name> to tail the logs of a pod. Executing these two commands for every pod, when you want to check the logs of multiple pods, is quite time consuming. A better experience will be to use a simple command as following –

tailpodlogs <deployment-name>

This can be achieved by creating following alias –

alias tailpodlogs='function _tailpodlogs(){ kubectl1 logs -f `kubectl1 get pods | grep $1 |head -n1 | cut -d " " -f1` --tail=$2; };_tailpodlogs'

The downside of this approach is that it will only work when both replica and containers counts in a deployment is 1 but then this is the case most of the times. So it should not be a huge problem.

3. Few more command aliases


Short command

Command alias

List pods in a namespace


alias listpods=’function _listpods(){ kubectl1 get pods; };_listpods’

Attach to a pod in order to inspect its content


alias attachpod=’function _attachpod(){ kubectl1 exec -it kubectl1 get pods | grep $1 |head -n1 | cut -d ” ” -f1 sh; };_attachpod’

This post highlights how creating aliases for kubernetes commands can help you to improve your experience with kubernetes significantly, achieve more with (very) less typing and remember the commands easily. Enjoy your time working on kubernetes !! I will cover some more aliases in details in my future posts.

Did you find this article valuable?

Support MechCloud by becoming a sponsor. Any amount is appreciated!