Day 22 - Prometheus 监控体系
📘 Day 22:Prometheus 监控体系
🎯 今日目标
- 用 Helm 部署 kube-prometheus-stack
- 理解 Prometheus 数据采集链路
- 查看集群核心指标(CPU/Memory/Network)
- 自定义告警规则
🧠 理论精讲(30 分钟)
Prometheus Stack 架构
1 | ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ |
核心指标速查
| 指标 | 含义 |
|---|---|
container_cpu_usage_seconds_total |
CPU 累计使用 |
container_memory_working_set_bytes |
内存使用量 |
kube_pod_status_phase |
Pod 状态 |
kube_deployment_status_replicas_ready |
Deploy 就绪副本 |
node_filesystem_avail_bytes |
节点磁盘可用 |
🔧 动手实操(120 分钟)
练习 22.1:安装 kube-prometheus-stack
1 | # 1. 添加 Helm 仓库 |
练习 22.2:访问 Prometheus 和 Grafana
1 | # 1. 获取 Grafana 登录密码 |
练习 22.3:ServiceMonitor 示例
1 | # 1. 部署一个带 metrics 的应用 |
练习 22.4:自定义告警规则
1 | # 创建 PrometheusRule |
🐛 排错练习(30 分钟)
场景:Prometheus 无法采集指标
1 | # 1. 检查 ServiceMonitor 是否创建 |
🏆 赛题模拟(40 分钟)
⚠️ 严格限时 40 分钟
题目:监控体系部署与配置
1 | 【操作要求】 |
📋 命令速查
| 命令 | 功能 | 注解 |
|---|---|---|
kubectl get --raw /metrics |
查看 apiserver 指标 | 原生 Prometheus 格式,kube-state-metrics 的补充 |
kubectl top nodes |
查看节点实时资源用量 | 依赖 metrics-server |
kubectl top pods -A --sort-by=cpu |
按 CPU 排序 Pod 用量 | 按 --sort-by=memory 可改为内存排序 |
kubectl port-forward -n monitoring svc/prometheus-server 9090:80 |
本地访问 Prometheus UI | 无 Ingress 时的快捷方式 |
kubectl port-forward -n monitoring svc/grafana 3000:80 |
本地访问 Grafana | 默认账密 admin/admin |
kubectl -n monitoring logs -l app=prometheus --tail=50 |
查看 Prometheus 日志 | Prometheus 启动失败时首要排查 |
kubectl -n monitoring exec -it prometheus-<pod> -- promtool query instant http://localhost:9090 'up' |
Prometheus CLI 查询 | promtool 是 Prometheus 自带的调试工具 |
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts |
添加 Prometheus Helm 仓库 | kube-prometheus-stack 包含 Prometheus+Grafana+AlertManager+NodeExporter |
helm install prometheus prometheus-community/kube-prometheus-stack -n monitoring --create-namespace |
一键安装监控全家桶 | Helm Chart 安装是最快捷的方式 |
kubectl get servicemonitor -A |
列出 ServiceMonitor | Prometheus Operator CRD,定义采集目标 |
kubectl get prometheusrules -A |
列出告警规则 | Prometheus Operator CRD |
kubectl get alertmanager -A |
列出 AlertManager 实例 | Prometheus Operator CRD |
kubectl -n kube-system logs -l k8s-app=metrics-server --tail=20 |
查看 metrics-server 日志 | kubectl top 不可用时的排错入口 |
📚 参考来源
| 来源 | 链接 / 说明 |
|---|---|
| Prometheus 官方文档 | https://prometheus.io/docs/ |
| Prometheus Operator 文档 | https://prometheus-operator.dev/ |
| kube-prometheus-stack Helm Chart | https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack |
| Grafana 官方文档 | https://grafana.com/docs/ |
| Kubernetes 官方:监控 | https://kubernetes.io/docs/tasks/debug/debug-cluster/resource-metrics-pipeline/ |
| PromQL 教程 | https://prometheus.io/docs/prometheus/latest/querying/basics/ |