在k8s中,怎么模拟负载,利用HPA进行自动扩缩容的场景?


1、通过下面的定义创建应用的pod

apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-apache
spec:
  selector:
    matchLabels:
      run: php-apache
  template:
    metadata:
      labels:
        run: php-apache
    spec:
      containers:
      - name: php-apache
        image: 172.20.58.152/tools/hpa-example:latest
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: 500m
          requests:
            cpu: 200m
---
apiVersion: v1
kind: Service
metadata:
  name: php-apache
  labels:
    run: php-apache
spec:
  ports:
  - port: 80
  selector:
    run: php-apache

 

 

2、通过下面的服务,模拟负载

 

kubectl run -i --tty load-generator --rm \
  --image=172.20.58.152/middleware/busybox:latest \
  --restart=Never -- /bin/sh -c \
  "while sleep 0.01; do wget -q -O- http://php-apache; done"

 

 

 

 

3、通过下面的命令,查看HPA,应用负载的变化,实例数的变化

kubectl get hpa php-apache --watch

 

 

其中:

 

36%是已经扩容到7个实例之后,每个pod的平均cpu使用率

posted @ 2023-03-14 17:33  Zhai_David  阅读(99)  评论(0编辑  收藏  举报