Improve your productivity with Kubernetes using aliases

 Background

Kubernetes is the de-facto standard for orchestrating containers. If you are working on a daily basis with Kubernetes, you are most likely using the kubectl to interact with the Kubernetes cluster for performing different operations like listing pods, checking for deployments, viewing logs etc. Working with kubectl using a terminal is the most common approach in such a scenario.  

Earlier I had demonstrated how to use two power tools kubectx and kubens. In this post, we will look at how to give kubectl a huge productivity boost by using programmatically generated aliases. 

A quick word on the alias

If you come from a scripting background with experience in bash or shell scripting or even Powershell scripts, you might be familiar with aliases. These are like shortcuts that we can add to the terminal session to execute a command or piece of code. One of the most commonly used alias while working with kubectl is k. Instead of typing kubectl every time, most people create an alias with k and we can replace/substitute kubectl with k. 

We can persist the aliases across sessions and store them permanently. In the case of bash aliases on Linux or Unix systems, mostly these are stored in the .bashrc profile file under the home directory of the user. The same thing can be done on a Windows environment by creating a function as an alias in a PowerShell profile document. 

Need for kubectl aliases

Now that we understand a bit about aliases, let us see how we can use them with Kubernetes and kubectl to be specific. Kubernetes has various objects like Pods, Deployments, Replicasets, Nodes, Endpoints, PersistentVolume, PersistentVolumeClaims etc. Each of these objects has support for different operations like create, update, delete, edit, get. Along with the different types of operations, there are also different flags that can be used while working with these objects using kubectl like -A or --all for listing objects from all namespaces.

Kubectl also supports displaying information in different formats like JSON or YAML. In order to improve efficiency and productivity while working with kubectl, we can alias the commonly used commands as shown below:

  • alias kg="k get"
  • alias kgpo="kg po" or alias kgpo="kg pods"
  • alias kgno="kg no" or alias kgno="kg nodes"
  • alias kd="k describe"
  • alias kaf="k apply -f"
  • alias kdf="k delete -f"

Note that some Kubernetes objects have short forms like po for pods and no for nodes. We can get the list of resources supported by Kubernetes along with their short names using the following command

kubectl api-resources


The above screenshot shows a partial list of resources, their short names, API versions etc.

If we were to create aliases for each of the resources and different operations supported for that resources, it will be a very time consuming and cumbersome process. This is where a smart guy names Ahmet Alp Balkan came up with a programmatic way to generate aliases for kubectl.

kubectl-aliases

The Github repository contains the list of over 800 aliases that are programmatically generated. It also contains instructions about how to set up these aliases. We need to download the file and source it in our bash profile which makes these aliases available for us to use with the terminal session.

There is a set of conventions followed while naming or generating these aliases. 

  • k = kubectl
    • sys = --namespace kube-system
  • commands
    • g = get
    • d = describe
    • rm = delete
    • a : apply -f
    • ak : apply -k
    • k : kustomize
    • ex : exec -t -t
    • lo : logs -f
  • resources
    • po = pod
    • dep = deployment
    • ing = ingress
    • svc = service
    • cm = configmap
    • sec = secret
    • ns = namespace
    • no = node
  • flags
    • output formats : oyaml, ojson, owide
    • all : -all or --all-namespaces depending on command
    • sl : --show-labels
    • w : -w/--watch
  • value flags : should be at the end
    • n = -n/--namespace
    • f = -f/--filename
    • l = -l/--selector

Using these conventions it is quite convenient to work with kubectl. This reduces typing errors and improves our efficiency greatly. Here are a few examples

  • k get pods --> kgpo
  • k get pods --all-namespaces --> kgpoall
  • k get pods -n kube-system --> ksyspo
  • k get pods -o yaml -> kgpooyaml
  • k get configmaps -n keda --> kgcmn keda

If you are on Windows machine, you need not be left behind. There is a PowerShell version of these aliases. Here is the link to the Github repository created by Shanoor. 

Youtube video

I created a short video demonstrating these in much more detail. Catch all the action in the below Youtube video.

 


Conclusion

We all want to improve our daily ways of working. I am sure you will benefit from this little tip if you are a regular user of Kubectl. Hopefully in your role as a developer or DevOps engineer or SRE role you find this useful. Do let me know in the comments of this post or in the Youtube video if you have any feedback.

Until next time, Code with Passion and Strive for Excellence.

Share:
spacer

No comments:

Post a Comment