[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