在现代的云端应用中,监控是一项非常重要的工作。Kubernetes 作为一种常见的容器编排系统,其内置的监控机制已经越来越成熟,而 Prometheus 作为一个开源的监控系统也是非常不错的选择。在本文中,我们将介绍如何在 Kubernetes 中使用 Prometheus 进行监控的实现方法。
Prometheus 简介
Prometheus 是一个开源的监控系统,可以用来记录并查询应用的指标数据。它支持多种数据源和查询方式,可以有效地监控和分析复杂的应用系统。在 Kubernetes 中,Prometheus 可以与 Kubernetes API 结合使用,以获取 Pod,Service,Node 等数据,并提供一些可视化的指标数据来监控 Kubernetes 集群的健康状况。
Kubernetes 中使用 Prometheus 进行监控
在 Kubernetes 中使用 Prometheus 进行监控需要以下几个步骤:
- 部署 Prometheus 的实例
- 在 Kubernetes 中定义需要监控的对象
- 配置 Prometheus 中的指标数据源
- 配置 Prometheus 中的告警规则
步骤一:部署 Prometheus 的实例
部署 Prometheus 的方式有很多种,可以使用 Kubernetes 中的 Helm Chart,也可以直接使用 Prometheus 的 Docker 镜像。下面我们以 Helm Chart 的方式进行演示。
首先需要确认已经安装了 Helm,然后添加 Prometheus 的 Chart 仓库:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts helm repo update
然后部署 Prometheus:
helm install prometheus prometheus-community/kube-prometheus-stack
这个命令会在 Kubernetes 集群中创建一个名为 prometheus 的资源。使用 kubectl 命令可以查看创建的资源:
kubectl get all -n prometheus
步骤二:在 Kubernetes 中定义需要监控的对象
在使用 Prometheus 进行监控之前,需要在 Kubernetes 中定义需要监控的对象。可以监控的对象有很多种,比如 Pod,Service,Node 等。我们以监控一个名为 my-app 的 Deployment 为例,首先需要在 Deployment 的 YAML 文件中添加一些注释:
// www.javascriptcn.com code example
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
environment: production
spec:
containers:
- name: my-app
image: my-app:v1
ports:
- name: http
containerPort: 8080
livenessProbe:
httpGet:
path: /healthz
port: http
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /
port: http
initialDelaySeconds: 5
periodSeconds: 10
env:
- name: NODE_ENV
value: "production"
- name: TZ
value: "Asia/Shanghai"
imagePullSecrets:
- name: my-app-secret添加注释如下:
metadata:
annotations:
prometheus.io/scrape: 'true' # 开启抓取
prometheus.io/path: /metrics # 抓取的路径
prometheus.io/port: '8080' # 内部端口在定义好注释之后,需要重新部署这个 Deployment:
kubectl apply -f my-app.yaml
步骤三:配置 Prometheus 中的指标数据源
定义了需要监控的对象之后,就需要向 Prometheus 注册这些对象的指标数据源。在 Prometheus 中,可以通过配置文件或者 API 的方式来实现。这里我们使用配置文件的方式。
首先创建一个 ConfigMap,用于存储 Prometheus 的配置文件:
// www.javascriptcn.com code example
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
namespace: prometheus
data:
prometheus.yml: |
global:
scrape_interval: 1m
scrape_configs:
- job_name: kubernetes-pods
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_label_app]
regex: my-app
action: keep
- job_name: kubernetes-nodes
kubernetes_sd_configs:
- role: node在这个 ConfigMap 中,我们定义了两个抓取数据的 job:kubernetes-pods 和 kubernetes-nodes。其中 kubernetes-pods 的配置方式是通过在 Pod 的注释中添加 prometheus.io/scrape,prometheus.io/path 和 prometheus.io/port 属性进行抓取。我们只抓取 label 中包含 app=my-app 的 Pod。kubernetes-nodes 则用于抓取 Node 的数据。
接下来,将这个 ConfigMap 挂载到 Prometheus 的实例中:
// www.javascriptcn.com code example
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus
namespace: prometheus
spec:
replicas: 1
selector:
matchLabels:
app: prometheus
template:
metadata:
labels:
app: prometheus
spec:
containers:
- name: prometheus
image: prom/prometheus:v2.22.0
ports:
- name: http
containerPort: 9090
volumeMounts:
- name: prometheus-config
mountPath: /etc/prometheus
command:
- /bin/prometheus
args:
- --config.file=/etc/prometheus/prometheus.yml
volumes:
- name: prometheus-config
configMap:
name: prometheus-config步骤四:配置 Prometheus 中的告警规则
我们可以在 Prometheus 中定义告警规则,当某个指标超出预设的值时,可以通过邮件、微信等方式通知相关人员。在 Prometheus 中定义告警规则也非常方便,只需要编辑 prometheus.yml 文件即可。
// www.javascriptcn.com code example
groups:
- name: my-app-rules
rules:
- alert: HighRequestLatency
expr: http_request_duration_seconds_bucket{le="0.5"} > 100
for: 5m
labels:
severity: warning
annotations:
summary: High request latency on {{ $labels.instance }}
description: "{{ $labels.instance }} has more than 100 requests with latency > 500ms."这个告警规则用于检查 HTTP 请求的延迟,如果大于 0.5 秒的请求数超过 100,就会触发一个名为 HighRequestLatency 的告警。符合告警条件的指标将会被标记为 warning 级别,并在邮件或者其他渠道上进行通知。
总结
本文介绍了如何在 Kubernetes 中使用 Prometheus 进行监控,包括部署 Prometheus 实例、定义需要监控的对象、配置 Prometheus 中的指标数据源、以及配置 Prometheus 中的告警规则。通过本文的学习,你可以很快地掌握在 Kubernetes 中使用 Prometheus 进行监控的方法,并在实际工作中加以应用。
Source: FunTeaLearn,Please indicate the source for reprints https://funteas.com/post/64c2de9d83d39b48816d38e2