Feb 9th, โ€˜24/3 min read

Controlling Kubernetes Costs with OpenCost and Levitate

Setting up OpenCost with Levitate to monitor the cost of Kubernetes clusters

Controlling Kubernetes Costs with OpenCost and Levitate

Effective control of operational expenses plays a crucial role in the deployment and administration of Kubernetes. While Kubernetes empowers users with enhanced control over deployments, it requires a deep understanding and effective management of associated costs. OpenCost, tailor-made for Kubernetes cost monitoring, paired with Levitate, presents a holistic solution to address this challenge.

In this blog we will learn how to integrate existing OpenCost setup with Levitate for monitoring Kubernetes costs.

What is Levitate?

Levitate is a managed time series metrics and events warehouse. It is compatible with Prometheus and OpenTelemetry and provides better performance and workflows to tame high cardinality.

Levitate - a managed metrics and events warehouse
Levitate - a managed metrics and events warehouse
๐Ÿ’ก
Get started with Levitate for free by creating a free account without a credit card.

What is OpenCost?

OpenCost is a CNCF-backed open-source cost monitoring tool for cloud-native environments, built to offer a complete view of Kubernetes cluster costs across various cloud providers, regions, and on-premises installations. It enables real-time monitoring of Kubernetes cluster costs, providing a detailed breakdown for each resource.

OpenCost relies on the metrics emitted by node-exporter and kube-state-metrics (KSM). It queries the underlying TSDB storage for these metrics. It also ships with a web UI for visualization and a Grafana dashboard.

OpenCost also has built-in integrations with cloud providers using their APIs to retrieve billing information.

OpenCost Installation

OpenCost can be installed using Helm. It assumes an existing Prometheus Operator installation. You can follow along with the documentation and carry out the installation.

Connecting OpenCost to Levitate

OpenCost Metrics to Levitate
OpenCost Metrics to Levitate

Integrating OpenCost with Levitate is super easy as it requires a minimal configuration change to be deployed. OpenCost assumes that the required metrics to be queried are stored in a TSDB like Levitate. It involves following steps.

  1. Configure Prometheus Operator to remote write Kubernetes metrics such as kube-state-metrics and node-exporter to Levitate.
  2. Additionally, Prometheus Operator can be configured to scrape metrics that OpenCost emits and send them to Levitate.
  3. Read metrics from Levitate needed for OpenCost. Obtain the OpenCost Helm Chart values.yaml. If you have customized your Helm Chart values, use that instead of the default file.
  4. Update OpenCost Helm Chart values.yaml with Levitate Read Credentials. Obtain Read Credentials following this guide.
  5. Replace the default config with the below-modified config. Update placeholders with proper authentication credentials as obtained earlier.
prometheus:
    # -- Secret name that contains credentials for Prometheus
    secret_name: ~
    # -- Prometheus Basic auth username
    username: "<levitate_username>"
    # -- Key in the secret that references the username
    username_key: DB_BASIC_AUTH_USERNAME
    # -- Prometheus Basic auth password
    password: "<levitate_password>"
    # -- Key in the secret that references the password
    password_key: DB_BASIC_AUTH_PW
    # -- Prometheus Bearer token
    bearer_token: ""
    bearer_token_key: DB_BEARER_TOKEN
    external:
      # -- Use external Prometheus (eg. Levitate)
      enabled: true
      # -- External Prometheus url
      url: "<levitate_read_url>"
    internal:
      # -- Use in-cluster Prometheus
      enabled: false
      # -- Service name of in-cluster Prometheus
      serviceName: prometheus-server
      # -- Namespace of in-cluster Prometheus
      namespaceName: prometheus-system
      # -- Service port of in-cluster Prometheus
      port: 80
Ensure that Prometheus is an external data source enabled is set to true and internal enabled is set to false.
  1. Thats it! Clean and simple ๐Ÿ‘Œ Deploy Helm Chart with these updated configs
helm upgrade opencost opencost/opencost -f ./values.yaml --kubeconfig=/path/to/kubeconfig

OpenCost Grafana Dashboard

Import this dashboard is provided by OpenCost here.

OpenCost Grafana Dashboard
OpenCost Grafana Dashboard

Conclusion

In summary, effective Kubernetes management requires careful monitoring of operational costs. OpenCost, offering a comprehensive view of Kubernetes costs across diverse platforms, coupled with Levitate's efficient time-series data storage and powerful alerting workflows, creates a powerful cost monitoring system. Together, they enable real-time tracking, detailed cost breakdowns, and insightful visualizations, ensuring optimal financial efficiency in Kubernetes deployments.

๐Ÿ’ก
Get up and running with Levitate and forget the pain and toil of managing high cardinality metrics yourself. Start your free trial today.

Contents


Newsletter

Stay updated on the latest from Last9.

Authors

Aniket Rao

http://1x.engineer @last9io ๐Ÿ’ป Programmer | ๐ŸŒŒ Astrophile | ๐ŸŽฎ FIFA Player |๐ŸŒ๏ธโ€โ™‚๏ธAmateur Golfer

Handcrafted Related Posts