跳转到内容
AggregaAggregaAggregaAggrega
选择语言: 简体中文
本页面由机器翻译。阅读英文原文。

Azure Resource Manager 上的 PostgreSQL 与 Container Apps 服务器

一个托管的 PostgreSQL 数据库和读取它的服务器,只需作为开发者意图编写一次,就会被组装成一个 Azure Resource Manager 模板。数据库是 Azure Database for PostgreSQL 灵活服务器,服务器是 Azure Container Apps 应用,数据库的管理员密码是一个已存在的 Key Vault 机密,因此模板中任何地方都不会写入密码。

同一份开发者意图会被构建为一个模板,构建为两个依次部署、并通过服务器读取数据库主机而关联的模板,并且 — 有意为之 — 构建为一个会中止的构建,因为两个 component 会在模板内争用同一个名称。

本页的输出块复制自一次真实运行,参见 阅读这些页面上的输出。

文件

  • aggregate.yaml — 开发者意图:一个类型为 core/datastore/postgres 的 payments-db 数据库,以及一个类型为 core/compute/server、声明使用该数据库的 orders-api 服务器。这里不涉及任何 Azure 内容。
  • release-one-deployment.yaml — 两个 component 位于同一个边界内,因此组装成一个模板。它指明保存密码的 Key Vault 机密、放置数据库的网络、服务器运行所在的 Container Apps 环境以及服务器的镜像。
  • release-two-deployments.yaml — 同样的两个 component 位于两个边界内,因此组装成两个模板,每个输出目录一个。服务器的模板从数据库的部署中读取数据库的主机。
  • release-negative-name-collision.yaml 与 aggregate-name-collision.yaml — 预期会失败。 两个 component 的实例 ID 会变成同一个符号名称。
  • manifest.yaml — 将每种 component 类型和凭据 Feature 绑定到在此目标上实现它的 Recipe。
  • .aggrega-config.yaml — 指定目录实体从中解析的 hub 注册表。

Release 中每个与订阅相关的值都是占位符:订阅 ID 是全零 GUID,资源组、虚拟网络、DNS 区域、保管库、环境和镜像的名称代表运维人员已在运行的资源。部署前请将它们替换为你自己的。

起草示例

你可以用下面的命令构建这个示例,也可以让你的 AI 助手替你构建。

将示例写入一个以其命名的新文件夹:

Terminal window
aggrega draft --example arm-postgres-container-apps

然后它会打印接下来要运行的命令。第一条命令进入新文件夹:

Terminal window
cd arm-postgres-container-apps

构建

本示例为每次构建提供了一个 release,因此需要指定你要构建的那一个。每次运行只构建一个 release。每个 release 的首次构建都需要它的锁文件,由 pull 写入:

Terminal window
aggrega pull --release release-one-deployment.yaml
aggrega build --release release-one-deployment.yaml

构建会输出如下内容,其中 <workspace> 代表本示例的文件夹:

[… omitted: the pull's line, which reports the lockfile it wrote, and a lint notice about the credential Feature's origin]
Total build time: 4.9s
Build completed: 1 file written to <workspace>/dist

组装好的模板位于 dist/one-deployment/arm/arm-template.json。渲染到该目录的每个 component 都会为这一个文档做出贡献;provider 写出单个模板,而不是每个 component 一个文件。

借助 AI 助手

已配置 Aggrega 工具的 AI 助手也能完成同样的操作:把下面这条提示粘贴给它。

只使用 Aggrega 的工具,不要自己创建任何文件夹。如果工作区文件夹中还没有 `arm-postgres-container-apps` 文件夹,就调用一次 `draft` 工具,将 `example` 设为 `arm-postgres-container-apps`,且不提供 `dir`:它会把示例写入一个新的 `arm-postgres-container-apps` 文件夹。然后调用 `build` 工具,将 `workspace_root` 设为 `arm-postgres-container-apps`,`release` 设为 `release-one-deployment.yaml`,`auto_pull` 设为 true,`write` 设为 true。告诉我构建写入了哪些文件;它们位于 `arm-postgres-container-apps/dist/` 下。

一个模板,两个嵌套部署

两个 component 会组装成一个模板,每个都成为一个嵌套部署:类型为 Microsoft.Resources/deployments 的 Azure Resource Manager 资源,它携带一个 vendor 进来的模块自身的模板,以及 Release 的设置所映射到的参数。模板的 resources 部分包含以下条目:

符号名称贡献者
PaymentsDb数据库 component 的 Recipe,arm/postgres-flexible-server
OrdersApi服务器 component 的 Recipe,arm/server-container-apps

每个 Recipe 都封装了 Microsoft Azure Verified Modules 计划中的一个模块,并固定到某个 git 标签,该标签的提交和归档摘要由 Recipe 的 entity.lock 记录,因此每次构建 vendor 的字节都完全相同。服务器条目的开头部分,省略了模块的模板:

"OrdersApi": {
"apiVersion": "2025-04-01",
"metadata": {
"aggrega.ai/provenance": {
"aggrega.ai/aggregate-name": "aggregate",
"aggrega.ai/boundary-path": "azure",
[… omitted: a compiler-version entry, which moves with the build]
"aggrega.ai/component-id": "orders-api",
"aggrega.ai/environment": "production",
"aggrega.ai/provider": "arm",
[… omitted: a provider-version entry, which moves with the build]
"aggrega.ai/recipe-uri": "server-container-apps:1.0.0"
}
},
"name": "[format('OrdersApi-{0}', uniqueString(deployment().name))]",
[… omitted: the nested deployment's mode and parameters, and the module's template]
},

符号名称是 component 的实例 ID 转换成的单词:orders-api 变成 OrdersApi,payments-db 变成 PaymentsDb。嵌套部署自身的名称会加上 uniqueString(deployment().name),因此用两个不同的部署名称将模板部署两次,会创建两组各自独立的嵌套部署,而不是一组覆盖另一组。

每个 Recipe 都会关闭遥测。该模块默认会部署一个向 Microsoft 报告的遥测资源,而 Recipe 在每个嵌套部署上将 enableTelemetry 绑定为 false,因此模板中不含任何遥测资源:

"enableTelemetry": {
"value": false
},

服务器通过表达式读取数据库

Aggregate 声明服务器需要数据库的 host,并命名为 DB_HOST。灵活服务器的地址在 Azure 创建它之前并不存在,因此构建会传递一个表达式,由 Azure Resource Manager 在模板部署时对其求值:

"env": [
{
"name": "DB_HOST",
"value": "[reference('PaymentsDb').outputs.fqdn.value]"
},
{
"name": "DB_NAME",
"value": "orders"
},
{
"name": "DB_PORT",
"value": "5432"
},
{
"name": "DB_USER",
"value": "orders_admin"
}
],

host 是对 PaymentsDb 嵌套部署的 fqdn 输出所作的表达式;database、port 和 user 是普通字符串。编译器从不查找地址,因此 aggregate.yaml 不含任何 Azure 特有的内容,而对 PaymentsDb 的引用同时也使 Azure Resource Manager 先于服务器部署数据库。

密码是一个 Key Vault 引用

灵活服务器需要管理员密码,而服务及其模块都不会生成密码。Release 将一个 core/referenced-secret Feature 附加到数据库的 password 插槽,并为其提供该机密的资源 ID:

features:
password:
type: core/referenced-secret
infra:
origin: /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/orders-security/providers/Microsoft.KeyVault/vaults/orders-vault/secrets/orders-db-password

Manifest 将该 Feature 绑定到 arm/key-vault-secret,它不渲染任何资源:它原样发布该 ID,由数据库的 Recipe 将其拆分为保管库的 ID 和机密的名称。嵌套部署收到的是 Key Vault 引用,而不是密码:

"administratorLoginPassword": {
"reference": {
"keyVault": {
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/orders-security/providers/Microsoft.KeyVault/vaults/orders-vault"
},
"secretName": "orders-db-password"
}
},

模板中永远不会出现机密文本。 该引用由 Azure Resource Manager 在模板部署时,以部署该模板的身份来解析,因此该身份需要有读取机密的权限,且保管库必须允许模板部署从中读取。机密本身在此 Release 之外创建;Release 只指明它的名称,从不携带它的值。

附加操作放在 Release 中,是因为密码存放在哪里由运维人员决定。Aggregate 对此只字不提,而 Release 从不写入 values:。

部署模板

该模板是资源组级别的模板。provider 不写出参数文件,也不针对 Azure 运行任何内容:命令由运维人员执行。先预览模板将会更改什么,然后部署它:

Terminal window
az deployment group what-if --resource-group <group> --template-file dist/one-deployment/arm/arm-template.json
az deployment group create --resource-group <group> --name <name> --template-file dist/one-deployment/arm/arm-template.json

或者将它部署为一个部署堆栈,它从不删除自己不再管理的内容:

Terminal window
az stack group create --name <stack> --resource-group <group> \
--template-file dist/one-deployment/arm/arm-template.json \
--action-on-unmanage detachAll --deny-settings-mode none

--action-on-unmanage detachAll 会在后续模板不再声明某个资源时保持该资源不动,而 --deny-settings-mode none 不会对堆栈所管理的内容设置拒绝分配。ARM provider 页面说明了部署命令以及每个标志的作用。

两个模板,依次部署

release-two-deployments.yaml 将数据库和服务器放在两个边界中,构建同一个 Aggregate。每个边界写出自己的模板,服务器的模板从数据库的部署中读取数据库的主机:

Terminal window
aggrega pull --release release-two-deployments.yaml
aggrega build --release release-two-deployments.yaml

构建写出两个文件,每个输出目录一个:

[… omitted: the pull's line, which reports the lockfile it wrote, and a lint notice about the credential Feature's origin]
Total build time: 3.0s
Build completed: 2 files written to <workspace>/dist

dist/two-deployments-database/arm/arm-template.json 是数据库的模板,dist/two-deployments-server/arm/arm-template.json 是服务器的模板。Aggregate 与构建单个模板时所用的相同,只有 Release 不同。

数据库的模板发布服务器读取的内容

服务器会读取数据库的部分条目,因此数据库的模板会为每个条目增加一个字符串输出,按 component 和条目命名。主机是单个模板的表达式,经由 string() 传递,使每个值都以文本形式传递:

"payments_zhdb__host": {
"type": "string",
"value": "[string(reference('PaymentsDb').outputs.fqdn.value)]"
},

模板还会在其 metadata 中记录自己所部署的名称:

"metadata": {
"aggrega.ai/deployment-name": "aggrega--orders--two-deployments-database-zsarm"
}

服务器的模板读取该部署

服务器无法引用另一个模板声明的资源,因此它通过资源 ID 读取数据库的部署,并从中取得输出:

"value": "[reference(format('/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Resources/deployments/{2}', subscription().subscriptionId, resourceGroup().name, 'aggrega--orders--two-deployments-database-zsarm'), '2025-04-01').outputs.payments_zhdb__host.value]"

该 ID 由服务器自身的订阅和资源组构建,这假定数据库部署到同一个资源组。当数据库的边界在 annotations 中声明 aggrega.ai/azure-subscription 或 aggrega.ai/azure-resource-group 时,它会指明其模板部署到何处,此时读取会以字符串形式写出这些值。

先按记录的名称部署数据库

部署的名称是服务器所读内容的一部分,因此数据库的模板要先于服务器的模板部署,并且使用它所记录的确切名称。没有任何东西读取服务器的部署,所以它的名称由你决定:

Terminal window
az deployment group create --resource-group <group> -n aggrega--orders--two-deployments-database-zsarm --template-file dist/two-deployments-database/arm/arm-template.json
az deployment group create --resource-group <group> -n <name> --template-file dist/two-deployments-server/arm/arm-template.json

数据库的名称就是其模板的 metadata["aggrega.ai/deployment-name"] 中的名称。它由 Release 的名称和边界的输出目录推导而来,因此在 Release 中更改数据库边界的 directory: 会改变其部署的名称,重新构建的服务器模板则会读取新名称。请用同一个 Release 构建两个模板,并按记录的名称部署数据库。

会中止的构建

Terminal window
aggrega pull --release release-negative-name-collision.yaml
aggrega build --release release-negative-name-collision.yaml

aggregate-name-collision.yaml 声明了两个数据库,payments-db 和 payments--db。它们是两个不同的实例 id,且都是有效的服务器名称,但 provider 会把实例 id 折叠成单词来命名其嵌套部署,两者都折叠为 PaymentsDb。符号名称在模板中只指向一个资源,因此构建会停止:

[… omitted: the pull's lines, which report the same collision while writing the lockfile]
[TRANSLATION_ERROR E0038]
dispatch groups whose contributors derive one name more than once in one key space
caused by (2):
[TRANSLATION_ERROR] release-negative-name-collision.yaml
derived name collision: one name is derived more than once in one key space ("PaymentsDb", key space "symbolic-names", derived by component "payments--db", provider "arm", dispatch path "name-collision/arm")
---
[TRANSLATION_ERROR] release-negative-name-collision.yaml
derived name collision: one name is derived more than once in one key space ("PaymentsDb", key space "symbolic-names", derived by component "payments-db", provider "arm", dispatch path "name-collision/arm")

从中可以读出的内容:

  • 两端都有名称。 每个条目都给出 component,因此两者都可以在你写的文件中搜索到。两者都会被报告,因为两者推导出了同一个符号。
  • 共享的符号及其键空间有名称。 PaymentsDb 是两者都到达的符号,symbolic-names 是模板资源的命名空间。
  • 不会输出任何内容。 构建会停止,而不是写出一个其中一个嵌套部署悄悄替换另一个的模板。在 Aggregate 中重命名任一 component 即可解决。

相同的输入,相同的输出

用未改变的输入构建两次,会生成逐字节相同的模板。编译器不解析实时数据:它从不调用 Azure,也从不运行 Azure Resource Manager,因此重新构建没有任何可发现、可能改变字节的东西。基于 diff 的评审依赖的正是这一性质。

dist/ 与构建它的 Releases 一起提交——dist/one-deployment/、dist/two-deployments-database/ 和 dist/two-deployments-server/——因此对 Recipes 的更改会表现为模板的 diff。如上面的摘录所述,每个来源块中的两个版本条目会随构建的编译器而变化。*.lock 文件是生成的,不会提交;它们由已提交的文件重新生成。