[手动选题][tech]: 20230313.4 ️ Monitor Kubernetes cloud costs with open source tools.md

This commit is contained in:
六开箱 2023-03-16 18:56:46 +08:00
parent a55b89d4f6
commit 10df9550b3

View File

@ -0,0 +1,135 @@
[#]: subject: "Monitor Kubernetes cloud costs with open source tools"
[#]: via: "https://opensource.com/article/23/3/kubernetes-cloud-cost-monitoring"
[#]: author: "Matt Ray https://opensource.com/users/mattray-0"
[#]: collector: "lkxed"
[#]: translator: " "
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Monitor Kubernetes cloud costs with open source tools
======
[Kubernetes][1] 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][2] 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][3] ]**
OpenCost is an open source [CNCF Sandbox project][4] and [specification][5] 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][6] for both monitoring and metric storage. You can install it from the [Prometheus Community Kubernetes Helm Chart][7].
#### 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][8] installation or customize the deployment with the [OpenCost Helm Chart][9].
#### 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][10]**.
### 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][11]
The [kubectl cost][12] 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:
- 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.
- Right-size your workloads: Ensure you're using the right size containers for your workloads. Investigate over- and under-allocated containers.
- Autoscaling: Autoscaling can help you save costs by only using resources when needed.
- 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.
- 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.
- 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][13] website, get the code in [GitHub][14], check out the [OpenCost documentation][15], and get involved in the **#opencost** channel in the [CNCF Slack][16].
**[ Related read [How to prioritize cloud spending][17] ]**
--------------------------------------------------------------------------------
via: https://opensource.com/article/23/3/kubernetes-cloud-cost-monitoring
作者:[Matt Ray][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/mattray-0
[b]: https://github.com/lkxed/
[1]: https://www.redhat.com/en/topics/containers/what-is-kubernetes?intcmp=7013a000002qLH8AAM
[2]: https://www.opencost.io/
[3]: https://www.redhat.com/architect/hybrid-cloud-cost-tbm?intcmp=7013a000002qLH8AAM
[4]: https://www.cncf.io/projects/opencost/
[5]: https://github.com/opencost/opencost/blob/develop/spec/opencost-specv01.md
[6]: https://prometheus.io/
[7]: https://prometheus-community.github.io/helm-charts
[8]: https://www.opencost.io/docs/install#providing-your-own-prometheus
[9]: https://github.com/opencost/opencost-helm-chart/
[10]: http://localhost:9090
[11]: https://opensource.com/sites/default/files/2023-03/opencost.png
[12]: https://github.com/kubecost/kubectl-cost
[13]: https://opencost.io/
[14]: https://github.com/opencost
[15]: https://www.opencost.io/docs/
[16]: https://slack.cncf.io/
[17]: https://www.redhat.com/architect/manage-cloud-spending?intcmp=7013a000002qLH8AAM