k8s学习--kubernetes服务自动伸缩之水平收缩(pod副本收缩)VPA策略应用案例

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介: k8s学习--kubernetes服务自动伸缩之水平收缩(pod副本收缩)VPA策略应用案例

@TOC


前言

有任何疑问或不懂的地方均可评论或私信,欢迎交流
关于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.11 master01 2cpu双核 4G 100G
192.168.10.12 worker01 2cpu双核 4G 100G
192.168.10.13 worker02 2cpu双核 4G 100G

版本 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驱逐,重新部署新的足够资源的服务。

相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
23小时前
|
Kubernetes 固态存储 调度
k8s学习--如何控制pod调度的位置
k8s学习--如何控制pod调度的位置
|
1天前
|
Kubernetes API 调度
k8s学习--pod的所有状态详解(图例展示)
k8s学习--pod的所有状态详解(图例展示)
|
1天前
|
存储 Kubernetes 调度
k8s学习--k8s群集部署zookeeper应用及详细解释
k8s学习--k8s群集部署zookeeper应用及详细解释
|
1天前
|
Kubernetes Cloud Native 云计算
云原生之旅:Kubernetes 集群的搭建与实践
【8月更文挑战第67天】在云原生技术日益成为IT行业焦点的今天,掌握Kubernetes已成为每个软件工程师必备的技能。本文将通过浅显易懂的语言和实际代码示例,引导你从零开始搭建一个Kubernetes集群,并探索其核心概念。无论你是初学者还是希望巩固知识的开发者,这篇文章都将为你打开一扇通往云原生世界的大门。
41 17
|
4天前
|
Kubernetes Cloud Native Ubuntu
云原生之旅:Kubernetes集群搭建与应用部署
【8月更文挑战第65天】本文将带你进入云原生的世界,通过一步步指导如何在本地环境中搭建Kubernetes集群,并部署一个简单的应用。我们将使用Minikube和Docker作为工具,探索云原生技术的魅力所在。无论你是初学者还是有经验的开发者,这篇文章都将为你提供有价值的信息和实践技巧。
|
8天前
|
存储 Kubernetes 关系型数据库
阿里云ACK备份中心,K8s集群业务应用数据的一站式灾备方案
阿里云ACK备份中心,K8s集群业务应用数据的一站式灾备方案
|
1月前
|
存储 Kubernetes 负载均衡
CentOS 7.9二进制部署K8S 1.28.3+集群实战
本文详细介绍了在CentOS 7.9上通过二进制方式部署Kubernetes 1.28.3+集群的全过程,包括环境准备、组件安装、证书生成、高可用配置以及网络插件部署等关键步骤。
185 3
CentOS 7.9二进制部署K8S 1.28.3+集群实战
|
1月前
|
Kubernetes 负载均衡 前端开发
二进制部署Kubernetes 1.23.15版本高可用集群实战
使用二进制文件部署Kubernetes 1.23.15版本高可用集群的详细教程,涵盖了从环境准备到网络插件部署的完整流程。
55 2
二进制部署Kubernetes 1.23.15版本高可用集群实战
|
1天前
|
Kubernetes 应用服务中间件 nginx
k8s学习--k8s集群使用容器镜像仓库Harbor
本文介绍了在CentOS 7.9环境下部署Harbor容器镜像仓库,并将其集成到Kubernetes集群的过程。环境中包含一台Master节点和两台Node节点,均已部署好K8s集群。首先详细讲述了在Harbor节点上安装Docker和docker-compose,接着通过下载Harbor离线安装包并配置相关参数完成Harbor的部署。随后介绍了如何通过secret和serviceaccount两种方式让Kubernetes集群使用Harbor作为镜像仓库,包括创建secret、配置节点、上传镜像以及创建Pod等步骤。最后验证了Pod能否成功从Harbor拉取镜像运行。
|
1月前
|
存储 Kubernetes 测试技术
k8s使用pvc,pv,sc关联ceph集群
文章介绍了如何在Kubernetes中使用PersistentVolumeClaim (PVC)、PersistentVolume (PV) 和StorageClass (SC) 来关联Ceph集群,包括创建Ceph镜像、配置访问密钥、删除默认存储类、编写和应用资源清单、创建资源以及进行访问测试的步骤。同时,还提供了如何使用RBD动态存储类来关联Ceph集群的指南。
51 7
下一篇
无影云桌面