Kubernetes Cost Optimization with Kubecost

Get insights into Kubernetes and optimize costs

Anjali Gour
Globant
4 min readNov 14, 2022

--

In this microservices-based architecture era, where you run multiple Kubernetes clusters at scale, monitoring and allocating costs becomes a tough assignment. Using Kubecost, you can benefit from greater visibility into the current and historical Kubernetes spending and resource allocation and experience cost transparency in Kubernetes environments that support multiple applications, teams, departments, and more. Kubecost is a vendor-agnostic tool for real-time cost monitoring and optimization and provides insights into Kubernetes. Kubecost can help us save billions of dollars on Kubernetes spending.

In this article, we will discuss the key features of Kubecost and study its implementation.

Key Features of Kubecost

The following are the essential features of Kubecost.

  • Cost Allocation
    Kubecost allocates cost to all native Kubernetes concepts like namespace, deployment, service, etc., and allows customization based on organizational concepts like a team, individual application, product/project, department, etc.
  • Unified cost monitoring
    It shows in-cluster costs like CPU and memory with out-of-cluster spending from cloud infrastructure services like S3 buckets, RDS instances, etc.,(supports all services from AWS, GCP, and Azure) for complete cost visibility.
  • Optimization Insights
    Kubecost generates insights to save on the infrastructure spent. This customized recommendation is based on our environment and behavior patterns.
  • Alerts & governance (paid feature)
    We can set up customized real-time alerts (via Slack or email) and generate reports.

Apart from this unified multi-cluster view, saving reports, SSO/SAML login, custom metrics retention, business or enterprise support, etc., are some additional features in the paid versions.

Kubecost comes in 3 pricing models — free, business, and Enterprise. We will be using a free pricing model. Remember, the installation steps remain the same for different pricing models.

Let´s now discuss implementing and setting up the Kubecost tool for a Kubernetes cluster.

Kubecost Implementation

The prerequisites for Kubecost implementation are a Kubernetes Cluster (version 1.8+) and the Helm client (version 3.0+). If you already have that, we can look at how we can configure Kubecost from scratch for a Kubernetes cluster.

  1. Add the Kubecost helm repository.
helm repo add kubecost https://kubecost.github.io/cost-analyzer/

Verify that the helm repository has been added with the `helm repo list` command.

2. Install Kubecost.

 helm upgrade — install kubecost kubecost/cost-analyzer — namespace kubecost — create-namespace

3. Verify if the kubecost/cost-analyzer pod is running in the kubecost namespace.

kubectl get pods -n kubecost

4. Once the pods are running, map port 9090 of the pod to any available port on your machine. (e.g., we are using 9090)

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

5. Access the Kubecost dashboard in a web browser at the http://localhost:9090 URL.

The below dashboards show cost allocation metrics that include CPU, GPU, Memory, etc., consumption for a cluster, and namespaces-wise.

This will install Kubecost in the current cluster. In the dashboard, you can see metrics related to Kubernetes, such as Cost Allocation, Cluster Efficiency, etc., and take actions based on them. Also, you can refer to the official Kubecost documentation available here for further implementation.

Summary

With this, you will have a Kubecost pod running in your cluster, which can help you to look at the cost allocation trends and search for cost savings and reliability improvements.

Opt for paid versions if you are looking for additional features such as alerts and governance, unified multi-cluster view, saving reports, SSO/SAML login, etc.

References

https://www.kubecost.com/

https://guide.kubecost.com/hc/en-us

--

--