Cloud Service Mesh ワークロードから Compute Engine VM にトラフィックを転送する
このページでは、GKE 上の Cloud Service Mesh ワークロードから、BackendService をフロントエンドとする Compute Engine VM にネットワーク トラフィックを安全に転送する方法について説明します。
GKE から Compute Engine VM にトラフィックを転送する場合、Compute Engine VM または BackendService が Cloud Service Mesh に参加する必要はありません。ただし、Compute Engine VM と BackendService は、Cloud Service Mesh GKE クラスタと同じプロジェクトに存在する必要があります。この制限は、この機能が公開プレビュー版である間存在します。MTLS は Compute Engine VM ではサポートされていません
始める前に
以降のセクションでは、次の作業が完了していることを前提としています。
また、次のコマンドを実行して、BackendService をフロントエンドとするサンプルの Compute Engine VM をデプロイすることもできます。
サンプルの Compute Engine VM と BackendService をデプロイします。
gcloud compute instance-templates create td-httpd-vm-template \ --scopes=https://www.googleapis.com/auth/cloud-platform \ --tags=http-td-server \ --image-family=debian-11 \ --image-project=debian-cloud \ --metadata=startup-script="#! /bin/bash sudo apt-get update -y sudo apt-get install apache2 -y sudo service apache2 restart echo '<!doctype <html><body><h1>'\`$(/bin/hostname)\`'</h1></body></html>' | sudo tee /var/www/html/index.html" gcloud compute instance-groups managed create http-td-mig-us-east1 \ --zone=VM_ZONE \ --size=2 \ --template=td-httpd-vm-template gcloud compute health-checks create http http-helloworld-health-check gcloud compute firewall-rules create http-vm-allow-health-checks \ --network=default \ --action=ALLOW \ --direction=INGRESS \ --source-ranges=0.0.0.0/0 \ --target-tags=http-td-server \ --rules=tcp:80 gcloud compute backend-services create helloworld \ --global \ --load-balancing-scheme=INTERNAL_SELF_MANAGED \ --protocol=HTTP \ --health-checks http-helloworld-health-check gcloud compute backend-services add-backend helloworld \ --instance-group=http-td-mig-us-east1 \ --instance-group-zone=VM_ZONE \ --global
ここで
- VM_ZONE は、Compute Engine VM をデプロイするゾーンです。
Compute Engine VM を GCPBackend として構成する
このセクションでは、GCPBackend を使用して Compute Engine VM を GKE ワークロードに公開します。GCPBackend は次の要素で構成されています。
- フロントエンド情報 - 具体的には、GKE Workloads がこの GCPBackend の呼び出しに使用するホスト名とポート。
- バックエンド情報 - サービス名、ロケーション、プロジェクト番号などの BackendService の詳細。
GCPBackend には、ホスト名とポートの詳細情報、BackendService の詳細(サービス名、ロケーション、プロジェクト番号)が含まれます。GKE ワークロードは、HTTP リクエストで GCPBackend ホスト名とポートを使用して Compute Engine VM にアクセスする必要があります。
ホスト名 DNS をクラスタ内で解決可能にするには(デフォルトでは解決できません)、選択したホスト名の下のすべてのホストを任意の IP アドレスに解決するように Google Cloud DNS を構成する必要があります。この DNS エントリを構成するまで、リクエストは失敗します。 Google Cloud DNS 構成は、カスタムドメインごとに 1 回だけ設定します。
マネージド ゾーンを作成する
gcloud dns managed-zones create prod \ --description="zone for gcpbackend" \ --dns-name=gcpbackend \ --visibility=private \ --networks=default
この例では、DNS 名は gcpbackend、VPC ネットワークは default です。
ドメインを解決できるようにレコードを設定します。
gcloud beta dns record-sets create *.gcpbackend \ --ttl=3600 --type=A --zone=prod \ --rrdatas=10.0.0.1
以前のドメインのホスト名を使用して GCPBackend を作成します。
cat <<EOF > gcp-backend.yaml apiVersion: networking.gke.io/v1 kind: GCPBackend metadata: name: vm-gcp-backend namespace: NAMESPACE spec: type: "BackendService" hostname: hello-world.gcpbackend backendservice: name: helloworld location: global EOF kubectl apply -f gcp-backend.yaml
この例では、GCP_BACKEND_NAME は
vm-gcp-backend
です。テスト Pod を作成して、GKE と Compute Engine VM 間の接続を確認します。
cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: testcurl namespace: default spec: containers: - name: curl image: curlimages/curl command: ["sleep", "3000"] EOF kubectl exec testcurl -c curl -- curl http://hello-world.gcpbackend:80
これで、GKE ワークロードは hello-world.gcpbackend:80 に HTTP リクエストを送信して Compute Engine VM にアクセスできるようになりました。
既存の Kubernetes サービスや Istio Service エントリと競合しないように、GCPBackend には一意の名前を使用する必要があります。競合する場合、優先順位は(高いものから低いものへ)Kubernetes Service、istio ServiceEntry、GCPBackend です。
Virtual Service と GCPBackend は同じ名前空間に存在する必要があり、Compute Engine VM は Cloud Service Mesh GKE クラスタと同じプロジェクトに存在する必要があります。