新しいクラスタでクラスタ内をマネージドに移行する

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

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

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

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

カナリア デプロイ

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

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

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

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

クラスタ内 Anthos Service Mesh を使用したクラスタを設定する

クラスタを作成してクラスタ内 Anthos Service Mesh をインストールする

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

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

    gcloud container clusters create cluster-with-in-cluster-asm \
      --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-in-cluster-asm \
      cluster-with-in-cluster-asm
    
  3. クラスタのコンテキスト名が変更されていることを確認します。

    kubectl config get-contexts --output="name"
    
  4. Anthos Service Mesh 1.18.7 をインストールするバージョンを現在の作業ディレクトリにダウンロードします。

    curl https://storage.googleapis.com/csm-artifacts/asm/asmcli_1.18 > asmcli
    

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

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

    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed
    100  167k  100  167k    0     0   701k      0 --:--:-- --:--:-- --:--:--  701k
    
  5. asmcli スクリプトを実行可能にします。

    chmod +x asmcli
    
  6. asmcli を使用してクラスタ内 Anthos Service Mesh をインストールします。

    ./asmcli install \
      --project_id PROJECT_ID \
      --cluster_name cluster-with-in-cluster-asm \
      --cluster_location us-central1-a \
      --output_dir . \
      --enable_all \
      --ca mesh_ca
    

    asmcli ツールが完了するまで、数分かかる場合があります。このツールは、進捗状況がわかるように情報メッセージを出力します。

    成功すると、出力は次のようになります。

    ...
    asmcli: Successfully installed ASM.
    

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

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

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

    kubectl \
      --context cluster-with-in-cluster-asm \
      label --overwrite namespace asm-ingress istio.io/rev=asm-1187-26
    

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

    namespace/asm-ingress labeled
    
  3. Anthos Service Mesh の Ingress ゲートウェイをデプロイします。

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

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

    serviceaccount/asm-ingressgateway created
    service/asm-ingressgateway created
    deployment.apps/asm-ingressgateway created
    gateway.networking.istio.io/asm-ingressgateway created
    

Online Boutique をデプロイする

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

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

    kubectl \
      --context cluster-with-in-cluster-asm \
      label --overwrite namespace onlineboutique istio.io/rev=asm-1187-26
    

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

    namespace/onlineboutique labeled
    
  3. ユーザー トラフィックを模倣する負荷生成ツールなど、Online Boutique の 12 のサービスをデプロイします。

    kubectl \
      --context cluster-with-in-cluster-asm \
      --namespace=onlineboutique \
      apply -f anthos-service-mesh-samples/docs/shared/online-boutique/kubernetes-manifests.yaml
    kubectl \
      --context cluster-with-in-cluster-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-in-cluster-asm \
      --namespace asm-ingress \
      get service --output jsonpath='{.items[0].status.loadBalancer.ingress[0].ip}'
    
  5. Ingress ゲートウェイのパブリック IP アドレスをコピーし、ウェブブラウザからアクセスします。Online Boutique サンプルアプリが表示されます。

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

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

このセクションでは、移行先のクラスタを作成します。クラスタ内 Anthos Service Mesh を使用するクラスタから 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 のインスタンスにシフトするように、クラスタ内 Anthos Service Mesh を使用するクラスタを構成します。これを行うには、クラスタ内 Anthos Service Mesh を使用するクラスタに次の 2 つの Istio リソースをデプロイします。

  • マネージド Anthos Service Mesh クラスタの Online Boutique エンドポイントの情報をクラスタ内 Anthos Service Mesh に提供する ServiceEntry
  • トラフィックを半々に分割するようにクラスタ内 Anthos Service Mesh の Ingress ゲートウェイに指示する VirtualService。
  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. クラスタ内 Anthos Service Mesh を使用するクラスタに ServiceEntry をデプロイします。

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

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

    kubectl \
      --context cluster-with-in-cluster-asm \
      --namespace asm-ingress \
      get service
    

    Online Boutique のホームページを何度も更新し、毎回ページのフッターを確認します。リクエストの 50% が、マネージド Anthos Service Mesh を使用するクラスタの Pod によって処理されます。

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

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

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

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

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

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

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

    gcloud container clusters delete cluster-with-in-cluster-asm \
      --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.

リソースを削除する

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

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

次のステップ