DEV Community

Prithiviraj R
Prithiviraj R

Posted on

Optimize Kubernetes Costs in EKS with KubeCost

In the modern cloud-native landscape, cost optimization is crucial to managing Kubernetes
workloads effectively. Kubecost is a powerful tool that provides real-time visibility into Kubernetes
cost management, enabling organizations to monitor, optimize, and reduce their cloud spending.
This blog will explore Kubecost in detail, covering its features, benefits, installation process, and best practices.
What is Kubecost?
Kubecost is an open-source cost monitoring and optimization tool for Kubernetes. It helps
organizations gain insights into their cluster costs, allocate expenses to specific teams or projects,
and optimize resource usage to reduce unnecessary spending.
Key Features of Kubecost

  1. Real-time Cost Visibility: Provides granular insights into cluster costs, including nodes, storage, networking, and external cloud services.
  2. Cost Allocation: Enables cost breakdown by namespace,deployment, pod, service, or team.
  3. Optimization Recommendations: Suggests ways to optimize workloads, such as rightsizing pods and scaling down underutilized resources.
  4. Multi-Cluster Support: Monitors costs across multiple Kubernetes clusters from a single dashboard.
  5. Alerts & Notifications: Notifies users about budget overruns or inefficient resource usage.
  6. Integration with Cloud Providers: Works seamlessly with AWS, Azure, and GCP to provide unified cost analysis.

Why Use Kubecost?

  • Cost Transparency: Understand where your cloud expenses are going and allocate budgets accordingly.
  • Improved Efficiency: Identify resource wastage and optimize workloads.
  • Data-Driven Decision Making: Utilize analytics and recommendations to fine-tune cost management strategies.
  • Integration with FinOps: Align Kubernetes cost management with financial operations for better governance. Installation and Setup Kubecost can be installed on a Kubernetes cluster using Helm, the preferred package manager for Kubernetes applications.

Image description

Prerequisites:

  • Install the following tools: Helm 3.9+, kubectl, and optionally eksctl and AWS CLI.
  • You have access to an Amazon EKS cluster.
  • If your cluster is version 1.23 or later, you must have the Amazon EBS CSI driver installed on your cluster. You can also follow the instructions below to install the Amazon EBS CSI driver.
  • If your cluster is version 1.30 or later, there is by default no longer a default StorageClass assigned. See the EKS 1.30 release notes for one of several alternatives Installation Steps: Install AWS EBS CSI Driver
eksctl create iamserviceaccount\
--name ebs-csi-controller-sa\
--namespace kube-system\
--cluster $CLUSTER_NAME\
--attach-policy-arn arn:aws:iam::aws:policy/service-
role/AmazonEBSCSIDriverPolicy
\
--approve \
--role-only \
--role-name AmazonEKS_EBS_CSI_DriverRole
export SERVICE_ACCOUNT_ROLE_ARN=$(aws iam get-role --role-name
AmazonEKS_EBS_CSI_DriverRole --output json | jq -r '.Role.Arn')
Enter fullscreen mode Exit fullscreen mode

Install the Amazon EBS CSI add-on for EKS using the AmazonEKS_EBS_CSI_DriverRole by issuing
the following command:

eksctl create addon --name aws-ebs-csi-driver --cluster $CLUSTER_NAME \
--service-account-role-arn $SERVICE_ACCOUNT_ROLE_ARN --force
Enter fullscreen mode Exit fullscreen mode

Step 1: Install Kubecost on your Amazon EKS cluster
In your environment, run the following command from your terminal to install Kubecost on your existing Amazon EKS cluster:

helm upgrade -i kubecost \
oci://public.ecr.aws/kubecost/cost-analyzer --version <$VERSION> \
--namespace kubecost --create-namespace \
-f https://raw.githubusercontent.com/kubecost/cost-analyzer-helm-
chart/<$VERSION>/cost-analyzer/values-eks-cost-monitoring.yaml
Enter fullscreen mode Exit fullscreen mode

Image description

Step 2: Generate Kubecost dashboard endpoint
Run the following command to enable port-forwarding to expose the Kubecost dashboard:

kubectl port-forward --namespace kubecost deployment/cost-analyzer 9090
Enter fullscreen mode Exit fullscreen mode

Note: In this demo, I changed the service to a Nodeport

Image description

Step 3: Access Monitoring dashboards
You can now access Kubecost's UI by visiting http://localhost:9090 in your local web browser.
Here, you can monitor your Amazon EKS cluster cost and efficiency. Depending on your organization’s requirements and setup, you may have different options to expose Kubecost for internal access. There are a few examples that you can use for your references:

Image description

Image description

Image description

Image description

Best Practices for Using Kubecost

  1. Regular Monitoring: Schedule periodic reviews of cost reports to identify trends and anomalies.
  2. Set Alerts: Configure alerts for budget thresholds to prevent cost overruns.
  3. Tagging & Labeling: Ensure proper labeling of namespaces and workloads for accurate cost allocation.
  4. Leverage Recommendations: Utilize Kubecost’s insights to optimize resource utilization and minimize waste.
  5. Enable Multi-Cluster Monitoring: For large-scale deployments, enable multi-cluster support to get a unified cost view.

Conclusion
Kubecost is a valuable tool for Kubernetes users looking to gain cost visibility and optimize their
cloud spending. By implementing Kubecost, organizations can improve cost efficiency, prevent
budget overruns, and enhance their Kubernetes financial governance. Start using Kubecost today
to take control of your Kubernetes costs and drive better resource management strategies.

Happy Learning
Prithiviraj Rengarajan
DevOps Engineer

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay