ANALYSIS
在 K8s 上部署 Prometheus 和 Grafana
🔧 准备工作
环境要求
- 操作系统: Ubuntu 20.04+
- Kubernetes: 正常运行的集群
- Helm: 3.x 版本
- kubectl: 已配置且可用
安装必要工具
PRTCL // BASH
# 安装 Helm 3curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
# 安装 kubectl(如未安装)sudo snap install kubectl --classic
# 验证安装helm versionkubectl cluster-info创建监控命名空间
PRTCL // BASH
kubectl create namespace monitoring📦 部署 Prometheus
添加 Helm 仓库
PRTCL // BASH
helm repo add prometheus-community https://prometheus-community.github.io/helm-chartshelm repo update安装 Prometheus Stack
PRTCL // BASH
helm install prometheus prometheus-community/kube-prometheus-stack \ --namespace monitoring \ --set prometheus.service.type=NodePort \ --set prometheus.service.nodePort=30090自定义配置(可选)
创建 values.yaml:
PRTCL // YAML
prometheus: prometheusSpec: retention: 15d resources: requests: memory: "512Mi" cpu: "500m" storageSpec: volumeClaimTemplate: spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 10Gi应用配置:
PRTCL // BASH
helm upgrade prometheus prometheus-community/kube-prometheus-stack \ --namespace monitoring \ -f values.yaml🎨 配置 Grafana
暴露 Grafana 服务
PRTCL // BASH
helm upgrade prometheus prometheus-community/kube-prometheus-stack \ --namespace monitoring \ --set grafana.service.type=NodePort \ --set grafana.service.nodePort=30000获取访问凭证
PRTCL // BASH
# 获取管理员密码kubectl get secret -n monitoring prometheus-grafana \ -o jsonpath='{.data.admin-password}' | base64 --decode// …existing code…
🔄 持久化存储配置
创建持久卷声明(PVC)
PRTCL // YAML
apiVersion: v1kind: PersistentVolumeClaimmetadata: name: grafana-pvc namespace: monitoringspec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi更新 Grafana 配置
PRTCL // YAML
grafana: persistence: enabled: true existingClaim: grafana-pvc size: 10Gi🌐 配置 Ingress 访问
创建 Ingress 规则
PRTCL // YAML
apiVersion: networking.k8s.io/v1kind: Ingressmetadata: name: monitoring-ingress namespace: monitoring annotations: nginx.ingress.kubernetes.io/rewrite-target: /spec: rules: - host: grafana.example.com http: paths: - path: / pathType: Prefix backend: service: name: prometheus-grafana port: number: 80应用 Ingress 配置
PRTCL // BASH
kubectl apply -f ingress.yaml📊 导入 Dashboard
常用 Dashboard ID
| 用途 | Dashboard ID | 说明 |
|---|---|---|
| 节点监控 | 1860 | Node Exporter 全量指标 |
| 集群概览 | 13105 | Kubernetes 集群监控 |
| 资源使用 | 8685 | Pod 资源使用详情 |
| 网络监控 | 12175 | 网络流量分析 |
导入步骤
- 访问 Grafana UI
- 点击
+>Import - 输入 Dashboard ID
- 选择数据源(默认
Prometheus) - 点击
Import
🔍 监控验证
检查组件状态
PRTCL // BASH
# 检查 Pod 状态kubectl get pods -n monitoring
# 检查服务状态kubectl get svc -n monitoring
# 查看资源使用kubectl top nodes验证数据采集
PRTCL // BASH
# 查看目标状态curl -s http://< 节点 IP>:30090/targets | grep "UP"
# 检查告警规则kubectl get prometheusrules -n monitoring⚠️ 常见问题排查
- Pod 无法启动
PRTCL // BASH
# 查看详细错误kubectl describe pod <pod-name> -n monitoring
# 查看容器日志kubectl logs <pod-name> -n monitoring- 数据采集异常
- 检查 ServiceMonitor 配置
- 验证标签选择器
- 确认端口暴露正确
- Grafana 访问问题
- 确认服务暴露方式
- 检查 Ingress 配置
- 验证网络策略
📝 运维建议
- 定期备份 Grafana 配置
PRTCL // BASH
kubectl cp monitoring/prometheus-grafana-xxx:/var/lib/grafana ./grafana-backup- 监控资源使用
PRTCL // BASH
# 设置资源告警kubectl apply -f prometheus-rules.yaml- 日志轮转配置
PRTCL // YAML
prometheus: prometheusSpec: retention: 15d retentionSize: "10GB"🔗 参考资源
作者:EchoWang
小红书:汪多多是只猫
B 站:汪多多是只猫
公众号:汪多多是只猫
R P
Rhine Lab Pioneer Division
Auth_Verified: 2026.04.08
Auth_Verified: 2026.04.08
