Kubernetes – supercharge your user experience with kubectl command aliases

Kubectl Command Aliases

Disclaimer : No part of this post / article has been generated using ChatGPT or any other generative AI tool.

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

ObjectiveShort commandCommand alias
List pods in a namespacelistpodsalias listpods=’function _listpods(){ kubectl1 get pods; };_listpods’
Attach to a pod in order to inspect its contentattachpodalias 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.

3 thoughts on “Kubernetes – supercharge your user experience with kubectl command aliases”

  1. Hey There. I found your blog using msn. This
    is a very well written article. I’ll make sure to bookmark it
    and come back to read more of your useful information. Thanks for the post.
    I’ll definitely return.

    Reply
  2. My cousin suggested this website to me. While I cannot confirm whether this post was written by him, no one else is as knowledgeable as you are regarding my difficulty. Your assistance is greatly appreciated.

    Reply
  3. You’re so awesome! I do not suppose I have read anything like this before.

    So great to discover someone with a few genuine thoughts on this topic.
    Seriously.. many thanks for starting this up. This web
    site is something that is required on the web, someone with a little originality!

    Reply

Leave a Comment