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,測試完成。
注意 自動伸縮完成副本數量的改變可能需要幾分鍾的時間。