只有其閘道可以呼叫的模型伺服器
在叢集中執行的模型伺服器會回應任何連到其位址的對象。在沒有網路原則的 pod 網路上(這是 Kubernetes 的預設),那就是叢集中的每個工作負載:放在模型前面的閘道是流量預期抵達的途徑,但並不是流量唯一能抵達的途徑。
Aggregate 已經說明哪些元件呼叫哪些元件:閘道的 upstream 連線指名模型伺服器。core/caller-allowlist 由操作員在 Release 中附加到模型伺服器,會把這個已宣告的連線轉成叢集網路的規則。這個工作區把這種安排編譯成 Kubernetes 資訊清單:位於命名空間 edge 的 NGINX 閘道、位於命名空間 models 的 Ollama 模型伺服器,以及附加項目寫在模型伺服器旁的網路原則。
這個工作區不撰寫任何自己的目錄實體。這裡的三個實體檔案——一個 Aggregate、一個 Manifest 與一個 Release——就是任何人需要撰寫的全部內容。
誰寫什麼
| 檔案 | 作者 | 它在此處說明的內容 |
|---|---|---|
aggregate.yaml | 開發者 | 閘道、模型伺服器,以及閘道連到它的 upstream 連線 |
release.yaml | 操作員 | 兩個命名空間,以及模型伺服器項目上帶有其 infra 的 allowlist 附加項目 |
manifest.yaml | 操作員 | 在 Kubernetes 上由哪個 Recipe 實現每個元件與 Feature |
Aggregate 不帶任何關於網路原則的內容。開發者宣告閘道所建立的連線;叢集是否拒絕其他每一個連線,是操作員針對此環境做的決定,寫在 components.model.features 下的 Release 中。Feature 所接受的每個輸入都是 infra,即操作員的支柱。開發者應該知道的是其後果:只要 Release 把這個 Feature 附加到某個元件,凡是 Aggregate 沒有宣告為指向該元件的 uses 邊的呼叫,都會被叢集拒絕,前提是其網路外掛程式強制執行網路原則。
它建置什麼
| 實例 | 目錄 Component | 命名空間 | 它在這裡是什麼 |
|---|---|---|---|
gateway | core/gateway/nginx | edge | 用戶端呼叫的閘道;它轉送到模型伺服器 |
model | core/ai-model/ollama | models | 模型伺服器,提供 llama3.1 |
一條 uses 邊:閘道的 upstream 插槽到模型伺服器。一個 Feature 附加項目,位於 Release:模型伺服器上的 allowlist,即 core/caller-allowlist。Release 確認叢集強制執行網路原則,並指名一個位於 Aggregate 之外的來源,即一個監控代理程式。
草擬範例
你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example model-server-caller-allowlist它接著會印出要接著執行的命令。第一個會切換到新資料夾:
cd model-server-caller-allowlist建置
aggrega build --release release.yaml --auto-pullRelease 在建置前需要鎖定檔,而鎖定檔是產生出來的、不隨附發布,所以全新複製的專案中沒有。先解析一個,或讓建置來填入:
aggrega pull --release release.yaml # resolve once, then build with no flagaggrega build --release release.yaml --auto-pull # or resolve during the build這個工作區從 .aggrega-config.yaml 所指名的預設登錄庫解析目錄實體;不會從任何 chart 存放庫擷取任何東西。
建置會印出以下內容,其中 <workspace> 代表此範例的資料夾:
warning: node "model" (provider "helm"): target "helm" does not support annotation aggrega.ai/namespace on artifacts-only input and ignored it[… omitted: the resolution's summary and a total build time, which differ per run]Build completed: 6 files written to <workspace>/dist該警告與原則有關。目標不會把 Feature 自己的資訊清單放進命名空間,因此附加項目的範本會寫入原則所屬的命名空間,也就是模型伺服器的 StatefulSet 所發出到的命名空間 — 如下方的 namespace: "models"。
搭配 AI 助理
以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。
只使用 Aggrega 工具,並且不要自己建立任何資料夾。若工作區資料夾中尚未有 `model-server-caller-allowlist` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `model-server-caller-allowlist`,且不指定 `dir`:它會把範例寫入新的 `model-server-caller-allowlist` 資料夾。接著呼叫 `build` 工具,將 `workspace_root` 設為 `model-server-caller-allowlist`、`release` 設為 `release.yaml`、`auto_pull` 設為 true、`write` 設為 true。告訴我建置寫入了哪些檔案;它們位於 `model-server-caller-allowlist/dist/` 之下。渲染樹
dist/├── edge/helm/│ ├── gateway-default-service.yaml│ └── gateway-deployment.yaml└── models/helm/ ├── model--allowlist--networkpolicy.yaml ├── model-default-service.yaml ├── model-headless-service.yaml └── model-statefulset.yaml附加項目新增一個檔案,即原則。其他每個檔案都與同一個 Release 在沒有它時渲染的內容逐位元組相同。
什麼可以連到模型伺服器
dist/models/helm/model--allowlist--networkpolicy.yaml,從其 spec 開始:
spec: podSelector: matchLabels: "app": "model" policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: "kubernetes.io/metadata.name": "models" podSelector: matchLabels: "app": "model" - namespaceSelector: matchLabels: "kubernetes.io/metadata.name": "edge" podSelector: matchLabels: "app": "gateway" - namespaceSelector: matchLabels: "kubernetes.io/metadata.name": "monitoring" podSelector: matchLabels: "app.kubernetes.io/name": "prometheus" ports: - port: 11434 protocol: "TCP"原則位於 models,也就是模型伺服器的 StatefulSet 所發出到的命名空間,它選取模型伺服器的 pod,並允許來自三個來源的連線,連到模型伺服器的容器所宣告的那一個連接埠:
- 模型伺服器自己的 pod,位於
models,讓同一個伺服器的各個複本能互相連到。 - 閘道的 pod,位於命名空間
edge。 命名空間選擇器與 pod 選擇器位於同一個項目中,因此 pod 必須同時符合兩者。另一個命名空間中帶有相同標籤的工作負載 — 例如在別處再次部署的同一個 Aggregate — 不是閘道,也不會被允許。 - Release 所指名的監控代理程式:位於命名空間
monitoring中,標籤為app.kubernetes.io/name: prometheus的 pod。
每個元件的 pod 都由它自己的控制器所寫的選擇器選取 — 模型伺服器的 StatefulSet 為 app: model,閘道的 Deployment 為 app: gateway — 而每個命名空間都是其工作負載所發出到的命名空間,兩者都是從渲染所發出的內容讀取,而不是手動重述。兩個選擇器都不帶版本,因此當新的建置推出時,前一個建置的 pod 在被取代之前仍會被選取並被允許。
什麼無法連到它
- 任何其他工作負載,無論在
models、edge或任何其他命名空間 — 包括monitoring中的另一個代理程式。 - Ingress 控制器,或叢集外的任何用戶端。 除非 Release 指名,否則 Aggregate 之外的任何東西都不會被允許,而這個 Release 只指名監控代理程式。用戶端經由閘道連到模型伺服器,那正是 Aggregate 所宣告的連線。
- 11434 以外的任何連接埠。
原則限制的是誰可以連線,而不是連線攜帶什麼:閘道轉送的請求會以閘道送出的樣子抵達模型。
建置無法得知的事
規則是否被強制執行。 網路原則由叢集的網路外掛程式強制執行,而且只在該外掛程式實作網路原則時才如此;外掛程式不實作的叢集會接受原則卻什麼也不強制執行。建置會寫出原則,但看不到叢集,這就是 Release 在附加項目上帶有 control_plane_present: true 的原因:那是操作員的聲明,表示目標叢集會強制執行它,而在做出該聲明之前,建置會中止。
還有三項限制來自網路原則的運作方式,而不是來自這個 Feature:
- 原則會疊加。 當叢集中另一個原則選取相同的 pod 時,兩者中任何一個允許的連線都會被允許。
- 閘道不受這個附加項目限制。 它保護的是模型伺服器;若要限制誰能連到閘道,請同樣把這個 Feature 附加到 Release 中閘道的項目,並在那裡指名它所服務的用戶端。
- 出站流量不受影響。 模型伺服器仍能連到它拉取權重的登錄庫,並照常解析名稱。
什麼會中止建置
以下每一項都是建置印出的一個句子,指出要變更什麼:
-
前提條件未被確認。 從 Release 中移除
control_plane_present: true:[… omitted: what resolving the lockfile prints, the same halt among it, and the halt's heading, the template position and the assertion's own source]error calling fail: assertion failed: core/caller-allowlist attachment "allowlist" on "model" writes a network policy, which restricts connections only where the cluster's network plugin enforces network policy; once the target cluster's plugin does, affirm it by setting infra.control_plane_present to true on this attachment in the Release -
其工作負載未發出到任何命名空間的呼叫端。 從
edge邊界移除aggrega.ai/namespace註記,閘道的 Deployment 就會在沒有命名空間的情況下被發出:[… omitted: what resolving the lockfile prints, the same halt among it, and the halt's heading, the artifact being rendered, the template position and the template's own source]error calling fail: core/caller-allowlist attachment "allowlist" on "model": "gateway" connects to "model" through its "upstream" socket, but the Deployment "gateway" it renders is emitted into no namespace, so the policy cannot name where its pods run; set aggrega.ai/namespace on the boundary holding "gateway", or on its own entry, in the Release, or have the manifest that carries it write one -
模型伺服器的工作負載未發出到任何命名空間,原因相同:原則只選取它自己命名空間中的 pod。
-
任一者的工作負載,若既不寫控制器選擇器,也不帶有建置蓋在其 pod 上的任何身分標籤 — 例如依賴叢集所產生選擇器的 chart Job。以它帶有的每個標籤來選取它,會在版本標籤第一次變更時停止匹配它的 pod,因此建置會直接停止。
-
其渲染沒有發出任何 pod 的呼叫端或模型伺服器 — 由自訂資源實現、其控制器稍後才建立 pod,或位於叢集外位址的元件。這樣的元件無法在原則中被指名,因此建置會停止,而不是寫出一個會拒絕它或什麼都不選取的原則。
-
admitted_sources中的某個來源,同時指名位址範圍與 pod,或兩者皆未指名。
編譯器不解讀的地方
編譯器把每個渲染所回報的選擇器、標籤與命名空間,以字串形式傳遞到原則中。它不會連絡叢集,不知道那裡執行的是哪個網路外掛程式,也不知道命名空間中已經在執行哪些工作負載。上述每個中止都是由目錄自己的實體宣告的,位於 Feature 的實現中,而且沒有任何一個依賴對叢集的任何了解。
調整它
- 允許 Ingress 控制器。 在
admitted_sources中加入一個項目,帶有它的命名空間與其 pod 的標籤 — 兩者放在同一個項目中,因此只有該命名空間中的那些 pod 會被允許。 - 允許位址範圍。 加入一個帶有
cidr的項目,例如10.20.0.0/16。 - 第二個呼叫端。 在 Aggregate 中加入從它到模型伺服器的
uses邊,並把它放在 Release 所宣告的命名空間中;下一次建置就會允許它,附加項目不需任何更動。 - Compose。 Feature 不在那裡實現:Compose 堆疊把一個群組的每個服務放在同一個網路上,而且一個服務的設定無法把另一個服務移出該網路,因此在 Compose 上繫結這個 Feature 的 Release 會因為缺少實現而中止。