Skip to Content
《使用 Kubernetes 实现云原生 DevOps(第二版)》
book

《使用 Kubernetes 实现云原生 DevOps(第二版)》

by Justin Domingus, John Arundel
May 2025
Intermediate to advanced
356 pages
3h 47m
Chinese
O'Reilly Media, Inc.
Content preview from 《使用 Kubernetes 实现云原生 DevOps(第二版)》

第 9 章 管理 Pod 管理 Pod

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

没有大问题,只有很多小问题。

亨利-福特

在上一章中,我们详细介绍了容器,并解释了在 Kubernetes 中,容器是如何组成 Pod 的。Pod 还有其他一些有趣的方面,我们将在本章介绍,包括标签、使用节点亲缘关系指导 Pod 调度、使用污点和容忍度禁止 Pod 在某些节点上运行、使用 Pod 亲缘关系使 Pod 保持在一起或分开,以及使用 DaemonSets 和 StatefulSets 等 Pod 控制器协调应用程序。我们还将介绍一些高级 Network+ 功能,包括 Ingress 控制器和服务网格工具。

标签

你知道 Pod(和其他 Kubernetes 资源)可以附加标签,这些标签在连接相关资源(例如,将来自服务的请求发送到相应的后端)时发挥着重要作用。让我们在本节中进一步了解标签和选择器。

什么是标签?

标签是附加到 pod 等对象上的键/值对。标签旨在用于指定对象的识别属性,这些属性对用户有意义并与之相关,但并不直接向核心系统暗示语义。

Kubernetes文档

换句话说,标签的存在是为了用对我们有意义的信息来标记资源,但它们对 Kubernetes 没有任何意义。例如,通常会给 Pod 贴上它们所属应用的标签:

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: demo

现在,这个标签本身没有任何作用。作为文档,它仍然有用:有人可以查看这个 Pod,了解它运行的是什么应用程序。但当我们将标签与选择器一起使用时,标签的真正威力就显现出来了。

选择器

选择器是与标签(或标签集)匹配的表达式。它是一种通过标签指定一组资源的方法。例如,服务资源有一个选择器,用于识别它将向哪些 Pod 发送请求。还记得"服务资源 "中的演示服务吗?

apiVersion: v1
kind: Service
...
spec:
  ...
  selector:
    app: demo

这是一个非常简单的选择器,它可以匹配任何带有app 标签且值为demo 的资源。如果一个资源根本没有app 标签,它也不会匹配这个选择器。如果资源带有app 标签,但其值不是demo ,也不会匹配该选择器。只有标签为app: demo 的合适资源(在本例中为 Pod)才会匹配,本服务将选择所有此类资源。

标签不仅用于连接服务和 Pod,在使用kubectl get 查询集群时,还可以使用--selector 标志直接使用标签:

kubectl get pods --all-namespaces --selector app=demo
NAMESPACE   NAME                    READY     STATUS    RESTARTS   AGE
demo        demo-5cb7d6bfdd-9dckm   1/1       Running   0          20s

您可能还记得,在"使用短标记 "中,--selector 可缩写为-l (表示标签)。

如果您想查看 Pod 上定义了哪些标签,请使用--show-labels 标志kubectl get

kubectl get pods --show-labels
NAME                    ... LABELS
demo-5cb7d6bfdd-9dckm   ... app=demo,environment=development

更多高级选择器

大多数情况下,你只需要一个简单的选择器,如 ...

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 上管理云原生数据

在 Kubernetes 上管理云原生数据

Jeff Carpenter, Patrick McFadin
《Kubernetes 最佳实践》第二版

《Kubernetes 最佳实践》第二版

Brendan Burns, Eddie Villalba, Dave Strebel, Lachlan Evenson
生产 Kubernetes

生产 Kubernetes

Josh Rosso, Rich Lander, Alex Brand, John Harris
Cloud Native DevOps mit Kubernetes

Cloud Native DevOps mit Kubernetes

John Arundel, Justin Domingus

Publisher Resources

ISBN: 9798341659179