Horizontal Pod Autoscaling可以根据CPU利用率自动伸缩一个Replication Controller、Deployment 或者Replica Set中的Pod数量。
Horizontal Pod Autoscaler需要使用Heapster所收集到的 度量数据,请确保Heapster被正确部署到Kubernetes集群中。
使用nginx测试
1)创建deployment和service
[root@node-01 ~]# cat deployment-nginx.yaml apiVersion: apps/v1beta1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 2 # tells deployment to run 2 pods matching the template template: # create pods using pod definition in this template metadata: labels: app: nginx spec: nodeSelector: app: nginx containers: - name: nginx image: nginx:1.8 ports: - containerPort: 80 livenessProbe: httpGet: path: / port: 80 initialDelaySeconds: 10 timeoutSeconds: 2 periodSeconds: 10 resources: limits: cpu: 200m memory: 30Mi requests: cpu: 100m memory: 20Mi --- apiVersion: v1 kind: Service metadata: name: nginx-deployment labels: app: nginx-deployment spec: ports: - port: 80 protocol: TCP selector: app: nginx
[root@node-01 ~]# kubectl apply -f deployment-nginx.yaml
创建一个HPA控制器,用于监控对象资源利用率
kubectl autoscale deployment nginx-deployment --min=2 --max=6 --cpu-percent=50 # 对nginx的deployment的对象创建HPA控制器,当CPU的使率超过50%时实现自动化扩容,支持1到6之前Pod副本数量,以使得Pod CPU使用率维持在50% 以内。
增加负载
$ kubectl run -i --tty load-generator --image=busybox /bin/sh Hit enter for command prompt $ while true; do wget -q -O- http://nginx-deployment; done
检查pod的负载情况
[root@node-01 ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE nginx-deployment Deployment/nginx-deployment 4%/20% 2 5 2 28h [root@node-01 ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE nginx-deployment Deployment/nginx-deployment 64%/20% 2 5 5 28h
同时看到replicas已经增加到了5,测试完成。
注意 自动伸缩完成副本数量的改变可能需要几分钟的时间。