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

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

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

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

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

カナリア デプロイ

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

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

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

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

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

費用

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

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

始める前に

  1. Google Cloud アカウントにログインします。Google Cloud を初めて使用する場合は、アカウントを作成して、実際のシナリオでの Google プロダクトのパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $300 分を差し上げます。
  2. Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。

    プロジェクト セレクタに移動

  3. Google Cloud プロジェクトで課金が有効になっていることを確認します

  4. 必要な API を有効にします。

    API を有効にする

  5. Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。

    プロジェクト セレクタに移動

  6. Google Cloud プロジェクトで課金が有効になっていることを確認します

  7. 必要な API を有効にします。

    API を有効にする

Cloud Shell を起動する

このチュートリアルでは、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 ゲートウェイの Deployment を介した公開 Ingress のみを許可する必要があります。frontend-external サービス リソースを削除します。

    kubectl \
      --namespace=onlineboutique \
      --context=cluster-with-istio \
      delete service frontend-external
    
  4. Istio Gateway リソースと Istio 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 サンプルアプリが表示されます。

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

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

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

  1. プロジェクト番号を環境変数に保存します。

    export PROJECT_NUMBER=$(gcloud projects \
      describe PROJECT_ID --format='get(projectNumber)')
    
  2. 新しいクラスタを作成します。

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

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

    kubectl config get-contexts --output="name"
    
  5. プロジェクトのフリートで Anthos Service Mesh を有効にします。フリートは、一緒に管理できる Kubernetes クラスタと他のリソースの論理グループです。

    gcloud container fleet mesh enable --project PROJECT_ID
    

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

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

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

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

    Waiting for membership to be created...done.
    Created a new membership [projects/your-project-id/locations/global/memberships/cluster-with-gke-membership] for the cluster [cluster-with-gke-membership]
    Generating the Connect Agent manifest...
    Deploying the Connect Agent on cluster [cluster-with-gke-membership] in namespace [gke-connect]...
    Deployed the Connect Agent on cluster [cluster-with-gke-membership] in namespace [gke-connect].
    Finished registering the cluster [cluster-with-gke-membership] with the Fleet.
    
  7. クラスタでマネージド Anthos Service Mesh を有効にします。

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

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

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

    gcloud container fleet mesh describe --project PROJECT_ID
    

    Anthos 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-managed-asm-membership:
        mesh:
          management: MANAGEMENT_AUTOMATIC
    membershipStates:
      projects/123456789123/locations/global/memberships/cluster-with-managed-asm-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'
    

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

  1. Anthos Service Mesh の Ingress ゲートウェイを asm-ingress という別の名前空間にデプロイします。Namespace を作成します。

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

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

    kubectl \
      --context cluster-with-managed-asm \
      --namespace=asm-ingress \
      apply -f anthos-service-mesh-samples/docs/shared/asm-ingress-gateway/asm-gateway-deployment-svc.yaml
    kubectl \
      --context cluster-with-managed-asm \
      --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-managed-asm \
      create namespace onlineboutique
    
  2. istio.io/rev=asm-managed ラベルを使用して、サービス メッシュに onlineboutique 名前空間を追加し、自動サイドカー プロキシ インジェクションを有効にします。

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

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

    kubectl \
      --context cluster-with-managed-asm \
      --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-managed-asm \
        --namespace asm-ingress \
        get service --output jsonpath='{.items[0].status.loadBalancer.ingress[0].ip}' \
      )
    

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

このセクションでは、Online Boutique へのユーザー トラフィックの 50% がマネージド Anthos Service Mesh を使用するクラスタの Online Boutique のインスタンスにシフトするように、Istio を使用するクラスタを構成します。これを行うには、2 つの Istio リソースを Istio を使用するクラスタにデプロイします。

  • ServiceEntry。マネージド Anthos Service Mesh クラスタの Online Boutique エンドポイントの情報を Istio に提供します。
  • VirtualService。トラフィックを 50 対 50 に分割するように Istio Ingress ゲートウェイに指示します。
  1. ServiceEntry リソース内で、マネージド Anthos 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% が、マネージド Anthos Service Mesh を使用するクラスタの Pod によって処理されます。

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

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

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

  2. Online Boutique にアクセスするには、ウェブブラウザでドメイン名にアクセスします。

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

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

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

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

クリーンアップ

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

プロジェクトを削除する

  1. Google Cloud コンソールで、[リソースの管理] ページに移動します。

    [リソースの管理] に移動

  2. プロジェクト リストで、削除するプロジェクトを選択し、[削除] をクリックします。
  3. ダイアログでプロジェクト ID を入力し、[シャットダウン] をクリックしてプロジェクトを削除します。

リソースを削除する

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

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

次のステップ