Envoy サイドカー サービス メッシュを設定する
この構成はプレビュー版をご利用のお客様に対してサポートされていますが、新規の Cloud Service Mesh ユーザーにはおすすめしません。詳細については、Cloud Service Mesh の概要をご覧ください。
このガイドでは、フリートでシンプルなサービス メッシュを構成する方法について説明します。このガイドでは、次の手順について説明します。
- Envoy サイドカー インジェクタをクラスタにデプロイする。インジェクタは、Envoy プロキシ コンテナをアプリケーション Pod に挿入します。
- 名前空間
store
内のサンプル サービスにリクエストをルーティングするように、サービス メッシュ内に Envoy サイドカーを構成する Gateway API リソースをデプロイする。 - シンプルなクライアントをデプロイして、デプロイの結果を確認する。
次の図は、構成されたサービス メッシュを示しています。
サイドカー インジェクタ構成のメッシュ名と Mesh
リソースの名前は同じであるため、クラスタに構成できる Mesh
は 1 つのみです。
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
サイドカー インジェクタをカスタマイズする必要がある場合は、次の手順に沿って、クラスタに合わせてサイドカー インジェクタをカスタマイズします。
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
マニフェストをmesh.yaml
ファイルに保存します。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
次の
HTTPRoute
マニフェストをstore-route.yaml
ファイルに保存します。このマニフェストでは、ホスト名example.com
が指定された HTTP トラフィックを、名前空間store
内の Kubernetes Servicestore
に転送するHTTPRoute
リソースを定義します。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
Deployment を検証する
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", ... }