High productivity working with Kubernetes

Install the command-line tool kubectl is the first thing we do when we start working with Kubernetes. Also, as I’m a little lazy I start to set up some aliases on my terminal to make my life easier.

Ahmet Alp Balkan, a software engineer from Google created this script whereas you can find hundreds of aliases

… where it will work like:

kdkubectl describe

kgdepallwkubectl get deployment —all-namespaces —watch

You can find more information on his blog here.

I believe it was a really clever idea, but I would say the most useful and simple alias is the “k”, which is for the “long” command kubectl itself.

Multi-tenancy

Working with multiple clusters.

Through the Kubernetes configuration file, you can set multiple clusters to easily work within the same terminal.

However, you would need to keep an eye on the current cluster to avoid run commands for the wrong server with

kubectl config current-context 

…and change it with

kubectl config use-context <cluster_name>

Working with namespaces

Kubernetes has a multi-tenancy feature, aka namespaces, that provides isolation and also fair resource sharing between multiple users, teams or projects within the same cluster.

You can permanently save the namespace for all subsequent kubectl commands in that context with the command below…

kubectl config set-context $(kubectl config current-context) --namespace=<insert-namespace-name-here>

… however, I believe those are information you will need constantly…

Keeping everything in the sight

One useful tool to keep the cluster name and the namespace in the sight is to use kube-ps1

Quite smart, isn’t it? You can find other layouts using the zsh terminal as well.

Another clever tool you can use is the Kubens and Kubectx where it will be easy to change clusters and namespaces with no hassle.

Logs

It is really easy to read container logs with the command kubectl logs <pod_name>, however, if you scale your application you will need to aggregate those logs in a fancy way, without running the command for each individual pod.

The most famous tool to reach these goals are: Stern and Kubetail

Both are a really nice tool. Using them you will be able to stream the logs using different colors. However, when you need to find a specific log message, and if you are not using grep to filter the log, this can be a bit cumbersome. As I didn’t find any other clever solution online I decided to create my own script where it will search all containers with a specific name, and open them in my preferable IDE

https://github.com/femrtnz/kube-scripts/blob/master/kubelogs.sh

Reading containers

The kubectl gives you the ability to retrieve the pod configuration, states, events, etc.. and you can also set the output type to JSON. Kubectl also gives you a jsonpath support out of the box:

$ kubectl get pods my-app -o jsonpath="Name: {.metadata.name} Status: {.status.phase}"
$ kubectl get pods -o json
$
kubectl get pods -o jsonpath='{.items[0].metadata.name}'
$ kubectl get pods -o jsonpath='{.items...metadata.name}'
$ kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'

Kubectl has built-in support for golang templates as well:

$ kubectl get no -o go-template='{{range .items}}{{if .spec.unschedulable}}{{.metadata.name}} {{.spec.externalID}}{{"\n"}}{{end}}{{end}}'$ kubectl get no -o go-template="{{range .items}}{{if .spec.unschedulable}}{{.metadata.name}} {{.spec.externalID}}:{{end}}{{end}}" | tr ":" "\n"

But honestly, I’ve found that the easiest way to explore any JSON output is using jq and jid:

$ kubectl get no -o json | jq -r '[.items[] | {name:.metadata.name, id:.spec.externalID, unschedulable:.spec.unschedulable}]'$ kubectl get no -o json | jq -r '.items[] | select(.spec.unschedulable!=true) | [.metadata.name,.spec.externalID]

You can find some great search ideas using those json tools here.

What about you? Did you find another useful tool that can improve your productivity when using kubernetes? Leave a comment!

--

--

--

Brazilian Software Engineer living in London. Java addicted. Clean Code and Software craftsmanship Enthusiast. Cloud. https://www.linkedin.com/in/femrtnz/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Precise Method and Constant Invalidation in TruffleRuby

Inter-Integrated Circuits — I2C Basics

Less Stressful Development

How to Reset Zigo Eon 73i

reset my phone

KubeEdge: Monitoring Edge Devices at the World’s Longest Sea Bridge

Building a supportive automation ecosystem with Splunk & Slack

Life and Death of an Object

Why the Digital Society needs the Open Society

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Felipe Martinez

Felipe Martinez

Brazilian Software Engineer living in London. Java addicted. Clean Code and Software craftsmanship Enthusiast. Cloud. https://www.linkedin.com/in/femrtnz/

More from Medium

TIBCO BW and EMS Integration

Understanding the Kubernetes cluster components.

Why is Kubernetes getting so popular? Do we really need Kubernetes?

Kubernetes Networking Basics