Better Programming

Advice for programmers.

Follow publication

Useful Kubectl Aliases That Will Speed Up Your Coding

Maroun Maroun
Better Programming
Published in
3 min readOct 6, 2020

Info on kubectl with the Kubernetes logo.
Photo by the author.

If you’re using the command line to interact with your Kubernetes cluster, you’re probably familiar with the kubectl command.

There are many cheatsheets available for kubectl that attempt to cover all of its available commands. While it’s very powerful, this command can be a bit cumbersome for some of the operations that you use on a daily basis. For example, the following command list contains only a small portion of the available commands of kubectl:

Source: IntelliPaat blog

In this article, I’ll share with you some of the aliases I’ve created that have significantly sped up my interaction with K8s.

Note: I’m using the fish shell, but the aliases below can be easily modified to work with any other shell.

Open your config.fish using your favorite vi editor and let the fun begin.

K8s Contexts

If you’re working with K8s, you probably have multiple contexts that you switch between during your day. Having the following abbreviations will make your life a lot easier:

Connecting to Your Favorite Pod

Let’s face it, you have a favorite pod and you occasionally get its shell and connect to it. However, the pod’s ID will be changed if you’re managing it through a K8s deployment. To avoid having to list all pods (kubectl get pods) and then exec to the desired pod, you can use:

Deleting a Pod

Sad, but it happens:

Monitoring Pods

Displaying Deployed Image

One of the commands I use the most. Sometimes, you want to know which image is currently deployed in your cluster. Easy!

Note: You can pass an argument for the desired deployment as follows:

current_image my-deployment-2

Port-Forwarding a Pod

Let’s assume you have a pod running RMQ. To port-forward it:

Flush a DB

A useful command I use when I’m debugging a dev cluster is clearing a DB. For example, for flushing all Redis keys, you can have something like:

More Aliases

You can have some more generic aliases that you use often. I will share with you some of mine, but you can always add your own:

Summary

Think about the commands you use most. If you find yourself typing too much or having to search for some command, then it’s probably a good candidate to be aliased. It’s also important to give your aliases a chance — you’ll probably forget to use them, but keep visiting your initialization script and try to remember your aliases.

Thanks for reading!

Maroun Maroun
Maroun Maroun

Written by Maroun Maroun

Software engineer | Music geek | Beer lover

Responses (2)

Write a response