2-8 k8s集群内pod的自动扩缩容技术(HPA)

1、HPA的简要介绍

HPA(Horizontal Pod Autoscaler)是Kubernetes中的一项重要服务,用于根据应用程序的负载自动调整Pod的数量。它通过监控Pod的资源使用情况,如CPU利用率或内存使用量,以及其他自定义指标,来动态地增加或减少Pod副本数量,以确保应用程序能够在不同的负载条件下保持良好的性能和响应能力。当负载升高时,HPA会自动创建更多的Pod来处理增加的请求,而当负载降低时,它会自动减少Pod的数量,从而实现资源的高效利用,避免资源浪费或因资源不足导致的服务性能下降。HPA使得Kubernetes集群能够根据实际的业务需求自动扩展或收缩应用程序,提高了集群的弹性和自动化水平,有助于保障应用的高可用性和稳定性,同时也降低了人工管理的成本和复杂性。

2、部署HPA

清除master上的污点

kubectl describe nodes master01 | grep Taints

在这里插入图片描述
kubectl taint nodes master01 node-role.kubernetes.io/master:NoSchedule-
在这里插入图片描述
在这里插入图片描述

部署 metrics-server

//在所有 Node 节点上传 metrics-server.tar 镜像包到 /opt 目录
cd /opt/

docker load -i metrics-server.tar

在这里插入图片描述

上传components.yaml
kubectl apply -f components.yaml

在这里插入图片描述
验证
在这里插入图片描述

部署HPA

yaml文件如下

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment1
  labels:
    test: nginx1
spec:
  replicas: 1
  selector:
    matchLabels:
      test: nginx1
  template:
    metadata:
      labels:
        test: nginx1
    spec:
      containers:
        - name: centos
          image: centos:7
          command: ["/bin/bash","-c","yum -y install epel-release;yum -y install stress;sleep 3600"]
          volumeMounts:
          - name: repo
            mountPath: /etc/yum.repos.d/
          resources:
            limits:
              cpu: "0.5"
              memory: 512Mi
      volumes:
      - name: repo
        hostPath:
          path: /opt/repo
          type: DirectoryOrCreate
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-nginx1
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: deployment1
  minReplicas: 1
  maxReplicas: 5
  targetCPUUtilizationPercentage: 50

应用成功
在这里插入图片描述

3、测试pod的扩容于缩容

测试stress包有没有安装完成

在这里插入图片描述

压力测试

查看pod节点的资源使用情况
在这里插入图片描述
进入pod节点,用stress命令上压力
在这里插入图片描述
kubectl get hpa -w – 实时监控pod的数量以及资源利用率
在这里插入图片描述

结果截图(过程可能会比较漫长)

在这里插入图片描述

在这里插入图片描述

4、HPA yaml 配置文件详解


apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment1
  labels:
    test: nginx1
spec:
  replicas: 1
  selector:
    matchLabels:
      test: nginx1
  template:
    metadata:
      labels:
        test: nginx1
    spec:
      containers:
        - name: centos
          image: centos:7
          command: ["/bin/bash","-c","yum -y install epel-release;yum -y install stress;sleep 3600"]
          volumeMounts:
          - name: repo
            mountPath: /etc/yum.repos.d/
          resources:
            limits:
              cpu: "0.5"
              memory: 512Mi
      volumes:
      - name: repo
        hostPath:
          path: /opt/repo
          type: DirectoryOrCreate
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-nginx1
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: deployment1
  minReplicas: 1
  maxReplicas: 5
  targetCPUUtilizationPercentage: 50


1. **Deployment 部分**- `apiVersion: apps/v1`:指定资源的 API 版本为 `apps/v1`,适用于 Deployment 资源。
    - `kind: Deployment`:定义资源类型为 Deployment。
    - `metadata`:包含 Deployment 的元数据。
        - `name: deployment1`:Deployment 的名称为 `deployment1`- `labels`:定义 Deployment 的标签,这里标签为 `test: nginx1`- `spec`:Deployment 的规范部分。
        - `replicas: 1`:指定要创建的 Pod 副本数量为 1- `selector`:用于选择要管理的 Pod 的标签,这里匹配 `test: nginx1` 标签的 Pod。
        - `template`:定义 Pod 的模板。
            - `metadata`:Pod 模板的元数据,同样带有 `test: nginx1` 标签。
            - `spec`:Pod 模板的规范部分。
                - `containers`:定义容器的列表,这里只有一个容器。
                    - `name: centos`:容器的名称为 `centos`- `image: centos:7`:使用的镜像为 `centos:7`- `command`:在容器启动时执行的命令,这里先安装 `epel-release``stress` 包,然后让容器睡眠 3600 秒。
                    - `volumeMounts`:指定挂载的卷,这里将名为 `repo` 的卷挂载到容器内的 `/etc/yum.repos.d/` 目录。
                    - `resources`:定义容器的资源限制,这里 CPU 限制为 0.5 核,内存限制为 512Mi。
                - `volumes`:定义 Pod 中的卷,这里创建一个名为 `repo` 的卷,类型为 `hostPath`,路径为 `/opt/repo`,如果路径不存在则创建。

2. **HorizontalPodAutoscaler 部分**- `apiVersion: autoscaling/v1`:指定资源的 API 版本为 `autoscaling/v1`,适用于 HorizontalPodAutoscaler 资源。
    - `kind: HorizontalPodAutoscaler`:定义资源类型为 HorizontalPodAutoscaler。
    - `metadata`:包含 HorizontalPodAutoscaler 的元数据。
        - `name: hpa-nginx1`:HorizontalPodAutoscaler 的名称为 `hpa-nginx1`- `spec`:HorizontalPodAutoscaler 的规范部分。
        - `scaleTargetRef`:指定要自动伸缩的目标资源,这里是 `apps/v1` 版本的 `Deployment`,名称为 `deployment1`- `minReplicas: 1`:指定最小的 Pod 副本数量为 1- `maxReplicas: 5`:指定最大的 Pod 副本数量为 5- `targetCPUUtilizationPercentage: 50`:指定目标 CPU 利用率为 50%,当实际 CPU 利用率超过或低于这个值时,会自动调整 Pod 的数量。

5、总结

1、HPA扩容速度于缩容速度的对比

  • 快速扩容 到达阈值pod的数量会快速增加
  • 缩容不是立刻缩 一旦资源使用量小于阈值 pod的数量会慢慢收缩 不是一蹴而就 的

2、k8s集群中可以修改pod个数的方式总结

  1. 通过命令行定义replicas的数量,在我们k8s集群中的搭建中提到
  2. 通过编写yaml文件,相信大家一定也有所了解了
  3. HPA的自动扩缩容技术,就是本帖子所提到的技术了

至此,我们整个集群的关于pod节点的高可用就基本宣告结束了,我们介绍了service的四层负载均衡,pod的探针,pod的亲和性与污点,持久化存储(PV PVC)、ingress提供的七层的负载均衡、以及我们的HPA自动扩容与缩容。下一块将是rancher界面的搭建,本地habor镜像仓库的部署,以及最后的promethus+grafana呈现的监控。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值