設定 Envoy 補充服務網格
搶先體驗方案客戶可使用這項設定,但我們不建議新 Cloud Service Mesh 使用者採用。詳情請參閱 Cloud Service Mesh 總覽。
本指南說明如何在 Fleet 中設定簡單的服務中介網。指南包含下列步驟:
- 將 Envoy 附加元件注入器部署至叢集。Injector 會將 Envoy Proxy 容器插入應用程式 Pod。
- 部署 Gateway API 資源,設定服務中繼網格中的 Envoy 補充,將要求轉送至命名空間
store
中的範例服務。 - 部署簡單的用戶端來驗證部署作業。
下圖顯示已設定的服務網格。
您只能在叢集中設定一個 Mesh
,因為側載注入器設定中的網格名稱和 Mesh
資源名稱必須相同。
部署 Envoy 補充注入器
如要部署補充注入器,請按照下列步驟操作:
設定專案資訊
# The project that contains your GKE cluster. export CLUSTER_PROJECT_ID=YOUR_CLUSTER_PROJECT_NUMBER_HERE # The name of your GKE cluster. export CLUSTER=YOUR_CLUSTER_NAME # The channel of your GKE cluster. Eg: rapid, regular, stable. export CHANNEL=YOUR_CLUSTER_CHANNEL # The location of your GKE cluster, Eg: us-central1 for regional GKE cluster, # us-central1-a for zonal GKE cluster export LOCATION=ZONE # The mesh name of the traffic director load balancing API. export MESH_NAME=YOUR_MESH_NAME # The project that holds the mesh resources. export MESH_PROJECT_NUMBER=YOUR_PROJECT_NUMBER_HERE export TARGET=projects/${MESH_PROJECT_NUMBER}/locations/global/meshes/${MESH_NAME} gcloud config set project ${CLUSTER_PROJECT_ID}
如要找出
MESH_NAME
,請按照下列方式指派值,其中MESH_NAME
是Mesh
資源規格中metadata.name
欄位的值:gketd-MESH_NAME
舉例來說,如果
Mesh
資源中的metadata.name
值為butterfly-mesh
,請將MESH_NAME
的值設為以下值:export MESH_NAME="gketd-butterfly-mesh"
套用 Mutating Webhook 的設定
以下各節提供將 MutatingWebhookConfiguration 套用至叢集的操作說明。建立 Pod 時,系統會叫用叢集內的許可控制器。存取控制器會與受管理的附屬元件插入器通訊,將 Envoy 容器新增至 Pod。
將下列變異 webhook 設定套用至叢集。
cat <<EOF | kubectl apply -f - apiVersion: admissionregistration.k8s.io/v1 kind: MutatingWebhookConfiguration metadata: labels: app: sidecar-injector name: td-mutating-webhook webhooks: - admissionReviewVersions: - v1beta1 - v1 clientConfig: url: https://meshconfig.googleapis.com/v1internal/projects/${CLUSTER_PROJECT_ID}/locations/${LOCATION}/clusters/${CLUSTER}/channels/${CHANNEL}/targets/${TARGET}:tdInject failurePolicy: Fail matchPolicy: Exact name: namespace.sidecar-injector.csm.io namespaceSelector: matchExpressions: - key: td-injection operator: Exists reinvocationPolicy: Never rules: - apiGroups: - "" apiVersions: - v1 operations: - CREATE resources: - pods scope: '*' sideEffects: None timeoutSeconds: 30 EOF
如果您需要自訂 sidecar 插入器,請按照下列步驟為叢集自訂 sidecar 插入器:
部署 store
服務
在本節中,您會在網格中部署 store
服務。
在
store.yaml
檔案中,儲存下列資訊清單:kind: Namespace apiVersion: v1 metadata: name: store --- apiVersion: apps/v1 kind: Deployment metadata: name: store namespace: store spec: replicas: 2 selector: matchLabels: app: store version: v1 template: metadata: labels: app: store version: v1 spec: containers: - name: whereami image: us-docker.pkg.dev/google-samples/containers/gke/whereami:v1 ports: - containerPort: 8080 --- apiVersion: v1 kind: Service metadata: name: store namespace: store spec: selector: app: store ports: - port: 8080 targetPort: 8080
將資訊清單套用至
gke-1
:kubectl apply -f store.yaml
建立服務網格
在
mesh.yaml
檔案中,儲存下列mesh
資訊清單。mesh
資源的名稱必須與注入器 configmap 中指定的網格名稱相符。在這個範例設定中,兩個位置都使用td-mesh
這個名稱:apiVersion: net.gke.io/v1alpha1 kind: TDMesh metadata: name: td-mesh namespace: default spec: gatewayClassName: gke-td allowedRoutes: namespaces: from: All
將
mesh
資訊清單套用至gke-1
,這會建立名為td-mesh
的邏輯網格:kubectl apply -f mesh.yaml
在
store-route.yaml
檔案中,儲存下列HTTPRoute
資訊清單。資訊清單定義了HTTPRoute
資源,可將指定主機名稱example.com
的 HTTP 流量,轉送至命名空間store
中的 Kubernetes 服務store
:apiVersion: gateway.networking.k8s.io/v1alpha2 kind: HTTPRoute metadata: name: store-route namespace: store spec: parentRefs: - name: td-mesh namespace: default group: net.gke.io kind: TDMesh hostnames: - "example.com" rules: - backendRefs: - name: store namespace: store port: 8080
將路徑資訊清單套用至
gke-1
:kubectl apply -f store-route.yaml
驗證部署作業
檢查
Mesh
狀態和事件,驗證Mesh
和HTTPRoute
資源是否已成功部署:kubectl describe tdmesh td-mesh
輸出結果會與下列內容相似:
... Status: Conditions: Last Transition Time: 2022-04-14T22:08:39Z Message: Reason: MeshReady Status: True Type: Ready Last Transition Time: 2022-04-14T22:08:28Z Message: Reason: Scheduled Status: True Type: Scheduled Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ADD 36s mc-mesh-controller Processing mesh default/td-mesh Normal UPDATE 35s mc-mesh-controller Processing mesh default/td-mesh Normal SYNC 24s mc-mesh-controller SYNC on default/td-mesh was a success
如要確保在預設命名空間中啟用附屬物注入功能,請執行下列指令:
kubectl get namespace default --show-labels
如果已啟用補充容器插入功能,輸出內容會顯示以下內容:
istio-injection=enabled
如果未啟用補充容器注入功能,請參閱「啟用補充容器注入功能」。
如要驗證部署作業,請部署用戶端 Pod,做為先前定義的
store
服務的用戶端。在client.yaml
檔案中,儲存下列內容:apiVersion: apps/v1 kind: Deployment metadata: labels: run: client name: client namespace: default spec: replicas: 1 selector: matchLabels: run: client template: metadata: labels: run: client spec: containers: - name: client image: curlimages/curl command: - sh - -c - while true; do sleep 1; done
部署規格:
kubectl apply -f client.yaml
在叢集中執行的補充注入器會自動將 Envoy 容器注入用戶端 Pod。
如要確認 Envoy 容器是否已插入,請執行下列指令:
kubectl describe pods -l run=client
輸出結果會與下列內容相似:
... Init Containers: # Istio-init sets up traffic interception for the Pod. istio-init: ... # td-bootstrap-writer generates the Envoy bootstrap file for the Envoy container td-bootstrap-writer: ... Containers: # client is the client container that runs application code. client: ... # Envoy is the container that runs the injected Envoy proxy. envoy: ...
在用戶端 Pod 佈建完成後,請從用戶端 Pod 傳送要求給 store
服務。
取得用戶端 Pod 的名稱:
CLIENT_POD=$(kubectl get pod -l run=client -o=jsonpath='{.items[0].metadata.name}') # The VIP where the following request will be sent. Because all requests # from the client container are redirected to the Envoy proxy sidecar, you # can use any IP address, including 10.0.0.2, 192.168.0.1, and others. VIP='10.0.0.1'
傳送要求至儲存服務,並輸出回應標頭:
TEST_CMD="curl -v -H 'host: example.com' $VIP"
在用戶端容器中執行測試指令:
kubectl exec -it $CLIENT_POD -c client -- /bin/sh -c "$TEST_CMD"
輸出結果會與下列內容相似:
< Trying 10.0.0.1:80... < Connected to 10.0.0.1 (10.0.0.1) port 80 (#0) < GET / HTTP/1.1 < Host: example.com < User-Agent: curl/7.82.0-DEV < Accept: */* < < Mark bundle as not supporting multiuse < HTTP/1.1 200 OK < content-type: application/json < content-length: 318 < access-control-allow-origin: * < server: envoy < date: Tue, 12 Apr 2022 22:30:13 GMT < { "cluster_name": "gke-1", "zone": "us-west1-a", "host_header": "example.com", ... }