Skip to Content
Kubernetes 认证管理员 (CKA) 学习指南 (Chinese Edition), 2nd Edition
book

Kubernetes 认证管理员 (CKA) 学习指南 (Chinese Edition), 2nd Edition

by Benjamin Muschko
January 2026
Intermediate to advanced
392 pages
4h 44m
Chinese
O'Reilly Media, Inc.
Content preview from Kubernetes 认证管理员 (CKA) 学习指南 (Chinese Edition), 2nd Edition

第12章 工作负载扩展

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

工作负载 需要进行扩展的原因有多种,尤其是在需求增长时需要保持最佳性能。例如,应用程序随着人气提升可能面临用户激增的情况,或者随着时间推移需要处理更庞大的数据量。

在Kubernetes中,工作负载扩展主要通过两种方式实现:增加分配给每个Pod的资源(垂直扩展),或调整并发运行的Pod数量(水平扩展)。水平扩展特别适用于处理波动性工作负载,确保应用程序在CPU、内存和I/O等资源承受不同压力时仍保持响应能力和弹性。

本章将指导您如何手动调整副本数量以应对应用程序负载增长。此外,我们将深入探讨 API 原语 HorizontalPodAutoscaler(HPA) ,该工具可根据 CPU 和内存等资源阈值自动扩展受管 Pod 集群。 本章不涉及由 API 原语 VerticalPodAutoscaler (VPA) 实现的垂直扩展,因其不在考试范围之内。

工作负载的手动扩展

手动扩展 工作负载 需要指定要运行的固定 Pod 数量。该数量应基于从生产环境收集的实际使用指标,或通过开发阶段的负载测试进行估算。

然而,由于应用流量可能出现突发性波动(急剧上升或逐渐下降),您需要持续监控并调整Pod数量 以匹配需求。若未能及时调整,可能导致资源浪费的过度配置,或造成性能下降和用户体验恶化的配置不足

手动缩放部署

通过部署控制副本数量的扩展(增加或减少)过程相当简单。您可以使用编辑 deployment 编辑运行中的对象并修改spec.replicas 属性值,或使用命令式缩放部署命令 。在实际生产环境中,建议编辑部署 YAML 清单文件,将其提交至版本控制系统并应用变更。以下命令将副本数从 4 增加至 6:

$ kubectl scale deployment app-cache --replicas=6
deployment.apps/app-cache scaled

通过-w命令行参数可实时观察副本创建过程。您将看到新创建 Pod 的状态从Container​Creat⁠ing变为Running

$ kubectl get pods -w
NAME                         READY   STATUS              RESTARTS   AGE
app-cache-5d6748d8b9-6cc4j   1/1     ContainerCreating   0          11s
app-cache-5d6748d8b9-6rmlj   1/1     Running             0          28m
app-cache-5d6748d8b9-6z7g5   1/1     ContainerCreating   0          11s
app-cache-5d6748d8b9-96dzf   1/1     Running             0          28m
app-cache-5d6748d8b9-jkjsv   1/1     Running             0          28m
app-cache-5d6748d8b9-svrxw   1/1     Running             0          28m

手动调整副本数量需要一定试错过程。您仍需监控系统负载,判断当前副本数量是否足以处理传入流量。

手动扩展 StatefulSet

另一种可手动扩展的API原语是 。StatefulSet旨在通过一组Pod管理有状态应用程序(如数据库)。与Deployment类似,StatefulSet定义了Pod模板;但其每个副本都保证具有唯一且持久的身份标识。同样地,StatefulSet也使用ReplicaSet来管理副本。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Kubernetes 上的生成式人工智能 (Chinese Edition)

Kubernetes 上的生成式人工智能 (Chinese Edition)

Roland Huß, Daniele Zonca
游戏化头脑风暴

游戏化头脑风暴

Dave Gray, Sunni Brown, James Macanufo

Publisher Resources

ISBN: 0642572314712