🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
HorizontalPodAutoscal(HPA) 通过追踪分析 RC 控制的所有目标 Pod 的负载变化情况,来确定是否需要针对性地调整目标 Pod 的副本数,这是 HPA(横向扩容) 的实现原理。 <br/> Kubernetes 对 Pod 扩容与缩容提供了手动和自动两种模式。 手动模式通过 `kubectl scale` 命令对一个 Deployment/RC 进行 Pod 副本数量的设置。 自动模式则需要用户根据某个性能指标或者自定义业务指标,并指定 Pod 副本数量的范围,系统将自动在这个范围内根据性能指标的变化进行调整。 <br/> **1. 手动扩容和缩容** ```shell # frontend 为 deployment 名字 $ kubectl scale deployment frontend --replicas 1 ``` <br/> **2. 自动扩容** 采用 HPA 进行自动扩容/缩容。 ```yaml apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata: name: php-apache namespace: default spec: scaleTargetRef: apiVersion: apps/v1beta1 kind: Deployment name: php-apache minReplicas: 1 maxReplicas: 10 metrics: - type: Resource resource: name: cpu targetAverageUtilization: 50 - type: Pods pods: metricName: packets-per-second targetAverageValue: 1k - type: Object object: metricName: requests-per-second target: apiVersion: extensions/v1beta1 kind: Ingress name: main-route targetValue: 10k status: observedGeneration: 1 lastScaleTime: <some-time> currentReplicas: 1 desiredReplicas: 1 currentMetrics: - type: Resource resource: name: cpu currentAverageUtilization: 0 currentAverageValue: 0 ``` **** 参考文档:https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/