一個 Aggregate,兩種大小的 MySQL 叢集
一個 MySQL InnoDB Cluster 與協調它的控制平面,從同一個未更動的 Aggregate 建置兩次:一次要求單一伺服器實例,一次要求三個。
課題在於大小存在於何處。 資料庫背後有多少伺服器,不是開發者寫的,也不是 Recipe 決定的 — 它是 Release 中一行操作員調整大小的設定,而且正是兩棵渲染樹之間的全部差異。
編譯器寫出什麼,以及不寫什麼
這個實現會發出一個 InnoDBCluster 自訂資源。它不會發出伺服器、Service 或路由器:Kubernetes 的 MySQL Operator 會在自訂資源被套用後於目標叢集內建立它們,那是在編譯器完成很久之後才發生的工作。
因此以下所有內容描述的,都是渲染出的自訂資源所要求的內容。執行中的叢集最終包含什麼,是控制器對該請求的回應,發生在編譯器從未看到的時刻 — 而編譯器對此不做任何宣稱,因為它無從查看。
唯一會變的事
.-- release-single.yaml ----- spec.instances: 1 /one Aggregate -- one Manifest -+one Component \one Recipe '- release-replicated.yaml -- spec.instances: 3兩個 Releases 都選擇相同的 Component 與相同的 Recipe,所以只有一個 Manifest,而不是每個 Release 一個。當兩個 Releases 只在大小上不同時,維運對應對這個差異沒有任何要說的,而第二份副本只會多出一件需要保持一致的東西,卻毫無益處。
檔案
aggregate.yaml— 開發者意圖,而且是兩個 Releases 共用的同一份文件:一個負責控制平面的mysql-operator元件、一個型別為core/datastore/mysql-innodb-cluster的db元件,以及開發者希望伺服器使用的字元集。這裡沒有任何內容說明有多少台伺服器。manifest.yaml— 維運對應,由兩個 Releases 共用。兩行:由哪個 Recipe 實現控制平面,以及由哪個實現資料庫。release-single.yaml、release-replicated.yaml— 操作員的大小設定。它們的差異在於metadata.name與server_instances,其他每個欄位都刻意保持相等,使得那一行就是全部差異。.aggrega-config.yaml— 指名標準程式庫目錄所解析自的 hub 登錄庫。這個範例不撰寫任何自己的實體。
草擬範例
你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example mysql-innodb-cluster它接著會印出要接著執行的命令。第一個會切換到新資料夾:
cd mysql-innodb-cluster建置
aggrega build --release release-single.yaml --auto-pullaggrega build --release release-replicated.yaml --auto-pull--auto-pull 會在建置期間寫入全新檢出內容所缺少的鎖定檔。每次建置都寫入 dist/,而且建置是新增它的檔案,而不是清除先前建置留下的內容。請在每次執行之間移除 dist/,才能單獨閱讀某一個 Release 的輸出:
rm -rf dist && aggrega build --release release-replicated.yaml --auto-pull每個 Release 渲染五個檔案:資料庫的自訂資源,以及讓控制平面運作起來的四個檔案。
dist/stack/helm/db/innodbcluster.yamldist/stack/helm/mysql-operator/clusterrole.yamldist/stack/helm/mysql-operator/clusterrolebinding.yamldist/stack/helm/mysql-operator/deployment.yamldist/stack/helm/mysql-operator/serviceaccount.yaml搭配 AI 助理
以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。
只使用 Aggrega 工具,並且不要自己建立任何資料夾。若工作區資料夾中尚未有 `mysql-innodb-cluster` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `mysql-innodb-cluster`,且不指定 `dir`:它會把範例寫入新的 `mysql-innodb-cluster` 資料夾。接著依此順序為每個 Release 各呼叫一次 `build` 工具 — `release-single.yaml` 與 `release-replicated.yaml` — 每次都將 `workspace_root` 設為 `mysql-innodb-cluster`、`release` 設為該檔案、`auto_pull` 設為 true、`write` 設為 true。告訴我每次建置寫入了哪些檔案;它們位於 `mysql-innodb-cluster/dist/` 之下。資料庫渲染成什麼
# dist/stack/helm/db/innodbcluster.yaml — aggrega build --release release-single.yamlapiVersion: mysql.oracle.com/v2kind: InnoDBClustermetadata: [… omitted: the labels and annotations, described below] name: dbspec: instances: 1 router: instances: 1 version: "8.4.0" secretName: "mysql-root" mycnf: | [mysqld] character-set-server=utf8mb4 collation-server=utf8mb4_0900_ai_ci渲染出的檔案還帶有 Aggrega 放在它渲染的每個成品上的標籤與註記 — 它來自哪個 Aggregate、哪個元件、哪個 Recipe、哪個邊界 — 這裡省略以便專注於請求本身。
這五個 spec 項目中有四個直接可追溯到某個撰寫的內容:instances 對應 Release 的 server_instances,version 對應 Component 的 engine_version 預設值,secretName 對應 Release 的 credentials_secret,而 mycnf 區塊對應開發者的 charset 與 collation。第五個 router.instances 在下方討論。
重點所在的比較
兩棵渲染樹之間的全部差異:
--- dist/stack/helm/db/innodbcluster.yaml (single)+++ dist/stack/helm/db/innodbcluster.yaml (replicated) spec:- instances: 1+ instances: 3 router: instances: 1五個檔案中的某一個檔案裡的一行。 四個控制平面檔案在兩個 Releases 之間逐位元組相同:協調單一實例請求的控制器,與協調三實例請求的控制器是同一個控制器,所以它沒有任何變動。
路由器數量也不變,而且它不是這個元件可自由調整的旋鈕。 元件發布的位址是路由器所回應的 Service,因此數量以自訂資源自己的預設值公開,且絕不低於它。那是 Release 可以要求的內容的一個界限,而不是這個範例所改變的大小,這就是為什麼那一行出現在上方的差異中卻沒有變化。
控制平面
[… omitted: the Deployment's metadata, selector and pod template up to the containers] containers: - name: controller image: container-registry.oracle.com/mysql/community-operator:26.7.0-2.3.0 [… omitted: the controller's readiness probe]兩個 Release 都不指名映像檔。 參照來自 Component 自己的 infra.image 預設值,所以什麼都不設定的 Release 仍會渲染一個帶有某個版本的 Deployment,而不是需要先填寫才能套用的那種。從自己的登錄庫鏡像 pull 的目標,會在 Release 中設定 infra.image,並取得該參照。
控制平面與資料庫一起宣告在 Aggregate 中,因為自訂資源本身是惰性的:只有在有控制器監看它時,它才會變成執行中的伺服器。兩者一起宣告,才能讓兩者作為一個單元被套用。
前提條件是被聲明的,而不是被探索的
編譯器預先執行,從不連絡叢集,所以無法檢查控制器是否已安裝。它改為詢問。若讓 control_plane_present 保持未設定,建置就會停止:
[… omitted: the pull's lines, which carry the same halt][CATALOG_ERROR E0033] Recipe/FeatureRecipe assertions caused by: Recipe "mysql-innodb-cluster" instance "db" assertion "mysql_operator_prerequisite": assertion "Recipe/mysql-innodb-cluster/db/mysql_operator_prerequisite": template: Recipe/mysql-innodb-cluster/db/mysql_operator_prerequisite:1:48: executing "Recipe/mysql-innodb-cluster/db/mysql_operator_prerequisite" at <fail "the MySQL Operator control plane must be installed in the target cluster before this realization emits its InnoDBCluster custom resource; once the control plane is present, affirm the prerequisite by setting infra.control_plane_present to true in the Release">: error calling fail: assertion failed: the MySQL Operator control plane must be installed in the target cluster before this realization emits its InnoDBCluster custom resource; once the control plane is present, affirm the prerequisite by setting infra.control_plane_present to true in the Release什麼都不會被寫出。沒有人在協調的自訂資源並不是比正常運作的資源更小的成功 — 它是一個能乾淨套用然後就擱在那裡的成品,所以建置拒絕產生它,並指名能說出相反內容的鍵。
root 帳號
這裡的兩個 Releases 都把叢集指向目標叢集中已存在的 Secret:
db: infra: credentials_secret: mysql-root不會為該 Secret 渲染任何東西,也沒有任何密碼進入輸出 — 自訂資源攜帶名稱,控制器從中讀取帳號。改為把 infra.credentials 設為 generated,則會根據附加到元件 root_password 槽位的認證,在自訂資源旁渲染該 Secret,這會把密碼放進渲染輸出,並且在輸出就是用來佈建叢集時是正確的選擇。
這個實現不建立的是邏輯資料庫或應用程式使用者。控制器建立叢集的 root 帳號,在它之下什麼都沒有,所以應用程式需要的結構描述與帳號,是由持有 root 的人建立。
該選用哪種實現
在不得需要先安裝任何東西時,選用 core/datastore/mysql 及其合成式 Recipe — 它不需要叢集中的控制器,也不需要連到任何 chart 存放庫,所以建置時不擷取任何東西,並且會建立應用程式所要求的邏輯資料庫與登入角色。在目標已經執行 MySQL Operator 控制平面時,選用 core/datastore/mysql-innodb-cluster,或像這個範例一樣安裝它,而應用程式需要的是對叢集內位址上某個叢集的 root 存取。
合成式實現的單一伺服器形態已在 ../mysql-standalone/ 中完整說明,這裡不再重複。
接縫在哪裡
開發者文件不受一個實例或三個實例之間的選擇影響,維運對應也是如此。這就是這個範例要具體呈現的分工:
- 開發者說明應用程式需要資料庫提供什麼;
- 維運說明資料庫由什麼構成 — 由哪個 Recipe 實現它;
- 操作員說明這一個有多大。
把 server_instances 改成自訂資源接受的任何值,這個目錄中的 aggregate.yaml 都不會變動。