Istio 1.11 以降から Cloud Service Mesh に移行する

このチュートリアルでは、Istio を使用する Google Kubernetes Engine(GKE)クラスタから、マネージド Cloud Service Mesh(Istio に準拠した、Google のフルマネージド サービス メッシュ)を使用する新しいクラスタにアプリケーションを移行する方法を説明します。

このチュートリアルの内容は次のとおりです。

  1. 新しい Google Kubernetes Engine クラスタを作成し、クラスタに Istio と Istio Ingress ゲートウェイをインストールします。このクラスタは既存の移行元クラスタとして機能します。
  2. Istio をインストールしたクラスタに、Online Boutique サンプル アプリケーションをデプロイします。
  3. 同じ Google Cloud プロジェクトで、Google Kubernetes Engine クラスタをもう 1 つ作成します。
  4. 2 番目のクラスタでマネージド Cloud Service Mesh を有効にして、Cloud Service Mesh の Ingress ゲートウェイをデプロイします。
  5. Cloud Service Mesh を使用するクラスタに Online Boutique をデプロイすることで、Istio を使用するクラスタでのデプロイを複製します。
  6. Istio をインストールしたクラスタ上で Istio のトラフィック分割機能を使用し、ユーザー トラフィックの 50% をそのクラスタから Cloud Service Mesh を使用するクラスタにシフトします。
  7. Istio が存在するクラスタのドメイン ネーム システム(DNS)エントリが Cloud Service Mesh が存在するクラスタをポイントするように設定することで、Istio から Cloud Service Mesh への移行を完了します。

ユーザー トラフィックは、Istio のクラスタと Cloud Service Mesh のクラスタの間で半々に分割されます。各クラスタには、Online Boutique が独自にデプロイされています。

カナリア デプロイ

カナリア デプロイとは、ソフトウェア開発手法の一つで、新しいバージョンをすべてのユーザーにリリースする前に一部のソフトウェアの新しいバージョンをテストします。この手法では、新しいバージョンに送信されるトラフィックの割合を段階的に増やしていきます。このチュートリアルでは、マネージド Cloud Service Mesh を使用する新しいクラスタを設定し、ユーザー トラフィックをそのクラスタに段階的にシフトします。最初の段階ではユーザー トラフィックを新しいクラスタに転送せず、その後、トラフィックの 50% を転送し、最後にすべてのトラフィックを転送します。本番環境では、1 回の増分量を小さくし、増分の回数を増やす必要があります。新しいクラスタで所定の割合のトラフィックが処理されていないことに気づいたら、その時点でパーセンテージを 0% に下げてロールバックできます。

カナリア コントロール プレーンとカナリア クラスタ

一般に、Istio からマネージド Cloud Service Mesh に移行する場合、次の 2 つの方法があります。

  • カナリア コントロール プレーンによる移行: この方法では、Istio がインストールされているのと同じクラスタでマネージド Cloud Service Mesh をプロビジョニングします。
  • カナリア クラスタによる移行: この方法では、新しいクラスタを作成し、そのクラスタでマネージド Cloud Service Mesh をプロビジョニングします。

このチュートリアルでは、カナリア クラスタによる移行について説明します。

費用

このチュートリアルでは、課金対象である次の Google Cloudコンポーネントを使用します。

このチュートリアルの終了後に作成したリソースを削除すれば、それ以上の請求は発生しません。詳細については、クリーンアップをご覧ください。

始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the required APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the required APIs.

    Enable the APIs

  8. Cloud Shell を起動する

    このチュートリアルでは Cloud Shell を使用します。これは Google Cloud でホストされているシェル環境であり、Google Cloud リソースの管理に使用できます。

    Cloud Shell には、Google Cloud CLIkubectlistioctl のコマンドライン ツールがプリインストールされています。gcloud CLI は、 Google Cloud用の主要な CLI です。

    Cloud Shell セッションを開くには、このページの右上にある をクリックして、[確認] をクリックします。ページの下部にあるフレーム内で Cloud Shell セッションが開きます。Cloud Shell セッションで次のコマンドを実行します。

    サンプルコードをダウンロードする

    使用する Kubernetes リソースと Istio リソースを含む git リポジトリのクローンを作成します。

      git clone https://github.com/GoogleCloudPlatform/anthos-service-mesh-samples.git
      git clone https://github.com/GoogleCloudPlatform/microservices-demo.git
    

    Istio を使用するクラスタを設定する

    クラスタを作成して Istio をインストールする

    このセクションでは、Istio を使用するクラスタを作成します。実際には、すでに使用しているクラスタになります。

    1. PROJECT_IDプロジェクト ID に置き換え、新しいクラスタを作成します。

      gcloud container clusters create cluster-with-istio \
        --project=PROJECT_ID \
        --zone=us-central1-a \
        --machine-type=e2-standard-2 --num-nodes=3
      
    2. クラスタが操作しやすいように、クラスタのコンテキスト名を変更します。

      kubectl config rename-context \
        gke_PROJECT_ID_us-central1-a_cluster-with-istio \
        cluster-with-istio
      
    3. クラスタのコンテキスト名が変更されていることを確認します。

      kubectl config get-contexts --output="name"
      
    4. クラスタに Istio をインストールします。説明を簡単にするために、Istio のデフォルトのプロファイルをインストールします。バージョンは、istioctl のインストールと同じものにします。

      istioctl install
      

      「y」と入力して Enter キーを押します。

      出力は次のようになります。

      This will install the Istio X.Y.Z default profile with ["Istio core" "Istiod" "Ingress gateways"] components into the cluster. Proceed? (y/N)
      ✔ Istio core installed
      ✔ Istiod installed
      ✔ Ingress gateways installed
      ✔ Installation complete
      Making this installation the default for injection and validation.
      

    Online Boutique をデプロイする

    1. Online Boutique を onlineboutique という別の名前空間にデプロイします。名前空間を作成します。

      kubectl \
        --context cluster-with-istio \
        create namespace onlineboutique
      
    2. Online Boutique の 12 個のサービスをデプロイします。この中には、ユーザー トラフィックを模倣する負荷生成ツールも含まれています。

      kubectl \
        --namespace=onlineboutique \
        --context=cluster-with-istio \
        apply -f microservices-demo/release/kubernetes-manifests.yaml
      
    3. 前のステップでは frontend-external というサービス(LoadBalancer タイプ)もデプロイされ、外部 IP アドレスが割り振られます。しかし、今回の目的は Istio Ingress ゲートウェイのデプロイによってパブリック Ingress を許可することだけなので、frontend-external サービス リソースを削除します。

      kubectl \
        --namespace=onlineboutique \
        --context=cluster-with-istio \
        delete service frontend-external
      
    4. Istio の Gateway リソースと VirtualService リソースをデプロイして、パブリック トラフィックが Online Boutique にアクセスできるようにします。

      kubectl \
        --namespace=onlineboutique \
        --context=cluster-with-istio \
        apply -f microservices-demo/istio-manifests/frontend-gateway.yaml
      
    5. Istio Ingress ゲートウェイの外部 IP アドレスを取得します。

      kubectl \
        --namespace istio-system \
        --context=cluster-with-istio \
        get service --output jsonpath='{.items[0].status.loadBalancer.ingress[0].ip}'
      
    6. istio-ingressgateway Service の外部 IP アドレスをコピーして、ウェブブラウザからアクセスします。Online Boutique サンプルアプリが表示されます。

    マネージド Cloud Service Mesh を使用する新しいクラスタを設定する

    クラスタを作成してマネージド Cloud Service Mesh をプロビジョニングする

    このセクションでは、移行先のクラスタを作成します。マネージド Cloud Service Mesh をプロビジョニングして Online Boutique をデプロイすることで、Istio を使用するクラスタでのデプロイを複製します。

    1. 新しいクラスタを作成します。

      gcloud container clusters create cluster-with-csm \
        --project=PROJECT_ID --zone=us-central1-a \
        --machine-type=e2-standard-4 --num-nodes=2 \
        --workload-pool PROJECT_ID.svc.id.goog
      
    2. クラスタが操作しやすいように、クラスタのコンテキスト名を変更します。

      kubectl config rename-context \
        gke_PROJECT_ID_us-central1-a_cluster-with-csm \
        cluster-with-csm
      
    3. クラスタのコンテキスト名が変更されたことを確認します。

      kubectl config get-contexts --output="name"
      
    4. プロジェクトのフリートで Cloud Service Mesh を有効にします。フリートとは、一緒に管理できるように Kubernetes クラスタと他のリソースを論理的にまとめたものです。

      gcloud container fleet mesh enable --project PROJECT_ID
      

      出力は次のようになります。

      Waiting for Feature Service Mesh to be created...done.
      
    5. プロジェクトのフリートにクラスタを登録します。

      gcloud container fleet memberships register cluster-with-csm-membership \
        --gke-cluster=us-central1-a/cluster-with-csm \
        --enable-workload-identity \
        --project PROJECT_ID
      

      出力は次のようになります。

      Waiting for membership to be created...done.
      Finished registering to the Fleet.
      
    6. クラスタでマネージド Cloud Service Mesh を有効にします。

      gcloud container fleet mesh update \
        --management automatic \
        --memberships cluster-with-csm-membership \
        --project PROJECT_ID
      

      出力は次のようになります。

      Waiting for Feature Service Mesh to be updated...done.
      
    7. クラスタでマネージド Cloud Service Mesh がプロビジョニングされており、使用可能な状態であることを確認します。

      gcloud container fleet mesh describe --project PROJECT_ID
      

      Cloud Service Mesh がプロビジョニングされ、クラスタで使用可能な状態になるまでに 10 分ほどかかることがあります。controlPlaneManagement.state: DISABLED または controlPlaneManagement.state: PROVISIONING が表示された場合は、controlPlaneManagement.state: ACTIVE が表示されるまで、数分ごとに上のコマンドを再実行する必要があります。

      出力は次のようになります。

      createTime: '2022-07-06T01:05:39.110120474Z'
      membershipSpecs:
        projects/123456789123/locations/global/memberships/cluster-with-csm-membership:
          mesh:
            management: MANAGEMENT_AUTOMATIC
      membershipStates:
        projects/123456789123/locations/global/memberships/cluster-with-csm-membership:
          servicemesh:
            controlPlaneManagement:
              details:
              - code: REVISION_READY
                details: 'Ready: asm-managed'
              state: ACTIVE
            dataPlaneManagement:
              details:
              - code: OK
                details: Service is running.
              state: ACTIVE
          state:
            code: OK
            description: 'Revision(s) ready for use: asm-managed.'
            updateTime: '2022-07-06T01:19:24.243993678Z'
      name: projects/your-project-id/locations/global/features/servicemesh
      resourceState:
        state: ACTIVE
      spec: {}
      state:
        state: {}
      updateTime: '2022-07-06T01:19:27.475885687Z'
      

    Cloud Service Mesh の Ingress ゲートウェイをデプロイする

    1. ここでは、Cloud Service Mesh の Ingress ゲートウェイを asm-ingress という別の Namespace にデプロイします。まず、この Namespace を作成します。

      kubectl \
        --context cluster-with-csm \
        create namespace asm-ingress
      
    2. istio.io/rev=asm-managed ラベルを使用して asm-ingress Namespace をサービス メッシュに追加し、自動サイドカー プロキシ インジェクションを有効にします。

      kubectl \
        --context cluster-with-csm \
        label namespace asm-ingress 'istio.io/rev=asm-managed'
      
    3. Cloud Service Mesh の Ingress ゲートウェイをデプロイします。

      kubectl \
        --context cluster-with-csm \
        --namespace=asm-ingress \
        apply -f anthos-service-mesh-samples/docs/shared/asm-ingress-gateway/asm-gateway-deployment-svc.yaml
      kubectl \
        --context cluster-with-csm \
        --namespace=asm-ingress \
        apply -f anthos-service-mesh-samples/docs/shared/asm-ingress-gateway/gateway.yaml
      

      出力は次のようになります。

      namespace/asm-ingress configured
      serviceaccount/asm-ingressgateway configured
      service/asm-ingressgateway configured
      deployment.apps/asm-ingressgateway configured
      gateway.networking.istio.io/asm-ingressgateway configured
      

    Online Boutique をデプロイする

    1. Online Boutique を onlineboutique という別の名前空間にデプロイします。Namespace を作成します。

      kubectl \
        --context cluster-with-csm \
        create namespace onlineboutique
      
    2. istio.io/rev=asm-managed ラベルを使用して、サービス メッシュに onlineboutique 名前空間を追加し、自動サイドカー プロキシ インジェクションを有効にします。

      kubectl \
        --context cluster-with-csm \
        label namespace onlineboutique 'istio.io/rev=asm-managed'
      
    3. ユーザー トラフィックを模倣する負荷生成ツールなど、Online Boutique の 12 のサービスをデプロイします。

      kubectl \
        --context cluster-with-csm \
        --namespace=onlineboutique \
        apply -f anthos-service-mesh-samples/docs/shared/online-boutique/kubernetes-manifests.yaml
      kubectl \
        --context cluster-with-csm \
        --namespace=onlineboutique \
        apply -f anthos-service-mesh-samples/docs/shared/online-boutique/virtual-service.yaml
      
    4. Cloud Service Mesh の Ingress ゲートウェイの外部 IP アドレスを取得します。

      kubectl \
        --context cluster-with-csm \
        --namespace asm-ingress \
        get service --output jsonpath='{.items[0].status.loadBalancer.ingress[0].ip}'
      
    5. asm-ingressgateway Service の外部 IP アドレスをコピーして、ウェブブラウザからアクセスします。Online Boutique のサンプルアプリが表示されます。次のセクションでこの外部 IP アドレスを使用するため、環境変数にコピーします。

      export INGRESS_IP_OF_CLUSTER_WITH_MANAGED_ASM=$( \
        kubectl \
          --context cluster-with-csm \
          --namespace asm-ingress \
          get service --output jsonpath='{.items[0].status.loadBalancer.ingress[0].ip}' \
        )
      

    Cloud Service Mesh を使用するクラスタをカナリア デプロイでテストする

    このセクションでは、Istio を使用するクラスタの構成を変更して、Online Boutique へのユーザー トラフィックの 50% を、マネージド Cloud Service Mesh を使用するクラスタ上の Online Boutique のインスタンスにシフトさせます。そのために、Istio を使用するクラスタに次の 2 つの Istio リソースをデプロイします。

    • ServiceEntry。マネージド Cloud Service Mesh クラスタの Online Boutique エンドポイントに関する情報を Istio に提供します。
    • VirtualService。トラフィックを 50 対 50 に分割するように Istio Ingress ゲートウェイに指示します。
    1. ServiceEntry リソース内で、マネージド Cloud Service Mesh クラスタの Ingress ゲートウェイの IP アドレスを設定します。

      sed -i "s/1.2.3.4/${INGRESS_IP_OF_CLUSTER_WITH_MANAGED_ASM}/" anthos-service-mesh-samples/docs/migrate-to-managed-asm/service-entry.yaml
      
    2. Istio を使用するクラスタに ServiceEntry をデプロイします。

      kubectl \
        --context cluster-with-istio \
        --namespace onlineboutique \
        apply -f anthos-service-mesh-samples/docs/migrate-to-managed-asm/service-entry.yaml
      
    3. Istio を使用するクラスタに VirtualService をデプロイします。

      kubectl \
        --context cluster-with-istio \
        --namespace onlineboutique \
        apply -f anthos-service-mesh-samples/docs/migrate-to-managed-asm/virtual-service.yaml
      
    4. ウェブブラウザで、Istio を使用するクラスタの Ingress ゲートウェイの IP アドレスにアクセスします。

      kubectl \
        --context cluster-with-istio \
        --namespace istio-system \
        get service
      

      Online Boutique のホームページを繰り返し更新し、そのたびにページのフッターを確認します。リクエストの 50% が、マネージド Cloud Service Mesh を使用するクラスタの Pod によって処理されるようになります。

    マネージド Cloud Service Mesh を使用するクラスタに移行する

    このセクションでは、ドメイン名を所有していて、その DNS(ドメイン ネームサーバー)設定にアクセスできることを前提としています。

    1. DNS 設定に A レコードを追加して、ドメイン名(example.com など)が Istio のあるクラスタ上で実行されている Ingress ゲートウェイの IP アドレスを指すようにします。

    2. ウェブブラウザにドメイン名を入力して Online Boutique にアクセスします。

    3. ロールバックが必要になった場合に DNS エントリをすばやく復元できるよう、DNS レコードの有効期間(TTL)を最小の値にします。

    4. ドメイン名の A レコードを、マネージド Cloud Service Mesh を使用するクラスタの Ingress ゲートウェイの外部 IP アドレスに設定します。

    5. 移行が正常に完了したら、Istio を使用するクラスタを削除します。

      gcloud container clusters delete cluster-with-istio \
        --zone=us-central1-a \
        --project=PROJECT_ID
      

    クリーンアップ

    このチュートリアルで使用したリソースについて Google Cloud アカウントに課金されることのないよう、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。

    プロジェクトを削除する

    1. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.

    リソースを削除する

    マネージド Cloud Service Mesh を使用するクラスタを削除します。

      gcloud container clusters delete cluster-with-managed-asm \
        --zone=us-central1-a \
        --project=PROJECT_ID
    

    次のステップ