heidsoft / devops

devops 经验总结实践与产品化
81 stars 44 forks source link

k8s 用promethues 采集k8s 数据指标的方法 #95

Open heidsoft opened 12 months ago

heidsoft commented 12 months ago

Prometheus 是一款开源的系统监控和报警工具包,它可以通过 HTTP 协议以拉的方式采集监控指标数据,然后对数据进行存储和实时报警。

以下是使用 Prometheus 采集 Kubernetes 数据指标的方法:

  1. 安装 Prometheus:你可以通过 Helm,在 Kubernetes 集群上安装 Prometheus。

    helm install stable/prometheus --name prometheus --namespace prometheus
  2. 安装 Prometheus Kubernetes 插件:Prometheus 提供了 Kubernetes 插件,可以方便地从 Kubernetes API 服务器获取集群、节点和 Pod 的元数据。

    helm install stable/prometheus-adapter --name prometheus-adapter --namespace prometheus
  3. 配置 Prometheus:在 Prometheus 的配置文件中,需要添加一些 scrape_configs 来指定从哪些服务和端口获取监控数据。例如,以下是一个用于采集 Kubernetes 节点数据的配置:

    - job_name: 'kubernetes-nodes'
     kubernetes_sd_configs:
     - role: node
     relabel_configs:
     - source_labels: [__address__]
       regex: '(.*):10250'
       replacement: '${1}:9100'
       target_label: __address__
  4. 查询和展示数据:Prometheus 提供了一种强大的查询语言 PromQL,可以用来查询和分析采集的数据。此外,你还可以使用 Grafana 等工具,将 Prometheus 的数据进行可视化展示。

这是一个配置片段,用于Prometheus(一种开源的监控和告警系统)来监控Kubernetes节点。解释如下:

总结一下,这个配置的作用是,对于Kubernetes的每个节点,Prometheus会将从服务发现获取到的地址(默认端口为10250)改为9100端口,然后进行监控。

heidsoft commented 12 months ago

https://observability.thomasriley.co.uk/monitoring-kubernetes/metrics/kubelet-cadvisor/#:~:text=Kubelet%20is%20a%20service%20that,tool%2C%20open%20sourced%20by%20Google. https://www.kubecost.com/kubernetes-devops-tools/cadvisor/ https://kubernetes.io/docs/concepts/cluster-administration/system-metrics/ https://www.rancher.cn/blog/2019/native-kubernetes-monitoring-tools-part-1 https://www.cloudforecast.io/blog/cadvisor-and-kubernetes-monitoring-guide/