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搭建和管理企业级网站应用
相关文章
|
1月前
|
Prometheus Kubernetes 监控
k8s部署针对外部服务器的prometheus服务
通过上述步骤,您不仅成功地在Kubernetes集群内部署了Prometheus,还实现了对集群外服务器的有效监控。理解并实施网络配置是关键,确保监控数据的准确无误传输。随着监控需求的增长,您还可以进一步探索Prometheus生态中的其他组件,如Alertmanager、Grafana等,以构建完整的监控与报警体系。
120 60
|
1月前
|
Prometheus Kubernetes 监控
k8s部署针对外部服务器的prometheus服务
通过上述步骤,您不仅成功地在Kubernetes集群内部署了Prometheus,还实现了对集群外服务器的有效监控。理解并实施网络配置是关键,确保监控数据的准确无误传输。随着监控需求的增长,您还可以进一步探索Prometheus生态中的其他组件,如Alertmanager、Grafana等,以构建完整的监控与报警体系。
203 62
|
4天前
|
存储 Kubernetes 网络协议
k8s的无头服务
Headless Service 是一种特殊的 Kubernetes 服务,其 `spec:clusterIP` 设置为 `None`,不会分配 ClusterIP,通过 DNS 解析提供服务发现。与普通服务不同,Headless Service 不提供负载均衡功能,每个 Pod 都有唯一的 DNS 记录,直接映射到其 IP 地址,适用于有状态应用的场景,如与 StatefulSet 一起部署数据库。示例中通过创建 Nginx 的 StatefulSet 和 Headless Service,展示了如何直接访问单个 Pod 并进行内容修改。
14 3
|
1月前
|
存储 Kubernetes 调度
|
22天前
|
JSON Kubernetes 容灾
ACK One应用分发上线:高效管理多集群应用
ACK One应用分发上线,主要介绍了新能力的使用场景
|
23天前
|
Kubernetes 持续交付 开发工具
ACK One GitOps:ApplicationSet UI简化多集群GitOps应用管理
ACK One GitOps新发布了多集群应用控制台,支持管理Argo CD ApplicationSet,提升大规模应用和集群的多集群GitOps应用分发管理体验。
|
1月前
|
Kubernetes Cloud Native 云计算
云原生之旅:Kubernetes 集群的搭建与实践
【8月更文挑战第67天】在云原生技术日益成为IT行业焦点的今天,掌握Kubernetes已成为每个软件工程师必备的技能。本文将通过浅显易懂的语言和实际代码示例,引导你从零开始搭建一个Kubernetes集群,并探索其核心概念。无论你是初学者还是希望巩固知识的开发者,这篇文章都将为你打开一扇通往云原生世界的大门。
120 17
|
1月前
|
Kubernetes 应用服务中间件 nginx
搭建Kubernetes v1.31.1服务器集群,采用Calico网络技术
在阿里云服务器上部署k8s集群,一、3台k8s服务器,1个Master节点,2个工作节点,采用Calico网络技术。二、部署nginx服务到k8s集群,并验证nginx服务运行状态。
450 1
|
1月前
|
Kubernetes Cloud Native 微服务
微服务实践之使用 kube-vip 搭建高可用 Kubernetes 集群
微服务实践之使用 kube-vip 搭建高可用 Kubernetes 集群
103 1
|
1月前
|
负载均衡 应用服务中间件 nginx
基于Ubuntu-22.04安装K8s-v1.28.2实验(二)使用kube-vip实现集群VIP访问
基于Ubuntu-22.04安装K8s-v1.28.2实验(二)使用kube-vip实现集群VIP访问
50 1