Skip to Content
《Kubernetes 最佳实践》第二版
book

《Kubernetes 最佳实践》第二版

by Brendan Burns, Eddie Villalba, Dave Strebel, Lachlan Evenson
May 2025
Intermediate to advanced
324 pages
3h 31m
Chinese
O'Reilly Media, Inc.
Content preview from 《Kubernetes 最佳实践》第二版

第 4 章 配置、保密和 RBAC

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

容器的可组合特性允许我们作为操作员在运行时将配置数据引入容器。这样,我们就可以将应用程序的功能与其运行环境解耦。通过容器运行时允许在运行时向容器传递环境变量或挂载外部卷的约定,您可以在实例化应用程序时有效地更改应用程序的配置。作为开发人员,必须考虑到这种行为的动态性质,并允许使用环境变量或从应用程序运行时用户可用的特定路径读取配置数据。

在将机密等敏感数据移入本地 Kubernetes API 对象时,了解 Kubernetes 如何确保对 API 的访问安全非常重要。Kubernetes 中最常用的安全方法是基于角色的访问控制(RBAC),它围绕特定用户或组可以对 API 采取的行动实施细粒度的权限结构。本章将介绍有关 RBAC 的一些最佳实践,并提供一个小入门。

通过配置映射和机密进行配置

Kubernetes 允许你通过 ConfigMaps 或秘密资源向我们的应用程序原生提供配置信息。两者之间的主要区别在于 pod 存储接收信息的方式和数据在 etcd 数据存储中的存储方式。

配置地图

让应用程序通过某种机制(如命令行参数、环境变量或系统可用文件)消耗配置信息是非常常见的。容器允许开发人员将这些配置信息与应用程序分离,从而实现真正的应用程序可移植性。ConfigMap API 允许注入所提供的配置信息。ConfigMap 非常适合应用程序的要求,可以提供键/值对或复杂的批量数据,如 JSON、XML 或专有配置数据。

ConfigMaps 不仅能为 pod 提供配置信息,还能为更复杂的系统服务(如控制器、CRD、操作员等)提供所需的信息。如前所述,ConfigMap API 更适用于非敏感数据的字符串数据。如果您的应用程序需要更敏感的数据,则 Secrets API 更为合适。

要让应用程序使用 ConfigMap 数据,可以将其作为安装到 pod 中的卷或环境变量注入。

秘密

的许多属性和理由都适用于秘密。主要区别在于秘密的基本性质。秘密数据的存储和处理方式应易于隐藏,如果环境配置为加密,还可能在静态时进行加密。秘密数据是以 base64 编码信息的形式表示的,而且必须明白这并没有加密。 一旦将秘密注入 pod,pod 本身就能看到纯文本的秘密数据。

秘密数据是指少量数据,Kubernetes 默认限制 base64 编码数据的大小为 1 MB,因此要确保实际数据约为 750 KB,因为编码会产生开销。Kubernetes 中有三种类型的秘密:

generic

通常只是普通的键/值对,这些键/值对是使用--from-literal= 参数从文件、目录或字符串文字中创建的,如下所示:

kubectl create secret generic mysecret --from-literal=key1=$3cr3t1
    --from-literal=key2=@3cr3t2
docker-registry

如果有imagePullsecret ,在 pod 模板中传递时,kubelet 会使用此,以提供向私有 Docker 注册表进行身份验证所需的凭据:

kubectl create secret docker-registry registryKey --docker-server
    myreg.azurecr.io --docker-username myreg --docker-password ...
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 实现云原生 DevOps(第二版)》

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

Justin Domingus, John Arundel
生产 Kubernetes

生产 Kubernetes

Josh Rosso, Rich Lander, Alex Brand, John Harris

Publisher Resources

ISBN: 9798341657250