验证k8s中HPA功能及测试

部署

使用yaml部署服务

apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-apache
  namespace: tools
spec:
  replicas: 1
  selector:
   matchLabels:
    app: php-apache
  template:
    metadata:
      labels:
        app: php-apache
    spec:
      containers:
        - name: php-apache
          image: registry.cn-beijing.aliyuncs.com/google_registry/hpa-example
          ports:
           - containerPort: 80
          resources:
             limits:
               cpu: 200m	#CPU利用率赫兹最大200M
             requests: 
               cpu: 200m

给刚刚新建的服务开启hpa设置

yaml部署hpa

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  namespace: tools
  name: php-apache
spec:
  maxReplicas: 5
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: php-apache
  targetCPUUtilizationPercentage: 10

这里hpa的含义为,当超过服务cpu的10%的时候,进行扩容,最小存在节点数为1个,最多5个

测试

起一个测试容器

apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox
  namespace: tools
spec:
  selector:
    matchLabels:
      app: busybox
  replicas: 1
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
      - name: busybox
        image: busybox:1.32
        imagePullPolicy: IfNotPresent
        args:
        - /bin/sh
        - -c
        - sleep 3600
      imagePullSecrets:
      - name: default-secret

进入容器控制台,执行测试命令:

while true; do wget -q -O- 容器组ip:80; done

在这里插入图片描述

观察

查看hpa,发现cpu飙升后,很快达到阈值,然后就达到了hpa的最大副本数5个

验证k8s中HPA功能及测试_第1张图片

都是刚起来的容器;

当停止测试脚本后,过一段时间,大概10分钟以内,容器会自动缩容至1个

踩坑点

参考:k8s——HPA的三个误区与避坑指南

你可能感兴趣的:(k8s,HPA)