在每个目标上都备份的数据存储:一个 core/backup 附加
一个 PostgreSQL 数据库:在 Kubernetes 上通过 Velero 和 CloudNativePG 备份,在 AWS 上通过 Amazon RDS 自身的自动备份(在 Terraform 和 CloudFormation 上)以及通过 AWS Backup 备份,全部来自运维人员在每个 Release 中编写的同一个 core/backup 附加;在没有备份产品的目标上则会被拒绝。
core/backup 表示一个数据存储按计划备份,且每个备份保留若干天。它不说明如何实现:每个目标的 Manifest 绑定在该目标上实现它的 FeatureRecipe,而每个 FeatureRecipe 都以其自身产品的术语写出备份——一个 Velero schedule、一个 CloudNativePG 计划备份、一个 RDS 实例的设置、一个 AWS Backup 计划。没有产品可实现它的目标会按名称拒绝该附加,因此构建会停止,而不是部署一个没有为其写出备份的数据存储。
此工作区通过六个 Releases 构建一个 Aggregate,每个都附加相同的备份:每天 02:30 运行,每个备份保留 14 天。本页展示在完成构建的五个中每个目标为其渲染的内容,并引用预期会停止的第六个所打印的内容。
它构建了什么
| 实例 | 目录中的 Component | 运行位置 |
|---|---|---|
db | core/datastore/postgres | 每个 Release |
backups | core/storage/s3-object-store | 两个 Kubernetes Releases:备份写入的存储桶 |
velero | core/storage/velero | Velero Release |
barman-cloud | core/storage/barman-cloud-plugin | CloudNativePG Release |
Aggregate 声明了数据库,以及在 Kubernetes 上备份它所需的内容:一个存储桶和两个备份控制器。备份控制器按集群安装一次,而不是随数据库安装,因此它是一个独立的组件;在 AWS 上,备份保留在托管服务自身的存储中,这三者都不需要。一个参数 backup_controller 通过每个组件的 active: 开关来切换它们:
spec: parameters: [… omitted: the comment above the parameter] backup_controller: type: string default: none [… omitted: the parameter's description] components: [… omitted: the comment above each component] db: type: core/datastore/postgres
backups: type: core/storage/s3-object-store active: '{{ ne .parameters.backup_controller "none" }}' values: bucket: db-backups
velero: type: core/storage/velero active: '{{ eq .parameters.backup_controller "velero" }}'
barman-cloud: type: core/storage/barman-cloud-plugin active: '{{ eq .parameters.backup_controller "cloudnative-pg" }}'开关为 false 的组件会在构建任何内容之前被移除,因此将该参数保留为 none 的 AWS 和 Docker Compose Releases 只构建数据库。
附加
开发者无需为备份编写任何内容:db 在 Aggregate 中不携带任何附加。运维人员在每个 Release 中,在该组件的 features 下将 core/backup 附加到它,并在那里说明备份何时运行以及每个备份保留多长时间。在 Kubernetes 上,该附加还通过其指向存储桶的 store 连接说明备份写入何处:
components: db: annotations: aggrega.ai/namespace: orders [… omitted: the comment above the attachment] features: backup: type: core/backup infra: schedule: "30 2 * * *" retention_days: 14 uses: store: bucket: target: backupsAWS Releases 编写相同的附加,但不带 store 连接,因为 RDS 和 AWS Backup 将备份保存在它们自己的存储中,而那里的实现会拒绝连接到一个不会被写入的存储桶。AWS Backup Release 添加了 infra.role_arn,即 AWS Backup 运行每次备份时所使用的 IAM 角色。
六个 Releases
| Release | Manifest | core/backup 的实现方 | 构建结果 |
|---|---|---|---|
release-helm-velero.yaml | manifest-helm-velero.yaml | helm/backup-velero | 完成 |
release-helm-cloudnative-pg.yaml | manifest-helm-cloudnative-pg.yaml | helm/backup-cloudnative-pg | 完成 |
release-terraform.yaml | manifest-terraform.yaml | terraform/backup-native | 完成 |
release-cloudformation.yaml | manifest-cloudformation.yaml | cloudformation/backup-native | 完成 |
release-terraform-aws-backup.yaml | manifest-terraform-aws-backup.yaml | terraform/backup-aws-backup | 完成 |
release-compose.yaml | manifest-compose.yaml | 不发送 | 中止 |
Manifest 决定由哪个产品备份数据库。两个 Kubernetes Manifests 以两种方式运行数据库——为 Velero 使用 helm/postgres(一个 StatefulSet),为插件使用 helm/postgres-cloudnative-pg(一个 CloudNativePG Cluster)——并分别绑定备份的实现。manifest-terraform.yaml 和 manifest-terraform-aws-backup.yaml 将数据库映射到同一个 Recipe,区别在于它们绑定到 core/backup 的 FeatureRecipe;由它们构建的 Releases 除了各自指定的 Manifest 之外,仅在角色上有所不同。
Kubernetes Releases 还以 core/referenced-secret 附加的形式附加存储桶自身的密钥对,以及 Velero 用于签名请求的凭据文件,这些附加指向集群中已有的 Secrets,因此不会有任何密钥写入任何渲染的文件。AWS Releases 中每个特定于账户的值都是占位符。
生成示例
你可以用下面的命令构建这个示例,也可以让你的 AI 助手替你构建。
将示例写入一个以其命名的新文件夹:
aggrega scaffold --example datastore-backup然后它会打印接下来要运行的命令。第一条命令进入新文件夹:
cd datastore-backup构建
工作区从 .aggrega-config.yaml 指定的 hub 注册表读取标准库目录。Velero 和 Barman Cloud 插件通过各自项目的 chart 安装,因此两个 Kubernetes Releases 需要构建能够访问这些项目的 chart 仓库;其他每个 Release 仅从目录渲染。
aggrega build --release release-helm-velero.yaml --auto-pull # one Release在文档仓库的检出中(它包含由 aggrega scaffold --example 写出的副本所不具备的共享构建辅助脚本),./check.sh 会构建全部六个 Releases 并检查每个结果。它为每个 Release 打印一行,并且仅当每个都按本页所述结束时才以 0 退出:
RELEASE EXIT EXPECTED VERDICT----------------------------------------------------------------------------------------------------release-helm-velero.yaml 0 complete OK: completed, the backup the README showsrelease-helm-cloudnative-pg.yaml 0 complete OK: completed, the backup the README showsrelease-terraform.yaml 0 complete OK: completed, the backup the README showsrelease-cloudformation.yaml 0 complete OK: completed, the backup the README showsrelease-terraform-aws-backup.yaml 0 complete OK: completed, the backup the README showsrelease-compose.yaml 1 halt OK: halted with the expected refusal
check: every build ended as expected.借助 AI 助手
已配置 Aggrega 工具的 AI 助手也能完成同样的操作:把下面这条提示粘贴给它。
只使用 Aggrega 工具,不要自行创建任何文件夹。如果工作区文件夹中还没有 `datastore-backup` 文件夹,调用一次 `scaffold` 工具,将 `example` 设为 `datastore-backup`,且不设置 `dir`:它会把示例写入新的 `datastore-backup` 文件夹。然后调用 `build` 工具,将 `workspace_root` 设为 `datastore-backup`,`release` 设为 `release-helm-velero.yaml`,`auto_pull` 设为 true,`write` 设为 true。告诉我这次构建写入了哪些文件;它们位于 `datastore-backup/dist/` 下。Kubernetes,使用 Velero
release-helm-velero.yaml 写出数据库的 StatefulSet 和 Services、存储桶的对应资源、Velero 自身 chart 渲染的对象,以及一个用于该附加的文件,其中包含一个 Velero 存储位置和一个 schedule:
apiVersion: velero.io/v1kind: BackupStorageLocationmetadata: [… omitted: the labels and annotations the build writes on every resource] name: "orders-db-backup" namespace: velerospec: provider: velero.io/aws objectStorage: bucket: "db-backups" prefix: "orders-db-backup" config: region: "us-east-1" s3Url: "http://backups-default.orders:8333" s3ForcePathStyle: "true"---apiVersion: velero.io/v1kind: Schedulemetadata: [… omitted: the labels and annotations, as on the storage location] name: "orders-db-backup" namespace: velerospec: schedule: "30 2 * * *" template: includedNamespaces: - "orders" orLabelSelectors: - matchLabels: "app": "db" storageLocation: "orders-db-backup" ttl: "336h0m0s" snapshotVolumes: false defaultVolumesToFsBackup: true两者都位于命名空间 velero 中,这是 Velero 读取 schedule 的唯一命名空间,并以数据库的命名空间、其名称以及附加的名称命名,因为集群上的每个 Release 都写入这同一个命名空间。schedule 按数据库渲染的 StatefulSet 的标签,在数据库所在的命名空间中选择数据库的 pod,Velero 的节点代理将其卷中的文件复制到存储桶中。存储桶的地址是 backups 组件发布的地址,其 Service 名称以其运行所在的命名空间限定,因为 Velero 运行在另一个命名空间中;区域也是存储桶的:在此运行存储桶的 SeaweedFS 不读取区域,并发布 us-east-1。14 天是 Velero 的存活时间,以小时计。
Kubernetes,使用 CloudNativePG
release-helm-cloudnative-pg.yaml 将数据库写为一个 CloudNativePG Cluster,并写出存储桶、插件 chart 渲染的对象,以及附加的文件,其中包含插件的对象存储和一个计划备份,与 Cluster 一起位于其自身的命名空间中:
apiVersion: barmancloud.cnpg.io/v1kind: ObjectStoremetadata: [… omitted: the labels and annotations the build writes on every resource] name: "db-backup" namespace: "orders"spec: configuration: destinationPath: "s3://db-backups/orders/db-backup" endpointURL: "http://backups-default.orders:8333" s3Credentials: accessKeyId: name: "db-backup" key: "ACCESS_KEY_ID" secretAccessKey: name: "db-backup" key: "ACCESS_SECRET_KEY" retentionPolicy: "14d" instanceSidecarConfiguration: env: - name: "AWS_DEFAULT_REGION" value: "us-east-1"---apiVersion: postgresql.cnpg.io/v1kind: ScheduledBackupmetadata: [… omitted: the labels and annotations, as on the object store] name: "db-backup" namespace: "orders"spec: schedule: "0 30 2 * * *" cluster: name: "db" method: "plugin" pluginConfiguration: name: "barman-cloud.cloudnative-pg.io"CloudNativePG 的 schedule 有六个字段,第一个表示秒,因此附加的 30 2 * * * 在前面加上 0 写出。插件使用对象存储所指定的 Secret 中的密钥对签名请求,这里为 db-backup,以对象存储本身命名,因为该附加没有设置 infra.credentials_secret。
数据库自身的 Recipe 也读取该附加,并将插件指定为 Cluster 的预写日志归档器:
[… omitted: apiVersion, kind and metadata]spec: [… omitted: the instance count, the image, the storage and the resources] plugins: - name: "barman-cloud.cloudnative-pg.io" isWALArchiver: true parameters: barmanObjectName: "db-backup" [… omitted: the bootstrap settings]因此,日志会在每日基础备份之间持续归档到存储桶中,恢复可以到达这 14 天内的任意时刻:这就是 CloudNativePG 的时间点恢复,此实现保持其开启。
Terraform
release-terraform.yaml 只写出数据库的模块。该附加的实现 terraform/backup-native 不渲染任何文件:备份是 RDS 实例的一项设置,因此数据库的 Recipe 读取该附加,并在写出实例的位置写出它:
[… omitted: the provenance comment, the provider requirement, and the instance's engine, compute, storage, database and HA arguments] ## ── Backup ─────────────────────────────────────────────── backup_retention_period = 14 backup_window = "02:30-03:00"[… omitted: the subnet group and security groups, and the address output]RDS 接受一个每日开始时间而不是 cron 表达式,因此 30 2 * * * 变为一个在 UTC 02:30 开启、持续 30 分钟的备份窗口,这是 RDS 接受的最短时长。
CloudFormation
release-cloudformation.yaml 写出一个包含该实例的堆栈模板,其中两项相同的设置使用其 CloudFormation 属性名:
[… omitted: the template's format version, the outputs carrying what the database publishes, and the opening of its resources] "Db": { [… omitted: the deletion policy and the provenance metadata] "Properties": { "AllocatedStorage": 10, "BackupRetentionPeriod": 14, "DBInstanceClass": "db.t3.small", [… omitted: the database name, the subnet group, the engine and the credentials settings] "MultiAZ": false, "Port": 5432, "PreferredBackupWindow": "02:30-03:00", [… omitted: the storage type and the security groups] }, "Type": "AWS::RDS::DBInstance" }[… omitted: the closing of the resources and of the template]AWS Backup
release-terraform-aws-backup.yaml 则由一个绑定 terraform/backup-aws-backup 的 Manifest 构建。此实现在数据库的模块旁写出附加自己的模块,其中包含一个备份保管库、一个计划和一个指定该实例的选择:
[… omitted: the provenance comment, the provider requirement, and the partition, Region and account lookups]resource "aws_backup_vault" "backup" { name = "db-backup"}
resource "aws_backup_plan" "backup" { name = "db-backup"
rule { rule_name = "db-backup" target_vault_name = aws_backup_vault.backup.name schedule = "cron(30 2 * * ? *)" start_window = 60
lifecycle { delete_after = 14 } }}
resource "aws_backup_selection" "backup" { name = "db-backup" plan_id = aws_backup_plan.backup.id iam_role_arn = "arn:aws:iam::123456789012:role/example-backup"
resources = [ "arn:${data.aws_partition.current.partition}:rds:${data.aws_region.current.region}:${data.aws_caller_identity.current.account_id}:db:db", ]}schedule 以 AWS 自己的 cron 形式写出,按 UTC 读取,在其时间后一小时内尚未开始的备份将不会启动。实例的 ARN 是一个 Terraform 表达式,由 AWS provider 在规划时所配置的内容和实例的名称组成;角色是 Release 指定的角色,按原样传递。
实例在 AWS Backup 的备份之外保留其自身的自动备份,与没有附加时一样:保留 7 天,在 RDS 选择的窗口中进行,因为附加的 schedule 属于 AWS Backup 而不是实例:
[… omitted: the provenance comment, the provider requirement, and the instance's engine, compute, storage, database and HA arguments] ## ── Backup ─────────────────────────────────────────────── backup_retention_period = 7[… omitted: the subnet group and security groups, and the address output]在附加上设置 infra.keep_automated_backups: false 会关闭这些备份,使 AWS Backup 的备份成为实例唯一的备份。
构建不做的事
构建写出文件后即停止。它不运行任何备份,也不访问任何集群、存储桶或 AWS API。它不解析任何 ARN、账户或区域:在 AWS 上,每一项都是一个表达式或 Release 写入的值,由 Terraform、CloudFormation 或 AWS 在应用文件时读取。它也不检查集群中的任何内容:
- Velero 的 schedule 只在部署了 Velero 的地方运行,插件的备份只在运行插件、CloudNativePG operator(1.26 或更高版本)和 cert-manager 的地方运行。此工作区部署 Velero 和插件;operator 和 cert-manager 被假定已经存在。
- Kubernetes Releases 指定的 Secrets 由集群持有,位于各自被读取的命名空间中:
backup-store-access-key-id和backup-store-secret-access-key位于存储桶旁,velero-cloud-credentials位于velero中,持有一个 AWS 共享凭据文件,db-backup位于数据库旁,在ACCESS_KEY_ID和ACCESS_SECRET_KEY下持有密钥对。构建不渲染其中任何一个。
会停止的 Release
release-compose.yaml 在 Docker Compose 上将相同的备份附加到数据库。那里没有 FeatureRecipe 实现 core/backup,因此 manifest-compose.yaml 不绑定任何 FeatureRecipe,aggrega build --release release-compose.yaml --auto-pull 会停止并指出该附加,而不是部署一个没有备份的数据库:
[… omitted: what resolving the lockfile prints, the same halt among it][OPS_ERROR E0099] recipe resolution caused by (1): no recipe resolved for component/feature instance: add an explicit Manifest mapping for its type: feature "backup" (type "core/backup"); no feature recipe the lockfile pins implements core/backup:1.0.0 for capability "compose", the capability its component's Recipe binds to; with no catalog directory, the feature recipes searched are only those the lockfile pins, not a registry's; pin one a registry publishes in the Manifest, where one for another capability renders as its own output beside the component's, or write one for "compose", then map the type to it该工作区未指定任何目录文件夹,因此构建只能在其 lockfile 固定的 FeatureRecipes 中搜索,而未为 core/backup 绑定任何条目的 Manifest 也不会固定任何条目;消息会如实说明这一点,而不是声称 hub 中没有任何条目。若将标准库目录的检出副本指定为目录文件夹,同一中止会说明目录中没有任何 FeatureRecipe 为 compose 实现 core/backup:1.0.0,并列出为其他目标实现它的十个:cloudformation/backup-aws-backup、cloudformation/backup-native、helm/backup-cloudnative-pg、helm/backup-k8up、helm/backup-kasten、helm/backup-kubestash、helm/backup-mariadb-operator、helm/backup-velero、terraform/backup-aws-backup 和 terraform/backup-native。
标准库目录中没有针对此目标的 core/backup FeatureRecipe。其中每一个都绑定到另一个目标,因此若在此处固定,它会作为单独的输出渲染在 Compose 文件旁边,而该输出面向的是此 Release 并不部署到的目标。可行的做法是从 Release 中移除该附加,在那里部署不带备份的数据库;或者像其他五个 Releases 那样,通过面向拥有备份产品的目标的 Manifest 部署它。