前言
Kubernetes 默认情况下使用 cAdvisor 来收集容器的各项指标,足以满足大多数人的需求,但还是有所欠缺,比如缺少对以下几个指标的收集:OOM kill;容器重启的次数;容器的退出码
missing-container-metrics 这个项目弥补了 cAdvisor(此组件为kubelet的一部分) 的缺陷,新增了以上几个指标,集群管理员可以利用这些指标迅速定位某些故障。
支持docker 和 containerd
部署
helm repo add missing-container-metrics draganm.github.io/missing-con…
修改missing-container-metrics/values.yaml中useDocker为true(取决是dokcer还是containerd)
helm install missing-container-metrics . -n missing-container-metrics
该服务为DaemonSets类型
资源占用
观察周期7天
container_cpu_usage_seconds 大部分时间小于5m
container_total_memory_usage 大部分时间小于20MB
暴露指标
container_restarts :容器的重启次数。
container_ooms :容器的 OOM 杀死数。这涵盖了容器 cgroup 中任何进程的 OOM 终止。
container_last_exit_code :容器的最后退出代码
添加监控
- job_name: 'missing-container-metrics'
scrape_interval: 15s
scheme: https
tls_config:
insecure_skip_verify: true
bearer_token_file: xxx
kubernetes_sd_configs:
- api_server: xxx
role: pod
bearer_token_file: xxx
tls_config:
insecure_skip_verify: true
relabel_configs:
- source_labels: [__meta_kubernetes_pod_name]
regex: missing-container-metrics(.*)
action: keep
- source_labels: [__address__]
action: replace
target_label: instance
- target_label: __address__
replacement: xxx
- source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_pod_name, __meta_kubernetes_pod_container_port_number]
regex: ([^;]+);([^;]+);([^;]+)
target_label: __metrics_path__
replacement: xxxx/api/v1/namespaces/${1}/pods/http:${2}:${3}/proxy/metrics
上述是通过api-server 获取的数据,如果Prometheus和target之间网络是通的,可以去掉22-25行,并修改scheme: http 这里是举个例子,k8s内部exporter理论上都可以通过自动发现的方式去获取,只需要一个job就好,后续文章会来分享。
测试
抓取三天内,被oom kill 的pod
sum(increase(container_ooms[3d])) by (namespace, pod)>0
在rancher中查看pod最近事件
实际生产使用中告警触发规则,比较前后5分钟oom次数,若增量大于0且容器发生重启时即触发告警。
- alert: container_ooms_kill
expr: sum by(namespace, pod, instance) (increase(container_ooms[5m])) * on(pod) group_left(container) ((ceil(increase(kube_pod_container_status_restarts_total[5m])) > 0)) > 0
for: 0m
labels:
severity: warning
annotations:
summary: Pod {{ $labels.pod }} has been OOM Killed
description: A process in this Pod {{ $labels.pod }} has been OOM Killed due to exceeding the Kubernetes memory limit in the last 15 minutes