Gain Better Visibility Into Kubernetes Cost Allocation
Best practices and different approaches for implementing cost monitoring in your organization when adopting Kubernetes and service-based architecture.
Join the DZone community and get the full member experience.Join For Free
The Complexity of Measuring Kubernetes Costs
Adopting Kubernetes and service-based architecture can benefit organizations – teams move faster, and applications scale more easily. However, visibility into cloud costs is made more complicated with this transition. This is because applications and their resource needs are often dynamic, and teams share core resources without transparent prices attached to workloads. Additionally, organizations that realize the full benefit of Kubernetes often run resources on disparate machine types and even multiple cloud providers.
In this blog post, we’ll look at best practices and different approaches for implementing cost monitoring in your organization for a shrowback/chargeback program and how to empower users to act on this information. We’ll also look at Kubecost, which provides an open-source approach for ensuring consistent and accurate visibility across all Kubernetes workloads.
A common Kubernetes setup with team workloads spread across Kubernetes nodes and clusters
Let’s look further into best practices for accurately allocating and monitoring Kubernetes workload costs and spending on related managed services.
Accurately allocating resource costs is the first critical step to creating great cost visibility and achieving high-cost efficiency within a Kubernetes environment.
To correctly do this, you need to allocate costs at the workload level by the individual container. Once workload allocation is complete, costs can be correctly assigned to teams, departments, or even individual developers by aggregating different workload collections. One framework for allocating cost at the workload level is as follows:
Let’s break this down a bit.
The average amount of resources consumed is measured by the Kubernetes scheduler or by the amount provisioned from a cloud provider, depending on the particular resource being measured. We recommend measuring memory and CPU allocation by the maximum request and usage. Using this methodology reflects the number of resources reserved by the Kubernetes scheduler itself. On the other hand, resources like load balancers and persistent volumes are strictly based on the amount provisioned from a provider.
The Kubernetes API can directly measure the period of time a resource is consumed. This is determined by the amount of time spent in a Running state for resources like memory, CPU, and GPU. To have numbers that are accurate enough for cloud chargeback, we recommend that teams reconcile this data with the amount of time a particular cloud resource, such as a node, was provisioned by a cloud provider. More on this in the section below.
Resource prices are determined by observing the cost of each particular resource in your environment. For example, the price of a CPU hour on a m5.xlarge spot instance in the us-east-1 AWS zone will be different than the on-demand price for that same instance.
Once costs are appropriately allocated across individual workloads with this framework, they can then be easily aggregated by any Kubernetes concept, such as namespace, label, annotation, or controller.
Kubernetes Cost Monitoring
With costs allocated by Kubernetes concept (pod or controller), you can begin to accurately map spend to any internal business concept, such as team, product, department, or cost center. It’s common practice for organizations to segment team workloads by Kubernetes namespace, whereas others may use concepts like Kubernetes labels or annotations to identify which team a workload belongs to.
Another key element for cost monitoring across different applications, teams, etc., is determining who should pay for idle or slack capacity. This specifically refers to unused cluster resources that are still being billed to your company. Often these are either billed to a central infrastructure cost center or distributed proportionally to application teams. Assigning these costs to the team(s) responsible for provisioning decisions has shown to have positive results by aligning the incentive to have an efficiently sized cluster.
Reconciling to Cloud Bill
Kubernetes provides a wealth of real-time data. This can be used to give developers access to immediate cost metrics. While this real-time data is often precise, it may not perfectly correspond to a cloud provider’s billing data. For example, when determining an AWS spot node's hourly rate, users need to wait on either the Spot data feed or the Cost and Usage Report to determine exact market rates. For billing and chargeback purposes, you should reconcile data to your actual bill.
Get Better Visibility and Governance with Kubecost
We’ve looked at how you can directly observe data to calculate the cost of Kubernetes workloads. Another option is to leverage Kubecost, a cost and capacity management solution built on open source that provides visibility across Kubernetes environments. Kubecost provides cost visibility and insights across Kubernetes workloads as well as the related managed services they consume, such as S3 or RDS. This product collects real-time data from Kubernetes and also reconciles with your cloud billing data to reflect the actual prices you have paid.
A Kubecost screenshot showing cost by Kubernetes cost by namespace
With a solution like Kubecost in place, you can empower application engineers to make informed real-time decisions and implement immediate and long-term practices to optimize and govern cloud spending. This includes adopting cost optimization insights without risking performance, implementing Kubernetes budgets and alerts, showback/chargeback programs, or even cost-based automation.
Kubecost community version is available for free with all of these features described – and you can find the Kubecost Helm chart in the Rancher App Catalog. Rancher gives you broad visibility and control; Kubecost gives you direct insight into spend and how to optimize. Together they provide a complete cost management story for teams using Kubernetes.
Published at DZone with permission of Webb Brown. See the original article here.
Opinions expressed by DZone contributors are their own.