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(第二版)》

第 8 章 运行容器 运行容器

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

如果你有一个棘手的问题无法回答,那就先从一个你无法回答的简单问题开始。

马克斯-泰格马克

在前几章中,我们主要关注 Kubernetes 的操作方面:从哪里获取集群、如何维护集群以及如何管理集群资源。现在让我们来看看 Kubernetes 最基本的对象:容器。我们将从技术层面了解容器的工作原理、它们与 Pod 的关系,以及如何将容器映像部署到 Kubernetes。

在本章中,我们还将介绍容器安全这一重要话题,以及如何根据最佳实践使用 Kubernetes 中的安全功能,以安全的方式部署应用程序。最后,我们将了解如何在 Pod 上挂载磁盘卷,从而允许容器共享和持久化数据。

集装箱和舱

我们已经在第 2 章中介绍了 Pod,并谈到了部署如何使用 ReplicaSets 来维护一组复制 Pod,但我们还没有真正详细地了解 Pod 本身。Pod 是 Kubernetes 中的调度单元。一个 Pod 对象代表一个容器或一组容器,在 Kubernetes 中运行的一切都通过 Pod 来实现:

Pod 代表在同一执行环境中运行的应用程序容器和卷的集合。Pod 是 Kubernetes 集群中最小的可部署工件,而不是容器。这意味着 Pod 中的所有容器始终位于同一台机器上。

Kelsey Hightower 等人,Kubernetes Up & Running

到目前为止,在本书中Pod容器这两个术语或多或少可以互换使用:演示应用程序 Pod 中只有一个容器。但在更复杂的应用程序中,一个 Pod 很可能包含两个或更多容器。因此,让我们来看看它是如何工作的,并了解何时以及为何要在 Pod 中将容器组合在一起。

什么是集装箱?

在问为什么要在 Pod 中安装多个容器之前,我们先来回顾一下容器到底是什么。

"容器的到来 "一书中我们可以了解到,容器是一个标准化的软件包,其中包含一个软件及其依赖项、配置、数据等:运行所需的一切。但它究竟是如何运行的呢?

在 Linux 和大多数其他操作系统中,机器上运行的一切都通过进程来实现。进程代表一个正在运行的应用程序(如 Chrome 浏览器、top 或 Visual Studio Code)的二进制代码和内存状态。所有进程都存在于同一个全局命名空间中:它们可以互相查看和交互,共享同一个资源池,如 CPU、内存和文件系统。(Linux 命名空间有点像 Kubernetes 命名空间,虽然从技术上讲不是一回事)。

从操作系统的角度看,容器代表一个孤立的进程(或进程组),它存在于自己的命名空间中。容器内的进程看不到容器外的进程,反之亦然。容器不能访问属于另一个容器的资源,也不能访问容器外的进程。容器边界就像一个环形栅栏,可以阻止进程肆意运行并占用彼此的资源。

就容器内的进程而言,它运行在自己的机器上,可以完全访问其所有资源,没有其他进程在运行。如果在容器内运行一些命令,就可以看到这一点:

kubectl run busybox --image busybox:1.28 --rm -it --restart=Never /bin/sh
If you don't see a command prompt, try pressing enter.
/ # ps ax
PID   USER     TIME  COMMAND
    1 root      0:00 /bin/sh
 8 root 0:00 ps ...
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