组件 | 功能 |
---|---|
Prometheus Server | 核心组件,负责数据采集、存储、告警规则计算及PromQL查询。 |
Client Library | 为应用提供内置指标采集SDK(如Go、Java等语言支持)。 |
Exporters | 采集第三方系统指标并转换为Prometheus格式。常见Exporter: |
- Node Exporter:主机资源监控(CPU/内存/磁盘等)。 | |
- cAdvisor:容器资源监控。 | |
- kube-state-metrics:K8S资源对象状态监控(Pod/Deployment等)。 | |
- Blackbox Exporter:网络探测(HTTP/TCP/ICMP等)。 | |
Service Discovery | 动态发现监控目标(支持Kubernetes、Consul、DNS等)。 |
Alertmanager | 告警路由、去重、静默,支持邮件/钉钉/企业微信等通知渠道。 |
Pushgateway | 临时存储短期任务的推送数据,供Prometheus拉取。 |
Grafana | 可视化平台,集成Prometheus数据源,提供丰富的监控仪表盘。 |
数据场景限制:适合指标监控,不适用于日志/事件存储。
长期存储:本地存储设计为短期保留,需借助InfluxDB等外部方案。
集群支持:原生集群机制不完善,可通过Thanos/Cortex实现高可用。
官网:prometheus.io
GitHub:github.com/prometheus
Node Exporter指标说明:链接
安装 Prometheus
cd /opt/
tar xf prometheus-2.35.0.linux-amd64.tar.gz
mv prometheus-2.35.0.linux-amd64 /usr/local/prometheus
prometheus.yml
解析:global:
scrape_interval: 15s # 数据采集间隔
evaluation_interval: 15s # 告警规则评估间隔
scrape_timeout: 10s # 数据采集超时时间
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
配置系统启动文件
prometheus.service
:cat > /usr/lib/systemd/system/prometheus.service <<'EOF'
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path=/usr/local/prometheus/data/ \
--storage.tsdb.retention.time=15d \
--web.enable-lifecycle
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
启动 Prometheus
systemctl start prometheus
systemctl enable prometheus
netstat -natp | grep :9090
http://:9090
查看 Web UI。Node Exporter(系统级监控)
cd /opt/
tar xf node_exporter-1.3.1.linux-amd64.tar.gz
mv node_exporter-1.3.1.linux-amd64/node_exporter /usr/local/bin
cat > /usr/lib/systemd/system/node_exporter.service <<'EOF'
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/node_exporter \
--collector.ntp \
--collector.mountstats \
--collector.systemd \
--collector.tcpstat
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl start node_exporter
systemctl enable node_exporter
netstat -natp | grep :9100
- job_name: nodes
metrics_path: "/metrics"
static_configs:
- targets:
- 192.168.80.30:9100
- 192.168.80.11:9100
- 192.168.80.12:9100
labels:
service: kubernetes
curl -X POST http://192.168.80.30:9090/-/reload
MySQL Exporter
cd /opt/
tar xf mysqld_exporter-0.14.0.linux-amd64.tar.gz
mv mysqld_exporter-0.14.0.linux-amd64/mysqld_exporter /usr/local/bin/
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost' IDENTIFIED BY 'abc123';
systemctl start mysqld_exporter
systemctl enable mysqld_exporter
netstat -natp | grep :9104
- job_name: mysqld
metrics_path: "/metrics"
static_configs:
- targets:
- 192.168.80.15:9104
labels:
service: mysqld
Nginx Exporter
./configure --prefix=/usr/local/nginx \
--add-module=/usr/local/nginx-module-vts
make && make install
http {
vhost_traffic_status_zone;
vhost_traffic_status_filter_by_host on;
server {
listen 8080;
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
}
systemctl start nginx-exporter
systemctl enable nginx-exporter
netstat -natp | grep :9913
- job_name: nginx
metrics_path: "/metrics"
static_configs:
- targets:
- 192.168.80.15:9913
labels:
service: nginx
安装 Grafana
yum install -y grafana-7.4.0-1.x86_64.rpm
systemctl start grafana-server
systemctl enable grafana-server
netstat -natp | grep :3000
http://:3000
,默认账号:admin/admin
。配置数据源
http://:9090
。导入监控面板
基于文件的服务发现
- targets:
- 192.168.80.30:9100
- 192.168.80.15:9100
labels:
app: node-exporter
job: node
- job_name: nodes
file_sd_configs:
- files:
- targets/node*.yaml
refresh_interval: 2m
基于 Consul 的服务发现
consul agent -server -bootstrap -ui -data-dir=/usr/local/consul/data -bind=192.168.80.14 -client=0.0.0.0 -node=consul-server01 &
{
"services": [
{
"id": "node_exporter-node01",
"name": "node01",
"address": "192.168.80.30",
"port": 9100,
"tags": ["nodes"],
"checks": [{
"http": "http://192.168.80.30:9100/metrics",
"interval": "5s"
}]
}
]
}
- job_name: nodes
consul_sd_configs:
- server: 192.168.80.30:8500
tags:
- nodes
refresh_interval: 2m
monitor-sa
(用于监控组件)创建命名空间:
kubectl create ns monitor-sa
部署 DaemonSet:
# node-export.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-exporter
namespace: monitor-sa
labels:
name: node-exporter
spec:
selector:
matchLabels:
name: node-exporter
template:
metadata:
labels:
name: node-exporter
spec:
hostPID: true
hostIPC: true
hostNetwork: true
containers:
- name: node-exporter
image: prom/node-exporter:v0.16.0
ports:
- containerPort: 9100
args:
- --path.procfs=/host/proc
- --path.sysfs=/host/sys
volumeMounts:
- name: proc
mountPath: /host/proc
- name: sys
mountPath: /host/sys
volumes:
- name: proc
hostPath:
path: /proc
- name: sys
hostPath:
path: /sys
kubectl apply -f node-export.yaml
验证:
kubectl get pods -n monitor-sa -o wide
curl http://<节点IP>:9100/metrics # 检查指标数据
创建 ServiceAccount 和 RBAC 授权:
kubectl create serviceaccount monitor -n monitor-sa
kubectl create clusterrolebinding monitor-clusterrolebinding \
--clusterrole=cluster-admin \
--serviceaccount=monitor-sa:monitor
创建 Prometheus 配置 ConfigMap:
# prometheus-cfg.yaml
kind: ConfigMap
apiVersion: v1
metadata:
name: prometheus-config
namespace: monitor-sa
data:
prometheus.yml: |
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'kubernetes-node'
kubernetes_sd_configs:
- role: node
relabel_configs:
- source_labels: [__address__]
regex: '(.*):10250'
replacement: '${1}:9100'
target_label: __address__
- job_name: 'kubernetes-apiserver'
kubernetes_sd_configs:
- role: endpoints
scheme: https
tls_config:
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
kubectl apply -f prometheus-cfg.yaml
部署 Prometheus:
# prometheus-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus-server
namespace: monitor-sa
spec:
replicas: 1
selector:
matchLabels:
app: prometheus
template:
metadata:
labels:
app: prometheus
spec:
serviceAccountName: monitor
containers:
- name: prometheus
image: prom/prometheus:v2.35.0
args:
- --config.file=/etc/prometheus/prometheus.yml
- --storage.tsdb.path=/prometheus
ports:
- containerPort: 9090
volumeMounts:
- name: config
mountPath: /etc/prometheus
volumes:
- name: config
configMap:
name: prometheus-config
kubectl apply -f prometheus-deploy.yaml
创建 Service 暴露端口:
# prometheus-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: prometheus
namespace: monitor-sa
spec:
type: NodePort
ports:
- port: 9090
nodePort: 31000
selector:
app: prometheus
kubectl apply -f prometheus-svc.yaml
访问 Web UI:
http://:31000
部署 Grafana:
# grafana.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: grafana
namespace: monitor-sa
spec:
replicas: 1
selector:
matchLabels:
app: grafana
template:
metadata:
labels:
app: grafana
spec:
containers:
- name: grafana
image: grafana/grafana:9.0.0
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: grafana
namespace: monitor-sa
spec:
type: NodePort
ports:
- port: 3000
nodePort: 32000
selector:
app: grafana
kubectl apply -f grafana.yaml
配置数据源:
http://:32000
,默认账号 admin/admin
http://prometheus.monitor-sa.svc:9090
导入仪表盘:
3119
或 315
),导入 JSON。创建 Alertmanager 配置:
# alertmanager-cm.yaml
kind: ConfigMap
apiVersion: v1
metadata:
name: alertmanager
namespace: monitor-sa
data:
alertmanager.yml: |
global:
smtp_smarthost: 'smtp.qq.com:465'
smtp_from: '[email protected]'
smtp_auth_username: '[email protected]'
smtp_auth_password: 'your-smtp-token' # QQ邮箱授权码
route:
group_by: [alertname]
receiver: 'default'
receivers:
- name: 'default'
email_configs:
- to: '[email protected]'
kubectl apply -f alertmanager-cm.yaml
部署 Alertmanager:
# alertmanager-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: alertmanager
namespace: monitor-sa
spec:
replicas: 1
selector:
matchLabels:
app: alertmanager
template:
metadata:
labels:
app: alertmanager
spec:
containers:
- name: alertmanager
image: prom/alertmanager:v0.24.0
args:
- --config.file=/etc/alertmanager/alertmanager.yml
volumeMounts:
- name: config
mountPath: /etc/alertmanager
volumes:
- name: config
configMap:
name: alertmanager
---
apiVersion: v1
kind: Service
metadata:
name: alertmanager
namespace: monitor-sa
spec:
type: NodePort
ports:
- port: 9093
nodePort: 30066
selector:
app: alertmanager
kubectl apply -f alertmanager-deploy.yaml
配置 Prometheus 告警规则:
# 在 prometheus-cfg.yaml 中添加告警规则
rule_files:
- "alert-rules.yml"
服务发现:
kubernetes_sd_configs
自动发现节点、Pod、Service。告警路由:
group_by
和 repeat_interval
避免告警洪泛。高可用建议:
触发测试告警:
node-exporter
,观察 Prometheus 的 Targets
状态。http://:30066
)是否收到告警。邮件接收:
通过以上步骤,可在 Kubernetes 集群中快速搭建 Prometheus + Grafana + Alertmanager 的全栈监控告警系统。关键点包括:
自动发现:利用 Kubernetes 原生机制动态监控资源。
可视化:通过 Grafana 仪表盘实时展示数据。
告警闭环:结合 Alertmanager 实现邮件通知,确保问题及时响应。
kind: ConfigMap
apiVersion: v1
metadata:
labels:
app: prometheus
name: prometheus-config
namespace: prom
data:
prometheus.yml: |
# A scrape configuration for running Prometheus on a Kubernetes cluster.
# This uses separate scrape configs for cluster components (i.e. API server, node)
# and services to allow each to use different authentication configs.
#
# Kubernetes labels will be added as Prometheus labels on metrics via the
# `labelmap` relabeling action.
#
# If you are using Kubernetes 1.7.2 or earlier, please take note of the comments
# for the kubernetes-cadvisor job; you will need to edit or remove this job.
# Scrape config for API servers.
#
# Kubernetes exposes API servers as endpoints to the default/kubernetes
# service so this uses `endpoints` role and uses relabelling to only keep
# the endpoints associated with the default/kubernetes service using the
# default named port `https`. This works for single API server deployments as
# well as HA API server deployments.
global:
scrape_interval: 15s
scrape_timeout: 10s
evaluation_interval: 1m
scrape_configs:
- job_name: 'kubernetes-apiservers'
kubernetes_sd_configs:
- role: endpoints
# Default to scraping over https. If required, just disable this or change to
# `http`.
scheme: https
# This TLS & bearer token file config is used to connect to the actual scrape
# endpoints for cluster components. This is separate to discovery auth
# configuration because discovery & scraping are two separate concerns in
# Prometheus. The discovery auth config is automatic if Prometheus runs inside
# the cluster. Otherwise, more config options have to be provided within the
# .
tls_config:
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
# If your node certificates are self-signed or use a different CA to the
# master CA, then disable certificate verification below. Note that
# certificate verification is an integral part of a secure infrastructure
# so this should only be disabled in a controlled environment. You can
# disable certificate verification by uncommenting the line below.
#
# insecure_skip_verify: true
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
# Keep only the default/kubernetes service endpoints for the https port. This
# will add targets for each API server which Kubernetes adds an endpoint to
# the default/kubernetes service.
relabel_configs:
- source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
action: keep
regex: default;kubernetes;https
# Scrape config for nodes (kubelet).
#
# Rather than connecting directly to the node, the scrape is proxied though the
# Kubernetes apiserver. This means it will work if Prometheus is running out of
# cluster, or can't connect to nodes for some other reason (e.g. because of
# firewalling).
- job_name: 'kubernetes-nodes'
# Default to scraping over https. If required, just disable this or change to
# `http`.
scheme: https
# This TLS & bearer token file config is used to connect to the actual scrape
# endpoints for cluster components. This is separate to discovery auth
# configuration because discovery & scraping are two separate concerns in
# Prometheus. The discovery auth config is automatic if Prometheus runs inside
# the cluster. Otherwise, more config options have to be provided within the
# .
tls_config:
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
kubernetes_sd_configs:
- role: node
relabel_configs:
- action: labelmap
regex: __meta_kubernetes_node_label_(.+)
- target_label: __address__
replacement: kubernetes.default.svc:443
- source_labels: [__meta_kubernetes_node_name]
regex: (.+)
target_label: __metrics_path__
replacement: /api/v1/nodes/${1}/proxy/metrics
# Scrape config for Kubelet cAdvisor.
#
# This is required for Kubernetes 1.7.3 and later, where cAdvisor metrics
# (those whose names begin with 'container_') have been removed from the
# Kubelet metrics endpoint. This job scrapes the cAdvisor endpoint to
# retrieve those metrics.
#
# In Kubernetes 1.7.0-1.7.2, these metrics are only exposed on the cAdvisor
# HTTP endpoint; use "replacement: /api/v1/nodes/${1}:4194/proxy/metrics"
# in that case (and ensure cAdvisor's HTTP server hasn't been disabled with
# the --cadvisor-port=0 Kubelet flag).
#
# This job is not necessary and should be removed in Kubernetes 1.6 and
# earlier versions, or it will cause the metrics to be scraped twice.
- job_name: 'kubernetes-cadvisor'
# Default to scraping over https. If required, just disable this or change to
# `http`.
scheme: https
# This TLS & bearer token file config is used to connect to the actual scrape
# endpoints for cluster components. This is separate to discovery auth
# configuration because discovery & scraping are two separate concerns in
# Prometheus. The discovery auth config is automatic if Prometheus runs inside
# the cluster. Otherwise, more config options have to be provided within the
# .
tls_config:
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
kubernetes_sd_configs:
- role: node
relabel_configs:
- action: labelmap
regex: __meta_kubernetes_node_label_(.+)
- target_label: __address__
replacement: kubernetes.default.svc:443
- source_labels: [__meta_kubernetes_node_name]
regex: (.+)
target_label: __metrics_path__
replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor
# Scrape config for service endpoints.
#
# The relabeling allows the actual service scrape endpoint to be configured
# via the following annotations:
#
# * `prometheus.io/scrape`: Only scrape services that have a value of `true`
# * `prometheus.io/scheme`: If the metrics endpoint is secured then you will need
# to set this to `https` & most likely set the `tls_config` of the scrape config.
# * `prometheus.io/path`: If the metrics path is not `/metrics` override this.
# * `prometheus.io/port`: If the metrics are exposed on a different port to the
# service then set this appropriately.
- job_name: 'kubernetes-service-endpoints'
kubernetes_sd_configs:
- role: endpoints
relabel_configs:
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]
action: replace
target_label: __scheme__
regex: (https?)
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
action: replace
target_label: __address__
regex: ([^:]+)(?::\d+)?;(\d+)
replacement: $1:$2
- action: labelmap
regex: __meta_kubernetes_service_label_(.+)
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: kubernetes_namespace
- source_labels: [__meta_kubernetes_service_name]
action: replace
target_label: kubernetes_name
# Example scrape config for pods
#
# The relabeling allows the actual pod scrape endpoint to be configured via the
# following annotations:
#
# * `prometheus.io/scrape`: Only scrape pods that have a value of `true`
# * `prometheus.io/path`: If the metrics path is not `/metrics` override this.
# * `prometheus.io/port`: Scrape the pod on the indicated port instead of the
# pod's declared ports (default is a port-free target if none are declared).
- job_name: 'kubernetes-pods'
# if you want to use metrics on jobs, set the below field to
# true to prevent Prometheus from setting the `job` label
# automatically.
honor_labels: false
kubernetes_sd_configs:
- role: pod
# skip verification so you can do HTTPS to pods
tls_config:
insecure_skip_verify: true
# make sure your labels are in order
relabel_configs:
# these labels tell Prometheus to automatically attach source
# pod and namespace information to each collected sample, so
# that they'll be exposed in the custom metrics API automatically.
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: namespace
- source_labels: [__meta_kubernetes_pod_name]
action: replace
target_label: pod
# these labels tell Prometheus to look for
# prometheus.io/{scrape,path,port} annotations to configure
# how to scrape
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
regex: ([^:]+)(?::\d+)?;(\d+)
replacement: $1:$2
target_label: __address__
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scheme]
action: replace
target_label: __scheme__
regex: (.+)
创建 Namespace:
apiVersion: v1
kind: Namespace
metadata:
name: monitoring
创建 ServiceAccount:
apiVersion: v1
kind: ServiceAccount
metadata:
name: outside-prometheus
namespace: monitoring
创建 ClusterRole:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: outside-prometheus
rules:
- apiGroups: [""]
resources: ["nodes", "services", "endpoints", "pods", "nodes/proxy"]
verbs: ["get", "list", "watch"]
- apiGroups: ["networking.k8s.io"]
resources: ["ingresses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["configmaps", "nodes/metrics"]
verbs: ["get"]
- nonResourceURLs: ["/metrics"]
verbs: ["get"]
创建 ClusterRoleBinding:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: outside-prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: outside-prometheus
subjects:
- kind: ServiceAccount
name: outside-prometheus
namespace: monitoring
应用 RBAC 配置:
kubectl apply -f rbac.yaml
获取 Token:
TOKEN=$(kubectl get secret $(kubectl -n monitoring get secret | awk '/outside-prometheus/{print $1}') -n monitoring -o jsonpath={.data.token} | base64 -d)
echo $TOKEN
将 Token 保存到 Prometheus 节点:
echo $TOKEN > /usr/local/prometheus/kubernetes-api-token
复制 Kubernetes CA 证书到 Prometheus 节点:
scp /etc/kubernetes/pki/ca.crt <prometheus_node>:/usr/local/prometheus/
修改 Prometheus 配置文件:
scrape_configs:
# 集群 API Server 自动发现
- job_name: 'kubenetes-apiserver'
kubernetes_sd_configs:
- role: endpoints
api_server: https://192.168.80.10:6443 # API Server 地址
tls_config:
ca_file: /usr/local/prometheus/ca.crt # Kubernetes CA 证书
authorization:
credentials_file: /usr/local/prometheus/kubernetes-api-token # Token 文件
scheme: https
tls_config:
ca_file: /usr/local/prometheus/ca.crt
authorization:
credentials_file: /usr/local/prometheus/kubernetes-api-token
relabel_configs:
- source_labels: ["__meta_kubernetes_namespace", "__meta_kubernetes_endpoints_name", "__meta_kubernetes_endpoint_port_name"]
regex: default;kubernetes;https
action: keep
# 集群节点自动发现
- job_name: 'kubernetes-nodes'
kubernetes_sd_configs:
- role: node
api_server: https://192.168.80.10:6443
tls_config:
ca_file: /usr/local/prometheus/ca.crt
authorization:
credentials_file: /usr/local/prometheus/kubernetes-api-token
relabel_configs:
- source_labels: ["__address__"]
regex: (.*):10250
action: replace
target_label: __address__
replacement: $1:9100
- action: labelmap
regex: __meta_kubernetes_node_label_(.+)
重载 Prometheus 配置:
curl -X POST http://localhost:9090/-/reload
访问 Prometheus Web UI:
http://:9090/targets
,检查 Targets 状态是否为 UP
。检查服务发现:
RBAC 授权:
Token 和 CA 证书:
Relabel 配置:
relabel_configs
重写标签,确保 Prometheus 能够正确抓取目标数据。服务发现类型:
role: endpoints
:用于发现 Kubernetes 服务的 Endpoints。role: node
:用于发现 Kubernetes 集群的节点。注意: