跳到內容
AggregaAggregaAggregaAggrega
選擇語言: 繁體中文
本頁為機器翻譯。閱讀英文原文。

在每個目標上備份的資料庫:一個 core/backup 附加項目

一個 PostgreSQL 資料庫,在 Kubernetes 上透過 Velero 與 CloudNativePG 備份,在 AWS 上透過 Amazon RDS 自己的自動備份(於 Terraform 與 CloudFormation 上)以及透過 AWS Backup 備份,全都來自操作員在每個 Release 中撰寫的一個 core/backup 附加項目,而在沒有備份產品的目標上則是拒絕。

core/backup 說明資料庫依排程備份,且每份備份保留若干天。它對如何做隻字未提:每個目標的 Manifest 繫結在該處實現它的 FeatureRecipe,而每個都以自己產品的詞彙寫出備份——Velero 排程、CloudNativePG 排程備份、RDS 執行個體的設定、AWS Backup 計畫。沒有產品可實現它的目標,會依名稱拒絕該附加項目,所以建置會中止,而不是在沒有為它寫出的備份下部署資料庫。

此工作區透過六個 Releases 建置一個 Aggregate,每個都附加同一個備份:每天 02:30,每份備份保留 14 天。頁面展示在完成的五個中每個目標為它渲染什麼,並引用預期會中止的第六個所印出的內容。

它建置什麼

實例目錄 Component它在哪裡執行
dbcore/datastore/postgres每個 Release
backupscore/storage/s3-object-store兩個 Kubernetes Releases:備份所去的儲存貯體
velerocore/storage/veleroVelero Release
barman-cloudcore/storage/barman-cloud-pluginCloudNativePG Release

Aggregate 宣告資料庫,以及在 Kubernetes 上備份它所需要的東西:一個儲存貯體,以及兩個備份控制器。備份控制器是每個叢集安裝一次,而不是隨資料庫安裝,所以它是自己的元件;在 AWS 上,備份留在受管理服務自己的儲存中,三者都不需要。一個參數 backup_controller,透過每個元件的 active: 開關切換它們:

aggregate.yaml
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 連線,指名備份去向:

release-helm-velero.yaml
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: backups

AWS 的 Releases 以沒有 store 連線的方式寫出同一個附加項目,因為 RDS 與 AWS Backup 把備份保存在自己的儲存中,而那裡的實現會拒絕連到沒有東西會寫入的儲存貯體的連線。AWS Backup Release 加上 infra.role_arn,即 AWS Backup 執行每份備份所用的 IAM 角色。

六個 Releases

ReleaseManifest實現 core/backup 的是建置
release-helm-velero.yamlmanifest-helm-velero.yamlhelm/backup-velero完成
release-helm-cloudnative-pg.yamlmanifest-helm-cloudnative-pg.yamlhelm/backup-cloudnative-pg完成
release-terraform.yamlmanifest-terraform.yamlterraform/backup-native完成
release-cloudformation.yamlmanifest-cloudformation.yamlcloudformation/backup-native完成
release-terraform-aws-backup.yamlmanifest-terraform-aws-backup.yamlterraform/backup-aws-backup完成
release-compose.yamlmanifest-compose.yaml無中止

Manifest 決定由哪個產品備份資料庫。兩個 Kubernetes Manifests 以兩種方式執行資料庫——供 Velero 使用的 StatefulSet helm/postgres,以及供外掛程式使用的 CloudNativePG Cluster helm/postgres-cloudnative-pg——並為每一種繫結備份的實現。manifest-terraform.yaml 與 manifest-terraform-aws-backup.yaml 把資料庫對應到同一個 Recipe,差別在它們繫結到 core/backup 的 FeatureRecipe,而由它們建置的 Releases,除了各自指名的 Manifest 之外,只在角色上不同。

Kubernetes Releases 也把儲存貯體自己的金鑰對,以及 Velero 用來簽署請求的認證檔,作為 core/referenced-secret 附加項目附加,指名叢集已持有的 Secret,所以沒有任何金鑰寫入任何渲染出的檔案。AWS Releases 中每個特定於帳戶的值都是預留位置。

草擬範例

你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。

把範例寫入一個以它命名的新資料夾:

Terminal window
aggrega draft --example datastore-backup

它接著會印出要接著執行的命令。第一個會切換到新資料夾:

Terminal window
cd datastore-backup

建置

此工作區從 .aggrega-config.yaml 所指名的 hub 登錄庫讀取標準程式庫目錄。Velero 與 Barman Cloud 外掛程式是透過各自專案自己的 chart 安裝的,所以兩個 Kubernetes Releases 需要那些專案的 chart 存放庫可從建置處連到;其他每個 Release 只從目錄渲染。

Terminal window
aggrega build --release release-helm-velero.yaml --auto-pull # one Release

在文件存放庫的檢出內容中,它持有由 aggrega draft --example 寫出的副本所不帶的共用建置輔助程式,./check.sh 會建置全部六個 Releases 並檢查每個結果。它每個 Release 印出一行,且只有在每個都如此頁所述地結束時,才以 0 結束:

RELEASE EXIT EXPECTED VERDICT
----------------------------------------------------------------------------------------------------
release-helm-velero.yaml 0 complete OK: completed, the backup the README shows
release-helm-cloudnative-pg.yaml 0 complete OK: completed, the backup the README shows
release-terraform.yaml 0 complete OK: completed, the backup the README shows
release-cloudformation.yaml 0 complete OK: completed, the backup the README shows
release-terraform-aws-backup.yaml 0 complete OK: completed, the backup the README shows
release-compose.yaml 1 halt OK: halted with the expected refusal
check: every build ended as expected.

搭配 AI 助理

以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。

只使用 Aggrega 工具,且不要自己建立任何資料夾。若 Aggrega 工作區資料夾中還沒有 `datastore-backup` 資料夾,請呼叫 `draft` 工具一次,將 `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 儲存位置與一個排程:

dist/helm/db--backup--backup.yaml
apiVersion: velero.io/v1
kind: BackupStorageLocation
metadata:
[… omitted: the labels and annotations the build writes on every resource]
name: "orders-db-backup"
namespace: velero
spec:
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/v1
kind: Schedule
metadata:
[… omitted: the labels and annotations, as on the storage location]
name: "orders-db-backup"
namespace: velero
spec:
schedule: "30 2 * * *"
template:
includedNamespaces:
- "orders"
orLabelSelectors:
- matchLabels:
"app": "db"
storageLocation: "orders-db-backup"
ttl: "336h0m0s"
snapshotVolumes: false
defaultVolumesToFsBackup: true

兩者都位於命名空間 velero,這是 Velero 唯一會讀取排程的命名空間,並以資料庫的命名空間、資料庫的名稱與附加項目的名稱來命名,因為叢集上的每個 Release 都寫入同一個命名空間。排程會依資料庫所渲染的 StatefulSet 的標籤,在它所放置的命名空間中選取資料庫的 pod,而 Velero 的節點代理程式會把它們磁碟區中的檔案複製到儲存貯體。儲存貯體的位址是 backups 元件所發布的位址,其 Service 名稱以它執行所在的命名空間限定,因為 Velero 在另一個命名空間中執行;區域也是儲存貯體的:在此執行它的 SeaweedFS 不讀取區域,並發布 us-east-1。14 天是 Velero 的存活時間,以小時計。

Kubernetes,搭配 CloudNativePG

release-helm-cloudnative-pg.yaml 會將資料庫寫成 CloudNativePG 的 Cluster,並寫出儲存貯體、外掛程式的 chart 所渲染的物件,以及附加項目的檔案(其中包含外掛程式的物件儲存區與一個排程備份),與 Cluster 一同放在它自己的命名空間中:

dist/helm/db--backup--backup.yaml
apiVersion: barmancloud.cnpg.io/v1
kind: ObjectStore
metadata:
[… 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/v1
kind: ScheduledBackup
metadata:
[… 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 的排程有六個欄位,第一個是秒,所以附加項目的 30 2 * * * 前面會加上 0 來寫。外掛程式使用物件儲存區所指名的 Secret 中的金鑰組為其請求簽章,這裡是 db-backup,以物件儲存區本身的名稱為名,因為附加項目沒有設定 infra.credentials_secret。

資料庫自己的 Recipe 也會讀取附加項目,並將外掛程式指定為 Cluster 的預寫式記錄封存器:

dist/helm/db/cluster.yaml
[… 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 會讀取附加項目,並將它寫在執行個體被寫出的地方:

dist/terraform/modules/db/main.tf
[… 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 的屬性名稱帶有相同的兩項設定:

dist/cloudformation/cloudformation-stack.json
[… 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 所建置。這個實現會在資料庫的模組旁,寫出附加項目自己的模組,其中包含一個備份保存庫、一個計畫,以及一個指名該執行個體的選取項目:

dist/terraform/modules/db--backup/backup.tf
[… 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",
]
}

排程以 AWS 自己的 cron 形式撰寫,並以 UTC 解讀;若備份在其時間後一小時內尚未開始,就不會啟動。該執行個體的 ARN 是一個 Terraform 運算式,由 AWS 提供者在規劃時所設定的內容與執行個體的名稱組成;角色是 Release 所指名的那一個,並依原樣傳遞。

執行個體在 AWS Backup 之外也保留自己的自動備份,就像沒有附加項目時一樣:保留 7 天,時段由 RDS 選擇,因為附加項目的排程是 AWS Backup 的,而不是執行個體的:

dist/terraform/modules/db/main.tf
[… 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、帳戶或 Region:在 AWS 上,這些各是一個運算式,或是 Release 寫入的一個值,由 Terraform、CloudFormation 或 AWS 在套用檔案時讀取。它也不檢查叢集中的任何內容:

  • Velero 的排程只會在部署了 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 中的 velero-cloud-credentials(內含一份 AWS 共用認證檔),以及資料庫旁的 db-backup(以 ACCESS_KEY_ID 與 ACCESS_SECRET_KEY 存放金鑰組)。建置不會渲染其中任何一個。

會中止的 Release

release-compose.yaml 將相同的備份附加到 Docker Compose 上的資料庫。那裡沒有任何 FeatureRecipe 實現 core/backup,所以 manifest-compose.yaml 沒有繫結任何一個,而 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

工作區沒有指定目錄資料夾,所以建置只有其鎖定檔所固定的 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。

標準程式庫目錄中沒有任何 FeatureRecipe 在這個目標上實現 core/backup。它們各自繫結到另一個目標,所以在這裡固定之後,會在 Compose 檔案旁渲染成自己的輸出,而那是這個 Release 不部署的目標。可行的做法是從 Release 中移除該附加項目,讓資料庫在那裡部署時沒有備份,或是透過有備份產品的目標所用的 Manifest 來部署,就像其他五個 Releases 所做的那樣。