Kubecost 简介
官方网站:https://docs.kubecost.com/
Kubecost 是 Stackwatch 开发的一款部署在 K8s 集群上可以帮助用户进行资源管理和监控的开源工具。 Kubecost 社区版可以免费使用,提供了一套完整的成本管理解决方案,包含资源消耗计量计费的可视化,当前资源的使用分析和导出报告等,辅助用户优化和治理云支出。 Kubecost 的开源版本将计费结果的存储周期限定为 15 天。如果需要将指标保留 15 天以上,您必须升级至付费版本。
部署环境
笔者为了测试简单起见,使用的是 KubeSphere 3.0 环境。 KubeSphere 本身已经内置了 Prometheus 组件,在接下来的演示中,笔者也将基于现有的 Prometheus 组件进行 Kubecost 的安装。
注意:如果使用的是 KubeSphere 2.0 或者自建集群,请检查集群满足以下组件最小版本要求:
- kube-state-metrics - v1.6.0 (May 19)
- cAdvisor - kubelet v1.11.0 (May 18)
- node-exporter - v0.16 (May 18) [Optional Dependency]
部署 Kubecost
这里由于 Kubesphere 已经集成了 Prometheus 相关组件,并且笔者的环境中使用的是 helm v3 版本,
可以根据以下操作步骤完成安装流程。
- 下载并修改
values.yaml
文件
wget https://raw.githubusercontent.com/kubecost/cost-analyzer-helm-chart/master/cost-analyzer/values.yaml
根据集群的配置修改prometheus.fqdn
和prometheus.enabled
这 2 个字段。
global:
# zone: cluster.local (use only if your DNS server doesn't live in the same zone as kubecost)
prometheus:
enabled: false # If false, Prometheus will not be installed -- only actively supported on paid Kubecost plans
fqdn: http://prometheus-k8s.kubesphere-monitoring-system.svc:9090 #example fqdn. Ignored if enabled: true
...
- 使用 helm 安装 Kubecost
kubectl create namespace kubecost
helm repo add kubecost https://kubecost.github.io/cost-analyzer/
helm install kubecost kubecost/cost-analyzer --namespace kubecost --set kubecostToken="eXVua3VucmFvQGdtYWlsLmNvbQ==xm343yadf98" --values values.yaml
- 修改 Prometheus 配置
kubectl get secret additional-scrape-configs -n kubesphere-monitoring-system -o=jsonpath='{.data.prometheus-additional\.yaml}' | base64 -d > ./prometheus-additional.yaml
编辑上面保存的文件,加入以下配置:
- job_name: kubecost
honor_labels: true
scrape_interval: 1m
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
dns_sd_configs:
- names:
- kubecost-cost-analyzer.kubecost.svc.cluster.local
type: 'A'
port: 9003
4. 更新 additional-scrape-configs secret
kubectl delete secret additional-scrape-configs -n kubesphere-monitoring-system
kubectl create secret generic additional-scrape-configs --from-file=prometheus-additional.yaml -n kubesphere-monitoring-system --dry-run -o=jsonpath='{.data.prometheus-additional\.yaml}'
git clone https://github.com/kubesphere/ks-installer.git
git checkout release-3.0
cd ks-installer/roles/ks-monitor/files/prometheus/prometheus/
# 将dta字段替换为前面dry-run生成的编码数据
vim additional-scrape-configs.yaml
cd ..
# 使配置生效
kubectl apply -f prometheus
查看 Prometheus kubecost target

开启端口转发
kubectl port-forward --address 0.0.0.0 --namespace kubecost deployment/kubecost-cost-analyzer 9090
- 访问dashboard




总结
Kubecost 为 Kubernetes 工作负载以及它们所消耗的相关管理服务提供成本可见性和洞察力。该产品收集 Kubernetes 的实时数据,还能与你的云计费数据进行核对,以反映支付的实际价格,是一套完成的成本管理解决方案。
参考
- https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/additional-scrape-config.md
- https://kubecost.com/install
- http://docs.kubecost.com/custom-prom.html