这篇文章已超过一年。旧文章可能包含过时的内容。请检查页面中的信息自发布以来是否已不再正确。
使用 Kuberhealthy 查看 K8s KPI
在 Kuberhealthy v2.0.0 的基础上继续构建
去年 11 月在 KubeCon San Diego 2019 上,我们宣布发布 Kuberhealthy 2.0.0 - 将 Kuberhealthy 转型为用于合成监控的 Kubernetes Operator。这项新功能赋予开发者创建自己的 Kuberhealthy 检查容器的能力,用于对他们的应用和集群进行合成监控。社区很快就采用了这项新功能,我们感谢所有在他们的集群中实现和测试 Kuberhealthy 2.0.0 的人。感谢所有报告问题并在 #kuberhealthy Slack 频道上贡献讨论的人。我们迅速着手通过更新版本的 Kuberhealthy 来解决你们的所有反馈。此外,我们创建了一个指南,介绍如何轻松安装和使用 Kuberhealthy,以便捕获一些有用的合成 KPIs。
部署 Kuberhealthy
要安装 Kuberhealthy,请确保你已安装 Helm 3。如果未安装,你可以使用此 deploy 文件夹中生成的平面 spec 文件。如果你不使用 Prometheus Operator,则应使用 kuberhealthy-prometheus.yaml;如果使用,则应使用 kuberhealthy-prometheus-operator.yaml。如果你根本不使用 Prometheus,仍然可以使用 Kuberhealthy,通过 此 spec 文件集成 JSON 状态页面和/或 InfluxDB。
使用 Helm 3 安装
1. 在所需的 Kubernetes 集群/上下文中创建命名空间 "kuberhealthy"
kubectl create namespace kuberhealthy
2. 将当前命名空间设置为 "kuberhealthy"
kubectl config set-context --current --namespace=kuberhealthy
3. 将 kuberhealthy repo 添加到 Helm
helm repo add kuberhealthy https://comcast.github.io/kuberhealthy/helm-repos
4. 根据你的 Prometheus 实现,使用适合你的集群的命令安装 Kuberhealthy
- 如果你使用 Prometheus Operator
helm install kuberhealthy kuberhealthy/kuberhealthy --set prometheus.enabled=true,prometheus.enableAlerting=true,prometheus.enableScraping=true,prometheus.serviceMonitor=true
- 如果你使用 Prometheus,但不是 Prometheus Operator
helm install kuberhealthy kuberhealthy/kuberhealthy --set prometheus.enabled=true,prometheus.enableAlerting=true,prometheus.enableScraping=true
有关配置适当 scrape annotations 的更多详细信息,请参阅下方的 Prometheus 集成详细信息部分。
- 最后,如果你不使用 Prometheus
helm install kuberhealthy kuberhealthy/kuberhealthy
运行 Helm 命令应会自动安装最新版本的 Kuberhealthy (v2.2.0) 以及一些基本检查。如果你运行 kubectl get pods
,你应该会看到两个 Kuberhealthy pod。这些 pod 负责创建、协调和跟踪测试 pod。这两个 Kuberhealthy pod 也提供 JSON 状态页面和 /metrics
端点。你看到的创建的其他所有 pod 都是 checker pod,用于执行检查并在完成后关闭。
配置附加检查
接下来,你可以运行 kubectl get khchecks
。默认情况下,你应该会看到安装了三个 Kuberhealthy 检查
- daemonset:部署并拆除 daemonset,以确保集群中的所有节点都能正常工作。
- deployment:创建一个部署,然后触发滚动更新。测试该部署是否可以通过 Service 访问,然后删除所有内容。此过程中的任何问题都会导致此检查报告失败。
- dns-status-internal:验证内部集群 DNS 是否按预期工作。
要查看其他可用的外部检查,请查阅 外部检查注册表,你可以在其中找到其他 yaml 文件,将其应用到你的集群中以启用各种检查。
Kuberhealthy 检查 pod 在 Kuberhealthy 启动后不久 (1-2 分钟) 应开始运行。此外,check-reaper cronjob 每隔几分钟运行一次,以确保完成的 checker pod 同时存在不超过 5 个。
要查看这些检查的状态页面,你需要通过编辑 kuberhealthy
Service 并设置 Type: LoadBalancer
将 kuberhealthy
Service 暴露到外部,或使用 kubectl port-forward service/kuberhealthy 8080:80
。查看时,Service 端点将显示一个 JSON 状态页面,如下所示
{
"OK": true,
"Errors": [],
"CheckDetails": {
"kuberhealthy/daemonset": {
"OK": true,
"Errors": [],
"RunDuration": "22.512278967s",
"Namespace": "kuberhealthy",
"LastRun": "2020-04-06T23:20:31.7176964Z",
"AuthoritativePod": "kuberhealthy-67bf8c4686-mbl2j",
"uuid": "9abd3ec0-b82f-44f0-b8a7-fa6709f759cd"
},
"kuberhealthy/deployment": {
"OK": true,
"Errors": [],
"RunDuration": "29.142295647s",
"Namespace": "kuberhealthy",
"LastRun": "2020-04-06T23:20:31.7176964Z",
"AuthoritativePod": "kuberhealthy-67bf8c4686-mbl2j",
"uuid": "5f0d2765-60c9-47e8-b2c9-8bc6e61727b2"
},
"kuberhealthy/dns-status-internal": {
"OK": true,
"Errors": [],
"RunDuration": "2.43940936s",
"Namespace": "kuberhealthy",
"LastRun": "2020-04-06T23:20:44.6294547Z",
"AuthoritativePod": "kuberhealthy-67bf8c4686-mbl2j",
"uuid": "c85f95cb-87e2-4ff5-b513-e02b3d25973a"
}
},
"CurrentMaster": "kuberhealthy-7cf79bdc86-m78qr"
}
此 JSON 页面显示在你的集群中运行的所有 Kuberhealthy 检查。如果你的 Kuberhealthy 检查运行在不同的命名空间中,你可以通过在状态页面 URL 后添加 GET
变量 namespace
参数来过滤它们:?namespace=kuberhealthy,kube-system
。
编写你自己的检查
Kuberhealthy 设计为可通过自定义检查容器进行扩展,任何人都可以编写这些容器来检查任何内容。这些检查可以用任何语言编写,只要它们被打包在容器中即可。这使得 Kuberhealthy 成为创建你自己的合成检查的优秀平台!
创建你自己的检查是验证你的客户端库、模拟真实用户工作流程以及对你的服务或系统正常运行时间建立高度信心的绝佳方式。
要了解更多关于编写你自己的检查以及简单示例,请查阅 自定义检查创建文档。
Prometheus 集成详细信息
当启用 Prometheus (非 operator) 时,Kuberhealthy Service 会添加以下 annotation
prometheus.io/path: /metrics
prometheus.io/port: "80"
prometheus.io/scrape: "true"
在你的 prometheus 配置中,添加以下示例 scrape_config,根据添加的 prometheus annotation 抓取 Kuberhealthy Service
- job_name: 'kuberhealthy'
scrape_interval: 1m
honor_labels: true
metrics_path: /metrics
kubernetes_sd_configs:
- role: service
namespaces:
names:
- kuberhealthy
relabel_configs:
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
action: keep
regex: true
你也可以使用此示例 job 指定要抓取的目标端点
- job_name: kuberhealthy
scrape_interval: 1m
honor_labels: true
metrics_path: /metrics
static_configs:
- targets:
- kuberhealthy.kuberhealthy.svc.cluster.local:80
应用相应的 prometheus 配置后,你应该能够看到以下 Kuberhealthy 指标
kuberhealthy_check
kuberhealthy_check_duration_seconds
kuberhealthy_cluster_states
kuberhealthy_running
创建关键绩效指标 (KPI)
利用这些 Kuberhealthy 指标,我们的团队已能够基于以下定义、计算和 PromQL 查询收集 KPI。
可用性
我们将可用性定义为 K8s 集群控制平面正常运行并按预期工作。这通过我们在设定的时间内创建部署、执行滚动更新和删除部署的能力来衡量。
我们通过测量 Kuberhealthy 的 deployment check 的成功和失败来计算。
可用性 = 正常运行时间 / (正常运行时间 * 停机时间)
正常运行时间 = 部署检查通过次数 * 检查运行间隔
停机时间 = 部署检查失败次数 * 检查运行间隔
检查运行间隔 = 检查运行的频率 (在你的 KuberhealthyCheck Spec 中设置的
runInterval
)PromQL 查询 (过去 30 天的可用性百分比)
1 - (sum(count_over_time(kuberhealthy_check{check="kuberhealthy/deployment", status="0"}[30d])) OR vector(0)) / sum(count_over_time(kuberhealthy_check{check="kuberhealthy/deployment", status="1"}[30d]))
利用率
我们将利用率定义为用户对产品 (k8s) 及其资源 (pod、service 等) 的使用情况。这通过我们的客户正在使用的节点、部署、statefulset、持久卷、service、pod 和 job 的总数来衡量。我们通过计算节点、部署、statefulset、持久卷、service、pod 和 job 的总数来计算。
持续时间 (延迟)
我们将持续时间定义为控制平面的容量和吞吐量利用率。我们通过捕获 Kuberhealthy deployment check 运行的平均持续时间来计算。
- PromQL 查询 (部署检查平均运行持续时间)
avg(kuberhealthy_check_duration_seconds{check="kuberhealthy/deployment"})
错误 / 告警
我们将错误定义为所有与 k8s 集群和 Kuberhealthy 相关的告警。每次我们的 Kuberhealthy 检查失败时,我们都会收到失败的告警。
谢谢!
再次感谢社区中所有人的贡献和帮助!我们很高兴看到你们构建什么。一如既往,如果你发现问题、有功能请求或需要提交拉取请求,请在 Github 项目上提出 issue。