Skip to Content
基础设施即代码,第2版
book

基础设施即代码,第2版

by Kief Morris
May 2025
Beginner to intermediate
430 pages
4h 37m
Chinese
O'Reilly Media, Inc.
Content preview from 基础设施即代码,第2版

第 5 章. 作为代码构建基础设施堆栈

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

本章结合第 3章和第 4章,介绍使用代码管理基础架构平台提供的基础架构资源的方法。

基础架构堆栈是我用来讨论如何做到这一点的概念。堆栈是一起定义和更改的基础设施资源的集合。堆栈中的资源通过堆栈管理工具一起配置,以创建堆栈实例。对堆栈代码进行更改后,使用相同的工具应用到实例中。

本章介绍在堆栈中分组基础设施资源的模式。

什么是基础设施堆栈?

基础架构堆栈是基础架构资源的集合,您可以将其作为一个单元进行定义、配置和更新(图 5-1)。

您可以编写源代码来定义堆栈的元素,即基础设施平台提供的资源和服务。例如,你的堆栈可能包括虚拟机("计算资源")、磁盘卷("存储资源")和子网("网络资源")

您运行堆栈管理工具,该工具会读取您的堆栈源代码,并使用云平台的应用程序接口来组装代码中定义的元素,为您的堆栈提供一个实例。

iac2 0501
图 5-1 基础设施堆栈 基础设施堆栈是作为一个组进行管理的基础设施元素的集合

堆栈管理工具的例子包括

一些服务器配置工具(我将在第 11 章中详细介绍)具有与基础架构堆栈协同工作的扩展功能。例如Ansible Cloud ModulesChef Provisioning(现已报废)、Puppet Cloud ManagementSalt Cloud

"作为术语的 "堆栈

大多数堆栈管理工具并不自称堆栈管理工具。每个工具都有自己的术语来描述其管理的基础架构单元。在本书中,我将介绍适用于这些工具的模式和实践。

我选择用堆栈这个词。

很多人都告诉我,这个概念有一个更好的说法。每个人心中都有一个完全不同的词。到目前为止,业界还没有就如何称呼这一概念达成一致。因此,在达成一致之前,我将继续使用堆栈这个词。

堆栈代码

每个堆栈都由源代码定义,这些源代码声明了堆栈应包含哪些基础架构元素。Terraform 代码(.tf文件)和 CloudFormation 模板都是基础设施堆栈代码的示例。堆栈项目包含定义堆栈基础架构的源代码。

例 5-1显示了虚构Stackmaker工具的堆栈源代码项目的文件夹结构。

例 5-1. 使用虚构工具的堆栈项目文件夹结构
stack-project/
   ├── src/
   │   ├── dns.infra
   │   ├── load_balancers.infra
   │   ├── networking.infra
   │   └── webserver.infra
   └── test/

堆栈实例

您可以使用单个堆栈项目提供多个堆栈实例。为项目运行堆栈工具时,它会使用平台 API 确保堆栈实例存在,并使其与项目代码相匹配。如果堆栈实例不存在,工具会创建它。如果堆栈实例存在,但与代码不完全匹配,工具会修改实例使其匹配。

我经常把这个过程描述为将代码 "应用 "到实例中。

如果您修改了代码并重新运行该工具,它就会更改堆栈实例,使之与您的修改一致。如果您在不修改代码的情况下再次运行该工具,堆栈实例将保持不变。 ...

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

构建数据集成解决方案

构建数据集成解决方案

Jay Borthen
金融领域的机器学习与数据科学蓝图

金融领域的机器学习与数据科学蓝图

Hariom Tatsat, Sahil Puri, Brad Lookabaugh
可观察性工程

可观察性工程

Charity Majors, Liz Fong-Jones, George Miranda

Publisher Resources

ISBN: 9798341658493