一、部署配置¶
整个监控体系涉及的技术栈较多,几乎可覆盖真实企业中的所有场景。主要技术栈如下:
- Prometheus:监控主服务
- node-exporter:数据采集器
- kube-state-metrics:数据采集器
- metrics-server:数据采集器
- Consul:自动发现
- blackbox:黑盒拨测
- Alertmanager:监控告警服务
- Grafana:数据展示服务
- prometheusAlert:告警消息转发服务
1.1 Prometheus部署¶
部署对外可访问Prometheus:
- 首先需要创建Prometheus所在命名空间;
- 然后创建Prometheus使用的RBAC规则;
- 创建Prometheus的configmap来保存配置文件;
- 创建service暴露Prometheus服务;
- 创建deployment部署Prometheus容器;
- 最后创建Ingress实现外部域名访问Prometheus。
部署顺序如图下:

1.1.1 创建命名空间¶
$ kubectl create namespace monitor
1.1.2 创建RBAC规则¶
创建RBAC规则,包含ServiceAccount、ClusterRole、ClusterRoleBinding三类YAML文件。
[root@master01 7]# vim prometheus-rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus
namespace: monitor
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: prometheus
rules:
# 核心资源监控权限
- apiGroups: [""]
resources: ["nodes", "nodes/proxy", "services", "endpoints", "pods"]
verbs: ["get", "list", "watch"]
# 节点指标访问权限(如 kubelet 的 /metrics/cadvisor)
- apiGroups: [""]
resources: ["nodes/metrics"]
verbs: ["get"]
# Ingress 资源权限(适配新版 API)
- apiGroups: ["networking.k8s.io"]
resources: ["ingresses"]
verbs: ["get", "list", "watch"]
# EndpointSlices 发现权限(适用于 Kubernetes 1.21+)
- apiGroups: ["discovery.k8s.io"]
resources: ["endpointslices"]
verbs: ["get", "list", "watch"]
# 非资源 URL 权限(指标抓取)
- nonResourceURLs: ["/metrics", "/metrics/cadvisor", "/stats/prometheus"]
verbs: ["get"]
#- apiGroups: [""]
# resources: ["nodes","nodes/proxy","services","endpoints","pods"]
# verbs: ["get", "list", "watch"]
#- apiGroups: ["extensions"]
# resources: ["ingress"]
# verbs: ["get", "list", "watch"]
#- nonResourceURLs: ["/metrics"]
# verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: prometheus
namespace: monitor
#应用
[root@master01 7]# kaf prometheus-rbac.yaml
确认验证:
[root@master01 7]# kubectl get sa prometheus -n monitor
NAME SECRETS AGE
prometheus 1 16s
[root@master01 7]# kubectl get clusterrole prometheus
NAME CREATED AT
prometheus 2025-04-10T09:56:01Z
[root@master01 7]# kubectl get clusterrolebinding prometheus
NAME ROLE AGE
prometheus ClusterRole/cluster-admin 43s
1.1.3 创建ConfigMap类型的Prometheus配置文件¶
[root@master01 7]# vim prometheus-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
namespace: monitor
data:
prometheus.yml: |
global:
scrape_interval: 15s
evaluation_interval: 15s
external_labels:
cluster: "kubernetes"
############ 数据采集job ###################
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['127.0.0.1:9090']
labels:
instance: prometheus
############ 指定告警规则文件路径位置 ###################
rule_files:
- /etc/prometheus/rules/*.rules
[root@master01 7]# kaf prometheus-config.yaml
确认验证:
[root@master01 7]# kubectl get cm prometheus-config -n monitor
NAME DATA AGE
prometheus-config 1 7s
1.1.4 创建ConfigMap类型的prometheus rules配置文件¶
使用ConfigMap方式创建prometheus rules配置文件:
包含的内容是两块,分别是 general.rules 和 node.rules 。使用以下命令创建Prometheus的另外两个配置文件:
[root@master01 7]# vim prometheus-rules.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-rules
namespace: monitor
data:
general.rules: |
groups:
- name: general.rules
rules:
- alert: InstanceDown
expr: |
up{job=~"other-ECS|k8s-nodes|prometheus"} == 0
for: 1m
labels:
severity: critical
annotations:
summary: "Instance {{ $labels.instance }} 停止工作"
description: "{{ $labels.instance }} 主机名:{{ $labels.hostname }} 已经停止1分钟以上."
node.rules: |
groups:
- name: node.rules
rules:
- alert: NodeFilesystemUsage
expr: |
100 - (node_filesystem_avail_bytes / node_filesystem_size_bytes) * 100 > 85
for: 1m
labels:
severity: warning
annotations:
summary: "Instance {{ $labels.instance }} : {{ $labels.mountpoint }} 分区使用率过高"
description: "{{ $labels.instance }} 主机名:{{ $labels.hostname }} : {{ $labels.mountpoint }} 分区使用大于85% (当前值: {{ $value }})"
#应用
[root@master01 7]# kaf prometheus-rules.yaml
确认验证:
[root@master01 7]# kubectl get cm -n monitor prometheus-rules
NAME DATA AGE
prometheus-rules 2 37m
注意事项:如果后面修改cm配置文件后,不仅要apply -f cm配置文件,还要热加载Prometheus,不然配置文件不生效
#修改后重新进行应用
[root@master01 7]# kaf prometheus-rules.yaml
#热加载
[root@master01 7]# curl -XPOST http://prometheus.zhang-qing.com/-/reload
1.1.5 创建prometheus svc¶
[root@master01 7]# vim prometheus-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: prometheus
namespace: monitor
labels:
k8s-app: prometheus
spec:
type: ClusterIP
ports:
- name: http
port: 9090
targetPort: 9090
selector:
k8s-app: prometheus
[root@master01 7]# kaf prometheus-svc.yaml
1.1.6 创建prometheus deploy¶
由于Prometheus需要对数据进行持久化,以便在重启后能够恢复历史数据。所以这边我们通过早先课程部署的NFS做存储来实现持久化。
说明:这里需要参考前面部署完成sc,不然pvc无法使用
当前我们使用NFS提供的StorageClass来做数据存储。
[root@master01 7]# vim prometheus-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: prometheus-data-pvc
namespace: monitor
spec:
accessModes:
- ReadWriteMany
storageClassName: "nfs-storage"
resources:
requests:
storage: 10Gi
[root@master01 7]# kaf prometheus-pvc.yaml
# 验证
[root@master01 7]# kg pv,pvc -n monitor
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvc-0200a663-a4d2-4a80-8297-199488d0b336 10Gi RWX Delete Bound monitor/prometheus-data-pvc nfs-storage 2m50s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/prometheus-data-pvc Bound pvc-0200a663-a4d2-4a80-8297-199488d0b336 10Gi RWX nfs-storage 14m
Prometheus控制器文件:
[root@master01 7]# vim prometheus-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus
namespace: monitor
labels:
k8s-app: prometheus
spec:
replicas: 1
selector:
matchLabels:
k8s-app: prometheus
template:
metadata:
labels:
k8s-app: prometheus
spec:
serviceAccountName: prometheus
containers:
- name: prometheus
image: registry.cn-hangzhou.aliyuncs.com/abroad_images/prometheus:v2.36.0
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 9090
securityContext:
runAsUser: 65534
privileged: true
command:
- "/bin/prometheus"
args:
- "--config.file=/etc/prometheus/prometheus.yml"
- "--web.enable-lifecycle"
- "--storage.tsdb.path=/prometheus"
- "--storage.tsdb.retention.time=10d"
- "--web.console.libraries=/etc/prometheus/console_libraries"
- "--web.console.templates=/etc/prometheus/consoles"
resources:
limits:
cpu: 2000m
memory: 2048Mi
requests:
cpu: 1000m
memory: 512Mi
readinessProbe:
httpGet:
path: /-/ready
port: 9090
initialDelaySeconds: 5
timeoutSeconds: 10
livenessProbe:
httpGet:
path: /-/healthy
port: 9090
initialDelaySeconds: 30
timeoutSeconds: 30
volumeMounts:
- name: data
mountPath: /prometheus
subPath: prometheus
- name: config
mountPath: /etc/prometheus
- name: prometheus-rules
mountPath: /etc/prometheus/rules
- name: configmap-reload
image: registry.cn-hangzhou.aliyuncs.com/abroad_images/configmap-reload:v0.5.0
imagePullPolicy: IfNotPresent
args:
- "--volume-dir=/etc/config"
- "--webhook-url=http://localhost:9090/-/reload"
resources:
limits:
cpu: 100m
memory: 100Mi
requests:
cpu: 10m
memory: 10Mi
volumeMounts:
- name: config
mountPath: /etc/config
readOnly: true
volumes:
- name: data
persistentVolumeClaim:
claimName: prometheus-data-pvc
- name: prometheus-rules
configMap:
name: prometheus-rules
- name: config
configMap:
name: prometheus-config
#应用
[root@master01 7]# kaf prometheus-deploy.yaml
#验证
[root@master01 7]# kgp -n monitor
NAME READY STATUS RESTARTS AGE
prometheus-64dc78b55b-tpslm 2/2 Running 0 27s
部署的 Deployment 资源文件中的 containers 部分配置了两个容器,分别是:
- prometheus: Prometheus 容器是主容器,用于运行 Prometheus 进程。
- configmap-reload: 用于监听指定的 ConfigMap 文件中的内容,如果内容发生更 改,则执行 webhook url 请求,因为 Prometheus 支持通过接口重新加载配置文 件,所以这里使用这个容器提供的机制来完成 Prometheus ConfigMap 配置文件内 容一有更改,就执行Prometheus 的 /-/reload 接口,进行更新配置操作。
上面资源文件中 Prometheus 参数说明:
- --web.enable-lifecycle: 启用 Prometheus 用于重新加载配置的 /-/reload 接口
- --config.file: 指定 Prometheus 配置文件所在地址,这个地址是相对于容器内部而 言的
- --storage.tsdb.path: 指定 Prometheus 数据存储目录地址,这个地址是相对于容器 而言的
- --storage.tsdb.retention.time: 指定删除旧数据的时间,默认为 15d
- --web.console.libraries: 指定控制台组件依赖的存储路径
- --web.console.templates: 指定控制台模板的存储路径
确认验证:
[root@master01 7]# kubectl get deploy -n monitor
NAME READY UP-TO-DATE AVAILABLE AGE
prometheus 1/1 1 1 115s
[root@master01 7]# kgp -n monitor
NAME READY STATUS RESTARTS AGE
prometheus-64dc78b55b-tpslm 2/2 Running 0 27s
1.1.7 创建prometheus ingress实现外部域名访问¶
[root@master01 7]# vim prometheus-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: monitor
name: prometheus-ingress
spec:
ingressClassName: nginx
rules:
- host: prometheus.zhang-qing.com
http:
paths:
- pathType: Prefix
backend:
service:
name: prometheus
port:
number: 9090
path: /
[root@master01 7]# kaf prometheus-ingress.yaml
访问验证:
[root@master01 7]# curl prometheus.zhang-qing.com
<a href="/graph">Found</a>.
二、初识Prometheus监控平台¶
prometheus监控平台:
- Graph:用于绘制图表,可以选择不同的时间范围、指标和标签,还可以添加多个 图表进行比较。
- Alert:用于设置告警规则,当指标达到设定的阈值时,会发送告警通知。
- Explore:用于查询和浏览指标数据,可以通过查询表达式或者标签过滤器来查找数 据。
- Status:用于查看prometheus的状态信息,包括当前的targets、rules、alerts 等。
- Config:用于编辑prometheus的配置文件,可以添加、修改和删除配置项。
三、总结¶
- 全面的监控:Prometheus可以监控各种数据源,比如服务器、容器等,还支持度量数据和日志数据等多种类型的监控。
- 支持动态服务发现:Prometheus可以自动地发现并监控正在运行的服务,从而避免 手动配置。(后续课程会介绍到)
- 灵活的告警机制:Prometheus支持可配置的告警规则,可以根据不同的情况发出不 同的告警信息,并且可以通过API通知其他服务。(后续课程会介绍到)
- 多维数据模型:Prometheus的数据模型支持多维度的数据,可以使用标准的 PromQL查询语言对数据进行分析和展示。
- 高效的存储:Prometheus使用自己的时间序列数据库存储数据,采用一种基于时间 的存储方式,可以高效地处理大量数据。