技术 · 思考 · 实践 · 成长

首页/K8S/正文

Kubernetes 调度与扩缩容:亲和性、污点与 HPA

K8S5 次阅读

Pod 放哪台节点(调度)、忙了怎么办(扩缩容)—— 本文两大主题,结尾附弹性伸缩小案例。

一、nodeSelector(简单调度)

spec:
  nodeSelector: { disktype: ssd, zone: east }    # 节点标签匹配才调度
kubectl label node node1 disktype=ssd zone=east

二、亲和性(Affinity)

spec:
  affinity:
    nodeAffinity:                        # 节点亲和
      requiredDuringSchedulingIgnoredDuringExecution:    # 硬性
        nodeSelectorTerms:
        - matchExpressions:
          - { key: zone, operator: In, values: [east] }
      preferredDuringSchedulingIgnoredDuringExecution:   # 软性
      - weight: 100
        preference: { matchExpressions: [{ key: disktype, operator: In, values: [ssd] }] }
    podAntiAffinity:                     # Pod 反亲和(副本分散)
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          topologyKey: kubernetes.io/hostname
          labelSelector: { matchLabels: { app: myapp } }

反亲和让同一应用的副本分散在不同节点 —— 高可用标配。

三、污点与容忍

# 给节点打污点(禁止普通 Pod 调上来)
kubectl taint node node1 dedicated=gpu:NoSchedule
kubectl taint node node1-             # 去掉污点
spec:
  tolerations:                          # Pod 容忍污点
  - key: "dedicated"
    operator: "Equal"
    value: "gpu"
    effect: "NoSchedule"

典型:GPU 节点打污点,只有 GPU 任务容忍 —— 专用节点隔离。

四、HPA 自动扩缩容

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: { name: myapp-hpa }
spec:
  scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: myapp }
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target: { type: Utilization, averageUtilization: 70 }   # CPU 70% 扩
  - type: Resource
    resource:
      name: memory
      target: { type: Utilization, averageUtilization: 80 }
kubectl apply -f hpa.yaml
kubectl get hpa -w                    # 观察扩缩
kubectl top pods                      # 需要 metrics-server
# 装 metrics-server:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

前提:装 metrics-server、设置 requests(HPA 按 requests 算百分比)。

五、手动 vs 自动

方式场景
kubectl scale临时/预知活动
HPA流量波动的 Web 服务
VPA(垂直)调单 Pod 资源(进阶)
Cluster Autoscaler节点不够自动加机器(云)

六、注意

  • HPA 依赖 requests(没设就无法计算);
  • 缩容有冷却期(防抖动);
  • 有状态服务(DB)别用 HPA;
  • 扩容前确认节点资源够(否则 Pending)。

七、小案例:弹性伸缩配置

apiVersion: apps/v1
kind: Deployment
metadata: { name: web }
spec:
  replicas: 3
  selector: { matchLabels: { app: web } }
  template:
    metadata: { labels: { app: web } }
    spec:
      affinity:
        podAntiAffinity:                      # 副本分散
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              topologyKey: kubernetes.io/hostname
              labelSelector: { matchLabels: { app: web } }
      containers:
      - name: web
        image: nginx:1.25
        resources:
          requests: { cpu: "200m", memory: "256Mi" }    # HPA 基准
          limits:   { cpu: "1",    memory: "1Gi" }
        readinessProbe:
          httpGet: { path: /, port: 80 }
          periodSeconds: 5
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: { name: web-hpa }
spec:
  scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: web }
  minReplicas: 2
  maxReplicas: 20
  metrics:
  - type: Resource
    resource: { name: cpu, target: { type: Utilization, averageUtilization: 70 } }
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300        # 缩容防抖 5 分钟
# 压测观察自动扩容
kubectl run load --image=busybox -- /bin/sh -c "while true; do wget -q -O- http://web; done"
kubectl get hpa web-hpa -w

requests 基准 + 反亲和分散 + HPA 70% 扩 + 缩容防抖 —— 弹性伸缩的完整配置。

八、总结

nodeSelector/亲和性控制调度、反亲和分散副本、污点隔离专用节点;HPA 按 requests 百分比扩缩(配 metrics-server);缩容防抖、有状态不用 HPA。

凡人做一事,便须全副精神注在此一事,首尾不懈,不可见异思迁,做这样,想那样,坐这山,望那山。人而无恒,终身一无所成。——曾国藩《曾国藩家书》

评论交流

0 条评论 · 支持多级回复
验证码
暂无评论,来说说你的想法吧

相关文章

继续阅读