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版

第 7 章 配置堆栈实例 配置堆栈实例

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

"模式 "所述,使用单个堆栈代码项目可以更轻松地维护多个一致的基础架构实例:可重用堆栈 "中所述不过,您经常需要定制堆栈的不同实例。例如,在开发环境中,您可能需要一个服务器数量少于生产环境的集群。

下面是一个堆栈代码示例,它定义了一个容器托管集群,可配置服务器的最小和最大数量:

container_cluster: web_cluster-${environment}
  min_size: ${cluster_minimum}
  max_size: ${cluster_maximum}

如图 7-1 所示,您可以为每个环境传递不同的参数值。

Terraform 和 CloudFormation 等堆栈工具支持多种设置配置参数值的方法。这些方式通常包括在命令行中传递值、从文件中读取值,以及让基础架构代码从键值存储中检索值。

管理基础架构的团队需要决定如何使用这些功能来管理并向堆栈工具传递配置值。必须确保这些值的定义和应用在每个环境中的一致性。

iac2 0701
图 7-1. 使用相同代码,但每个环境的参数值不同

使用堆栈参数创建唯一标识符

如果在同一项目中创建多个堆栈实例(根据"模式:可重用堆栈"中讨论的可重用堆栈模式,则可能会看到需要唯一标识符的基础架构资源出现故障。要了解我的意思,请看下面定义应用服务器的伪代码:

server:
  id: appserver
  subnet_id: appserver-subnet

虚构云平台要求id 值必须是唯一的,因此当我运行stack 命令创建第二个堆栈时,它失败了:

> stack up environment=test --source mystack/src
SUCCESS: stack 'test' created
> stack up environment=staging --source mystack/src
FAILURE: server 'appserver' already exists in another stack

我可以在堆栈代码中使用参数来避免这些冲突。我修改了代码,使用一个名为environment 的参数来分配一个唯一的服务器 ID。我还将服务器添加到每个环境的不同子网中: ...

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