2026-10-08 18:19:34 +08:00
<!DOCTYPE html>
< html lang = "zh-CN" data-theme = "light" >
< head >
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< title > 基础设施即代码 · 阶段 4 模块 3 · SRE 从零到成熟< / title >
< meta name = "description" content = "基础设施即代码 模块目录:共 13 章、47 个小节。" >
< link rel = "stylesheet" href = "../../assets/doc.css" >
< / head >
< body >
< header class = "topbar" >
< div class = "tb-in" >
< a class = "brand" href = "../../index.html" > < span class = "logo" > SRE< / span > < div > 从零到成熟< small > 模块详解< / small > < / div > < / a >
< div class = "crumbs" >
< a href = "../../index.html" > 学习路径< / a > < span class = "sep" > /< / span >
< a href = "../../index.html#s4" > 阶段 4 · 上规模< / a > < span class = "sep" > /< / span >
< b > 基础设施即代码< / b >
< / div >
< div class = "spacer" > < / div >
< button class = "btn" id = "tocToggle" > ☰ 导航< / button >
< button class = "btn" id = "themeBtn" title = "切换到深色主题" > 深色< / button >
< / div >
< / header >
< div class = "layout" id = "layout" >
< aside class = "sidebar" >
< h2 > 章节目录< / h2 >
< nav id = "chapters" > < a class = "toc-a" href = "01-why.html" > < i class = "cn" > 1< / i > 为什么要 IaC: 手工改线上资源的代价< / a > < div class = "subnav" > < a class = "toc-a lv3" href = "01-why.html#sec-1-1" > 手工变更的四类代价< / a > < a class = "toc-a lv3" href = "01-why.html#sec-1-2" > 谁管什么:四类工具的职责边界< / a > < a class = "toc-a lv3" href = "01-why.html#sec-1-3" > 哪些资源必须进代码:四条判据< / a > < / div > < a class = "toc-a" href = "02-tf-state.html" > < i class = "cn" > 2< / i > Terraform state: 为什么它是命门< / a > < div class = "subnav" > < a class = "toc-a lv3" href = "02-tf-state.html#sec-2-1" > state 里到底存了什么< / a > < a class = "toc-a lv3" href = "02-tf-state.html#sec-2-2" > 本地 state 的四个灾难< / a > < a class = "toc-a lv3" href = "02-tf-state.html#sec-2-3" > remote backend 与加锁< / a > < a class = "toc-a lv3" href = "02-tf-state.html#sec-2-4" > state 的操作规矩< / a > < / div > < a class = "toc-a" href = "03-tf-module.html" > < i class = "cn" > 3< / i > module 化拆分:从一个巨型 main.tf 到可复用组件< / a > < div class = "subnav" > < a class = "toc-a lv3" href = "03-tf-module.html#sec-3-1" > 目录结构:把「可复用」和「可部署」分开< / a > < a class = "toc-a lv3" href = "03-tf-module.html#sec-3-2" > 输入输出契约与版本约束< / a > < a class = "toc-a lv3" href = "03-tf-module.html#sec-3-3" > 拆分的判据:什么时候拆,什么时候别拆< / a > < a class = "toc-a lv3" href = "03-tf-module.html#sec-3-4" > state 边界与跨 state 传值< / a > < / div > < a class = "toc-a" href = "04-tf-workflow.html" > < i class = "cn" > 4< / i > 工作流: fmt / validate / plan / apply< / a > < div class = "subnav" > < a class = "toc-a lv3" href = "04-tf-workflow.html#sec-4-1" > 四条命令的真实语义< / a > < a class = "toc-a lv3" href = "04-tf-workflow.html#sec-4-2" > plan 结果必须评审:评审什么< / a > < a class = "toc-a lv3" href = "04-tf-workflow.html#sec-4-3" > CI 里怎么做< / a > < a class = "toc-a lv3" href = "04-tf-workflow.html#sec-4-4" > 危险信号清单< / a > < / div > < a class = "toc-a" href = "05-tf-drift.html" > < i class = "cn" > 5< / i > 漂移治理:发现手工改动并收敛< / a > < div class = "subnav" > < a class = "toc-a lv3" href = "05-tf-drift.html#sec-5-1" > 漂移的三种来源< / a > < a class = "toc-a lv3" href = "05-tf-drift.html#sec-5-2" > 发现:三种手段,判据各不相同< / a > < a class = "toc-a lv3" href = "05-tf-drift.html#sec-5-3" > 收敛:四种手段与它们的风险< / a > < a class = "toc-a lv3" href = "05-tf-drift.html#sec-5-4" > 漂移预算与禁止清单< / a > < / div > < a class = "toc-a" href = "06-ansible.html" > < i class = "cn" > 6< / i > Ansible: inventory、playbook、role 与幂等预演< / a > < div class = "subnav" > < a class = "toc-a lv3" href = "06-ansible.html#sec-6-1" > inventory: 先定机器集合, 再谈任务< / a > < a class = "toc-a lv3" href = "06-ansible.html#sec-6-2" > playbook 只做编排,逻辑放 role< / a > < a class = "toc-a lv3" href = "06-ansible.html#sec-6-3" > 幂等性:唯一可自动验证的判据< / a > < a class = "toc-a lv3" href = "06-ansible.html#sec-6-4" > --check 预演:能信到什么程度< / a > < / div > < a class = "toc-a" href = "07-immutable.html" > < i class = "cn" > 7< / i > 不可变基础设施: Packer 构建镜像与蓝绿替换< / a > < div class = "subnav" > < a class = "toc-a lv3" href = "07-immutable.html#sec-7-1" > 漂移的数学:为什么巡检打不过手工< / a > < a class = "toc-a lv3" href = "07-immutable.html#sec-7-2" > Packer: 把配置固化进镜像< / a > < a class = "toc-a lv3" href = "07-immutable.html#sec-7-3" > 蓝绿替换:把「改配置」变成「换机器」< / a > < a class = "toc-a lv3" href = "07-immutable.html#sec-7-4" > 彻底消除漂移的三条机制< / a > < / div > < a class = "toc-a" href = "08-security.html" > < i class = "cn" > 8< / i > 安全与凭据: 最小权限、OIDC 与 state 里的明文< / a > < div class = "subnav" > < a class = "toc-a lv3" href = "08-security.html#sec-8-1" > 最小权限:按 state 边界切角色< / a > < a class = "toc-a lv3" href = "08-security.html#sec-8-2" > OIDC: 把长期密钥换成一次性的< / a > < a class = "toc-a lv3" href = "08-security.html#sec-8-3" > state 与 plan 里的明文密钥< / a > < a class = "toc-a lv3" href = "08-security.html#sec-8-4" > 护栏与审计< / a > < / div > < a class = "toc-a" href = "09-tags.html" > < i class = "cn" > 9< / i > 标签规范与成本归属:让每个团队看到自
< div class = "side-foot" >
< a class = "btn" href = "index.html" > ← 模块目录< / a >
< a class = "btn" href = "../../index.html" > 学习路径< / a >
< / div >
< / aside >
< div class = "mask" id = "mask" > < / div >
< main class = "main-col" >
< article >
< div class = "modhero" >
< span class = "mh-no" > 阶段 4 · 模块 3 / 5< / span >
< h1 > 基础设施即代码< / h1 >
< p class = "mh-sub" > 阶段 4 · 上规模 · Terraform state 与模块化、plan 评审、漂移治理、Ansible 幂等、不可变镜像、凭据与成本归属< / p >
< div class = "mh-meta" >
< span > 共 < b > 13< / b > 章< / span >
< span > < b > 47< / b > 个小节< / span >
< span > 约 < b > 75< / b > 千字< / span >
< / div >
< / div >
< p class = "lead" >
< b > 模块定位:< / b > 阶段 4 的前两个模块解决「集群里跑什么」和「怎么把它发上去」:< a href = "../s4-01-k8s/index.html" > Kubernetes 核心< / a > 把工作负载变成有期望状态的声明式对象,< a href = "../s4-02-gitops/index.html" > GitOps 与声明式交付< / a > 让集群内的对象从 Git 单向同步。但集群脚下的 VPC、子网、安全组、负载均衡、数据库、对象存储、IAM 角色、DNS 记录,仍然是人在控制台点出来的。这个模块处理的就是这一层:把 7 个环境、1180 个云资源、每月 4.2 万美元的基础设施,从「某个人记得怎么配」变成「一份可以 diff、可以评审、可以回滚的代码」。它是 GitOps 的延长线,只是同步的对象从 K8s 对象换成了云 API。
< br > < br >
< b > 学完之后你应该能:< / b > 给一套已经跑着的云资源补上 import 并让 plan 归零;把 remote backend 与加锁配好,说清 state 丢失后的三种后果与恢复路径;按生命周期边界把 3184 行的 main.tf 拆成 3 个 state 与 6 个可复用 module, 并给 module 加上版本约束和输入校验;把 fmt / validate / plan / apply 挂进 CI, 让 apply 只执行评审过的那份计划文件;用 refresh-only plan 与 import / state mv 治理漂移;写出幂等、可 < code > --check< / code > 预演的 Ansible playbook 与 role; 用 Packer 把配置固化进镜像再用实例刷新做蓝绿替换;用 OIDC 换掉长期 AK, 把 state 与 plan 里的明文密钥清干净;给全部资源打上可分摊成本的标签,让每个团队每月看到自己的账单;最后用一份变量文件在空账号里创建网络 + 集群 + 节点池,验证完完整销毁、次日账单归零。
< br > < br >
< b > 前置:< / b > 阶段 1 的 < a href = "../s1-03-container/index.html" > 容器与镜像< / a > (不可变镜像的心智模型与 tag 规范直接复用到第 7 章)和 < a href = "../s1-04-cicd/index.html" > CI/CD 流水线< / a > (第 4 章的 plan 评审与第 8 章的 OIDC 都要挂在流水线上);阶段 4 的 < a href = "../s4-01-k8s/index.html" > Kubernetes 核心< / a > (第 11 章的节点池、标签、taint 与 < code > aws-auth< / code > 会用到);阶段 3 的 < a href = "../s3-03-capacity/index.html" > 容量规划与成本< / a > (第 9 章的成本归属要与容量模型对得上口径)。需要一台装好 Terraform 1.7 以上的机器、一个允许创建并删除资源的云账号(实验预算 20– 40 美元足够)、以及一个能跑 Ansible 的 Linux 环境( WSL 或容器都可以)。
< br > < br >
< b > 建议投入:< / b > 22– 26 小时,其中至少 12 小时必须动手:迁移一次真实 state、写一个被两个环境同时引用的 module、故意制造一次漂移再收敛、把 plan 评审挂进 CI、跑完 apply → 验证 → destroy 的闭环。只读不练的话,第 5 章的漂移处置和第 11 章的销毁清场一定会在真实环境里翻车。
< / p >
< h2 id = "chapters-list" > 章节目录< / h2 >
< ol class = "chaplist" >
< li class = "chap-item" >
< div class = "ci-head" >
< span class = "ci-no" > 1< / span >
< a class = "ci-title" href = "01-why.html" > 为什么要 IaC: 手工改线上资源的代价< / a >
< a class = "ci-go" href = "01-why.html" > 进入 →< / a >
< / div >
< div class = "ci-secs" > < a href = "01-why.html#sec-1-1" > 手工变更的四类代价< / a > < a href = "01-why.html#sec-1-2" > 谁管什么:四类工具的职责边界< / a > < a href = "01-why.html#sec-1-3" > 哪些资源必须进代码:四条判据< / a > < / div >
< / li >
< li class = "chap-item" >
< div class = "ci-head" >
< span class = "ci-no" > 2< / span >
< a class = "ci-title" href = "02-tf-state.html" > Terraform state: 为什么它是命门< / a >
< a class = "ci-go" href = "02-tf-state.html" > 进入 →< / a >
< / div >
< div class = "ci-secs" > < a href = "02-tf-state.html#sec-2-1" > state 里到底存了什么< / a > < a href = "02-tf-state.html#sec-2-2" > 本地 state 的四个灾难< / a > < a href = "02-tf-state.html#sec-2-3" > remote backend 与加锁< / a > < a href = "02-tf-state.html#sec-2-4" > state 的操作规矩< / a > < / div >
< / li >
< li class = "chap-item" >
< div class = "ci-head" >
< span class = "ci-no" > 3< / span >
< a class = "ci-title" href = "03-tf-module.html" > module 化拆分:从一个巨型 main.tf 到可复用组件< / a >
< a class = "ci-go" href = "03-tf-module.html" > 进入 →< / a >
< / div >
< div class = "ci-secs" > < a href = "03-tf-module.html#sec-3-1" > 目录结构:把「可复用」和「可部署」分开< / a > < a href = "03-tf-module.html#sec-3-2" > 输入输出契约与版本约束< / a > < a href = "03-tf-module.html#sec-3-3" > 拆分的判据:什么时候拆,什么时候别拆< / a > < a href = "03-tf-module.html#sec-3-4" > state 边界与跨 state 传值< / a > < / div >
< / li >
< li class = "chap-item" >
< div class = "ci-head" >
< span class = "ci-no" > 4< / span >
< a class = "ci-title" href = "04-tf-workflow.html" > 工作流: fmt / validate / plan / apply< / a >
< a class = "ci-go" href = "04-tf-workflow.html" > 进入 →< / a >
< / div >
< div class = "ci-secs" > < a href = "04-tf-workflow.html#sec-4-1" > 四条命令的真实语义< / a > < a href = "04-tf-workflow.html#sec-4-2" > plan 结果必须评审:评审什么< / a > < a href = "04-tf-workflow.html#sec-4-3" > CI 里怎么做< / a > < a href = "04-tf-workflow.html#sec-4-4" > 危险信号清单< / a > < / div >
< / li >
< li class = "chap-item" >
< div class = "ci-head" >
< span class = "ci-no" > 5< / span >
< a class = "ci-title" href = "05-tf-drift.html" > 漂移治理:发现手工改动并收敛< / a >
< a class = "ci-go" href = "05-tf-drift.html" > 进入 →< / a >
< / div >
< div class = "ci-secs" > < a href = "05-tf-drift.html#sec-5-1" > 漂移的三种来源< / a > < a href = "05-tf-drift.html#sec-5-2" > 发现:三种手段,判据各不相同< / a > < a href = "05-tf-drift.html#sec-5-3" > 收敛:四种手段与它们的风险< / a > < a href = "05-tf-drift.html#sec-5-4" > 漂移预算与禁止清单< / a > < / div >
< / li >
< li class = "chap-item" >
< div class = "ci-head" >
< span class = "ci-no" > 6< / span >
< a class = "ci-title" href = "06-ansible.html" > Ansible: inventory、playbook、role 与幂等预演< / a >
< a class = "ci-go" href = "06-ansible.html" > 进入 →< / a >
< / div >
< div class = "ci-secs" > < a href = "06-ansible.html#sec-6-1" > inventory: 先定机器集合, 再谈任务< / a > < a href = "06-ansible.html#sec-6-2" > playbook 只做编排,逻辑放 role< / a > < a href = "06-ansible.html#sec-6-3" > 幂等性:唯一可自动验证的判据< / a > < a href = "06-ansible.html#sec-6-4" > --check 预演:能信到什么程度< / a > < / div >
< / li >
< li class = "chap-item" >
< div class = "ci-head" >
< span class = "ci-no" > 7< / span >
< a class = "ci-title" href = "07-immutable.html" > 不可变基础设施: Packer 构建镜像与蓝绿替换< / a >
< a class = "ci-go" href = "07-immutable.html" > 进入 →< / a >
< / div >
< div class = "ci-secs" > < a href = "07-immutable.html#sec-7-1" > 漂移的数学:为什么巡检打不过手工< / a > < a href = "07-immutable.html#sec-7-2" > Packer: 把配置固化进镜像< / a > < a href = "07-immutable.html#sec-7-3" > 蓝绿替换:把「改配置」变成「换机器」< / a > < a href = "07-immutable.html#sec-7-4" > 彻底消除漂移的三条机制< / a > < / div >
< / li >
< li class = "chap-item" >
< div class = "ci-head" >
< span class = "ci-no" > 8< / span >
< a class = "ci-title" href = "08-security.html" > 安全与凭据: 最小权限、OIDC 与 state 里的明文< / a >
< a class = "ci-go" href = "08-security.html" > 进入 →< / a >
< / div >
< div class = "ci-secs" > < a href = "08-security.html#sec-8-1" > 最小权限:按 state 边界切角色< / a > < a href = "08-security.html#sec-8-2" > OIDC: 把长期密钥换成一次性的< / a > < a href = "08-security.html#sec-8-3" > state 与 plan 里的明文密钥< / a > < a href = "08-security.html#sec-8-4" > 护栏与审计< / a > < / div >
< / li >
< li class = "chap-item" >
< div class = "ci-head" >
< span class = "ci-no" > 9< / span >
< a class = "ci-title" href = "09-tags.html" > 标签规范与成本归属:让每个团队看到自己的账单< / a >
< a class = "ci-go" href = "09-tags.html" > 进入 →< / a >
< / div >
< div class = "ci-secs" > < a href = "09-tags.html#sec-9-1" > 标签体系:八类强制标签与它们的值域< / a > < a href = "09-tags.html#sec-9-2" > 一致性落地:从「靠自觉」到「建不出来」< / a > < a href = "09-tags.html#sec-9-3" > 成本归属到团队< / a > < / div >
< / li >
< li class = "chap-item" >
< div class = "ci-head" >
< span class = "ci-no" > 10< / span >
< a class = "ci-title" href = "10-antipattern.html" > 反模式合集:四种把 IaC 做成负债的写法< / a >
< a class = "ci-go" href = "10-antipattern.html" > 进入 →< / a >
< / div >
< div class = "ci-secs" > < a href = "10-antipattern.html#sec-10-1" > 反模式一:本地 state< / a > < a href = "10-antipattern.html#sec-10-2" > 反模式二:手工改线上资源< / a > < a href = "10-antipattern.html#sec-10-3" > 反模式三:一个巨型 main.tf< / a > < a href = "10-antipattern.html#sec-10-4" > 反模式四:无评审 apply< / a > < / div >
< / li >
< li class = "chap-item" >
< div class = "ci-head" >
< span class = "ci-no" > 11< / span >
< a class = "ci-title" href = "11-lab.html" > 综合实操:一键创建网络 + 集群 + 节点池并完整销毁< / a >
< a class = "ci-go" href = "11-lab.html" > 进入 →< / a >
< / div >
< div class = "ci-secs" > < a href = "11-lab.html#sec-11-1" > 目标、资源清单与成本< / a > < a href = "11-lab.html#sec-11-2" > 代码骨架:三层、三个 state、三种生命周期< / a > < a href = "11-lab.html#sec-11-3" > 创建与验证:验收判据写死在命令里< / a > < a href = "11-lab.html#sec-11-4" > 销毁与残留清理:真正的验收< / a > < / div >
< / li >
< li class = "chap-item" >
< div class = "ci-head" >
< span class = "ci-no" > 12< / span >
< a class = "ci-title" href = "12-cheat.html" > 速查表与高频坑< / a >
< a class = "ci-go" href = "12-cheat.html" > 进入 →< / a >
< / div >
< div class = "ci-secs" > < a href = "12-cheat.html#sec-12-1" > 速查表< / a > < a href = "12-cheat.html#sec-12-2" > 十二个高频坑< / a > < a href = "12-cheat.html#sec-12-3" > 三分钟决策树< / a > < / div >
< / li >
< li class = "chap-item" >
< div class = "ci-head" >
< span class = "ci-no" > 13< / span >
< a class = "ci-title" href = "13-checklist.html" > 完成标准与自测< / a >
< a class = "ci-go" href = "13-checklist.html" > 进入 →< / a >
< / div >
< div class = "ci-secs" > < a href = "13-checklist.html#sec-x1" > 自测题< / a > < a href = "13-checklist.html#sec-x2" > 下一步< / a > < / div >
< / li >
< / ol >
< div class = "box tip" >
< span class = "bt" > 怎么用这一模块< / span >
< p > 左侧是< b > 章节目录树< / b > :先看章标题快速定位,进入某一章后该章的小节会展开并跟随滚动高亮;点其它章标题可直接跳到对应页面。< / p >
< p > 每章结尾都有「上一章 / 下一章」可以连续阅读,代码块右上角可以一键复制。< / p >
< / div >
< / article >
< nav class = "pager" >
< a class = "prev" href = "../s4-02-gitops/index.html" > < span class = "k" > ← 上一模块< / span > < span class = "v" > GitOps 与声明式交付< / span > < / a >
< a class = "next" href = "../s4-04-mesh/index.html" > < span class = "k" > 下一模块 →< / span > < span class = "v" > 服务网格与网关< / span > < / a >
< / nav >
< p class = "foot" >
基础设施即代码 · 模块目录< br >
< a href = "../../index.html" > 返回学习路径< / a >
< / p >
< / main >
< / div >
< button id = "toTop" title = "回到顶部" > ↑< / button >
< script src = "../../assets/doc.js" > < / script >
2026-10-14 22:42:38 +08:00
< script src = "../../../../assets/back.js" data-portal-back = "knowledge/sre-learning-path" data-portal-label = "知识库 › SRE 从零到成熟学习路径" > < / script >
2026-10-08 18:19:34 +08:00
< / body >
< / html >