一、引言
在容器化技术普及的今天,Kubernetes(K8S)已成为编排和管理容器的事实标准。随着K8S集群规模不断扩大,应用数量和复杂度持续增加,如何有效监控K8S集群的性能状态,及时发现和解决潜在问题,成为运维团队面临的重要挑战。本文将详细介绍如何搭建一套完整的K8S性能监控系统,包括Prometheus、Grafana、Node Exporter、kube-state-metrics等核心组件的安装配置,以及关键监控指标的解读和告警规则的设置。
二、监控系统架构设计
2.1 监控系统组成
一个完整的K8S性能监控系统通常由以下组件组成:
- 数据采集层:负责收集各种监控指标
- Node Exporter:收集节点(物理机/虚拟机)的系统指标
- kube-state-metrics:收集K8S资源对象(如Pod、Deployment、Service等)的状态指标
- cAdvisor:收集容器的资源使用情况
- Prometheus Adapter:将自定义指标暴露给K8S API Server
- 数据存储层:负责存储监控数据
- Prometheus:时序数据库,存储各种监控指标
- 数据展示层:负责可视化展示监控数据
- Grafana:强大的可视化工具,支持多种数据源
- 告警系统:负责根据预设规则发出告警
- Alertmanager:处理Prometheus发送的告警
2.2 监控系统架构图
┌───────────────────────────────────────────────────────────────────┐
│ 数据展示层 │
│ ┌───────────────────────┐ │
│ │ Grafana │ │
│ └───────────────────────┘ │
└───────────────────────────┬───────────────────────────────────────┘
│
┌───────────────────────────▼───────────────────────────────────────┐
│ 数据存储层 │
│ ┌───────────────────────┐ │
│ │ Prometheus │ │
│ └───────────────────────┘ │
└───────────────────────────┬───────────────────────────────────────┘
│
┌───────────────────────────▼───────────────────────────────────────┐
│ 数据采集层 │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Node Exporter │ │kube-state-metrics│ │ cAdvisor │ │
│ └─────────────────┘ └─────────────────┘ └─────────────────┘ │
└───────────────────────────┬───────────────────────────────────────┘
│
┌───────────────────────────▼───────────────────────────────────────┐
│ 告警系统 │
│ ┌───────────────────────┐ │
│ │ Alertmanager │ │
│ └───────────────────────┘ │
└───────────────────────────────────────────────────────────────────┘
三、Prometheus安装与配置
3.1 添加Prometheus社区Helm仓库
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
3.2 创建自定义配置文件
创建prometheus-values.yaml文件,自定义Prometheus配置:
# prometheus-values.yaml
server:
persistentVolume:
enabled: true
size: 50Gi
storageClass: "local-path" # 根据实际环境修改
alertmanager:
persistentVolume:
enabled: true
size: 10Gi
storageClass: "local-path" # 根据实际环境修改
nodeExporter:
enabled: true
kubeStateMetrics:
enabled: true
pushgateway:
enabled: false # 如不需要Push Gateway,可禁用
grafana:
enabled: true
adminPassword: "admin" # 修改Grafana管理员密码
persistence:
enabled: true
size: 10Gi
storageClass: "local-path" # 根据实际环境修改
3.3 安装Prometheus
# 创建命名空间
kubectl create namespace monitoring
# 使用Helm安装Prometheus
helm install prometheus prometheus-community/kube-prometheus-stack \
-n monitoring \
-f prometheus-values.yaml
3.4 验证安装
# 查看Pod状态
kubectl get pods -n monitoring
# 查看服务状态
kubectl get services -n monitoring
四、Grafana配置与可视化
4.1 访问Grafana界面
# 端口转发,临时访问Grafana
kubectl port-forward -n monitoring service/prometheus-grafana 3000:80
# 在浏览器中访问:http://localhost:3000
# 用户名:admin
# 密码:在prometheus-values.yaml中设置的密码
4.2 添加Prometheus数据源
- 登录Grafana后,点击左侧菜单中的"Configuration" > "Data Sources"
- 点击"Add data source"
- 选择"Prometheus"
- 在"URL"字段中输入:
http://prometheus-server.monitoring.svc.cluster.local - 点击"Save & Test"
4.3 导入预定义Dashboard
Grafana提供了许多预定义的Dashboard,可以快速导入使用:
- 点击左侧菜单中的"Create" > "Import"
- 在"Import via grafana.com"字段中输入Dashboard ID
- 选择Prometheus数据源
- 点击"Import"
常用Dashboard ID:
- 3119:Kubernetes Cluster监控
- 1860:Node Exporter Full监控
- 10280:Kubernetes Workload监控
五、关键监控指标解读
5.1 节点级监控指标
- node_cpu_seconds_total:CPU使用时间,用于计算CPU利用率
- node_memory_MemAvailable_bytes:可用内存大小
- node_disk_usage_bytes:磁盘使用量
- node_network_receive_bytes_total:网络接收字节数
- node_network_transmit_bytes_total:网络发送字节数
5.2 Pod级监控指标
- container_cpu_usage_seconds_total:容器CPU使用时间
- container_memory_usage_bytes:容器内存使用量
- container_fs_usage_bytes:容器文件系统使用量
- container_network_receive_bytes_total:容器网络接收字节数
- container_network_transmit_bytes_total:容器网络发送字节数
5.3 K8S资源对象监控指标
- kube_pod_status_phase:Pod状态
- kube_deployment_spec_replicas:Deployment期望的副本数
- kube_deployment_status_replicas_available:Deployment可用的副本数
- kube_node_status_condition:节点状态条件
- kube_persistentvolumeclaim_status_phase:PVC状态
六、告警规则配置
6.1 创建告警规则文件
创建alert-rules.yaml文件,定义告警规则:
# alert-rules.yaml
groups:
- name: k8s-node.rules
rules:
- alert: NodeCPUUsageHigh
expr: 100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
for: 5m
labels:
severity: warning
annotations:
summary: "Node CPU usage high (instance {{ $labels.instance }})"
description: "CPU usage is above 80% (current value: {{ $value }})"
- alert: NodeMemoryUsageHigh
expr: node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100 < 20
for: 5m
labels:
severity: warning
annotations:
summary: "Node memory usage high (instance {{ $labels.instance }})"
description: "Memory usage is above 80% (current value: {{ $value }})"
- alert: NodeDiskUsageHigh
expr: (node_filesystem_size_bytes{fstype!~"tmpfs|squashfs|autofs"} - node_filesystem_free_bytes{fstype!~"tmpfs|squashfs|autofs"}) / node_filesystem_size_bytes{fstype!~"tmpfs|squashfs|autofs"} * 100 > 80
for: 5m
labels:
severity: warning
annotations:
summary: "Node disk usage high (instance {{ $labels.instance }}, mountpoint {{ $labels.mountpoint }})"
description: "Disk usage is above 80% (current value: {{ $value }})"
- name: k8s-pod.rules
rules:
- alert: PodCPUUsageHigh
expr: sum by (pod) (rate(container_cpu_usage_seconds_total{container!="POD", container!=""}[5m])) * 100 > 80
for: 5m
labels:
severity: warning
annotations:
summary: "Pod CPU usage high (pod {{ $labels.pod }})"
description: "CPU usage is above 80% (current value: {{ $value }})"
- alert: PodMemoryUsageHigh
expr: sum by (pod) (container_memory_usage_bytes{container!="POD", container!=""}) / sum by (pod) (container_spec_memory_limit_bytes{container!="POD", container!=""}) * 100 > 80
for: 5m
labels:
severity: warning
annotations:
summary: "Pod memory usage high (pod {{ $labels.pod }})"
description: "Memory usage is above 80% (current value: {{ $value }})"
6.2 配置Alertmanager接收告警
修改prometheus-values.yaml文件,配置Alertmanager接收告警:
alertmanager:
config:
global:
resolve_timeout: 5m
receivers:
- name: "email-notifications"
email_configs:
- to: "your-email@example.com"
from: "alertmanager@example.com"
smarthost: "smtp.example.com:587"
auth_username: "alertmanager@example.com"
auth_password: "your-password"
require_tls: true
route:
group_by: ["job"]
group_wait: 30s
group_interval: 5m
repeat_interval: 12h
receiver: "email-notifications"
6.3 应用告警规则
# 更新Prometheus配置
kubectl apply -f alert-rules.yaml -n monitoring
# 重启Prometheus Pod使配置生效
kubectl delete pod -l app=prometheus -n monitoring
七、性能监控最佳实践
7.1 监控数据保留策略
根据业务需求和存储资源,合理设置Prometheus数据保留时间:
# 在prometheus-values.yaml中添加
server:
retention: "15d" # 保留15天的数据
retentionSize: "50GB" # 数据最大存储容量
7.2 分片和远程存储
对于大规模集群,考虑使用Prometheus分片和远程存储:
# 在prometheus-values.yaml中配置远程存储
server:
remoteWrite:
- url: "http://thanos-receive.example.com/api/v1/receive"
remoteRead:
- url: "http://thanos-query.example.com/api/v1/read"
read_recent: true
7.3 告警分级和抑制
对告警进行分级管理,避免不必要的告警:
# 在alert-rules.yaml中添加告警分级
labels:
severity: critical # 可以是critical, warning, info等
7.4 定期优化和清理
定期检查和优化监控系统,清理无用的指标和告警规则。
八、故障排查与常见问题
8.1 Prometheus无法收集指标
- 检查Exporter是否正常运行
- 检查Prometheus配置中的scrape_configs
- 检查网络连接是否正常
8.2 Grafana无法显示数据
- 检查Grafana数据源配置是否正确
- 检查Prometheus服务是否正常
- 检查查询语句是否正确
8.3 告警不触发或误触发
- 检查告警规则表达式是否正确
- 检查Alertmanager配置是否正确
- 检查Prometheus和Alertmanager之间的连接是否正常
九、总结
通过搭建一套完整的K8S性能监控系统,可以实时监控集群的运行状态,及时发现和解决潜在问题,保障业务的稳定运行。本文详细介绍了如何使用Prometheus、Grafana等工具搭建K8S性能监控系统,包括组件安装、配置、可视化、告警规则设置等方面的内容,并提供了关键监控指标的解读和性能监控的最佳实践。希望通过本文的介绍,能够帮助读者建立一套适合自己业务需求的K8S性能监控系统。
所有评论(0)