Memcached — 一個 Aggregate、兩個目標,以及一個被拒絕的密碼
這個精簡範例把單一 Memcached 記憶體內快取,從一個由開發者撰寫的 Aggregate 編譯成兩種實現:
- 用於本機開發的 Docker Compose,以及
- 合成式 helm Recipe,直接在 Kubernetes 上執行官方上游
memcached映像檔(沒有外部 chart)。
合成式 helm Recipe 是建議的 Kubernetes 路徑:它只依賴官方映像檔標籤,而不是第三方 chart,而且渲染出的資源集合恰好就是工作負載所宣告的。
同一個 aggregate.yaml 驅動兩者。每種實現只有 Manifest 所選的 Recipe 層會改變。另外兩個 Aggregates 會為快取附加密碼,它們的 Releases 是反例:建置會中止,因為沒有任何實現能強制執行 Memcached 密碼。
檔案
| 檔案 | 角色 |
|---|---|
aggregate.yaml | 開發者意圖:一個 cache 元件,型別為 core/datastore/memcached,沒有認證 — 快取在無驗證的情況下執行。 |
aggregate-auth.yaml | 同一個快取,並把注入值密碼認證附加到元件的選用 password 槽位(Compose 路徑)。其建置會中止。 |
aggregate-helm-synth-auth.yaml | 同一個快取,並為合成式 helm 路徑附加可參照的密碼認證。其建置會中止。 |
manifest-compose.yaml | 把 Memcached 型別對應到 Compose Recipe(compose/memcached),並把認證對應到 dotenv-secret。 |
manifest-helm-synth.yaml | 把 Memcached 型別對應到合成式 helm Recipe(helm/memcached),並把認證對應到 k8s-secret-from-parameter。 |
release-compose.yaml / release-helm-synth.yaml | 兩種實現的操作員記錄。 |
release-negative-compose-auth.yaml / release-negative-helm-synth-auth.yaml | 針對兩個密碼 Aggregates 的操作員記錄。預期每次建置都會失敗。 |
它示範的內容
- 一個撰寫介面,多個目標。 單一 Aggregate 是堆疊是什麼的事實來源。每個 Manifest 說明如何實現它。
- 無狀態的資料儲存。 Memcached 把項目保存在記憶體中,不持久儲存任何東西,因此元件的型別是一般的
server,而不是stateful-server。在 Kubernetes 上,Recipe 渲染的是 Deployment,而不是 StatefulSet,且不請求持久性磁碟區;在 Compose 上,服務不宣告資料磁碟區。 - Workload 合成(Kubernetes)。 合成式 helm Recipe 宣告一個 Workload — 一個 Deployment 加上一個執行官方
memcached映像檔的 ClusterIP Service — helm 提供者會把它轉譯成 Kubernetes 資訊清單,不需外部 chart。渲染出的資源集合恰好就是 Workload 所宣告的。由於 Compose Recipe 與合成式 helm Recipe 都叫memcached,Manifests 以提供者範圍的方式參照它們,即compose/memcached與helm/memcached。 - 實現無法提供的保護會被拒絕,而不是被丟棄。 元件宣告一個選用的
password槽位。memcached映像檔不強制執行任何密碼:它的兩種驗證模式是只透過二進位通訊協定使用的 SASL,以及 ASCII 驗證檔,而目錄中的 Memcached 用戶端兩者都不使用。若 Recipe 接受該附加項目,渲染出的快取會回應每一個用戶端,而 Aggregate 卻聲稱它受到保護,因此每個 Recipe 反而會中止建置,並指名該槽位。
草擬範例
你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example memcached-cache它接著會印出要接著執行的命令。第一個會切換到新資料夾:
cd memcached-cache建置
此工作區沒有 .aggrega-config.yaml,因此標準程式庫實體從 hub 登錄庫 hub.aggrega.ai 解析,不需要目錄旗標;--auto-pull 會在建置期間寫入全新複製的副本所缺少的鎖定檔:
# Local development (Docker Compose)aggrega build --release release-compose.yaml --auto-pull
# Kubernetes (synthesized helm Recipe, official image)aggrega build --release release-helm-synth.yaml --auto-pull渲染出的成品位於 dist/ 之下。
搭配 AI 助理
以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。
只使用 Aggrega 工具,並且不要自己建立任何資料夾。若工作區資料夾中尚未有 `memcached-cache` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `memcached-cache`,且不指定 `dir`:它會把範例寫入新的 `memcached-cache` 資料夾。接著依此順序為每個 Release 各呼叫一次 `build` 工具 — `release-compose.yaml` 與 `release-helm-synth.yaml` — 每次都將 `workspace_root` 設為 `memcached-cache`、`release` 設為該檔案、`auto_pull` 設為 true、`write` 設為 true。告訴我每次建置寫入了哪些檔案;它們位於 `memcached-cache/dist/` 之下。對照:快取無法強制執行的密碼
兩個反例 Releases 建置密碼 Aggregates,而且每次建置都預期會失敗:
aggrega build --release release-negative-compose-auth.yaml --auto-pullaggrega build --release release-negative-helm-synth-auth.yaml --auto-pull每一個都在 Memcached Recipe 中中止,在寫入任何東西之前,且該次中止以 Recipe 自己的訊息作結:
a password is attached to the password slot of "cache", but the memcachedimage this Recipe runs cannot enforce a password: it authenticates only throughSASL over the binary protocol or an ASCII authentication file, which thecatalog's Memcached clients do not use, so the cache would accept every clientanyway; remove the attachment, and restrict who can reach the cache through itsnetwork此拒絕是針對附加項目本身,而不是認證的傳遞方式,所以每個能填入該槽位的 Feature 都會以同樣方式中止。受管理的實現行為相同:Amazon ElastiCache 不提供 Memcached 驗證,它的 Recipes 也會在附加項目上中止。因此 Memcached 快取是由誰能連到它來保護 — 也就是它所放置的網路 — 而基礎的 aggregate.yaml 才是應建置的形式。