Skip to content

Metrics Server

什么是 Metrics Server

Metrics Server 是集群级别的资源使用量聚合器,为 HPA 和 kubectl top 提供 CPU/Memory 数据。

安装

bash
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

# 如果 TLS 验证失败(测试环境)
kubectl patch deployment metrics-server -n kube-system \
  --type='json' \
  -p='[{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--kubelet-insecure-tls"}]'

使用

bash
# 查看节点资源使用
kubectl top nodes

# 查看 Pod 资源使用
kubectl top pods -n production
kubectl top pods -n production --sort-by=cpu
kubectl top pods -n production --sort-by=memory

# 查看容器资源使用
kubectl top pods -n production --containers

与 HPA 集成

yaml
# HPA 使用 Metrics Server 提供的数据
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
spec:
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70

注意事项

  • Metrics Server 只保留最近的数据,不适合长期存储
  • 生产环境监控应使用 Prometheus
  • Metrics Server 数据延迟约 15-60 秒

本站内容由 褚成志 整理编写,仅供学习参考