如何安装 OpenCost 并接入 Grafana?
解读
面试官问“如何安装 OpenCost 并接入 Grafana”,并不是让你背官网命令,而是考察三件事:
- 你是否知道 OpenCost 在国内云环境下的镜像加速与 Helm 源替换;
- 你是否能把 Prometheus 的 remote_write / additionalScrapeConfigs 与 Grafana 的 数据源、Dashboard、变量模板 串成一条闭环;
- 你是否具备成本可观测意识:能把 K8s 资源单价、折扣、分摊规则、标签治理一次性说清楚,并用 Grafana 做二次聚合,最终让财务、SRE、业务三方都看懂。
知识点
- OpenCost 核心组件:cost-model(Go 服务,暴露 /metrics)、Prometheus(必须 2.35+,支持 OpenMetrics 格式)、Kubecost-Core(可选 UI)。
- 国内镜像:docker.io/kubecost1 被墙,需提前同步到 阿里云 ACR 或腾讯云 TCR,并在 Helm values 中统一替换。
- Helm 安装参数:必须关闭
prometheus.server.persistentVolume(测试环境)、开启prometheus.server.retention=15d(生产建议 30d),并把opencost.exporter.defaultClusterId设为可读的集群别名,方便 Grafana 变量过滤。 - Grafana 数据源:使用 Prometheus 类型,URL 指向
http://prometheus-server.monitoring.svc.cluster.local:80,开启 BasicAuth 并配置只读账号。 - 官方 Dashboard ID:6784(K8s-OpenCost-Overview),需二次改造:把
cluster变量改成label_values(opencost_cluster_memory_cost, cluster),避免空值;把namespace变量加.*通配,支持多选。 - 关键指标:
opencost_node_total_hourly_cost:节点总成本,含折扣;opencost_pod_memory_cost:Pod 内存费用,单位元/小时;opencost_namespace_shared_cost:公共 namespace 分摊,需用 Grafana 表达式sum by (namespace) (opencost_namespace_shared_cost * 24 * 30)做月账。
- 告警规则:当
sum(rate(opencost_pod_cpu_cost[1d])) > 0.8 * sum(opencost_node_total_hourly_cost)连续 3h,触发 高成本 Pod 阈值,通过 Grafana 告警路由到钉钉群。 - 权限治理:OpenCost 需要 cluster-reader 权限(get nodes/pods/pv/storageclass),但禁止 list secrets;Grafana 侧只给 Viewer 角色,防止修改单价配置。
答案
第一步,准备国内可用镜像。把 kubecost1/cost-model:prod-1.105.2 同步到 registry.cn-hangzhou.aliyuncs.com/abc/opencost:1.105.2,并写一份 values.yaml:
opencost:
exporter:
defaultClusterId: "beijing-prod"
image:
registry: registry.cn-hangzhou.aliyuncs.com
repository: abc/opencost
tag: 1.105.2
prometheus:
server:
persistentVolume:
enabled: false
retention: 15d
global:
scrape_interval: 60s
第二步,Helm 安装。添加国内镜像源:
helm repo add opencost https://opencost.github.io/opencost-charts
helm repo update
helm upgrade --install opencost opencost/opencost \
-n monitoring --create-namespace \
-f values.yaml
第三步,验证指标。端口转发后执行:
kubectl port-forward -n monitoring svc/opencost 9003:9003
curl -s http://localhost:9003/metrics | grep opencost_cluster_memory_cost
能看到值即说明采集成功。
第四步,接入 Grafana。在 Grafana 9.5+ 中新建 Prometheus 数据源,URL 填 http://prometheus-server.monitoring.svc.cluster.local,保存并测试。导入官方 Dashboard ID 6784,把 cluster 变量改成 label_values(opencost_cluster_memory_cost, cluster),保存为“K8s-成本-生产视图”。
第五步,做二次聚合。新建一个 Stat Panel,标题“本月已耗费用”,查询:
sum(
opencost_node_total_hourly_cost * 24 * 30
* on(cluster) group_left()
(up{job="opencost"})
)
单位选“元”,保留两位小数,放到业务大盘最显眼位置。
第六步,告警。在 Grafana Alert 里新建规则“高成本 Pod 连续 3h”,条件同上,通知渠道选 钉钉机器人,加签名校验,消息模板里带上 {{ $labels.namespace }}/{{ $labels.pod }} 与实时费用,方便值班直接定位。
至此,OpenCost 安装与 Grafana 接入完成,财务、SRE、业务三方可在同一套视图里看到实时成本、趋势预测、超标告警,实现“成本可观测”闭环。
拓展思考
- 多云场景:若集群跨阿里云、华为云,需在 OpenCost 的
cloudProvider字段分别配置 ACK 折扣系数 与 CCE 竞价实例单价,并在 Grafana 用cluster=~"$cluster"变量做多云对比,回答时可提到“通过 Thanos sidecar 把各地 Prometheus 聚合成全局 Store,实现一张总账大盘”。 - 标签治理:业务方不打
team/owner标签会导致成本无法分摊。可推动在 CI/CD 中强制注入cost-center标签,并用 Grafana 的 Transform 做“无标签 Pod 费用”占比告警,超过 5% 自动@负责人。 - FinOps 闭环:每月初用 Grafana Reporting 自动生成 PDF 账单,通过 Grafana OnCall 轮值发送给财务;同时把上月节省的费用换算成“可购买 Pod 数量”,用 Gauge 面板 展示,激励业务继续优化。
- 安全合规:OpenCost 的
/model/allocationAPI 会返回 Pod 级明细,需在前置 NGINX Ingress 加 OAuth2-Proxy,只允许公司内网 SSO 登录,防止云厂商账单敏感数据泄露。