相容 S3 的物件儲存體——一個元件,兩個目標,必要的金鑰組
此範例將一個相容 S3 的物件儲存體,即 core/storage/s3-object-store 元件,編譯成它的兩種自行託管的實現:
- Docker Compose,用於本機開發,透過
compose/s3-object-store,以及 - 在 Kubernetes 上的合成 helm Recipe
helm/s3-object-store,沒有外部 chart。
兩者都從專案發布的 chrislusf/seaweedfs 映像檔執行 SeaweedFS(Apache-2.0),作為一個保存一個儲存貯體的伺服器處理程序,儲存貯體在伺服器開始提供服務之前建立。同一個元件也由其他 Recipes 實現在 Amazon S3 與 Google Cloud Storage 上;無論 Manifest 選取哪一種實現,使用端讀到的都是同樣發布的端點、儲存貯體與區域。
元件的兩個金鑰槽位,access_key_id 與 secret_access_key,是選用的,因為受管理的服務不會從儲存貯體取得金鑰。SeaweedFS 把這組金鑰當作伺服器自己的 S3 帳號,而且沒有預設值,因此當任一槽位為空時,它的兩種實現都會中止。aggregate.yaml 什麼都不填,並展示建置中止的情況;另外兩個 Aggregates 則以各目標傳遞金鑰的形式填入兩個槽位:aggregate-auth.yaml 用於 Compose 路徑(以環境變數呈現的 inject-value 認證),而 aggregate-helm-synth-auth.yaml 用於合成的 helm 路徑(具體化為 Kubernetes Secrets 的可參照認證)。
檔案
| 檔案 | 角色 |
|---|---|
aggregate.yaml | 開發者意圖:一個 store 元件,型別為 core/storage/s3-object-store,保存儲存貯體 media,沒有附加任何金鑰——建置它會中止,並指名為空的 access_key_id 槽位。 |
aggregate-auth.yaml | 同一個儲存體,兩把金鑰都以 inject-value 認證附加(Compose 路徑)。 |
aggregate-helm-synth-auth.yaml | 同一個儲存體,兩把金鑰都以可參照的認證附加,用於合成的 helm 路徑——每把金鑰都具體化為 Kubernetes Secret,由 StatefulSet 參照。 |
manifest-compose.yaml | 將儲存體型別對應到 Compose Recipe(compose/s3-object-store),並將認證對應到 dotenv-secret。 |
manifest-helm-synth.yaml | 將儲存體型別對應到合成式 helm Recipe(helm/s3-object-store),並將認證對應到 k8s-secret-from-parameter。 |
release-compose.yaml / release-helm-synth.yaml | aggregate.yaml 兩種實現的操作員記錄;它不附加任何金鑰,因此每次建置都會中止。 |
release-compose-auth.yaml / release-helm-synth-auth.yaml | 兩個填入兩個槽位的 Aggregates 的操作員記錄。 |
它示範的內容
- 一個元件,多種實現。 Aggregate 指名儲存貯體,而不提任何關於伺服器的事。每個 Manifest 說明 如何 實現儲存體,而選取受管理實現的 Manifest 則會把同一個 Aggregate 改為部署到 Amazon S3 或 Cloud Storage。
- 儲存貯體隨儲存體而來。 開發者的
values.bucket就是儲存體所保存並發布的儲存貯體。Kubernetes 上的初始化容器與 Compose 上的初始化服務,會在伺服器開始提供服務之前建立它,並讓既有的儲存貯體維持原樣,因此使用端絕不會遇到沒有儲存貯體的儲存體。SeaweedFS 不接受的儲存貯體名稱會讓建置中止,並指名該規則。 - 單一磁碟區上的有狀態伺服器。 在 Kubernetes 上,合成式 Recipe 渲染出一個單一複本的 StatefulSet,其磁碟區宣告掛載在
/data,另有一個 ClusterIP Service 以及 StatefulSet 所指名的 headless Service;在 Compose 上,服務把具名磁碟區掛載在同一個路徑,並在主機上發布 S3 連接埠。 - 沒有預設值的必要金鑰組。 帳號存在時,SeaweedFS 會驗證每個 S3 請求,因此不能用目錄所有讀者都持有的任何金鑰來代替。每個 Recipe 在渲染任何東西之前,都會在槽位為空時中止,並指名該槽位與應附加的內容。附加的金鑰組成為伺服器的帳號,而機密金鑰同時也是伺服器的 filer 簽署金鑰,因此 S3 連接埠旁的 gRPC 連接埠也不接受任何未經驗證的變更。
- 使用端讀取什麼。 儲存體發布
bucket、endpoint(Kubernetes 上為http://store-default:8333,Compose 上為http://store:8333)、host、port8333、protocols3與regionus-east-1,也就是 S3 用戶端在伺服器未回報區域時所讀取的區域。連接到它的使用端,會把自己的 S3 用戶端指向該端點,並在自己的連線上出示金鑰組。
草擬範例
你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example s3-object-store它接著會印出要接著執行的命令。第一個會切換到新資料夾:
cd s3-object-store建置
此工作區沒有 .aggrega-config.yaml,因此標準程式庫實體從 hub 登錄庫 hub.aggrega.ai 解析,不需要目錄旗標;--auto-pull 會在建置期間寫入全新複製的副本所缺少的鎖定檔:
# Local development (Docker Compose)aggrega build --release release-compose-auth.yaml --auto-pull
# Kubernetes (synthesized helm Recipe)aggrega build --release release-helm-synth-auth.yaml --auto-pull渲染出的成品位於 dist/ 之下。
建置未附加任何金鑰的 aggregate.yaml,在任一目標上都會在渲染任何東西之前中止:
aggrega build --release release-helm-synth.yaml --auto-pull[… omitted: the pull's lines, which report the same halt, and a lint notice][CATALOG_ERROR E0033] Recipe/FeatureRecipe assertions caused by: Recipe "s3-object-store" instance "store" assertion "key_slots_filled": assertion "Recipe/s3-object-store/store/key_slots_filled": template: Recipe/s3-object-store/store/key_slots_filled:1:138: executing "Recipe/s3-object-store/store/key_slots_filled" at <fail (printf "the %s slot is empty, and SeaweedFS takes the access_key_id and secret_access_key pair as the account its S3 API authenticates every request by, so there is no default for either: attach a referenceable credential, such as core/referenced-secret, to %s" $slot $slot)>: error calling fail: assertion failed: the access_key_id slot is empty, and SeaweedFS takes the access_key_id and secret_access_key pair as the account its S3 API authenticates every request by, so there is no default for either: attach a referenceable credential, such as core/referenced-secret, to access_key_idrelease-compose.yaml 因同一個斷言而中止,其補救方式指名了 Compose 路徑採用的形式:「attach a credential to access_key_id delivering AWS_ACCESS_KEY_ID, such as core/secret-value with key AWS_ACCESS_KEY_ID and its origin a Compose variable reference such as ${AWS_ACCESS_KEY_ID}」(為 access_key_id 附加一個傳遞 AWS_ACCESS_KEY_ID 的認證,例如鍵為 AWS_ACCESS_KEY_ID、origin 為 Compose 變數參照的 core/secret-value)。修正方式就是另外兩個 Aggregates 所採取的步驟:在 store 元件的 features.access_key_id 與 features.secret_access_key 下,各附加一個認證。
搭配 AI 助理
以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。
只使用 Aggrega 工具,且不要自行建立任何資料夾。如果工作區資料夾中還沒有 `s3-object-store` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `s3-object-store`,且不指定 `dir`:它會把範例寫入新的 `s3-object-store` 資料夾。接著依此順序為每個 Release 呼叫一次 `build` 工具——`release-compose-auth.yaml` 與 `release-helm-synth-auth.yaml`——每次都將 `workspace_root` 設為 `s3-object-store`,將 `release` 設為該檔案,將 `auto_pull` 設為 true,並將 `write` 設為 true。然後以同樣方式呼叫 `build` 工具,將 `release` 設為 `release-helm-synth.yaml`:它本來就會被拒絕,所以請把拒絕訊息讀給我聽。告訴我前兩次建置寫出了哪些檔案;它們位於 `s3-object-store/dist/` 底下。認證傳遞說明
在 Compose 路徑上,每把金鑰都是 inject-value 機密(aggregate-auth.yaml):它的值透過元件的環境管道傳遞,變數名稱就是其 values.key 所指名的 AWS_ACCESS_KEY_ID 或 AWS_SECRET_ACCESS_KEY,這兩個正是 Recipe 交給伺服器的變數;以任何其他名稱傳遞的附加項目都會讓建置中止,並指名該槽位。每個 origin 都是 Compose 變數參照,因此渲染出的檔案帶的是參照,而不是金鑰:
environment: AWS_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID} AWS_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY} WEED_JWT_FILER_SIGNING_KEY: ${S3_SECRET_ACCESS_KEY}合成的 helm 路徑使用可參照的認證(aggregate-helm-synth-auth.yaml):每把金鑰都具體化為以元件與槽位命名的 Kubernetes Secret,而 StatefulSet 透過環境變數的 secretKeyRef 參照它,因此兩把金鑰都不會出現在 StatefulSet 資訊清單中——只出現在 Secrets 中:
env: - name: AWS_ACCESS_KEY_ID valueFrom: secretKeyRef: name: store-access-key-id key: access-key-id - name: AWS_SECRET_ACCESS_KEY valueFrom: secretKeyRef: name: store-secret-access-key key: secret-access-key - name: WEED_JWT_FILER_SIGNING_KEY valueFrom: secretKeyRef: name: store-secret-access-key key: secret-access-keyaggregate-helm-synth-auth.yaml 中的 origin 值是開發用預留位置,會原樣寫入發出的 Secrets。對於承載實際流量的 Release,請改將 core/referenced-secret 對應到 k8s-existing-secret,並讓每個 origin 指名命名空間中已存在的 Secret,且該 Secret 以附加項目的 key 保存金鑰。
執行它(Compose)
aggrega build --release release-compose-auth.yaml --auto-pullexport S3_ACCESS_KEY_ID=<an access key identifier of your choosing>export S3_SECRET_ACCESS_KEY=<a secret key of your choosing>docker compose -f dist/app/compose/docker-compose.yaml up -d儲存貯體服務建立 media 之後,S3 API 就會在 http://localhost:8333 回應。請用匯出的金鑰組與區域 us-east-1,把任何 S3 用戶端指向該位址。要拆除,請執行 docker compose -f dist/app/compose/docker-compose.yaml down。