设置 Monitoring 使用 Prometheus and Grafana
你将构建什么
monitoring stack 使用 Prometheus collecting metrics 从你的 cluster and Grafana displaying 它们 on dashboard.
学习目标
- 部署 Prometheus in Kubernetes cluster
- 部署 Grafana and 连接它 到 Prometheus
- 创建 Grafana dashboard 到 visualize cluster metrics
- 理解如何 Prometheus scrapes metrics
前置要求
- Kubernetes cluster (or minikube) 使用 kubectl
- Helm 安装
- 基础理解的 metrics and monitoring 概念
步骤
- 1
添加 Prometheus Helm 仓库
添加 Prometheus community Helm chart 仓库 and 更新你的局部 chart 缓存.
代码helm repo add prometheus-community https://prometheus-community.github.io/helm-charts helm repo update预期结果
Prometheus Helm 仓库是 添加 and 更新.
- 2
安装 Prometheus
Use Helm 到安装 Prometheus stack (包含 Prometheus, Alertmanager, and Grafana) in 新命名空间.
代码kubectl create namespace monitoring helm install prometheus prometheus-community/kube-prometheus-stack -n monitoring预期结果
Prometheus and Grafana 是 deployed in monitoring 命名空间.
- 3
Access Grafana
Port-向前 Grafana service 到 access 它 locally. 默认用户名是 'admin' and 密码是 'prom-运算符'.
代码kubectl port-forward svc/prometheus-grafana -n monitoring 3000:80预期结果
打开 http://localhost:3000 and log in 到 Grafana 使用 admin/prom-运算符.
- 4
Explore dashboards
kube-prometheus-stack comes 使用 pre-构建 dashboards. Go 到 Dashboards in Grafana and explore pre-配置 Kubernetes monitoring dashboards.
预期结果
你 see dashboards showing cluster CPU, memory, network, and Pod metrics.
- 5
添加健康检查端点
为应用添加 /health 端点返回 200 OK,供编排系统(Kubernetes、负载均衡器)探测存活。健康检查是生产部署的基础设施。
代码# app.py (Flask example) from flask import Flask, jsonify app = Flask(__name__) @app.route("/health") def health(): return jsonify({"status": "ok"}), 200 @app.route("/ready") def ready(): # Check DB connection etc. return jsonify({"ready": True}), 200预期结果
curl localhost:5000/health 返回 {"status":"ok"} 和 200 状态码。
自己动手试试
你学到了什么
你 deployed 完整 monitoring stack 使用 Prometheus and Grafana. 这个 gives 你 visibility 到你的 cluster health and 性能, 哪个是 必要 for 运行 production 系统.
相关资源
相关术语
- Prometheus
Metrics 集合系统
- Grafana
Visualization and dashboards
相关术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
- Prometheus
Metrics 集合系统
- Grafana
Visualization and dashboards
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。