Monitor Kubernetes cloud costs with open source tools

OpenCost is a cloud cost monitoring tool that integrates seamlessly with Kubernetes, allowing you to track your cloud spend in real-time so you can optimize your resources accordingly.
2 readers like this.
A person holding on to clouds that look like balloons

Opensource.com

Kubernetes is a powerful platform for managing dynamic containerized applications in the cloud, but it may be difficult to understand where costs are incurred. Managing the cost efficiency of Kubernetes resources can be a challenge. That's where OpenCost comes in. OpenCost is a cloud cost monitoring tool that integrates seamlessly with Kubernetes, allowing you to track your cloud spend in real-time so you can optimize your resources accordingly.

[ Also read How to measure the cost of running applications ]

OpenCost is an open source CNCF Sandbox project and specification for the real-time monitoring of cloud costs associated with Kubernetes deployments. The specification models current and historical Kubernetes cloud spend and resource allocation by service, deployment, namespace, labels, and much more. This data is essential for understanding and optimizing Kubernetes for both cost and performance from the applications down through the infrastructure.

Requirements and installation

Getting started with OpenCost is a relatively straightforward process. OpenCost uses Prometheus for both monitoring and metric storage. You can install it from the Prometheus Community Kubernetes Helm Chart.

Install Prometheus

Begin by installing Prometheus using the following command:

$ helm install my-prometheus --repo https://prometheus-community.github.io/helm-charts prometheus \
 --namespace prometheus --create-namespace \
 --set pushgateway.enabled=false --set alertmanager.enabled=false -f \
https://raw.githubusercontent.com/opencost/opencost/develop/kubernetes/prometheus/extraScrapeConfigs.yaml

Install OpenCost

Next, install OpenCost using the kubectl command:

$ kubectl apply --namespace opencost -f \
https://raw.githubusercontent.com/opencost/opencost/develop/kubernetes/opencost.yaml

This command deploys OpenCost to your cluster and starts collecting data. That is all that most installations require. You can use your own Prometheus installation or customize the deployment with the OpenCost Helm Chart.

Testing and access

OpenCost automatically detects whether it runs on AWS, Azure, or GCP, and you can configure it to provide pricing for on-premises Kubernetes deployments. Begin by forwarding ports for API and UI access:

$ kubectl port-forward --namespace opencost service/opencost 9003 9090

Within about five minutes, you can verify the UI and server are running, and you may access the OpenCost UI at http://localhost:9090.

Monitor costs

You are ready to start monitoring your cloud costs with OpenCost deployed to your Kubernetes cluster. The OpenCost dashboard provides real-time visibility into your cloud spend, allowing you to identify cost anomalies and optimize your cloud resources. You can view your cloud spend by nodes, namespaces, pods, tags, and more.

OpenCost monitoring console

(Matthew Ray, CC BY-SA 4.0)

The kubectl cost plugin provides easy CLI queries to Kubernetes cost allocation metrics. It allows developers, operators, and others to determine the cost and efficiency for any Kubernetes workload quickly.

$ kubectl cost --service-port 9003 \
--service-name opencost --kubecost-namespace opencost \
--allocation-path /allocation/compute pod \
--window 5m --show-efficiency=true

+-------+---------+-------------+----------+---------------+
|CLUSTER|NAMESPACE|POD          |MONTH RATE|COST EFFICIENCY|
+-------+---------+-------------+----------+---------------+
|cl-one |kube-syst|coredns-db...| 1.486732 |      0.033660 |
|       |         |coredns-...dm| 1.486732 |      0.032272 |
|       |         |kube-prox...7| 1.359577 |      0.002200 |
|       |         |kube-prox...x| 1.359577 |      0.002470 |
|       |opencost |opencost...5t| 0.459713 |      0.187180 |
|       |kube-syst|aws-node-cbwl| 0.342340 |      0.134960 |
|       |         |aws-node-gbfh| 0.342340 |      0.133760 |
|       |prometheu|my-prome...pv| 0.000000 |      0.000000 |
|       |         |my-prome...hn| 0.000000 |      0.000000 |
|       |         |my-prome...89| 0.000000 |      0.000000 |
+-------+---------+-------------+----------+---------------+
| SUMMED|         |             | 6.837011 |               |
+-------+---------+-------------+----------+---------------+

You can also integrate an API to extract the data programmatically into your platform of choice.

Kubernetes optimization strategies

Now that you have a handle on your cloud costs, it's time to optimize your Kubernetes environment. Optimization is an iterative process. Start at the top of the stack (containers) and work through each layer. The efficiencies compound at each step. There are many ways to optimize Kubernetes for cost efficiency, such as:

  1. Look for abandoned workloads and unclaimed volumes: Pods and storage that are no longer in use or are disconnected continue to consume resources without providing value.
  2. Right-size your workloads: Ensure you're using the right size containers for your workloads. Investigate over- and under-allocated containers.
  3. Autoscaling: Autoscaling can help you save costs by only using resources when needed.
  4. Right-size your cluster: Too many or too-large nodes may be inefficient. Finding the right balance between capacity, availability, and performance may greatly reduce costs.
  5. Investigate cheaper node types: There's a lot of variation in CPU, RAM, networking, and storage. Switching to ARM architectures may unlock even greater savings.
  6. Invest in a FinOps team: A dedicated team within your organization can look for ways to unlock greater savings by coordinating reserved instances, spot instances, and savings plans.

Get started today

Monitoring costs in a Kubernetes environment can be challenging, but with OpenCost, it doesn't have to be. To get started with OpenCost and take control of your cloud spend, visit the OpenCost website, get the code in GitHub, check out the OpenCost documentation, and get involved in the #opencost channel in the CNCF Slack.

[ Related read How to prioritize cloud spending ]

Matt Ray
OpenCost Community Manager for Kubecost. Co-host of the Software Defined Talk podcast.

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.