前言

有任何疑问或不懂的地方均可评论或私信,欢迎交流
关于VPA的详细解释
链接: VPA的详细解释

策略
在VPA中,updateMode 是一个重要的配置选项,它决定了VPA如何应用其提供的资源建议。根据不同的设置,VPA可以采取不同的策略来更新Pod的资源配置:

Off:
VPA不会应用任何资源推荐,只是收集和显示数据。


Auto
概述:
自动调整策略在无需重启 Pod 的情况下动态调整其资源请求。

特性:
动态调整: Pod 的资源请求在运行时逐步增加。

优点:
无需重启 Pod 即可调整资源,最小化中断。
提供更及时的资源调整,适合短周期和不稳定的负载。

缺点:
由于容器运行时资源的硬限制,可能无法完全满足新资源请求,导致资源不足。


Recreate
概述:
当资源需求变化时,使用重建策略重新启动 Pod 以调整资源请求。

特性:
重启调整:Pod 会被杀死并重新创建,以适应新的资源请求。

优点:
确保 Pod 能获取到新的资源请求,避免运行时的资源限制问题。
适合稳定负载且可以容忍短暂停机的应用。

缺点:
重启 Pod 会引起短暂的服务中断。
不适合要求高可用性且不能容忍重启的工作负载。


Initial
概述:
初始调整策略仅在 Pod 第一次创建时设置资源请求,不会对运行中的 Pod 进行调整。
特性:
静态调整:在 Pod 创建时基于历史数据设定初始资源请求。

优点:
避免了运行时调整带来的复杂性。
适合长期运行的负载,初始设置资源合理即可。

缺点:
无法调整已经运行的 Pod 的资源,若需求变化则需手动干预。

应用

环境

虚拟机

Ip主机名cpu内存硬盘
192.168.10.11master012cpu双核4G100G
192.168.10.12worker012cpu双核4G100G
192.168.10.13worker022cpu双核4G100G

版本 centos7.9
已部署k8s-1.27

1.VPA应用案例 updateMode: “Off”

(1)创建应用实例

VPA不会应用任何资源推荐,只是收集和显示数据。

vim 03-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest 
        imagePullPolicy: IfNotPresent
        resources:
          requests:
            cpu: 100m
            memory: 250Mi
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: default
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx
kubectl apply -f 03-nginx.yaml
kubectl get pods

在这里插入图片描述

kubectl get svc

在这里插入图片描述

(2)创建vpa

使用updateMode: "Off"模式,这种模式仅获取资源推荐,不更新Pod

 vim nginx-vpa.yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: nginx-vpa
  namespace: default
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"
kubectl apply -f nginx-vpa.yaml
 kubectl get vpa

稍等片刻
在这里插入图片描述

kubectl describe vpa nginx-vpa
Name:         nginx-vpa
Namespace:    default
Labels:       <none>
Annotations:  <none>
API Version:  autoscaling.k8s.io/v1
Kind:         VerticalPodAutoscaler
Metadata:
  Creation Timestamp:  2024-06-09T11:10:34Z
  Generation:          1
  Resource Version:    19449
  UID:                 be19f937-fb0c-4c33-a559-4e5aa52043b8
Spec:
  Resource Policy:
    Container Policies:
      Container Name:  nginx
      Max Allowed:
        Cpu:     2000m
        Memory:  2048Mi
      Min Allowed:
        Cpu:     250m
        Memory:  100Mi
  Target Ref:
    API Version:  apps/v1
    Kind:         Deployment
    Name:         nginx
  Update Policy:
    Update Mode:  Off
Status:
  Conditions:
    Last Transition Time:  2024-06-09T11:11:17Z
    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:     1142m
        Memory:  1194357142
Events:          <none>

解释如下:
Recommendation::包含对Pod资源需求的推荐值。
Container Recommendations::针对特定容器的推荐值。
Container Name::容器名称。
Lower Bound::推荐的下限资源量。
Target::推荐的最优资源量。
Uncapped Target::如果没有上限约束,则为目标资源量。
Upper Bound::推荐的上限资源量。

kubectl get svc

在这里插入图片描述

yum -y install httpd-tools
ab -c 1000 -n 100000000 http://192.168.10.11:30478/

打开一个新终端

kubectl describe vpa nginx-vpa
Name:         nginx-vpa
Namespace:    default
Labels:       <none>
Annotations:  <none>
API Version:  autoscaling.k8s.io/v1
Kind:         VerticalPodAutoscaler
Metadata:
  Creation Timestamp:  2024-06-09T11:10:34Z
  Generation:          1
  Resource Version:    20221
  UID:                 be19f937-fb0c-4c33-a559-4e5aa52043b8
Spec:
  Resource Policy:
    Container Policies:
      Container Name:  nginx
      Max Allowed:
        Cpu:     2000m
        Memory:  2048Mi
      Min Allowed:
        Cpu:     250m
        Memory:  100Mi
  Target Ref:
    API Version:  apps/v1
    Kind:         Deployment
    Name:         nginx
  Update Policy:
    Update Mode:  Off
Status:
  Conditions:
    Last Transition Time:  2024-06-09T11:11:17Z
    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:     802m
        Memory:  838810574
Events:          <none>

由于使用updateMode: “Off”,所以没有更新pod

kubectl get pods

在这里插入图片描述

2.VPA应用案例 updateMode: “Auto”

此模式当目前运行的pod的资源达不到VPA的推荐值,就会执行pod驱逐,重新部署新的足够资源的服务

(1)创建应用
vim 05-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        resources:
          requests:
            cpu: 100m
            memory: 50Mi
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: default
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx
kubectl apply -f 05-nginx.yaml
 kubectl get pods
 kubectl apply -f 05-nginx.yaml
 kubectl get pods

在这里插入图片描述

(2)创建vpa

vim nginx-vpa-auto.yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: nginx-vpa-auto
  namespace: default
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"
 kubectl apply -f nginx-vpa-auto.yaml
kubectl get vpa

在这里插入图片描述

(3)执行压测
kubectl get svc

在这里插入图片描述

ab -c 1000 -n 1000000000 http://192.168.10.11:31115/

打开另一个终端

 kubectl describe vpa nginx-vpa-auto
Name:         nginx-vpa-auto
Namespace:    default
Labels:       <none>
Annotations:  <none>
API Version:  autoscaling.k8s.io/v1
Kind:         VerticalPodAutoscaler
Metadata:
  Creation Timestamp:  2024-06-09T11:27:07Z
  Generation:          1
  Resource Version:    21845
  UID:                 453b03aa-fba5-4bcd-aa66-f85e43a57521
Spec:
  Resource Policy:
    Container Policies:
      Container Name:  nginx
      Max Allowed:
        Cpu:     2000m
        Memory:  2048Mi
      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:  2024-06-09T11:27:17Z
    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:     521m
        Memory:  545693548
Events:          <none>
 kubectl get event
LAST SEEN   TYPE      REASON                   OBJECT                        MESSAGE
38m         Normal    Scheduled                pod/nginx-59d7c8bd89-q27gc    Successfully assigned default/nginx-59d7c8bd89-q27gc to worker02
38m         Normal    Pulling                  pod/nginx-59d7c8bd89-q27gc    Pulling image "nginx:latest"
38m         Normal    Pulled                   pod/nginx-59d7c8bd89-q27gc    Successfully pulled image "nginx:latest" in 18.118818504s (18.118826419s including waiting)
38m         Normal    Created                  pod/nginx-59d7c8bd89-q27gc    Created container nginx
38m         Normal    Started                  pod/nginx-59d7c8bd89-q27gc    Started container nginx
11m         Normal    Killing                  pod/nginx-59d7c8bd89-q27gc    Stopping container nginx
38m         Normal    Scheduled                pod/nginx-59d7c8bd89-qb4p4    Successfully assigned default/nginx-59d7c8bd89-qb4p4 to worker01
38m         Normal    Pulling                  pod/nginx-59d7c8bd89-qb4p4    Pulling image "nginx:latest"
38m         Normal    Pulled                   pod/nginx-59d7c8bd89-qb4p4    Successfully pulled image "nginx:latest" in 17.842596583s (17.842603536s including waiting)
38m         Normal    Created                  pod/nginx-59d7c8bd89-qb4p4    Created container nginx
38m         Normal    Started                  pod/nginx-59d7c8bd89-qb4p4    Started container nginx
11m         Normal    Killing                  pod/nginx-59d7c8bd89-qb4p4    Stopping container nginx
38m         Normal    SuccessfulCreate         replicaset/nginx-59d7c8bd89   Created pod: nginx-59d7c8bd89-q27gc
38m         Normal    SuccessfulCreate         replicaset/nginx-59d7c8bd89   Created pod: nginx-59d7c8bd89-qb4p4
11m         Normal    Scheduled                pod/nginx-6f78fbb759-hcbc6    Successfully assigned default/nginx-6f78fbb759-hcbc6 to worker02
11m         Normal    Pulled                   pod/nginx-6f78fbb759-hcbc6    Container image "nginx:latest" already present on machine
11m         Normal    Created                  pod/nginx-6f78fbb759-hcbc6    Created container nginx
11m         Normal    Started                  pod/nginx-6f78fbb759-hcbc6    Started container nginx
10m         Normal    Killing                  pod/nginx-6f78fbb759-hcbc6    Stopping container nginx
9m51s       Normal    Scheduled                pod/nginx-6f78fbb759-jhf7h    Successfully assigned default/nginx-6f78fbb759-jhf7h to worker02
9m51s       Normal    Pulled                   pod/nginx-6f78fbb759-jhf7h    Container image "nginx:latest" already present on machine
9m51s       Normal    Created                  pod/nginx-6f78fbb759-jhf7h    Created container nginx
9m51s       Normal    Started                  pod/nginx-6f78fbb759-jhf7h    Started container nginx
6m31s       Normal    Killing                  pod/nginx-6f78fbb759-jhf7h    Stopping container nginx
6m31s       Normal    EvictedByVPA             pod/nginx-6f78fbb759-jhf7h    Pod was evicted by VPA Updater to apply resource recommendation.
6m30s       Normal    Scheduled                pod/nginx-6f78fbb759-mcppm    Successfully assigned default/nginx-6f78fbb759-mcppm to worker02
6m30s       Normal    Pulled                   pod/nginx-6f78fbb759-mcppm    Container image "nginx:latest" already present on machine
6m30s       Normal    Created                  pod/nginx-6f78fbb759-mcppm    Created container nginx
6m30s       Normal    Started                  pod/nginx-6f78fbb759-mcppm    Started container nginx
9m51s       Normal    Scheduled                pod/nginx-6f78fbb759-s4xzm    Successfully assigned default/nginx-6f78fbb759-s4xzm to worker01
9m51s       Normal    Pulled                   pod/nginx-6f78fbb759-s4xzm    Container image "nginx:latest" already present on machine
9m51s       Normal    Created                  pod/nginx-6f78fbb759-s4xzm    Created container nginx
9m51s       Normal    Started                  pod/nginx-6f78fbb759-s4xzm    Started container nginx
7m31s       Normal    EvictedByVPA             pod/nginx-6f78fbb759-s4xzm    Pod was evicted by VPA Updater to apply resource recommendation.
7m31s       Normal    Killing                  pod/nginx-6f78fbb759-s4xzm    Stopping container nginx
7m30s       Normal    Scheduled                pod/nginx-6f78fbb759-x986h    Successfully assigned default/nginx-6f78fbb759-x986h to worker01
7m30s       Normal    Pulled                   pod/nginx-6f78fbb759-x986h    Container image "nginx:latest" already present on machine
7m30s       Normal    Created                  pod/nginx-6f78fbb759-x986h    Created container nginx
7m30s       Normal    Started                  pod/nginx-6f78fbb759-x986h    Started container nginx
11m         Normal    Scheduled                pod/nginx-6f78fbb759-z4sxb    Successfully assigned default/nginx-6f78fbb759-z4sxb to worker01
11m         Normal    Pulled                   pod/nginx-6f78fbb759-z4sxb    Container image "nginx:latest" already present on machine
11m         Normal    Created                  pod/nginx-6f78fbb759-z4sxb    Created container nginx
11m         Normal    Started                  pod/nginx-6f78fbb759-z4sxb    Started container nginx
10m         Normal    Killing                  pod/nginx-6f78fbb759-z4sxb    Stopping container nginx
11m         Normal    SuccessfulCreate         replicaset/nginx-6f78fbb759   Created pod: nginx-6f78fbb759-hcbc6
11m         Normal    SuccessfulCreate         replicaset/nginx-6f78fbb759   Created pod: nginx-6f78fbb759-z4sxb
9m52s       Normal    SuccessfulCreate         replicaset/nginx-6f78fbb759   Created pod: nginx-6f78fbb759-jhf7h
9m52s       Normal    SuccessfulCreate         replicaset/nginx-6f78fbb759   Created pod: nginx-6f78fbb759-s4xzm
7m31s       Normal    SuccessfulCreate         replicaset/nginx-6f78fbb759   Created pod: nginx-6f78fbb759-x986h
6m31s       Normal    SuccessfulCreate         replicaset/nginx-6f78fbb759   Created pod: nginx-6f78fbb759-mcppm
38m         Normal    ScalingReplicaSet        deployment/nginx              Scaled up replica set nginx-59d7c8bd89 to 2
11m         Normal    ScalingReplicaSet        deployment/nginx              Scaled up replica set nginx-6f78fbb759 to 2
10m         Warning   FailedToUpdateEndpoint   endpoints/nginx               Failed to update endpoint default/nginx: Operation cannot be fulfilled on endpoints "nginx": the object has been modified; please apply your changes to the latest version and try again
9m52s       Normal    ScalingReplicaSet        deployment/nginx              Scaled up replica set nginx-6f78fbb759 to 2

从输出信息可以了解到,vpa执行了EvictedByVPA,自动停掉了nginx,然后使用 VPA推荐的资源启动了新的nginx,我们查看下nginx的pod可以得到确认

kubectl describe pods nginx-6f78fbb759-mcppm
Name:             nginx-6f78fbb759-mcppm
Namespace:        default
Priority:         0
Service Account:  default
Node:             worker02/192.168.10.13
Start Time:       Sun, 09 Jun 2024 19:29:09 +0800
Labels:           app=nginx
                  pod-template-hash=6f78fbb759
Annotations:      cni.projectcalico.org/containerID: 145e1f8509982da00e61eb180bb9f3b52e527bc94d8657237d340fd9bf01475a
                  cni.projectcalico.org/podIP: 10.244.30.72/32
                  cni.projectcalico.org/podIPs: 10.244.30.72/32
                  vpaObservedContainers: nginx
                  vpaUpdates: Pod resources updated by nginx-vpa-auto: container 0: cpu request, memory request
Status:           Running
IP:               10.244.30.72
IPs:
  IP:           10.244.30.72
Controlled By:  ReplicaSet/nginx-6f78fbb759
Containers:
  nginx:
    Container ID:   docker://97f865221289a48d042dfd039b2eb33876c2d08a89c2f7863fd7e42b5f7f8018
    Image:          nginx:latest
    Image ID:       docker-pullable://nginx@sha256:0f04e4f646a3f14bf31d8bc8d885b6c951fdcf42589d06845f64d18aec6a3c4d
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sun, 09 Jun 2024 19:29:10 +0800
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:        250m
      memory:     262144k
    Environment:  <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-gfzhm (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-gfzhm:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  8m56s  default-scheduler  Successfully assigned default/nginx-6f78fbb759-mcppm to worker02
  Normal  Pulled     8m56s  kubelet            Container image "nginx:latest" already present on machine
  Normal  Created    8m56s  kubelet            Created container nginx
  Normal  Started    8m56s  kubelet            Started container nginx

随着服务的负载的变化,VPA的推荐值也会不断变化。当目前运行的pod的资源达不到VPA的推荐值,就会执行pod驱逐,重新部署新的足够资源的服务。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐