Kubernetes Pod垂直自動スケーリング(VPA)について話す

Kubernetes Pod垂直自動スケーリング(VPA)について話す

スコフィールド初心者の運用と保守の話

VPAの概要


VPAのフルネームはVerticalPod Autoscalerで、これは垂直ポッドの自動スケーリングを意味します。コンテナのリソース使用量に応じてCPUとメモリのリクエストを自動的に設定するため、ノードで適切なスケジューリングを行って各ポッドに適切なリソースを提供できます。
リソースを過剰に要求するコンテナを縮小するだけでなく、その使用状況に応じていつでも不十分なリソースの容量を増やすことができます。
PS:VPAはポッドのリソース制限を変更しません。

あまりナンセンスではありません。画像に移動して、VPAワークフローを参照してください。

Kubernetes Pod垂直自動スケーリング(VPA)について話す

次に、実際の戦闘を開始します

メトリックサーバーのデプロイ


1.デプロイメントマニフェストファイルをダウンロードします


wget  https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.7/components.yaml

2.components.yamlファイルを変更します

  • ミラーアドレスを変更しました。gcr.ioは私自身の倉庫です
  • メトリックサーバーの起動パラメーターの引数を変更しました。そうしないと、ソースkubelet_summaryからメトリックを完全に取得できないというエラーが報告されます。

- name: metrics-server
        image: scofield/metrics-server:v0.3.7
        imagePullPolicy: IfNotPresent
        args:
          - --cert-dir=/tmp
          - --secure-port=4443
          - /metrics-server
          - --kubelet-insecure-tls
          - --kubelet-preferred-address-types=InternalIP

3.展開を実行します


kubectl  apply -f components.yaml

4.検証


[root@k8s-node001 metrics-server]# kubectl  get po -n kube-system
NAME                                       READY   STATUS    RESTARTS   AGE
metrics-server-7947cb98b6-xw6b8            1/1     Running   0          10m

能获取要top信息视为成功
[root@k8s-node001 metrics-server]# kubectl  top nodes
NAME          CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
k8s-node001   618m         7%       4796Mi           15%
k8s-node003   551m         6%       5522Mi           17%
k8s-node004   308m         3%       5830Mi           18%
k8s-node005   526m         6%       5997Mi           38%
k8s-node002   591m         7%       5306Mi           33%

垂直ポッドオートスケーラーをデプロイする


1.オートスケーラープロジェクトのクローンを作成します


git clone https://github.com/kubernetes/autoscaler.git

2.デプロイメントファイルを変更し、gcrウェアハウスを自分のウェアハウスに変更します


admission-controller-deployment.yaml
us.gcr.io/k8s-artifacts-prod/autoscaling/vpa-admission-controller:0.8.0
改为
scofield/vpa-admission-controller:0.8.0

recommender-deployment.yaml
us.gcr.io/k8s-artifacts-prod/autoscaling/vpa-recommender:0.8.0
改为
image: scofield/vpa-recommender:0.8.0

updater-deployment.yaml
us.gcr.io/k8s-artifacts-prod/autoscaling/vpa-updater:0.8.0
改为
scofield/vpa-updater:0.8.0

3.展開


[root@k8s-node001 vertical-pod-autoscaler]#  cd autoscaler/vertical-pod-autoscaler
[root@k8s-node001 vertical-pod-autoscaler]#  ./hack/vpa-up.sh
Warning: apiextensions.k8s.io/v1beta1 CustomResourceDefinition is deprecated in v1.16+, unavailable in v1.22+; use apiextensions.k8s.io/v1 CustomResourceDefinition
customresourcedefinition.apiextensions.k8s.io/verticalpodautoscalers.autoscaling.k8s.io created
customresourcedefinition.apiextensions.k8s.io/verticalpodautoscalercheckpoints.autoscaling.k8s.io created
clusterrole.rbac.authorization.k8s.io/system:metrics-reader created
clusterrole.rbac.authorization.k8s.io/system:vpa-actor created
clusterrole.rbac.authorization.k8s.io/system:vpa-checkpoint-actor created
clusterrole.rbac.authorization.k8s.io/system:evictioner created
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-reader created
clusterrolebinding.rbac.authorization.k8s.io/system:vpa-actor created
clusterrolebinding.rbac.authorization.k8s.io/system:vpa-checkpoint-actor created
clusterrole.rbac.authorization.k8s.io/system:vpa-target-reader created
clusterrolebinding.rbac.authorization.k8s.io/system:vpa-target-reader-binding created
clusterrolebinding.rbac.authorization.k8s.io/system:vpa-evictionter-binding created
serviceaccount/vpa-admission-controller created
clusterrole.rbac.authorization.k8s.io/system:vpa-admission-controller created
clusterrolebinding.rbac.authorization.k8s.io/system:vpa-admission-controller created
clusterrole.rbac.authorization.k8s.io/system:vpa-status-reader created
clusterrolebinding.rbac.authorization.k8s.io/system:vpa-status-reader-binding created
serviceaccount/vpa-updater created
deployment.apps/vpa-updater created
serviceaccount/vpa-recommender created
deployment.apps/vpa-recommender created
Generating certs for the VPA Admission Controller in /tmp/vpa-certs.
Generating RSA private key, 2048 bit long modulus (2 primes)
............................................................................+++++
.+++++
e is 65537 (0x010001)
Generating RSA private key, 2048 bit long modulus (2 primes)
............+++++
...........................................................................+++++
e is 65537 (0x010001)
Signature ok
subject=CN = vpa-webhook.kube-system.svc
Getting CA Private Key
Uploading certs to the cluster.
secret/vpa-tls-certs created
Deleting /tmp/vpa-certs.
deployment.apps/vpa-admission-controller created
service/vpa-webhook created

4.結果を確認すると、metrics-serverとvpaがすでに正常に実行されていることがわかります。


[root@k8s-node001 autoscaler-master]# kubectl  get po -n kube-system
NAME                                        READY   STATUS    RESTARTS   AGE
metrics-server-7947cb98b6-xw6b8             1/1     Running   0          46m
vpa-admission-controller-7d87559549-g77h9   1/1     Running   0          10m
vpa-recommender-84bf7fb9db-65669            1/1     Running   0          10m
vpa-updater-79cc46c7bb-5p889                1/1     Running   0          10m

例1updateMode: "オフ"


1.まず、nginxサービスをデプロイし、それを名前空間にデプロイします:vpa


apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
  namespace: vpa
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources:
          requests:
            cpu: 100m
            memory: 250Mi

結果を見てください。2つのポッドが正常に実行されました。


[root@k8s-node001 examples]# kubectl  get po -n vpa
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-6884b849f7-fswx5   1/1     Running   0          5m54s
pod/nginx-6884b849f7-wz6b8   1/1     Running   0          5m54s

2.安価な圧力テストのために、NodePortタイプのサービスを作成します


[root@k8s-node001 examples]# cat  nginx-vpa-ingress.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: vpa
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx

[root@k8s-node001 examples]# kubectl  get svc -n vpa
NAME    TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
nginx   NodePort   10.97.250.131   <none>        80:32621/TCP   55s

[root@k8s-node001 examples]# curl -I   192.168.100.185:32621
HTTP/1.1 200 OK

3. VPAを作成するには、
最初にupdateModeを使用します。「オフ」モード。このモードはリソースのみを取得します。ポッドを更新しないことをお勧めします。


[root@k8s-node001 examples]# cat   nginx-vpa-demo.yaml
apiVersion: autoscaling.k8s.io/v1beta2
kind: VerticalPodAutoscaler
metadata:
  name: nginx-vpa
  namespace: vpa
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: nginx
  updatePolicy:
    updateMode: "Off"
  resourcePolicy:
    containerPolicies:
    - containerName: "nginx"
      minAllowed:
        cpu: "250m"
        memory: "100Mi"
      maxAllowed:
        cpu: "2000m"
        memory: "2048Mi"

4.展開結果を表示する


[root@k8s-node001 examples]# kubectl  get vpa -n vpa
NAME        AGE
nginx-vpa   2m34s

5. describeを使用して、主にコンテナの推奨事項に焦点を当ててvpaの詳細を表示します


[root@k8s-node001 examples]# kubectl  describe  vpa nginx-vpa   -n vpa
Name:         nginx-vpa
Namespace:    vpa
....略去10000字 哈哈......
  Update Policy:
    Update Mode:  Off
Status:
  Conditions:
    Last Transition Time:  2020-09-28T04:04:25Z
    Status:                True
    Type:                  RecommendationProvided
  Recommendation:
    Container Recommendations:
      Container Name:  nginx
      Lower Bound:
        Cpu:     250m
        Memory:  262144k
      Target:
        Cpu:     250m
        Memory:  262144k
      Uncapped Target:
        Cpu:     25m
        Memory:  262144k
      Upper Bound:
        Cpu:     803m
        Memory:  840190575
Events:          <none>

その中で


Lower Bound:                 下限值
Target:                              推荐值
Upper Bound:                 上限值
Uncapped Target:           如果没有为VPA提供最小或最大边界,则表示目标利用率
上述结果表明,推荐的 Pod 的 CPU 请求为 25m,推荐的内存请求为 262144k 字节。

6.ここで、nginxで
圧力テスト実行し、圧力テストコマンド実行します


[root@k8s-node001 examples]# ab -c 100 -n 10000000 http://192.168.100.185:32621/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.100.185 (be patient)
Completed 1000000 requests
Completed 2000000 requests
Completed 3000000 requests

7.数分でVPA推奨事項の変更を観察します


[root@k8s-node001 ~]# kubectl  describe  vpa nginx-vpa   -n vpa |tail -n 20 
  Conditions:
    Last Transition Time:  2020-09-28T04:04:25Z
    Status:                True
    Type:                  RecommendationProvided
  Recommendation:
    Container Recommendations:
      Container Name:  nginx
      Lower Bound:
        Cpu:     250m
        Memory:  262144k
      Target:
        Cpu:     476m
        Memory:  262144k
      Uncapped Target:
        Cpu:     476m
        Memory:  262144k
      Upper Bound:
        Cpu:     2
        Memory:  387578728
Events:          <none>

ここでupdateMode: "Off"を設定しているため、ポッドが更新されないため、出力情報から、VPAがポッドの推奨値を示していることがわかります。CPU:476m。

例2updateMode: "Auto"


1.ここで、updateMode: "Auto"を設定して、VPAの動作を確認します。
ここで、リソースを次のように変更しました:メモリ:50Mi、CPU:100m


[root@k8s-node001 examples]# kubectl  apply -f nginx-vpa.yaml
deployment.apps/nginx created
[root@k8s-node001 examples]# cat nginx-vpa.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
  namespace: vpa
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources:
          requests:
            cpu: 100m
            memory: 50Mi

[root@k8s-node001 examples]# kubectl  get po  -n vpa
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7ff65f974c-f4vgl   1/1     Running   0          114s
nginx-7ff65f974c-v9ccx   1/1     Running   0          114s

2. vpaを再度デプロイします。ここでは、VPAデプロイメントファイルnginx-vpa-demo.yamlは
updateMode: "Auto"と名前:nginx-vpa-2のみを変更します。


[root@k8s-node001 examples]# cat  nginx-vpa-demo.yaml
apiVersion: autoscaling.k8s.io/v1beta2
kind: VerticalPodAutoscaler
metadata:
  name: nginx-vpa-2
  namespace: vpa
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: nginx
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: "nginx"
      minAllowed:
        cpu: "250m"
        memory: "100Mi"
      maxAllowed:
        cpu: "2000m"
        memory: "2048Mi"

[root@k8s-node001 examples]# kubectl apply -f nginx-vpa-demo.yaml
verticalpodautoscaler.autoscaling.k8s.io/nginx-vpa created

[root@k8s-node001 examples]# kubectl  get vpa -n vpa
NAME        AGE
nginx-vpa-2   9s

3.再度圧力テスト


ab -c 1000 -n 100000000 http://192.168.100.185:32621/

4.数分後、describeを使用してvpaの詳細を表示し、ContainerRecommendationsのみに注意を払います。


[root@k8s-node001 ~]# kubectl  describe  vpa nginx-vpa-2    -n vpa |tail -n 30
      Min Allowed:
        Cpu:     250m
        Memory:  100Mi
  Target Ref:
    API Version:  apps/v1
    Kind:         Deployment
    Name:         nginx
  Update Policy:
    Update Mode:  Auto
Status:
  Conditions:
    Last Transition Time:  2020-09-28T04:48:25Z
    Status:                True
    Type:                  RecommendationProvided
  Recommendation:
    Container Recommendations:
      Container Name:  nginx
      Lower Bound:
        Cpu:     250m
        Memory:  262144k
      Target:
        Cpu:     476m
        Memory:  262144k
      Uncapped Target:
        Cpu:     476m
        Memory:  262144k
      Upper Bound:
        Cpu:     2
        Memory:  262144k
Events:          <none>

ターゲットはCPU:587m、メモリ:262144kになります

5.イベントイベントを見てください


[root@k8s-node001 ~]# kubectl  get event -n vpa
LAST SEEN   TYPE      REASON              OBJECT                        MESSAGE
33m         Normal    Pulling             pod/nginx-7ff65f974c-f4vgl    Pulling image "nginx"
33m         Normal    Pulled              pod/nginx-7ff65f974c-f4vgl    Successfully pulled image "nginx" in 15.880996269s
33m         Normal    Created             pod/nginx-7ff65f974c-f4vgl    Created container nginx
33m         Normal    Started             pod/nginx-7ff65f974c-f4vgl    Started container nginx
26m         Normal    EvictedByVPA        pod/nginx-7ff65f974c-f4vgl    Pod was evicted by VPA Updater to apply resource recommendation.
26m         Normal    Killing             pod/nginx-7ff65f974c-f4vgl    Stopping container nginx
35m         Normal    Scheduled           pod/nginx-7ff65f974c-hnzr5    Successfully assigned vpa/nginx-7ff65f974c-hnzr5 to k8s-node005
35m         Normal    Pulling             pod/nginx-7ff65f974c-hnzr5    Pulling image "nginx"
34m         Normal    Pulled              pod/nginx-7ff65f974c-hnzr5    Successfully pulled image "nginx" in 40.750855715s
34m         Normal    Scheduled           pod/nginx-7ff65f974c-v9ccx    Successfully assigned vpa/nginx-7ff65f974c-v9ccx to k8s-node004
33m         Normal    Pulling             pod/nginx-7ff65f974c-v9ccx    Pulling image "nginx"
33m         Normal    Pulled              pod/nginx-7ff65f974c-v9ccx    Successfully pulled image "nginx" in 15.495315629s
33m         Normal    Created             pod/nginx-7ff65f974c-v9ccx    Created container nginx
33m         Normal    Started             pod/nginx-7ff65f974c-v9ccx    Started container nginx

出力情報から、vpaがEvictedByVPAを実行し、nginxを自動的に停止し、VPAが推奨するリソースを使用して新しいnginxを開始した
ことがわかります。確認のために、nginxポッドを確認できます。


[root@k8s-node001 ~]# kubectl  describe po nginx-7ff65f974c-2m9zl -n vpa
Name:         nginx-7ff65f974c-2m9zl
Namespace:    vpa
Priority:     0
Node:         k8s-node004/192.168.100.184
Start Time:   Mon, 28 Sep 2020 00:46:19 -0400
Labels:       app=nginx
              pod-template-hash=7ff65f974c
Annotations:  cni.projectcalico.org/podIP: 100.67.191.53/32
              vpaObservedContainers: nginx
              vpaUpdates: Pod resources updated by nginx-vpa: container 0: cpu request, memory request
Status:       Running
IP:           100.67.191.53
IPs:
  IP:           100.67.191.53
Controlled By:  ReplicaSet/nginx-7ff65f974c
Containers:
  nginx:
    Container ID:   docker://c96bcd07f35409d47232a0bf862a76a56352bd84ef10a95de8b2e3f6681df43d
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:c628b67d21744fce822d22fdcc0389f6bd763daac23a6b77147d0712ea7102d0
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 28 Sep 2020 00:46:38 -0400
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:        476m
      memory:     262144k

リクエストに焦点を当てる:CPU:476m、メモリ:262144k
次に、デプロイメントファイルを振り返ります

      requests:
        cpu: 100m
        memory: 50Mi

これで、VPAが何をしたかを知ることができます。もちろん、サービスの負荷が変化しても、VPAの推奨事項はその後も変化し続けます。現在実行中のポッドのリソースがVPAの推奨値に達しない場合、ポッドの削除が実行され、十分なリソースを持つ新しいサービスが再デプロイされます。

VPAの使用制限


  • HPA(Horizo​​ntal Pod Autoscaler)では使用できません
  • たとえば、PodはDeploymentやStatefulSetなどのレプリカコントローラーを使用します

    VPAの利点は何ですか


  • ポッドリソースは必要に応じて使用されるため、クラスターノードが効率的に使用されます。
  • ポッドは、適切な利用可能なリソースを持つノードに配置されます。
  • CPUとメモリのリクエストに適切な値を決定するためにベンチマークタスクを実行する必要はありません。
  • VPAは、人の操作なしでいつでもCPUとメモリの要求を調整できるため、メンテナンス時間を短縮できます。
    最後に、VPAはKubernetesの比較的新しい機能であり、本番環境では大規模に実行されていません。オンライン環境で自動更新モードを使用することはお勧めしませんが、のリソース使用量をよりよく理解できます。推奨モードを使用してサービスを提供します。
    詳しくは公式サイトをご覧ください。

PS:フォローアップ記事はdev.kubeops.netに同期されます

注:記事の写真はインターネットからのものです。侵害がある場合は、時間内に削除するために私に連絡してください。

おすすめ

転載: blog.51cto.com/15060545/2657302