Optimizing K8s cluster costs with Kubecost

Optimizing K8s cluster costs with Kubecost

I will always remember the first time I started playing around with Kubernetes on Amazon EKS as a personal hobby and looked at my costs a month later. This was around when I was first dipping my feet into the waters of the DevOps tools ecosystem and container orchestration with Kubernetes. I had no idea that I would look at my billing statement and see a $1300 charge just sitting there leftover from forgetting to shut down a cluster.

Safe to say that I've grown quite a bit since then and I'm quite a bit more anal about shutting down unused resources, but that experience made me realize just how important cost estimation is for projects. When you venture into enterprise-level scale, there is a lot more at stake than a bit of personal money to be lost. Cost estimation is extremely important when managing stakeholder expectations and guidelines.

With that in mind, I'd like to introduce you to an interesting new tool I found called Kubecost.  

What is Kubecost?

Kubecost is a cost-optimization and estimation tool that gives you real-time visibility into how resources are being spent across your clusters in seconds, so you can take action based on current information. With it, you can easily see the cost of resources being consumed by individual applications and teams.

It also allows you to get an overhead view of your infrastructure by assigning out of cluster cloud assets (e.g. databases and storage buckets) to teams, products, services, and other native k8s concepts in order to measure the full cost of your cloud operations.

Using tools like Kubecost allows you to maintain a continuous watch over your spending and even set up notification via Slack and other channels to quickly catch cost overruns and infrastructure outage risks before they become a problem.

Let's try it out

We are going to run through a quick demonstration of how to get Kubecost installed in your cluster to begin monitoring your costs.

The easiest way to install Kubecost into your cluster is via the Helm install on the Kubecost website.

Prerequisites

  • A helm client installed (We are using Helm 3)
  • For clusters with RBAC enabled, run the following commands to grant necessary permissions:
kubectl create clusterrolebinding cluster-admin-binding --clusterrole=cluster-admin --user=<your-userid> 
kubectl create clusterrolebinding cluster-self-admin-binding --clusterrole=cluster-admin --serviceaccount=kube-system:default

Step 1: Install Kubecost

Running the following commands will also install Prometheus, Grafana, and kube-state-metrics in the namespace supplied. View install configuration options here.

Helm 2

helm repo add kubecost https://kubecost.github.io/cost-analyzer/ 
helm install kubecost/cost-analyzer --namespace kubecost --name kubecost --set kubecostToken="amRib2hybWFuQG91dGxvb2suY29txm343yadf98"

Helm 3

kubectl create namespace kubecost 
helm repo add kubecost https://kubecost.github.io/cost-analyzer/ 
helm install kubecost kubecost/cost-analyzer --namespace kubecost --set kubecostToken="amRib2hybWFuQG91dGxvb2suY29txm343yadf98"

Step 2. Enable Port Forward

kubectl port-forward --namespace kubecost deployment/kubecost-cost-analyzer 9090

Step 3. See the data!

You can now view the deployed frontend by visiting the following link. Publish :9090 as a secure endpoint on your cluster to remove the need to port forward.

http://localhost:9090

You can see my implementation here, which has a Agones game server running as well as Telepresence:

Next Steps

Kubecost has an open core model and its commercial product is built on top of the open-source project available on Github. Therefore, you're free to play around with it as much as you like before spinning up the enterprise version in production. From here, I would start playing around with Kubecost in your own environment and try setting up notifications and cost optimizations. I hope you enjoyed this article and stay tuned for more!