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

第3章 与Kubernetes交互

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

作为应用程序开发人员 ,您需要与 Kubernetes 集群交互以管理运行应用程序的对象。每次对集群的调用都会由 API 服务器组件 接收并处理。调用 API 服务器有多种方式,例如可使用基于 Web 的控制面板、命令行工具如 kubectl,或直接向RESTful API端点发送HTTPS请求。

考试不涉及通过可视化用户界面 与Kubernetes集群交互的内容。解答考题时唯一允许使用的客户端 是kubectl。本章将概述Kubernetes API基本操作与对象,并介绍使用kubectl管理对象的不同方式。

API 基本操作与对象

Kubernetes 基本操作 是平台架构中创建和运行应用程序的基础构建模块。即使作为 Kubernetes 新手,您可能也听说过 Pod、Deployment 和 Service 等术语,它们均属于 Kubernetes 基本操作。架构中还存在许多其他专用于特定功能的基本操作。

类比而言,不妨回顾面向对象编程的概念。在面向对象语言中,类定义了现实世界功能的蓝图:其属性和行为。Kubernetes 基本元素相当于面向对象编程中的类 。面向对象编程中类的实例即为对象,它管理自身状态并能与系统其他部分通信。每当创建 Kubernetes 对象时,你便生成这样的实例。

例如,Kubernetes 中的 Pod 就是这样一个类,其下可存在多个具有独立身份的实例。每个 Kubernetes 对象都拥有系统生成的唯一标识符(也称 UID),用于明确区分系统中的实体。后续我们将探讨 Kubernetes 对象的属性。图 3-1展示了 Kubernetes 基本单元与对象之间的关系。

Diagram illustrating the hierarchical relationship between a Kubernetes Pod primitive and its objects, labeled with unique identifiers for both "Frontend" and "Backend" components.
图 3-1. Kubernetes 对象身份

所有Kubernetes基本对象 都遵循通用结构 ,如图3-2所示,深入解析对象清单文件即可观察到该结构。Kubernetes清单文件主要采用YAML标记语言。

Diagram illustrating the structure of a Kubernetes object manifested in YAML, showing sections like API Version, Kind, Metadata, Spec, and Status.
图3-2. Kubernetes 对象结构

下面我们逐项解析各部分在Kubernetes系统中的作用:

API版本

Kubernetes API版本 定义了基本对象的结构,并用于验证数据的正确性。API版本的作用类似于XML文档中的XML模式或JSON文档中的JSON模式。该版本通常经历成熟度演进过程——例如从alpha版到beta版再到正式版。有时会看到用斜杠分隔的不同前缀(如apps)。 运行命令 `kubectl api-versions` 可列出与当前集群版本兼容的 API 版本。

Kind

类型定义了原始对象的类别(如Pod或Service),本质上回答了"当前处理的是何种资源"的问题。

元数据

元数据 描述对象的高级信息——例如名称、所属命名空间、是否定义标签和注释。该部分同时定义唯一标识符UID。

Spec

规范 (简称spec)声明预期状态——例如对象创建后应呈现何种形态?容器中应运行哪个镜像?或需设置哪些环境变量? ...

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