在部署prometheus监控k8s状态

教程来源于神州数码企业服务集团,本文章在实践中整理代码和说明

https://zhuanlan.zhihu.com/p/456205833

参考资料

https://devopscube.com/setup-prometheus-monitoring-on-kubernetes/

Prometheus Kubernetes 清单文件

https://github.com/bibinwilson/kubernetes-prometheus

一、关于 Prometheus

Prometheus 是一个开源的监控框架。它为 Kubernetes 容器编排平台提供了开箱即用的监控功能。

1
2
以下是 Prometheus 中重要的一些概念:
指标收集: Prometheus 使用拉取模型,通过 HTTP来实现检索指标。而对于那些 Prometheus 无法抓取指标的用例,可以选择使用 Pushgateway 将指标推送到 Prometheus 。例如,从 kubernetes jobs & Cronjobs 中收集自定义指标。指标端点:如果想要使用 Prometheus 来实现监控,那么系统就应该在/metrics端点上公开指标。Prometheus 会使用该端点来定期提取指标。PromQL:在 Prometheus 中附带有一种非常灵活的查询语言——PromQL,它可用于查询 Prometheus 仪表板中的指标。此外,Prometheus UI 和 Grafana 还使用 PromQL 查询来实现指标可视化。Prometheus Exporters: Exporters 是一个将来自第三方应用程序的现有指标转换为 Prometheus 指标格式的库。目前已经有许多官方和社区的 Prometheus Exporters。例如Prometheus node exporter。它以 Prometheus 的格式,公开了所有 Linux 系统级的指标。TSDB(时序数据库):Prometheus 使用 TSDB 存储着所有数据。默认情况下,所有数据都存储在本地。但是,也可以选择为 Prometheus TSDB 集成远程存储。

二、在 Kubernetes 上设置 Prometheus 监控

2.创建命名空间和集群角色

首先,我们将为所有的监控组件创建一个 Kubernetes 命名空间。

如果不创建专用的命名空间,那么所有的 Prometheus kubernetes 部署对象都会部署在默认的命名空间上。

执行以下命令创建一个名为 monitoring 的新命名空间:

1
kubectl create namespace monitoring

Prometheus 使用 Kubernetes API 从 Nodes、Pods、Deployments 等等中读取所有可用的指标。​​因此,我们需要创建一个包含 read access 所需 API 组的 RBAC 策略,并将该策略绑定到monitoring 命名空间。

2.1 创建一个名为 clusterRole.yaml的文件,并复制以下 RBAC 角色以创建角色。

在下面给出的角色中,可以看到,我们已经往 nodes, services endpoints, pods 和 ingresses 中添加了get,list以及watch权限。角色绑定被绑定到监控命名空间。如果有任何要从其他对象中检索指标的用例,则需要将其添加到此集群角色中。

https://github.com/techiescamp/kubernetes-prometheus/blob/master/clusterRole.yaml

使用下面的命令创建角色。

1
kubectl create -f clusterRole.yaml

2.2 创建 Config Map 以外部化 Prometheus 配置

Prometheus 的所有配置都是prometheus.yaml文件的一部分,而 Alertmanager 的所有警报规则都配置在prometheus.rules.

1
prometheus.yaml:这是主要的 Prometheus 配置,包含所有抓取配置、服务发现详细信息、存储位置、数据保留配置等prometheus.rules:此文件包含所有 Prometheus 警报规则

通过将 Prometheus 配置外部化到 Kubernetes 的 config map,那么就无需当需要添加或删除配置时,再来构建 Prometheus 镜像。这里需要更新配置映射并重新启动 Prometheus pod 以应用新配置。

带有所有Prometheus 抓取配置和警报规则的 config map 被挂载到 Prometheus 容器的prometheus.yaml 和 prometheus.rules 文件中,这些文件位于 /etc/prometheus

2.2.1 在Kubernetes中创建config-map

创建一个名为config-map.yaml的文件,并在下面的链接中复制文件内容
https://github.com/techiescamp/kubernetes-prometheus/blob/master/config-map.yaml

执行以下命令在 Kubernetes 中创建 config-map

1
kubectl create -f config-map.yaml

它在容器内创建两个文件。

注意:在 Prometheus 的术语中,从端点集合收集指标的配置称为job.
prometheus.yaml包含了用以发现动态运行在 Kubernetes集群的 pods 和 services 的所有配置。

1
2
3
Prometheus 抓取配置中有以下scrape jobs:
kubernetes-apiservers:它从 API 服务器获取所有指标。kubernetes-nodes:它收集所有 Kubernetes 节点指标。kubernetes-pods:如果 pod 元数据用prometheus.io/scrape和 prometheus.io/port来注释,那么所有的 pod 都得以发现。kubernetes-cadvisor:收集所有 cAdvisor 指标。kubernetes-service-endpoints:如果服务元数据使用 prometheus.io/scrape 和 prometheus.io/port 注释来进行注释,那么所有服务端点都将被报废。它可以用于黑盒监控。
prometheus.rules 包含用于向 Alertmanager 发送警报的所有警报规则。

2.3 创建 Prometheus 部署

第1步 创建prometheus-deployment配置文件并部署

创建一个名为prometheus-deployment.yaml的文件,并将以下内容复制到文件中。在此配置中,我们将 Prometheus 的 config map 作为文件安装在/etc/prometheu中。

https://github.com/techiescamp/kubernetes-prometheus/blob/master/prometheus-deployment.yaml

注意:这里没有为 Prometheus 存储使用任何持久存储卷,因为它是一个基本设置。在为生产用例设置 Prometheus 时,需要确保将持久存储添加到部署中。

第2步 使用上述文件在监控命名空间上创建部署

1
kubectl get deployments --namespace=monitoring

第3步 使用以下命令检查创建的部署。

1
kubectl get deployments --namespace=monitoring

2.4 设置 Kube 状态指标(Kube State Metrics)

Kube 状态指标服务将提供许多默认情况下不可用的指标。需要确保部署了 Kube 状态指标来监控像所有kubernetes API对象,例如deployments,pods,jobs,cronjobs等。

可以按照本文在 kubernetes 上设置 Kube 状态指标 ==>如何在 Kubernetes 上设置 Kube 状态指标

https://devopscube.com/setup-kube-state-metrics/

2.5 连接到 Prometheus 仪表板

我们可以通过三种不同的方式查看已部署的 Prometheus 仪表板。

1
2
3
1.使用 Kubectl 端口转发使用 
2.NodePort 或负载均衡器将 Prometheus 部署公开为服务。
3.如果部署了 Ingress 控制器,则可以添加一个Ingress 对象。

下面让我们具体看看这三个方式:

方式一:使用 Kubectl 端口转发

使用 kubectl 端口转发,我们可以使用本地主机上的选定端口从本地工作站访问 pod。此方法主要用于调试目的。

1.首先,获取 Prometheus pod 的名称。

1
kubectl get pods --namespace=monitoring

输出如下:

1
2
NAME                                     READY   STATUS    RESTARTS   AGE
prometheus-deployment-67cf879cc4-h8zb6 1/1 Running 0 80m

2.使用 pod 名称执行以下命令,从 localhost 端口 8080 访问 Prometheus。

1
kubectl port-forward prometheus-deployment-67cf879cc4-h8zb6 8080:9090 -n monitoring

3.在浏览器中访问服务器ip:8080,进入Prometheus 主页。

方式二:将 Prometheus 公开为服务 [NodePort & LoadBalancer]

要通过IP或DNS名称访问 Prometheus 仪表板,就需要将其公开为 Kubernetes 服务。

1.创建一个名为prometheus-service.yaml的文件,并复制以下内容。我们将在 port 30000 上的所有 kubernetes 节点 IP 上公开 Prometheus。

https://github.com/techiescamp/kubernetes-prometheus/blob/master/prometheus-service.yaml

注意:如果是在 AWS、Azure 或 Google Cloud 上,则可以使用 Loadbalancer 类型,这将创建一个负载均衡器并自动将其指向 Kubernetes 服务端点。

上述 YAML 服务的 annotations 确保服务端点由 prometheus 废弃。

prometheus.io/port应始终是上述 YAML 服务的目标端口

2.使用以下命令创建prometheus-service服务。

1
kubectl create -f prometheus-service.yaml --namespace=monitoring

创建后,就可以使用端口 30000 上的任何 Kubernetes 节点 IP 访问 Prometheus 仪表板。如果是在云上,就需要确保有正确的防火墙规则来从工作站访问端口 30000。



检查prometheus-service服务命令

1
kubectl -n monitoring get service prometheus-service

输出如下

1
2
NAME                 TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
prometheus-service NodePort 10.105.251.140 <none> 8080:30000/TCP 73m

3.浏览到status –> Targets,将看到所有 Kubernetes 端点使用下面的服务发现自动连接到了 Prometheus 的所有 Kubernetes 端点。

4.可以前往主页从下拉列表中选择需要的指标,并获取提到的时间范围的图表。下面的例子是容器内存利用率的示例图。

方式三:使用 Ingress 暴露 Prometheus

如果有一个现有的入口控制器设置,那么可以创建一个入口对象来将 Prometheus DNS 路由到 Prometheus 后端服务。

此外,可以在入口层为 Prometheus 添加 SSL。

这里是一个入口对象实例。有关SSL 的示例入口对象,可以参考该GitHub 链接

https://github.com/techiescamp/kubernetes-prometheus/blob/master/prometheus-ingress.yaml

2.6 设置警报管理器

Alertmanager 处理 Prometheus 指标的所有警报机制。有许多集成可用于从 Alertmanager 接收警报(Slack、电子邮件、API 端点等)

2.7 设置 Grafana

使用 Grafana 可以从 Prometheus 指标中创建仪表板来监控 kubernetes 集群,而且不必为仪表板编写所有 PromQL 查询。

Kubernetes 有许多可用的社区仪表板模板。可以根据需要导入并修改它。

2.8、设置节点导出器

Node Exporter 将提供所有 Kubernetes 节点的所有 Linux 系统级指标。

具体设置可以参照:在 Kubernetes 上设置节点导出器
https://devopscube.com/node-exporter-kubernetes/

node-exporter 的抓取配置是 Prometheus config map的一部分。部署节点导出器后,就可以在 Prometheus 中看到节点导出器的目标和指标。

三、结论

对于生产设置,还需要依据环境和数据量来考虑更多配置和参数,从而进行扩展和存储。