本頁為機器翻譯。閱讀英文原文。
NGINX 閘道 — 反向代理路由到上游,兩個目標
這個範例從一個由開發者撰寫的 Aggregate,編譯一個小而真實的堆疊 — 一個位於 web 後端前面的 NGINX 反向代理閘道 — 到兩個目標:
- 用於本機開發的 Docker Compose,以及
- 用於叢集的合成式 Kubernetes 資訊清單。
同一個 aggregate.yaml 驅動兩者。每個目標只有 Manifest 所選的 Recipe 層會改變。
檔案
| 檔案 | 角色 |
|---|---|
aggregate.yaml | 開發者意圖:一個 web 後端(core/devtool/adminer),以及一個 proxy 閘道(core/gateway/nginx),宣告對後端的 upstream 使用。 |
manifest-compose.yaml | 把閘道型別對應到 Compose Recipe(compose/nginx),並把後端對應到 Compose Adminer Recipe。 |
manifest-helm-synth.yaml | 把閘道型別對應到合成式 helm Recipe(helm/nginx),並把後端對應到離線的 Adminer helm Recipe。 |
release-compose.yaml / release-helm-synth.yaml | 兩個目標的操作員記錄。 |
它示範的內容
- 一個撰寫介面,多個目標。 單一 Aggregate 是堆疊是什麼的事實來源。每個 Manifest 說明如何實現它。
- 經由字串管道(String Pipe)的上游路由。 閘道宣告對後端的
upstream使用;後端 Recipe 的provides.host與provides.port會被傳遞到閘道,並渲染到它的proxy_pass目標中。編譯器路由這些字串而不解讀它們 — 所以 Compose 建置連接的是 compose 服務名稱,而 Kubernetes 建置連接的是 Service 名稱,來自同一條邊。 - Provider 慣用的設定。 在兩個目標上,閘道會在容器啟動時寫入一個極簡的 NGINX server 區塊,其
proxy_pass轉送到注入的上游,然後在前景啟動 nginx。Kubernetes 目標將此實現為無狀態的 Deployment 加一個 ClusterIP Service,沒有外部 Helm chart。 - 可重現的映像檔。 Kubernetes 目標執行固定在明確標籤的官方上游 nginx 映像檔,所以渲染出的 Deployment 參照穩定的映像檔。
草擬範例
你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example nginx-gateway它接著會印出要接著執行的命令。第一個會切換到新資料夾:
cd nginx-gateway建置
此工作區沒有 .aggrega-config.yaml,因此標準程式庫實體從 hub 登錄庫 hub.aggrega.ai 解析,不需要目錄旗標;--auto-pull 會在建置期間寫入全新複製的副本所缺少的鎖定檔:
# Local development (Docker Compose)aggrega build --release release-compose.yaml --auto-pull
# Kubernetes (synthesized manifests)aggrega build --release release-helm-synth.yaml --auto-pull渲染出的成品會落在 dist/ 之下。Kubernetes 目標以離線方式渲染一般資訊清單——不需要拉取 chart。
搭配 AI 助理
以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。
只使用 Aggrega 工具,並且不要自己建立任何資料夾。若工作區資料夾中尚未有 `nginx-gateway` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `nginx-gateway`,且不指定 `dir`:它會把範例寫入新的 `nginx-gateway` 資料夾。接著依此順序為每個 Release 各呼叫一次 `build` 工具 — `release-compose.yaml` 與 `release-helm-synth.yaml` — 每次都將 `workspace_root` 設為 `nginx-gateway`、`release` 設為該檔案、`auto_pull` 設為 true、`write` 設為 true。告訴我每次建置寫入了哪些檔案;它們位於 `nginx-gateway/dist/` 之下。注入的上游看起來如何
在 Compose 上,閘道服務執行 NGINX,其 server 區塊指向後端的 compose 服務名稱與連接埠:
server { listen 8000; location / { proxy_pass http://web:8080; } }在 Kubernetes 上,同樣的路由出現在容器命令中,該命令會在啟動 nginx 之前寫入伺服器設定,指向後端的 Service — helm 提供者依後端及其 default 服務鍵命名它:
server { listen 8080; location / { proxy_pass http://web-default:8080; } }兩者都從同一條 Aggregate 邊解析 — 後端的主機與連接埠透過 upstream 使用傳遞,而每個目標以自己的慣用方式渲染它們。
執行它(Compose)
aggrega build --release release-compose.yaml --auto-pulldocker compose -f dist/app/compose/docker-compose.yaml up -d開啟 http://localhost:8000 — NGINX 會把請求轉送到後端。使用 docker compose -f dist/app/compose/docker-compose.yaml down 拆除。