---

title: Kubernetes 资源配额与自动扩缩容实战

keywords:

  • Kubernetes
  • HPA
  • 资源请求
  • ResourceQuota
  • LimitRange
  • 自动扩容

description: 通过可验证的清单示例理解 requests 与 limits、配额与 HPA 的协同,确保集群资源稳定与弹性。

categories:

  • 文章资讯
  • 技术教程

---

Deployment 与资源请求/限制

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: nginx:1.25
          resources:
            requests:
              cpu: "200m"
              memory: "256Mi"
            limits:
              cpu: "500m"
              memory: "512Mi"

HPA(autoscaling/v2)按 CPU 利用率扩缩容

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 60

命名空间级配额与限制示例

apiVersion: v1
kind: ResourceQuota
metadata:
  name: rq-basic
spec:
  hard:
    pods: "50"
    requests.cpu: "10"
    requests.memory: "20Gi"
    limits.cpu: "20"
    limits.memory: "40Gi"
apiVersion: v1
kind: LimitRange
metadata:
  name: lr-default
spec:
  limits:
    - type: Container
      default:
        cpu: "500m"
        memory: "512Mi"
      defaultRequest:
        cpu: "200m"
        memory: "256Mi"
      max:
        cpu: "2"
        memory: "2Gi"
      min:
        cpu: "100m"
        memory: "128Mi"

验证要点

  • 安装 metrics-server 以使 HPA 可获取资源使用率。
  • 使用 kubectl describe hpa/web-hpa 观察当前指标与扩缩容事件。
  • 使用 kubectl top pods 验证 CPU/内存使用情况。

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部