# 不可变基础设施

**不可变基础设施**是指一经部署便永不修改、只会被替换的基础设施：要更新应用、更改配置或应用安全补丁，就构建并部署一个新的基础设施制品来替换现有的制品，任何人都不登录运行中的服务器做手动更改。<sup>[1](https://docs.hashicorp.com/well-architected-framework/define-and-automate-processes/define/immutable-infrastructure)</sup> 这一做法与可变基础设施相对，后者在原位进行更改，部署时修改现有服务器的配置，而不是替换它们。<sup>[2](https://learn.microsoft.com/azure/well-architected/operational-excellence/infrastructure-as-code-design)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心规则 | 一经部署，基础设施永不修改；每一次变更都会产生一个新制品，用以替换旧部署<sup>[1](https://docs.hashicorp.com/well-architected-framework/define-and-automate-processes/define/immutable-infrastructure)</sup> |
| 制品形式 | 虚拟机镜像（AWS AMI、Azure 镜像、GCP 镜像）、容器镜像（Docker/OCI）、无服务器部署包，或在 NixOS 中为一个系统闭包<sup>[1](https://docs.hashicorp.com/well-architected-framework/define-and-automate-processes/define/immutable-infrastructure)</sup><sup> • </sup><sup>[3](https://glossary.deployment.to/immutable-infrastructure/)</sup><sup> • </sup><sup>[4](https://raganmcgill.co.uk/c4e/engineering-excellence/Practice/practice-immutable-infrastructure)</sup> |
| 工具 | Terraform 等基础设施即代码工具通过“变更时销毁并重建资源”来管理不可变性<sup>[1](https://docs.hashicorp.com/well-architected-framework/define-and-automate-processes/define/immutable-infrastructure)</sup> |
| 主要收益 | 消除配置漂移，防止手动更改错误，通过重新部署先前的制品实现回滚，并通过版本控制和 CI/CD 日志形成审计轨迹<sup>[1](https://docs.hashicorp.com/well-architected-framework/define-and-automate-processes/define/immutable-infrastructure)</sup> |
| 主要成本 | 每次部署重建的不可变组件越多，部署在部署时间和运营成本上的开销就越大<sup>[5](https://aws.amazon.com/blogs/architecture/a-multi-dimensional-approach-helps-you-proactively-prepare-for-failures-part-3-operations-and-process-resiliency/)</sup> |
| 有状态数据的处理 | 数据库、用户上传、会话和日志迁移到托管服务、对象存储或主机外投递，因为主机本身将被销毁<sup>[6](https://github.com/mchittineni/ultimate-devops-guide/blob/main/infrastructure-as-code/what-is-immutable-infrastructure-and-how-do-you-adopt-it.md)</sup> |
| 术语起源 | “不可变服务器”（Immutable Server）由本·巴特勒-科尔在 Thoughtworks 内部邮件列表中提出；基夫·莫里斯于 2013 年 6 月 13 日在 martinfowler.com 上描述了这一模式<sup>[7](https://martinfowler.com/bliki/ImmutableServer.html)</sup> |

## 它解决的问题

在传统模式下，服务器是长期存活的：安装一次，永远打补丁，行为变得古怪时排查故障，最终因为累积的漂移使系统无法复现而重建。<sup>[8](https://stackharbor.com/en/knowledge-base/immutable-infrastructure/)</sup> CFEngine、[Puppet](https://www.edgepedia.org/zh/puppet-software) 和 Chef 等自动化配置工具的开发，是为了让新机器和现有机器都符合某项规范；理论上，服务器可以无限期运行而保持一致。由于现实世界中没有任何服务器配置能够被完全管理，运行中的服务器有充分的机会以不可预见的方式漂移或改变。<sup>[7](https://martinfowler.com/bliki/ImmutableServer.html)</sup>

AWS 将漂移定义为任何导致基础设施资源的状态或配置与预期不同的变更，并指出任何未受管理的配置变更都违背不可变基础设施的理念，应当被检测并修复。<sup>[9](https://docs.aws.amazon.com/wellarchitected/latest/reliability-pillar/rel_tracking_change_management_immutable_infrastructure.html)</sup>

**四项运维收益。** HashiCorp 的 Well-Architected 指南列举了不可变基础设施所应对的挑战：它通过部署完全相同的制品消除配置漂移；它防止手动更改错误，如拼写错误、遗漏步骤或将更改应用到错误的服务器；它通过重新部署先前的制品而非修复当前基础设施来实现可靠回滚；它简化安全合规，因为审计人员需要证据来证明生产基础设施未在批准流程之外被修改，而版本控制加 CI/CD 日志恰好能显示改了什么、何时改的、谁批准的。<sup>[1](https://docs.hashicorp.com/well-architected-framework/define-and-automate-processes/define/immutable-infrastructure)</sup>

## 工作原理

部署的单位是一个版本化的制品，构建一次，然后依次晋升到各环境：在预发环境测试过的同一制品被部署到生产环境，而不是重新构建。<sup>[4](https://raganmcgill.co.uk/c4e/engineering-excellence/Practice/practice-immutable-infrastructure)</sup> CI 流水线将应用代码、基础镜像和构建配方（Packer、kaniko 或 Bazel）结合，产出一个版本化镜像；Terraform 或 CloudFormation 引用该镜像 ID，更改这个 ID 就是部署。<sup>[6](https://github.com/mchittineni/ultimate-devops-guide/blob/main/infrastructure-as-code/what-is-immutable-infrastructure-and-how-do-you-adopt-it.md)</sup><sup> • </sup><sup>[8](https://stackharbor.com/en/knowledge-base/immutable-infrastructure/)</sup>

**虚拟机与容器。** [虚拟机](https://www.edgepedia.org/zh/virtual-machine)镜像将整个技术栈——包括操作系统、依赖项和应用代码——打包成 AWS AMI、Azure 镜像或 GCP 镜像等制品，用 Packer 构建，用 Terraform 部署。容器将应用及其依赖打包成不可更改的镜像，用 [Docker](https://www.edgepedia.org/zh/docker-software) 或 Packer 构建一次，用 [Kubernetes](https://www.edgepedia.org/zh/kubernetes) 或 Nomad 等编排器部署；运行中的容器永不修改，变更会触发重建和替换。<sup>[1](https://docs.hashicorp.com/well-architected-framework/define-and-automate-processes/define/immutable-infrastructure)</sup><sup> • </sup><sup>[10](https://docs.hashicorp.com/well-architected-framework/define-and-automate-processes/define/immutable-infrastructure/containers)</sup>

**发布与回滚。** 当 Kubernetes Deployment 中引用了新的镜像标签时，Kubernetes 执行滚动更新，逐步替换旧容器，同时保持可用性。<sup>[10](https://docs.hashicorp.com/well-architected-framework/define-and-automate-processes/define/immutable-infrastructure/containers)</sup> 在基础设施层面，蓝/绿节点组、滚动实例刷新或负载均衡器层面的金丝雀部署将坏镜像限制在一小部分流量上，回滚就是重新指向先前的版本。<sup>[6](https://github.com/mchittineni/ultimate-devops-guide/blob/main/infrastructure-as-code/what-is-immutable-infrastructure-and-how-do-you-adopt-it.md)</sup> 由于每个版本只构建一次并作为可部署制品存储，回滚与部署是同一操作，只是指向先前的镜像，没有特殊路径。<sup>[8](https://stackharbor.com/en/knowledge-base/immutable-infrastructure/)</sup>

## 配置管理发生了什么变化

不可变基础设施并不取消 [Ansible](https://www.edgepedia.org/zh/ansible-software)、Chef 或 Puppet 等配置管理工具；它把这些工具左移，移入镜像构建过程，在那里它们是表达“安装并配置这个”的便捷方式。<sup>[6](https://github.com/mchittineni/ultimate-devops-guide/blob/main/infrastructure-as-code/what-is-immutable-infrastructure-and-how-do-you-adopt-it.md)</sup> 学术文献划出了同样的界线：以配置为中心的 IaC（CFEngine、Puppet、Chef、Ansible）重新配置可变基础设施，而以预配为中心的 IaC 管理不可变基础设施，其中的资源被创建、配置一次，更新时被替换。<sup>[11](https://mjuz.rocks/assets/pdf/papers/2024_Reliable-Infrastructure-as-Code-for-Decentralized-Organizations.pdf)</sup> 一旦服务器从经过充分测试的基础镜像启动，配置管理工具就不应在它上面运行，因为它们会制造未经测试的变更机会；修复应进入基础镜像、经过测试，然后将没有该更改的服务器拆除并替换。<sup>[7](https://martinfowler.com/bliki/ImmutableServer.html)</sup>

云原生原则文档补充道，变更管理模型的选择与部署策略是两回事：任何支持替换的策略，如凤凰部署或金丝雀部署，都可以与不可变元素一起使用。<sup>[12](https://github.com/cloud-native-principles/cloud-native-principles/blob/master/cloud-native-immutable-infrastructure-principles.md)</sup>

## 有状态数据与边界

如果服务器是一次性的，其上的数据往往不是。有价值但无运行时需求的数据可以投递到实例之外，例如日志文件发送到中央 syslog 服务器；共享文件系统、AWS EBS 卷等云可挂载存储，以及 [Amazon RDS](https://www.edgepedia.org/zh/amazon-rds) 等托管服务，使数据在服务器替换后依然存活。<sup>[7](https://martinfowler.com/bliki/ImmutableServer.html)</sup> 在实践中，数据库迁移到托管服务或专用的有状态集群，用户上传迁移到对象存储，会话迁移到 Redis，日志和指标在产生时即投递到主机之外，因为主机将被删除。<sup>[6](https://github.com/mchittineni/ultimate-devops-guide/blob/main/infrastructure-as-code/what-is-immutable-infrastructure-and-how-do-you-adopt-it.md)</sup>

零停机变更带来了数据格式问题：如果新组件版本改变了数据格式，两个版本就无法共享同一存储。推荐的做法是将数据格式变更与软件发布解耦：编写同时兼容原始格式和新格式的软件，将数据迁移作为后台任务，并在后续版本中移除对旧格式的兼容。<sup>[12](https://github.com/cloud-native-principles/cloud-native-principles/blob/master/cloud-native-immutable-infrastructure-principles.md)</sup>

不可变性也有实际边界。真实系统常常在托管但可变的平台（如托管数据库或托管 Kubernetes 控制平面）之上运行不可变的应用层；规则是边界必须明确，而不是技术栈中的一切都必须不可变。<sup>[3](https://glossary.deployment.to/immutable-infrastructure/)</sup>

## 成本与权衡

不可变性可以按不同的粒度应用，取决于为新应用版本重建基础设施的哪一部分；重建的组件越多，每次部署在时间和运营成本上的开销就越大；作为交换，不可变基础设施更容易回滚。<sup>[5](https://aws.amazon.com/blogs/architecture/a-multi-dimensional-approach-helps-you-proactively-prepare-for-failures-part-3-operations-and-process-resiliency/)</sup> 启动缓慢的镜像会使自动扩缩容和发布变得缓慢，这就是预烘焙镜像胜过冗长的用户数据脚本的原因。<sup>[6](https://github.com/mchittineni/ultimate-devops-guide/blob/main/infrastructure-as-code/what-is-immutable-infrastructure-and-how-do-you-adopt-it.md)</sup> 烘焙、发布、切换、排空的不可变发布流水线通常由 Packer、Terraform 和云提供商 API 等步骤和外部服务拼装而成，而不是作为单一产品提供。<sup>[3](https://glossary.deployment.to/immutable-infrastructure/)</sup>

## 历史

基夫·莫里斯是《基础设施即代码：云中的服务器管理》*（Infrastructure as Code: Managing Servers in the Cloud）* 的合著者，他于 2013 年 6 月 13 日描述了不可变服务器模式，将其视为自动化配置的逻辑终点：服务器一经部署便永不修改，只是被新的更新实例所替换。他将这一术语的创造归功于他在 Thoughtworks 的同事本·巴特勒-科尔，后者在内部邮件列表中提出了该词；并指出 Netflix 一直处于使用该模式的前沿，开源了其为管理 AWS 上的 AMI 而开发的 Aminator 工具，而快速实例化新实例是自动扩缩容和恢复的关键驱动力。<sup>[7](https://martinfowler.com/bliki/ImmutableServer.html)</sup> 与之相关的心态体现在“像对待牲口而不是宠物那样对待你的服务器”这一说法中，云原生原则合集引用了基夫·莫里斯著作中的这一说法。<sup>[12](https://github.com/cloud-native-principles/cloud-native-principles/blob/master/cloud-native-immutable-infrastructure-principles.md)</sup> AWS 和 Azure 的 Well-Architected 框架现在都将不可变部署推荐为可靠性和运营卓越的实践。<sup>[9](https://docs.aws.amazon.com/wellarchitected/latest/reliability-pillar/rel_tracking_change_management_immutable_infrastructure.html)</sup><sup> • </sup><sup>[2](https://learn.microsoft.com/azure/well-architected/operational-excellence/infrastructure-as-code-design)</sup>

## References

1. [Create immutable infrastructure, HashiCorp Well-Architected Framework](https://docs.hashicorp.com/well-architected-framework/define-and-automate-processes/define/immutable-infrastructure)
2. [Architecture strategies for using infrastructure as code, Microsoft Azure Well-Architected Framework](https://learn.microsoft.com/azure/well-architected/operational-excellence/infrastructure-as-code-design)
3. [What is immutable infrastructure? Meaning + example, deployment.to glossary](https://glossary.deployment.to/immutable-infrastructure/)
4. [Immutable Infrastructure, Engineering Practice, Ragan McGill](https://raganmcgill.co.uk/c4e/engineering-excellence/Practice/practice-immutable-infrastructure)
5. [A multi-dimensional approach helps you proactively prepare for failures, Part 3: Operations and process resiliency, AWS Architecture Blog](https://aws.amazon.com/blogs/architecture/a-multi-dimensional-approach-helps-you-proactively-prepare-for-failures-part-3-operations-and-process-resiliency/)
6. [What is immutable infrastructure and how do you adopt it, ultimate-devops-guide (GitHub)](https://github.com/mchittineni/ultimate-devops-guide/blob/main/infrastructure-as-code/what-is-immutable-infrastructure-and-how-do-you-adopt-it.md)
7. [Immutable Server, Kief Morris on martinfowler.com, 13 June 2013](https://martinfowler.com/bliki/ImmutableServer.html)
8. [Immutable infrastructure: replace, do not patch, Stack Harbor](https://stackharbor.com/en/knowledge-base/immutable-infrastructure/)
9. [REL08-BP04 Deploy using immutable infrastructure, AWS Well-Architected Reliability Pillar](https://docs.aws.amazon.com/wellarchitected/latest/reliability-pillar/rel_tracking_change_management_immutable_infrastructure.html)
10. [Create immutable containers, HashiCorp Well-Architected Framework](https://docs.hashicorp.com/well-architected-framework/define-and-automate-processes/define/immutable-infrastructure/containers)
11. [Reliable Infrastructure as Code for Decentralized Organizations (2024)](https://mjuz.rocks/assets/pdf/papers/2024_Reliable-Infrastructure-as-Code-for-Decentralized-Organizations.pdf)
12. [Cloud Native Immutable Infrastructure Principles, cloud-native-principles (GitHub)](https://github.com/cloud-native-principles/cloud-native-principles/blob/master/cloud-native-immutable-infrastructure-principles.md)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Build, deployment, and development tooling › Deployment and continuous integration

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/immutable-infrastructure

English original: https://www.edgepedia.org/immutable-infrastructure
