一、啓動php
[root@k8s-master ~]# kubectl autoscale deployment nginx-deployment --max=8 --min=2 --cpu-percent=80 deployment "nginx-deployment" autoscaled
二、查看建立node
[root@k8s-master ~]# kubectl get all NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deploy/nginx-deployment 2 2 2 2 13h NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE hpa/nginx-deployment Deployment/nginx-deployment 80% 0% 2 8 17s NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/kubernetes 10.254.0.1 <none> 443/TCP 2d svc/nginx 10.254.145.15 <nodes> 80:32000/TCP 1d NAME DESIRED CURRENT READY AGE rs/nginx-deployment-2950479891 0 0 0 13h rs/nginx-deployment-3113009173 2 2 2 13h NAME READY STATUS RESTARTS AGE po/nginx-deployment-3113009173-h5plc 1/1 Running 0 17s po/nginx-deployment-3113009173-vckhg 1/1 Running 1 13h
三、修改副本數爲1nginx
[root@k8s-master ~]# kubectl edit deployment nginx-deployment 修改成1 replicas: 1 deployment "nginx-deployment" edited [root@k8s-master ~]# kubectl get all NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deploy/nginx-deployment 1 1 1 1 13h NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/kubernetes 10.254.0.1 <none> 443/TCP 2d svc/nginx 10.254.145.15 <nodes> 80:32000/TCP 1d NAME DESIRED CURRENT READY AGE rs/nginx-deployment-2950479891 0 0 0 13h rs/nginx-deployment-3113009173 1 1 1 13h NAME READY STATUS RESTARTS AGE po/nginx-deployment-3113009173-vckhg 1/1 Running 1 13h
[root@k8s-master ~]# kubectl get horizontalpodautoscaler NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE nginx-deployment Deployment/nginx-deployment 80% 0% 2 8 1m # Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata: creationTimestamp: 2019-01-22T01:00:02Z name: nginx-deployment namespace: default resourceVersion: "41194" selfLink: /apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers/nginx-deployment uid: 0c897472-1de1-11e9-9773-000c292bd9e1 spec: maxReplicas: 8 minReplicas: 2 scaleTargetRef: apiVersion: extensions/v1beta1 kind: Deployment name: nginx-deployment targetCPUUtilizationPercentage: 80 status: currentCPUUtilizationPercentage: 0 currentReplicas: 2 desiredReplicas: 2 lastScaleTime: 2019-01-22T01:00:02Z
[root@k8s-master ~]# kubectl edit deployment nginx-deployment replicas: 1 deployment "nginx-deployment" edited [root@k8s-master ~]# kubectl get all NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deploy/nginx-deployment 2 2 2 2 13h NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE hpa/nginx-deployment Deployment/nginx-deployment 80% 0% 2 8 6m NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/kubernetes 10.254.0.1 <none> 443/TCP 2d svc/nginx 10.254.145.15 <nodes> 80:32000/TCP 1d NAME DESIRED CURRENT READY AGE rs/nginx-deployment-2950479891 0 0 0 13h rs/nginx-deployment-3113009173 2 2 2 13h NAME READY STATUS RESTARTS AGE po/nginx-deployment-3113009173-9hlq1 1/1 Running 0 2s po/nginx-deployment-3113009173-vckhg 1/1 Running 1 13h
明明修改成1,怎麼還有2個?是由於hpa以下配置apache
spec: maxReplicas: 8 minReplicas: 2
[root@k8s-master ~]# kubectl edit hpa nginx-deployment 修改: spec: maxReplicas: 8 minReplicas: 5 horizontalpodautoscaler "nginx-deployment" edited [root@k8s-master ~]# kubectl get all NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deploy/nginx-deployment 5 5 5 2 13h NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE hpa/nginx-deployment Deployment/nginx-deployment 80% 0% 5 8 8m NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/kubernetes 10.254.0.1 <none> 443/TCP 2d svc/nginx 10.254.145.15 <nodes> 80:32000/TCP 1d NAME DESIRED CURRENT READY AGE rs/nginx-deployment-2950479891 0 0 0 13h rs/nginx-deployment-3113009173 5 5 2 13h NAME READY STATUS RESTARTS AGE po/nginx-deployment-3113009173-97l9c 0/1 ContainerCreating 0 2s po/nginx-deployment-3113009173-9hlq1 1/1 Running 0 2m po/nginx-deployment-3113009173-qq4h8 0/1 ContainerCreating 0 2s po/nginx-deployment-3113009173-sfp8z 0/1 ContainerCreating 0 2s po/nginx-deployment-3113009173-vckhg 1/1 Running 1 13h
看到自動伸縮的過程了吧!api
一、什麼是hpa緩存
Horizontal Pod Autoscaling能夠根據CPU使用率或應用自定義metrics自動擴展Pod數量(支持replication controller、deployment和replica set)。bash
客戶端;ui
經過kubectl建立一個horizontalPodAutoscaler對象,並存儲到etcd中this
服務端:google
api server:負責接受建立hpa對象,而後存入etcd
hpa controler和其餘的controler相似,每30s同步一次,將已經建立的hpa進行一次管理(從heapster獲取監控數據,查看是否須要scale, controler的store中就保存着從始至終建立出來的hpa,當作一個緩存),watch hpa有變化也會運行。從heapster中獲取scale數據,和hpa對比,計算cup利用率等信息,而後從新調整scale。根據hpa.Spec.ScaleTargetRef.Kind(例如Deployment,而後deployment控制器在調整pod數量),調整其值,發送到apiserver存儲到etcd,而後更新hpa到etcd.
二、示例
# 建立pod和service $ kubectl run php-apache --image=gcr.io/google_containers/hpa-example --requests=cpu=200m --expose --port=80 service "php-apache" created deployment "php-apache" created # 建立autoscaler $ kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10 deployment "php-apache" autoscaled $ kubectl get hpa NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE php-apache Deployment/php-apache/scale 50% 0% 1 10 18s # 增長負載 $ kubectl run -i --tty load-generator --image=busybox /bin/sh Hit enter for command prompt $ while true; do wget -q -O- http://php-apache.default.svc.cluster.local; done # 過一會就能夠看到負載升高了 $ kubectl get hpa NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE php-apache Deployment/php-apache/scale 50% 305% 1 10 3m # autoscaler將這個deployment擴展爲7個pod $ kubectl get deployment php-apache NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE php-apache 7 7 7 7 19m # 刪除剛纔建立的負載增長pod後會發現負載下降,而且pod數量也自動降回1個 $ kubectl get hpa NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE php-apache Deployment/php-apache/scale 50% 0% 1 10 11m $ kubectl get deployment php-apache NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE php-apache 1 1 1 1 27m
四、小結
一、首先最底層的資源永遠都是pod
二、比pod高級一點的資源叫rc副本控制器
三、在pod上面有一個高級的rs
四、那誰來管理rs呢?是deployment
五、HPA自動管理deployment,deployment設置爲1,HPA最低設置爲3,deployment這就會被自動設計爲3個