kubernetes雲平台管理實戰:HPA水平自動伸縮(十一)


一、自動伸縮

1、啟動

[root@k8s-master ~]# kubectl autoscale deployment nginx-deployment --max=8 --min=2 --cpu-percent=80
deployment "nginx-deployment" autoscaled

2、查看創建

[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

3、修改副本數為1

[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

二、hpa伸縮

1、編輯horizontalpodautoscaler 文件

[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

2、修改副本為1

[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如下配置

spec:
  maxReplicas: 8
  minReplicas: 2

3、修改副本數為5

[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

看到自動伸縮的過程了吧!

三、HAP-deployment-rs-rc-pod調用原理

1、什么是hpa

Horizontal Pod Autoscaling可以根據CPU使用率或應用自定義metrics自動擴展Pod數量(支持replication controller、deployment和replica set)。

  • 控制管理器每隔30s(可以通過–horizontal-pod-autoscaler-sync-period修改)查詢metrics的資源使用情況
  • 支持三種metrics類型
    • 預定義metrics(比如Pod的CPU)以利用率的方式計算
    • 自定義的Pod metrics,以原始值(raw value)的方式計算
    • 自定義的object metrics
  • 支持兩種metrics查詢方式:Heapster和自定義的REST API
  • 支持多metrics

客戶端;

    通過kubectl創建一個horizontalPodAutoscaler對象,並存儲到etcd中

服務端:

    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.

2、示例

# 創建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

3、關系原理圖 

4、小結

1、首先最底層的資源永遠都是pod
2、比pod高級一點的資源叫rc副本控制器
3、在pod上面有一個高級的rs
4、那誰來管理rs呢?是deployment
5、HPA自動管理deployment,deployment設置為1,HPA最低設置為3,deployment這就會被自動設計為3個

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM