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

第11章. 部署 与副本集

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

Kubernetes 的核心优势在于能够无缝扩展应用程序并轻松管理复制。为实现这些功能,Kubernetes 提供了两种基础组件:部署(Deployments) 和副本集(ReplicaSets)。

本章将指导您创建部署,该组件通过后台的副本集管理并扩展一组相同的Pod。部署还能轻松实现应用更新的分阶段发布,并在必要时回滚至旧版本——全程保持最小停机时间并实现最大控制力。

部署操作实践

容器中运行应用程序的基础单元是 Pod。使用单个 Pod 实例运行应用程序存在缺陷——它构成单点故障,因为所有针对应用程序的流量都会汇聚到该 Pod。当需求增加导致负载上升时(例如电商应用的购物高峰期,或越来越多微服务需要与认证提供商等集中式微服务功能通信时),这种行为尤其棘手。

在Pod中运行应用的另一关键问题是容错性 。当节点发生故障时,调度器集群组件不会重新调度Pod,这可能导致终端用户遭遇系统中断。本章将探讨支持应用可扩展性和容错性的Kubernetes机制。

ReplicaSet是 Kubernetes API 资源( ),用于控制运行应用程序的多个相同 Pod 实例(称为副本)。它具备按需扩展副本数量的能力。

部署Deployment)抽象了副本集的功能并进行内部管理。这意味着实际操作中无需手动创建、修改或删除副本集对象。部署会保存应用程序版本历史记录,可委托副本集回滚至旧版本以应对阻塞性或潜在高成本的生产问题,同时具备调整副本数量的能力。

图 11-1展示了部署、副本集及其所控制副本之间的关系。

Diagram illustrating the hierarchical relationship between a Deployment, a ReplicaSet, and three Pods, highlighting their roles in creating and maintaining replicas.
图 11-1. 部署与 ReplicaSet之间的关系

后续章节将详细说明部署的管理方法,包括扩展和发布功能。

创建部署

可通过命令行指令`create deployment` 创建部署( )。命令 ` ` 提供多种选项,其中部分为必填项。至少需指定部署名称和容器镜像。部署将此信息传递给副本集(ReplicaSet),用于管理副本。默认创建一个副本,但可通过`--replicas` 选项定义更高数量。

让我们观察命令的实际运行效果。以下命令创建名为app-cache 的部署,在四个副本的容器内运行对象缓存memcached

$ kubectl create deployment app-cache --image=memcached:1.6.8 --replicas=4
deployment.apps/app-cache created

部署与所控副本的映射通过标签选择实现: 。执行命令式操作时,kubectl会自动配置映射关系。示例11-1展示了YAML清单中的标签选择逻辑。该YAML清单既可用于声明式创建部署,也可通过解析先前命令创建的实例对象来实现。

示例 11-1. 部署的 YAML 清单
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-cache
  labels:
    app: app-cache ...
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