实验:k8s 利用HPA进行弹性扩缩容

椰子ya Linux 2021-05-08

官方文档

案例1

现在,php-apache 服务器已经运行,我们将通过 kubectl autoscale 命令创建 Horizontal Pod Autoscaler。 以下命令将创建一个 Horizontal Pod Autoscaler 用于控制我们上一步骤中创建的 Deployment,使 Pod 的副本数量维持在 1 到 10 之间。 大致来说,HPA 将(通过 Deployment)增加或者减少 Pod 副本的数量以保持所有 Pod 的平均 CPU 利用率在 50% 左右(由于每个 Pod 请求 200 毫核的 CPU,这意味着平均 CPU 用量为 100 毫核)。 算法的详情请参阅相关文档。

pod中必须定义

resources:
          limits:
            cpu: 500m
          requests:
            cpu: 200m

运行中的容器增加HPA

kubectl autoscale deployment nginx-deployment --cpu-percent=20 --min=1 --max=3

增加负载 

kubectl run -i --tty load-generator --rm --image=busybox --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://nginx-deployment; done"

限制写高了 跑了半天毫无波动哈哈哈哈

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx-deployment
  minReplicas: 1
  maxReplicas: 2
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 50

17、Utilization 百分比数值

     AverageValue绝对数值

PREV
实验:利用Kubeadm部署高可用Kubernetes
NEXT
实验:利用helm部署Prometheus和Grafana Labs监控K8S