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

MySQL — 一個 Aggregate,兩個目標

這個精簡範例從開發者撰寫的多個 Aggregate,把單一 MySQL 資料庫編譯到兩個目標:

  • Kubernetes,透過 helm 提供者的 Workload 合成(StatefulSet 直接渲染在官方 mysql 映像檔上,沒有外部 chart),以及
  • Docker Compose,用於本機開發。

同一個開發者意圖驅動每個目標。每個目標只有 Manifest 所選的 Recipe 層會改變。

檔案

檔案角色
aggregate-helm-synth.yamlKubernetes 基礎變體的開發者意圖:一個 db 元件,型別為 core/datastore/mysql,其值,以及僅必要的 root_password 認證。
aggregate-helm-synth-auth.yamlKubernetes 完整變體的開發者意圖:root_password 與 user_password 兩個認證槽位。
manifest-helm-synth.yaml把 MySQL 型別對應到合成式 helm Recipe(helm/mysql),並把每個認證對應到其 FeatureRecipe(env-secret、k8s-secret-from-parameter)。
release-helm-synth.yamlKubernetes 基礎變體的操作員記錄,帶有運作大小設定(engine_version、storage)。
release-helm-synth-auth.yamlKubernetes 完整變體的操作員記錄。
aggregate.yaml本機 Compose 目標的開發者意圖:同一個 db 元件,兩個認證槽位都以注入值認證傳遞。
manifest-compose.yaml把 MySQL 型別對應到 Compose Recipe(mysql),並把認證對應到 dotenv-secret。
release-compose.yaml本機 Compose 目標的操作員記錄,包含一個提供者專屬的 infra.overrides 項目,用來固定確切的容器映像檔。

它示範的內容

  • 一個撰寫介面,多個目標。 Aggregate 是堆疊是什麼的單一事實來源。每個 Manifest 說明如何實現它。
  • Kubernetes 上的 Workload 合成(建議)。 helm/mysql Recipe 宣告一個與能力無關的 Workload,帶有 form: stateful;helm 提供者會合成一個 StatefulSet、一個 ClusterIP Service、一個 Headless Service(StatefulSet 穩定的網路身分),以及資料目錄的 volumeClaimTemplate。容器的環境帶有 MYSQL_DATABASE、MYSQL_USER、MYSQL_ROOT_PASSWORD 與 MYSQL_PASSWORD。字元集與定序以 mysqld 伺服器命令列引數(--character-set-server、--collation-server)的方式生效,因為官方 mysql 映像檔是把它們當作伺服器旗標,而不是環境變數。
  • Compose 上的 Workload 合成。 Compose Recipe 宣告一個與能力無關的 Workload;compose 提供者會合成一個 docker-compose.yaml 服務,帶有 MySQL 映像檔、其環境、一個具名資料磁碟區,以及一個健康檢查。
  • 操作員逃生艙口。 Compose Release 設定 infra.overrides.image,這是供 Component 自己的結構描述未指名的提供者專屬事實使用的開放支柱,而渲染出的服務會執行那個確切的映像檔參照。請見 Provider 專屬的覆寫。
  • 雙槽位認證模型。 密碼是元件 features: 支柱上的附加項目,每個都是標記為 feature.aggrega.ai/kind: secret 的 Feature,與應用程式的 values 分開。Component 的 featuresSchema 宣告一個黑箱 root_password 槽位與一個透明的 user_password 槽位。

Kubernetes 認證傳遞

root_password 是黑箱槽位(inject: []):由 Recipe 控制傳遞。附加的注入值認證把值放到元件的注入管道上,而 Recipe 將它讀作 .context.inject.env.MYSQL_ROOT_PASSWORD,並發出一般的容器環境變數。不會為它產生 Kubernetes Secret。

user_password 是透明槽位(inject: [env, file])。完整變體附加一個可參照認證,它會具體化一個 Kubernetes Secret 並發布其身分;Recipe 透過 containers.attachments 參照該槽位,合成器會把它渲染成環境 secretKeyRef。產生出的 Secret 與容器的參照指名同一個資源,因為兩者都讀取 .features.user_password.provides.name。不附加 user_password(基礎變體)也同樣有效:Recipe 會退回內嵌的開發預設值(MYSQL_PASSWORD: apppass),並且不產生 Secret,因此最小的部署仍能渲染並執行。

草擬範例

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

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

Terminal window
aggrega draft --example mysql-standalone

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

Terminal window
cd mysql-standalone

建置

此工作區沒有 .aggrega-config.yaml,因此標準程式庫實體從 hub 登錄庫 hub.aggrega.ai 解析,不需要目錄旗標;--auto-pull 會在建置期間寫入全新複製的副本所缺少的鎖定檔:

Terminal window
# Kubernetes — synthesized helm StatefulSet (base: root credential only)
aggrega build --release release-helm-synth.yaml --auto-pull
# Kubernetes — synthesized helm StatefulSet (full: both credential slots)
aggrega build --release release-helm-synth-auth.yaml --auto-pull
# Local development (Docker Compose)
aggrega build --release release-compose.yaml --auto-pull

渲染出的成品位於 dist/ 之下。

搭配 AI 助理

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

只使用 Aggrega 工具,並且不要自己建立任何資料夾。若工作區資料夾中尚未有 `mysql-standalone` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `mysql-standalone`,且不指定 `dir`:它會把範例寫入新的 `mysql-standalone` 資料夾。接著依此順序為每個 Release 各呼叫一次 `build` 工具 — `release-helm-synth.yaml`、`release-helm-synth-auth.yaml` 與 `release-compose.yaml` — 每次都將 `workspace_root` 設為 `mysql-standalone`、`release` 設為該檔案、`auto_pull` 設為 true、`write` 設為 true。告訴我每次建置寫入了哪些檔案;它們位於 `mysql-standalone/dist/` 之下。

操作員的大小設定

Kubernetes 的 Releases 依環境滿足操作員調整大小的支柱:容器映像檔標籤來自 infra.engine_version(mysql:8.4),而 volumeClaimTemplate 的請求與儲存類別來自 infra.storage。開發者的 Aggregate 從不帶有這些欄位。

Provider 專屬的覆寫

大小設定支柱是具名欄位:Component 宣告 engine_version 與 storage,所以操作員設定其中任一個,是在填入 Component 所預期的欄位。infra.overrides 是用於 Component 無法預期之內容的支柱 — 它被宣告為接受任何巢狀結構的物件,而每個 Recipe 會記載它讀取哪些鍵。

release-compose.yaml 使用它。Compose Recipe 讀取一個鍵 image,設定之後,容器會執行那個確切的參照,而不是 Recipe 根據 engine_version 組成的參照:

infra:
engine_version: "8.4"
overrides:
image: "registry.internal.example/library/mysql:8.4"

渲染出的服務原樣帶有該覆寫:

services:
db:
image: registry.internal.example/library/mysql:8.4
[… omitted: the service's environment, labels, volume, network and health check]

這是通往同一個伺服器版本、經登錄庫鏡像或以摘要固定的建置的路徑 — 這是屬於目標環境的部署事實,而不屬於 Component 的契約。由於鍵集合是依 Recipe 而非依 Component 而定,Recipe 就是查看哪些鍵適用的地方:這裡的 Compose Recipe 讀取 image,而不讀取任何鍵的 Recipe 會這樣說明。