kubernetes之Pod水平自动伸缩(HPA)

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控制器,用于监控对象资源利用率node

kubectl autoscale deployment nginx-deployment --min=2 --max=6 --cpu-percent=50 # 对nginx的deployment的对象建立HPA控制器,当CPU的使率超过50%时实现自动化扩容,支持1到6以前Pod副本数量,以使得Pod CPU使用率维持在50% 之内。

增长负载nginx

$ 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的负载状况git

[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,测试完成。github

注意 自动伸缩完成副本数量的改变可能须要几分钟的时间。api

相关文章
相关标签/搜索