Skip to content
Home ยป Top 5 Practical Kubectl Aliases To Help Master Kubernetes

Top 5 Practical Kubectl Aliases To Help Master Kubernetes

If you’re working with a Kubernetes cluster then there is a good chance that you’re using kubectl, Kubernetes’ native command line tool. Kubectl is used for communicating with a Kubernetes cluster’s control plane via the Kubernetes API.

Kubectl is a standard when working with a Kubernetes cluster. This handy command line tool can be used to deploy applications to your Kubernetes cluster, inspect and manage cluster resources, and view container logs. It’s an extremely useful tool that is often considered a requirement when managing a Kubernetes cluster.

Here is a list of all kubectl operations. Most of these operations are frequently used when working in a Kubernetes cluster, but take a good look at the syntax for each of them. Some of these operations require commands that are just too long, and that’s just at face value! A good chunk of the common commands have a sub-command to make things even longer. Executing these operations multiple times a day can be time consuming.

That’s where kubectl aliases come in. Kubectl aliases can help drastically speed up your workflow and simplify Kubernetes cluster management. Ultimately, these aliases enable shorter syntax for frequently used kubectl commands which helps save time and improve efficiency.

Here is a list of the top 5 most simple and practical kubectl aliases to help you master Kubernetes. Keep in mind that these are my personal favourites that help me in my daily workflow.

5. alias "kd" = "kubectl describe"

We’re going to start off with a short but extremely sweet one. I cannot describe to you the amount of times I run the describe kubectl operation on a daily basis. When managing a Kubernetes cluster, you need to be able to pull a detailed state of your Kubernetes resources and the describe operation does exactly that.

Originally the syntax to describe a Kubernetes resource would work like so:

kubectl describe [RESOURCE-TYPE] [RESOURCE-NAME] 

With the handy kd alias, we can simply run the following:

kd [RESOURCE-TYPE] [RESOURCE-NAME]

I know, I know, it’s simple. A lot of folks would argue that this kubectl alias is practical as well. That’s what we’re aiming for people!

4. alias "krr" = "kubectl rollout restart"

Here’s a fun one. The rollout restart kubectl operation is used to start a new rollout process for either a Kubernetes Deployment, DaemonSet, or StatefulSet. A rollout process is performed by Kubernetes to gradually recreate pods from one of the three Kubernetes objects aforementioned. This is done by ensuring that old pods are never removed entirely until the new pods are running successfully. This capability alone is extremely useful when your application cannot afford any downtime and must remain available throughout the restart process.

You’ll have to rollout restart pods for many different reasons. Whether you’re updating some configuration or recovering failed pods, this kubectl operation is a useful and common one.

Originally the syntax to rollout restart a Kubernetes Deployment would work like so:

kubectl rollout restart deployment [DEPLOYMENT-NAME]

With the handy krr alias, we can simply run the following:

krr deployment [DEPLOYMENT-NAME]

You can also opt for an additional letter in the alias to represent the Kubernetes Object you’re working with. For example, instead of the kubectl alias krr, you can use the kubectl alias krrs to equal kubectl rollout restart StatefulSet.

3. alias "kw" = "watch kubectl get pod -o wide"

Another simple and practical kubectl alias that I use on a daily basis is kw. This kubectl alias relies on another command line tool called watch. Watch simply executes a program periodically and displays it fullscreen on your terminal.

In case you don’t have it installed already, here is how you can install watch on your macOS via Homebrew:

brew install watch

As you could probably guess, this kubectl alias will run the kubectl get pod -o wide command every 3 seconds by default. This kubectl alias is priceless when monitoring the state of your pods during an upgrade or a rollout.

Originally the syntax to do this would work like so:

watch kubectl get pods -o wide

With the awesome kw alias .. well .. you get the idea:

kw

2. alias "ks" = "kubectl get secret -o yaml"

This kubectl alias is one of my favourites. Whenever I share this kubectl alias with a colleague it’s always greeted with praise.

A Kubernetes Secret is an object that contains a small amount of sensitive data such as a password, a token, or a key. Because Kubernetes Secrets can be created independently of the pods that use them, there is less risk of the Secret (and its data) being exposed during the workflow of creating, viewing, and editing pods.

A big benefit of using a Kubernetes Secret is that you don’t need to include confidential data in your application code. The downside of this is that if you don’t have the confidential data stored anywhere else, you’ll often have to use the Kubernetes Secret as a source of truth.

kubectl get secret -o yaml allows you to display the contents of a Kubernetes Secret encoded in base64 outputted in yaml. Originally the syntax to do this would work like so:

kubectl get secret -o yaml [SECRET-NAME]

With the simple ks alias, we can run the following:

ks [SECRET-NAME]

This will then output the Kubernetes Secret in yaml that you can optionally decode to validate its value.

1. alias kx = "kubectl exec -it"

When working in a Kubernetes cluster, you will often have to get a shell to a running container that lives in a pod to better manage an application. If you’re trying to debug an issue, it is possible to save considerable time by jumping into a pod. Connecting to a running container allows you to view logs, verify processes, mount points, environment variables and package versions, among other things.

Kubectl allows use to connect to a running container with a single command, but considering that this command is so commonly ran, it deserves to be shorter.

The alias kx to represent kubectl exec -it is extremely handy. When connecting to a container, originally the syntax would work like so:

kubectl exec -it [POD-NAME] -- /bin/bash

Where exec is used to execute a command against a container in a pod, the flags -it allow us to pass stdin to the container as a TTY, and the command /bin/bash is the command we are executing against a container in a pod to acquire our shell.

Instead of writing this out every time we need to connect to a running container, we can simply run the following:

kx [POD-NAME] -- /bin/bash

Considering that I use this kubectl alias countless times a day, it’s safe to say that this one is my favourite.

Wrapping up

In this post we listed the top 5 best kubectl aliases to help you master Kubernetes. These kubectl aliases are my top 5 personal favourites that help me improve my daily workflow when managing multiple Kubernetes clusters.

Thank you for reading and I really hope you learned something new! If you have any questions, suggestions, or would like to share some of your own kubectl aliases, please add them in the comments section below.

Interesting in learning more about Kubernetes? Check out some of my other articles on the topic here!

Leave a Reply

Your email address will not be published. Required fields are marked *