Leanpub Header

Skip to main content

面向所有人的 Kubernetes

从零构建云原生数据中心

面向所有人的 Kubernetes
This book is 100% completeLast updated on 2026-10-28
+26,216 words in the last 30 days

大多数 Kubernetes 书籍教你 YAML。这本书教你构建一个数据中心。

你从 #!/usr/bin/env bash 开始。最终用一个命令就能配置、重载和拆除一个完整的多服务集群。没有魔法。没有黑盒。只有你、Bash,以及你亲手构建的云原生平台。

This book is a translation into Chinese (Simplified) of Kubernetes for All which was originally written in English

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
About

About

About the Book

开发者体验就是基础设施。每天都能交付的团队和害怕部署的团队之间的区别不在于天赋——而在于工具。快速反馈循环、不可变部署、自愈集群、可观测输出。这些都不是奢侈品。它们就是你在本书中要构建的东西。

大多数 Kubernetes 教程止步于 kubectl apply。你已经知道这些命令。你所缺少的是将这些命令粘合成一个开发平台的脚本——在 30 秒内配置集群、按照你期望的方式缓存镜像重建、在不丢失连接的情况下重载服务,并在出错时准确告诉你发生了什么。

这是一本关于云原生工程实践的书。不是幻灯片。不是白皮书。Bash、Docker、Helm 和 Kind——通过五个原则串联起来:基础设施即代码、不可变部署、快速反馈、大声失败并自愈、可观测。每一章都为其中之一服务。最后一章回应了它们:这些不是抽象的理想。它们就是你构建的东西。

你将构建什么

datacenter.sh ——一个配置并运行完整 Kubernetes 环境的单一脚本:16 个微服务、PostgreSQL、KRaft 模式下的 Kafka,以及一个 Web 前端。本地 Kind 集群或裸金属数据中心——相同的代码路径。你编写每一行。你拥有最终成果。

第一部分涵盖 Bash 基础,注重安全性和清晰性:set -o errexit 使脚本绝不会在损坏的状态下继续运行,生命周期钩子在退出时恢复终端状态,cron 模式仅在出错时转储完整日志。第二部分构建基础设施——在开始任何工作之前捕获缺失依赖的二进制验证、将密钥与脚本分离的配置管理、防止并发运行破坏集群状态的脚本锁定。第三部分涵盖 Kubernetes 操作:零停机滚动重启、使用参数数组的幂等 Helm 部署、就绪探针、以及尊重层缓存的 Docker 构建。第四部分贯穿完整的配置序列,从命名空间创建到确认一切正常运行的 curl /health,加上重载、恢复和清理。

每章结尾都有动手练习。附录包含标志参考、故障排除指南和术语表。

本书适合谁

你部署到 Kubernetes,你希望工具属于你自己——版本受控、可理解、快速。你能读懂 Bash。你想以生产质量编写它。你希望你的内部开发循环——修改代码、构建、部署、验证——只需几秒钟,而不是几分钟。

不需要认证。只需要 Docker、Kind,以及构建真实东西的渴望。

Share this book

This book is a translation into Chinese (Simplified) of Kubernetes for All which was originally written in English

Author

About the Author

Joel Bryan Juliano

Hi, I'm Joel.

I am Dutch-Filipino Software Engineer with 20+ years of experience.

And with over 20 years in the game, I’ve seen it all and loved every minute of it.

My journey has taken me through a variety of industries, from sports streaming to cybersecurity, both in the private sector and for the Dutch government, and everything in between.

Along the way, I’ve picked up a diverse set of skills and experiences, in which I document into books.

Translations

Translations

Contents

Table of Contents

引言:用 Bash 将 Kubernetes 粘合在一起

  1. 先决条件
  2. 为什么选择 Bash?
  3. 我们要构建什么
  4. 入口点: setup.sh
  5. 启动 datacenter.sh
  6. 动手练习

安 全 设 置、初 始 化 和 生 命 周 期 钩 子

  1. 全 局 Set 选 项
  2. 使 用 shellcheck 检 查 Shell 脚 本
  3. 脚 本 初 始 化
  4. 主 控 制 器
  5. 错 误 处 理 器
  6. 退 出 处 理 器
  7. 汇 总
  8. 动 手 练 习

退 出 码 与 受 控 退 出

  1. script_exit 函 数
  2. 退 出 码 约 定
  3. Bash 安 全 模 式 回 顾
  4. 更 新 后 的 脚 本
  5. 动 手 练 习

Cron 模 式 与 IO 重 定 向

  1. Cron 模 式 的 作 用
  2. 文 件 描 述 符
  3. 为 Cron 模 式 重 定 向
  4. 更 新 错 误 处 理 函 数
  5. 更 新 退 出 处 理 函 数
  6. 在 main 中 注 册 cron_init
  7. 更 新 后 的 脚 本
  8. 动 手 练 习

库 模 式:组 织 Bash 代 码 库

  1. lib/目 录
  2. 在 main 中 加 载 库
  3. 编 写 库 文 件
  4. 命 名 约 定
  5. 依 赖 关 系 图
  6. 两 个 现 在 值 得 了 解 的 实 用 库
  7. 动 手 练 习

终 端 颜 色 与 日 志 记 录

  1. 日 志 库
  2. 颜 色 初 始 化
  3. log 函 数
  4. verbose_print——详 细 模 式 输 出
  5. 更 新 init
  6. 更 新 main
  7. 更 新 错 误 处 理 函 数
  8. 动 手 练 习

验 证 辅 助 函 数 与 二 进 制 检 查

  1. lib/script.sh
  2. check_binary——必 需 工 具 检 测
  3. check_required_binaries——系 统 前 提 条 件
  4. is_kind——模 式 检 测
  5. check_arguments——变 量 存 在 性 验 证
  6. check_directories——路 径 验 证
  7. check_files——文 件 存 在 性 验 证
  8. check_repos——仓 库 存 在 性 守 卫
  9. build_path——PATH 构 建
  10. 在 init 链 中 使 用 辅 助 函 数
  11. 动 手 练 习

配 置 管 理

  1. 配 置 文 件
  2. lib/config.sh
  3. get_configurations — 交 互 式 设 置
  4. 在 main 中 读 取 配 置
  5. Docker 网 络 模 式
  6. 动 手 练 习

脚 本 锁 定:防 止 并 发 运 行

  1. 为 什 么 用 目 录 而 非 文 件
  2. lock_init
  3. 过 期 锁 处 理
  4. 动 手 练 习

完 整 参 数 解 析

  1. parse_params
  2. script_usage — 帮 助 文 本
  3. 连 接 parse_params 到 main
  4. 动 手 练 习

本 地 模 式 与 Kind 集 群 管 理

  1. 为 什 么 选 择 Kind
  2. lib/kind.sh
  3. start_env — 创 建 Kind 集 群
  4. stop_env — 停 止 集 群
  5. delete_env — 销 毁 集 群
  6. 加 载 镜 像 到 Kind
  7. 无 Docker 环 境 切 换
  8. 动 手 练 习

Docker 操 作

  1. lib/docker.sh
  2. safe_docker — 基 础 包 装
  3. safe_docker_build — 构 建 镜 像
  4. safe_docker_run — 运 行 容 器
  5. 加 载 镜 像 到 Kind
  6. Docker Compose 操 作
  7. docker_cleanup — 移 除 死 亡 容 器 和 镜 像
  8. 动 手 练 习

使 用 kubectl 管 理 Kubernetes

  1. lib/kubectl.sh
  2. safe_kubectl — 基 础 包 装
  3. kubectl_install_resource_from_file
  4. kubectl_install_database — PostgreSQL 设 置
  5. kubectl_restart — 滚 动 重 启
  6. DNS 管 理
  7. 重 新 启 动 所 有 Deployment
  8. 动 手 练 习

Helm: Kubernetes 的 包 管 理

  1. lib/helm.sh
  2. safe_helm — 基 础 包 装
  3. k8s/ 中 的 Helm 图 表
  4. helm_install_service — 通 用 服 务 安 装 器
  5. helm_install_core_service — 基 于 模 板 的 服 务
  6. 安 装 配 置 Release
  7. 更 新 函 数 — 原 位 升 级
  8. Traefik — 入 口 控 制 器
  9. 动 手 练 习

构 建 服 务

  1. 构 建 管 道
  2. lib/build.sh
  3. build_service — 核 心 构 建 函 数
  4. 服 务 组 函 数
  5. build_images_init — 构 建 分 派 器
  6. lib/install_deps.sh
  7. check_registry
  8. 动 手 练 习

从 零 开 始 部 署

  1. 系 统 架 构
  2. 完 整 部 署 序 列
  3. 逐 步 说 明
  4. 支 持 函 数
  5. Kubernetes 集 群 参 数
  6. Kubernetes 清 单 参 考
  7. 动 手 练 习
  8. 成 功 的 样 子

重 载 和 恢 复 操 作

  1. lib/reload_services.sh
  2. reload_services — 轻 量 级 服 务 刷 新
  3. reload_services_init — 重 载 分 派 器
  4. lib/reload_kubernetes.sh
  5. reload_all_kubernetes — 完 整 集 群 重 建
  6. 动 手 练 习

拆 除 和 清 理

  1. 停 止 环 境
  2. 删 除 环 境
  3. 完 整 Kubernetes 拆 除
  4. Docker 清 理
  5. 退 出 处 理 器:完 整 版 本
  6. 汇 总 完 整 脚 本
  7. 完 整 的 main() 函 数
  8. 完 整 的 init 函 数
  9. 完 整 生 命 周 期
  10. 你 构 建 的 开 发 者 体 验
  11. 动 手 练 习

使用 ArgoCD 的 GitOps

  1. lib/argocd.sh
  2. safe_argocd — 基础封装
  3. helm_install_argocd — 安装控制器
  4. argocd_wait_ready — 确认启动
  5. argocd_login — 为 CLI 认证
  6. argocd_create_app — 注册应用
  7. argocd_sync — 触发同步
  8. argocd_wait_healthy — 等待同步完成
  9. argocd_delete_app — 移除应用
  10. 把 ArgoCD 集成进 datacenter.sh
  11. 关于 OpenShift 的说明
  12. 动手练习

使用 Prometheus 和 Grafana 的指标与仪表板

  1. lib/monitoring.sh
  2. helm_install_monitoring — 完整技术栈
  3. monitoring_wait_ready — 确认启动
  4. monitoring_port_forward — 本地访问
  5. monitoring_apply_servicemonitor — 抓取你的服务
  6. monitoring_get_grafana_password — 获取凭据
  7. 把监控集成进 datacenter.sh
  8. kube-prometheus-stack 为你提供什么
  9. 动手练习

使用 OpenTelemetry 的分布式追踪

  1. lib/otel.sh
  2. safe_otel — 基础封装
  3. helm_install_otel_operator — 安装 Operator
  4. otel_wait_ready — 确认 Operator 正在运行
  5. otel_apply_collector — 部署 Collector
  6. otel_apply_instrumentation — 零代码自动插桩
  7. otel_wait_collector_ready — 确认 Collector
  8. Grafana Tempo — 追踪后端
  9. 把 OTel 集成进 datacenter.sh
  10. 把追踪、指标和日志连起来
  11. 动手练习

附 录: datacenter.sh 命 令 参 考

  1. setup.sh
  2. datacenter.sh
  3. 退 出 码
  4. 可 链 接 与 不 可 链 接 参 数
  5. 常 见 工 作 流
  6. 故 障 排 除

术 语 表

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub