Optimizing Kubernetes Cost: A Guide to Setting Up Kubecost on Minikube

Optimizing Kubernetes Cost: A Guide to Setting Up Kubecost on Minikube

Kubecost is a powerful cost allocation and optimization tool for #Kubernetes clusters that allows you to gain insights into your cluster’s resource usage and cost allocation. In this blog post, we will walk you through the process of setting up Kubecost on your Minikube cluster.

Prerequisites:

Before you begin, make sure you have the following prerequisites in place:

  • A #Kubernetes cluster: You can use any cloud-based cluster like EKS or GKE, or you can set up a local cluster using Minikube.

  • Kubectl: Ensure that you have kubectl installed and configured for your cluster.

  • Helm: Make sure you have Helm 3.0+ installed and configured.

Installation Steps

Follow the steps below to install Kubecost on your Minikube cluster:

Step 1: Start Minikube

First, start your Minikube cluster using the desired driver. In this example, we are using the Docker driver to start Minikube. You can use the following command to start Minikube:

minikube start --driver=docker

Step 2: Create a Namespace and Install Kubecost

Next, create a #Kubernetes namespace for Kubecost and install Prometheus, Grafana, and kube-state-metrics in that namespace. You can use Helm to install Kubecost with the following commands:

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

Note: Replace <kubecostToken> with your actual Kubecost token. If you don't have a Kubecost token, you can sign up for a free trial on the Kubecost website.

You can further customize the installation by providing additional configuration options as described in the Kubecost documentation.

Step 3: Access Kubecost Dashboard

After the installation is complete, you can access the Kubecost dashboard by port-forwarding the Kubecost service to your local machine. Use the following command:

kubectl -n kubecost port-forward svc/kubecost-cost-analyzer 9090

Then, open a web browser and go to http://localhost:9090 to access the Kubecost dashboard.

kubecost dashboard -

The Cost Allocation page shows you a breakdown of your expenses. By default, the breakdown is grouped by namespaces as shown below -

You can use the Aggregate By dropdown to see the breakdown by various #Kubernetes concepts like clusters, containers, deployments,and pods.

These aggregations are based on #Kubernetes labels referenced at the pod or namespace level as shown below -

Click on the assets to check the Date Range dropdown, you can change the date range of the report. You can use some common ranges like Last 7 days, Today, Yesterday, and Last 30 days, or you can enter a custom start and end date.

Once the changes in made save and load the webpage to view the changes -

The Savings page lists some recommended actions that can be taken to save some cost. These actions include Estimated Monthly savings available,Right-size your cluster node, Right-size your container requests, Remedy adandoned workloads etc,. . Click on each recommendation to learn more about them.

On the Health page, you can see the health score of your cluster, which is an assessment of infrastructure reliability and performance. Kubecost performs a few health tests like monitoring for high CPU and memory utilization. checking for crash looping pods, looking for failing jobs, network issues, and CPU throttling. These health checks ensure your cluster is running smoothly.

Set up Slack,Teams or email alerts by visiting the Alertspage.You can configure the alerts by adding a webhook url .

You can set a daily threshold at the cluster level or for individual namespaces and get alerts when the cost exceeds the threshold. Optionally, set it up to send you weekly updates and cluster health updates

Conclusion

Congratulations! You have successfully set up Kubecost on your Minikube cluster. Now you can use Kubecost to gain insights into your cluster’s resource usage and cost allocation, and optimize your #Kubernetes deployments for cost efficiency. Happy cost monitoring!