从云原生走向 AI 原生:一套面向未来的架构方法论 → 阅读《AI 原生基础设施》

监控与可观测性

已发行

HAMi 提供端到端的可观测性:Prometheus 指标、Grafana 仪表盘、WebUI 和增强事件,帮助你掌握 GPU 资源使用全貌。

监控体系概览

图 1: 监控体系概览
图 1: 监控体系概览

HAMi 的可观测性覆盖四个层面:

层面能力用途
指标Prometheus 端点GPU 资源使用、调度性能
可视化Grafana + WebUI实时监控、资源规划
事件Pod 事件增强调度结果追踪、问题定位
日志分级日志调试、故障排查

Prometheus 指标

GPU 资源指标

指标名含义标签
hami_vgpu_memory_usage_bytesGPU 显存使用量pod, namespace, device, container
hami_vgpu_utilization_percentGPU 算力使用率pod, namespace, device, container
hami_gpu_memory_total_bytes物理 GPU 显存总量node, device
hami_gpu_memory_used_bytes物理 GPU 显存使用量node, device

调度器指标

指标名含义
hami_scheduler_filter_duration_secondsFilter 阶段耗时
hami_scheduler_score_duration_secondsScore 阶段耗时
hami_scheduler_bind_duration_secondsBind 阶段耗时
hami_scheduler_bind_success_total绑定成功次数
hami_scheduler_bind_failure_total绑定失败次数

设备插件指标

指标名含义
hami_device_plugin_allocate_duration_secondsAllocate 耗时
hami_device_plugin_device_count注册设备数量
hami_device_plugin_healthy_status设备健康状态

指标端点配置

# values.yaml
monitor:
  enabled: true
  service:
    type: ClusterIP
    port: 41991
  serviceMonitor:
    enabled: true
    namespace: monitoring
    interval: 15s
    path: /metrics
# 手动查询指标
kubectl port-forward -n hami-system svc/hami-monitor 41991:41991
curl http://localhost:41991/metrics

Grafana 仪表盘

部署 Grafana

如果集群中没有 Grafana:

helm repo add grafana https://grafana.github.io/helm-charts
helm install grafana grafana/grafana -n monitoring --create-namespace

导入 HAMi 仪表盘

  1. 打开 Grafana UI
  2. 导航到 Dashboards → Import
  3. 输入 HAMi 仪表盘 JSON(或从 GitHub 获取)
  4. 选择 Prometheus 数据源
  5. 点击 Import

关键面板

  • GPU 集群概览:节点 GPU 总量、使用量、利用率
  • Pod 资源使用:每个 Pod 的显存和算力占用
  • 调度器性能:Filter/Score/Bind 耗时分布
  • 设备健康 GPU:健康状态和故障告警

WebUI

HAMi 提供内置 WebUI,直观展示 GPU 资源分配情况。

访问方式

# Port-forward 访问
kubectl port-forward -n hami-system svc/hami-webui 8080:80

# 浏览器访问
# http://localhost:8080

功能

  • 集群 GPU 资源总览
  • 节点级 GPU 使用详情
  • Pod 级资源分配视图
  • 实时刷新

事件增强

HAMi 在 Pod 事件中注入调度详情,帮助快速定位问题。

查看调度事件

kubectl describe pod <pod-name> -n <namespace>

事件中包含:

Events:
  Type    Reason           Age   From             Message
  ----    ------           ----  ----             -------
  Normal  Scheduled        10s   hami-scheduler   Successfully assigned pod to node-01
                                              GPU: 1/3, Memory: 4000MB
                                              Score: 85, Policy: binpack
  Normal  DeviceAllocated  10s   hami-device-plugin  Allocated GPU-0 on node-01

失败事件示例

Events:
  Type     Reason            Age   From             Message
  ----     ------            ----  ----             -------
  Warning  FailedScheduling  5s    hami-scheduler   0/3 nodes available:
    node-01: GPU memory insufficient (requested 8000MB, available 4000MB)
    node-02: device type mismatch (requested A100, has T4)
    node-03: quota exceeded (namespace ai-team)

日志系统

调度器日志

# 查看调度器日志
kubectl logs -n hami-system deployment/hami-scheduler

# 增加日志级别
kubectl edit deployment -n hami-system hami-scheduler
# 添加参数: --v=5

日志级别

级别内容用途
v=4节点摘要日常运维
v=5设备细节、评分过程问题排查
v=6详细调试信息深度调试

设备插件日志

kubectl logs -n hami-system daemonset/hami-device-plugin -c nvidia

结构化日志

调度器输出标准化错误码,便于自动化分析:

E0604 scheduler.go:724] Failed to schedule pod
  error_code=SCHED_INSUFFICIENT_GPU_MEMORY
  pod=ai-inference
  node=node-01
  requested=8000MB
  available=4000MB

告警规则

GPU 资源告警

apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: hami-gpu-alerts
  namespace: monitoring
spec:
  groups:
  - name: hami-gpu
    rules:
    - alert: GPUMemoryUsageHigh
      expr: hami_gpu_memory_used_bytes / hami_gpu_memory_total_bytes > 0.95
      for: 10m
      labels:
        severity: warning
      annotations:
        summary: "GPU memory usage exceeds 95% on {{ $labels.node }}"

    - alert: GPUNoAvailableMemory
      expr: (hami_gpu_memory_total_bytes - hami_gpu_memory_used_bytes) < 1073741824
      for: 5m
      labels:
        severity: critical
      annotations:
        summary: "Less than 1GB GPU memory available on {{ $labels.node }}"

调度异常告警

    - alert: ScheduleFailureRate
      expr: rate(hami_scheduler_bind_failure_total[5m]) / rate(hami_scheduler_bind_success_total[5m]) > 0.1
      for: 10m
      labels:
        severity: warning
      annotations:
        summary: "HAMi scheduler failure rate exceeds 10%"

系统健康检查

健康端点

# 调度器健康检查
curl http://hami-scheduler:8080/healthz

# 就绪检查
curl http://hami-scheduler:8080/readyz

自动化检查脚本

#!/bin/bash
# hami-health-check.sh

echo "=== HAMi Health Check ==="

# 1. 检查 Pod 状态
echo "--- Component Status ---"
kubectl get pods -n hami-system -o wide

# 2. 检查调度器健康
echo "--- Scheduler Health ---"
kubectl exec -n hami-system deployment/hami-scheduler -- wget -qO- http://localhost:8080/healthz

# 3. 检查 GPU 节点
echo "--- GPU Nodes ---"
kubectl get nodes -l accelerator=nvidia -o wide

# 4. 检查设备注册
echo "--- Device Registration ---"
for node in $(kubectl get nodes -l accelerator=nvidia -o name); do
  echo "$node:"
  kubectl get $node -o jsonpath='{.status.allocatable}' | grep hami
done

小结

本章介绍了 HAMi 的监控与可观测性体系:

  • Prometheus 指标:GPU 资源、调度器、设备插件多维度指标
  • Grafana 仪表盘:可视化 GPU 使用和调度性能
  • WebUI:内置资源总览界面
  • 事件增强:Pod 事件中包含调度详情
  • 日志系统:分级日志和标准化错误码
  • 告警规则:资源、调度和设备健康告警

参考资料

创建于 2026/06/04 更新于 2026/06/04 1195 字 阅读约 3 分钟