Sitemap
James Read’s Code, Containers and Cloud blog

Code inside containers that run on the cloud!

3 basic Kubernetes shortcuts for faster command line kung fu

--

Tips and tricks to get faster with the Kubernetes Command Line.

I’ve been using Kubernetes now since the early betas, and over the years I’ve used kubectl a lot. But, developers and sysadmins who do things more than once learn to automate, or at least learn how to get much faster. Here is what I’ve picked up;

Alias kubectl

This is a time-saver that first looks dumb… but I think it adds up to a real time savings. Instead of,kubectl get pods I type, k get pods. I find also my brain has to think that little less about starting the command, and more about what I’m looking for — pods, services, etc.

user@host: alias k=kubectl
user@host: k get pods
...

This will persist until you log out. To save this alias, add it to your .bashrc or .profile file.

Also, protip, alias ka=kubeadm too!

Change namespace — kubens (or kns!)

This is another real time saver if you spend a lot of time jumping between namespaces like I do. I spend most of my time with OpenShift, so I was used to tying oc project foo . Imagine my surprise when the syntax for vanilla kubernetes is;

user@host: kubectl config set-context --current --namespace foo

No way am I typing that all the time, life is too short. Let’s do this instead;

user@host: alias kubens='kubectl config set-context --current --namespace '
user@host: alias kns=kubens

Now, let’s see what that looks like;

user@host: kns olive-tin 
Context "olive-tin/api-ocp-teratan-net:6443/kube:admin" modified.
user@host: kns default
Context "olive-tin/api-ocp-teratan-net:6443/kube:admin" modified.

I picked this one from Diego Mendes on StackOverflow, thank you for many hours of my life back, Diego!

And then I found this fantastic repository; https://github.com/ahmetb/kubectl-aliases . They’re using this same logic for kg (kubectl get), kd (kubectl describe), krm (kubectl delete/rm) and many more.

Learn the shortcuts for resource types

It’s fine if you type k get pods all the time, that is easy enough. But k get configmaps , k get services, k get clusteroperators? There is a faster way — all these resources have a shorter version;

user@host: k get cm # configmaps
user@host: k get svc # services
user@host: k get co # cluster operators (OpenShift)

Summary

I hope those 3 shortcuts work for you, although there are so many others I could have mentioned.

Note: I wrote this article from the top of my head, and then in searching for more tips and tricks to add, I actually found quite a few other similar articles with almost identical tips. Sorry, I wasn’t original, purely by an accident — nevertheless I hope the tips helped you!

--

--

James Read
James Read

Written by James Read

Public Cloud and Open Source advocate. Red Hat Solution Architect during the day. Enthusiastic developer at night :) http://jread.com

Responses (6)