从 Istio 1.7 或更高版本迁移到 Anthos Service Mesh 和 Mesh CA

本文档介绍 Google Kubernetes Engine (GKE) 管理员可以如何安装 Anthos Service Mesh 并迁移当前使用 Istio 服务网格运行的工作负载。已部署的 Anthos Service Mesh 配置包括用于遥测的 Cloud Monitoring 和用于托管式高可用性网格证书管理的 Anthos Service Mesh 证书授权机构 (Mesh CA)。迁移过程中会保留定义网格拓扑的网关、虚拟服务和其他网格配置会在。

此过程涵盖单集群安装。对于多集群网格安装,请参阅在 GKE 上设置多集群网格,其中包括有关如何将集群添加到 Anthos Service Mesh 的步骤安装后。

如需完成本文档中的步骤,您必须将 Istio 1.7 或更高版本与 GKE 集群搭配使用。Anthos Service Mesh 不支持使用 Helm 进行安装或配置。我们建议网格管理员使用 IstioOperator API 进行网格配置。此过程可能会在切换证书授权机构时导致应用停机,因此我们建议您在计划维护窗口执行此过程。

Anthos Service Mesh 使用相同的 Istio 和 Envoy API 配置网格,因此不需要对现有资源进行更改。

迁移后在实现方面存在一些差异,如下所示:

  • Istio 控制平面会被 Anthos Service Mesh 控制平面取代。

  • Citadel 证书授权机构会被移除,并且证书由 Google Cloud Mesh CA 服务管理。

  • 遥测将发送到 Cloud Logging 和 Cloud Monitoring。Google Cloud 控制台中提供了信息中心和 SLO 管理界面。

  • 如果您有自定义的 IstioOperator 资源,脚本可以将其作为输入。

  • 您的开源 Istio 安装(1.7 或更高版本)将使用 Mesh CA 迁移到 Anthos Service Mesh 1.10 版。如果您使用的是其他版本的 Istio 或需要不同版本的 Anthos Service Mesh,或者想使用 Google 管理的控制平面部署 Anthos Service Mesh,请参阅 准备从 Istio 迁移

前提条件

您必须满足以下前提条件才能完成本指南:

  • 您拥有一个安装了 Istio 1.7 或更高版本的 GKE 集群。如果您没有 GKE 集群,或者您希望首先在新的(测试)集群上测试本指南,请按照附录中的步骤创建:使用测试应用部署的 Istio 1.7 版或更高版本的新 GKE 集群。

  • 使用 Cloud Shell 执行本指南中的步骤,因为本指南在 Cloud Shell 上进行了测试。

目标

在本指南中,您将选择迁移路径。您可以选择单步脚本路径或分步脚本迁移。

如需了解详情,请参阅选择迁移路径

如需获取有关此迁移的常见问题解答,请参阅从 Istio 1.7 或更高版本迁移到 Anthos Service Mesh 和 Mesh CA 常见问题解答

准备工作

在本指南中,您需要拥有安装 Istio 的 GKE 集群的管理员权限。要观察您的应用在迁移过程中的行为,我们建议您先在开发或预演环境中使用集群执行此过程。

Anthos Service Mesh 具有以下要求。您可以自行手动执行安装,也可以允许提供的工具在安装过程中代表您启用依赖项。

  1. 启用以下 Google Cloud API:

    • container.googleapis.com
    • meshca.googleapis.com
    • meshconfig.googleapis.com
    • gkehub.googleapis.com
    • stackdriver.googleapis.com
  2. 为 GKE 集群启用 Workload Identity 和 Stackdriver。

  3. 为您的集群添加标签以启用 Service 界面。

  4. 获取 Kubernetes 集群的集群管理员权限。

  5. 向舰队注册集群

  6. 在队列上启用 servicemesh 功能。

设置环境

如需设置您的环境,请按以下步骤操作:

  1. 在 Google Cloud 控制台中,激活 Cloud Shell。

    激活 Cloud Shell

    Cloud Shell 会话随即会在 Google Cloud 控制台的底部启动,并显示命令行提示符。Cloud Shell 是一个已安装 Google Cloud CLI 且已为当前项目设置值的 shell 环境。该会话可能需要几秒钟来完成初始化。

  2. 创建本指南中使用的环境变量:

    export PROJECT_ID=PROJECT_ID
    gcloud config set project ${PROJECT_ID}
    export PROJECT_NUM=$(gcloud projects describe ${PROJECT_ID} --format='value(projectNumber)')
    export CLUSTER_NAME=GKE_CLUSTER_NAME
    export CLUSTER_LOCATION=GKE_CLUSTER_REGION_OR_ZONE
    
  3. 创建 WORKDIR 文件夹:

    mkdir -p migrate-to-asm-working-dir && cd migrate-to-asm-working-dir && export WORKDIR=`pwd`
    
  4. 为本指南创建一个 KUBECONFIG 文件:

    touch asm-kubeconfig && export KUBECONFIG=`pwd`/asm-kubeconfig
    
  5. 连接到您的 GKE 集群:

    区域级集群

    gcloud container clusters get-credentials ${CLUSTER_NAME} \
       --zone ${CLUSTER_LOCATION}
    

    区域级集群

    gcloud container clusters get-credentials ${CLUSTER_NAME} \
       --region ${CLUSTER_LOCATION}
    
  6. 下载迁移脚本:

    curl -LO https://storage.googleapis.com/csm-artifacts/asm/migrate-to-asm
    chmod +x ./migrate-to-asm
    

选择迁移路径

您可以通过以下两种方式迁移到 Anthos Service Mesh。请先选择这两种策略中的一种,然后再继续阅读此部分:

  • 到 Anthos Service Mesh 的一步式迁移。顾名思义,您可以使用单个命令执行迁移到 Anthos Service Mesh 的所有必要步骤。如果您有许多集群,并且需要一种快速、简单的方式来将其升级到 Anthos Service Mesh,这可能会很有帮助。不过,此方法可能会导致应用停机。

  • 分步迁移到 Anthos Service Mesh。此方法可让您更好地控制每个步骤,并帮助您准确了解迁移到 Anthos Service Mesh 所需的内容。

到 Anthos Service Mesh 的一步式迁移

在本部分中,您需要将当前 Istio 1.7 版安装迁移到 Anthos Service Mesh 1.10 版。在本部分中,您可以通过运行单个步骤来执行迁移。如果要通过运行一系列步骤来执行迁移,请参阅迁移到 Anthos Service Mesh 的分步部分。

如需迁移到 Anthos Service Mesh,请运行以下命令。对于任何命令,您都可以使用 --dry-run 标志来输出命令,而不是运行命令,或者在脚本运行命令时使用 --verbose 标志来输出命令。如果您先前已配置依赖项(如准备工作部分所述),则可以省略 --enable-dependencies 标志。

无自定义资源

请勿使用自定义 IstioOperator 资源:

./migrate-to-asm migrate \
    --cluster_location $CLUSTER_LOCATION \
    --cluster-name $CLUSTER_NAME \
    --project-id $PROJECT_ID \
    --enable-dependencies \
    --verbose

使用自定义资源

使用自定义 IstioOperator 资源:

export ISTIO_OPERATOR_FILEPATH=PATH_OF_ISTIO_OPERATOR_YAML_FILE

./migrate-to-asm migrate \
    --cluster_location $CLUSTER_LOCATION \
    --cluster-name $CLUSTER_NAME \
    --project-id $PROJECT_ID \
    --enable-dependencies \
    --custom_overlay ${ISTIO_OPERATOR_FILEPATH} \
    --verbose

此命令会执行以下步骤:

  • 确保 Istio 版本为 1.7 或更高版本。
  • 在集群上启用 Workload Identity。Mesh CA 需要 Workload Identity。 您无需在现有节点池上启用 GKE 元数据服务器
  • 为 Anthos Service Mesh 启用所需的 API
  • 将集群注册到集群组。
  • 使用所需标签更新集群。
  • 评估 Google 管理的控制平面是否更适合指定的集群。
  • 使用最佳控制平面配置部署 Anthos Service Mesh。
  • 使用所需的 Anthos Service Mesh 标签重新标记所有已启用 Istio 的命名空间。
  • 在所有已启用 Anthos Service Mesh 的命名空间中重启工作负载,以便工作负载获得新的 Anthos Service Mesh 代理。
  • 移除 Istio 控制平面。

逐步迁移到 Anthos Service Mesh

在本部分中,您需要将 Istio 1.7 版安装迁移到 Anthos Service Mesh 1.10 版。在本部分中,您可以通过执行一系列步骤来执行迁移。如果您想要一步执行迁移,请参阅一步到 Anthos Service Mesh 的迁移部分。

迁移到 Anthos Service Mesh 需要执行以下步骤:

  1. 执行迁移前步骤,以验证并准备集群和环境以迁移到 Anthos Service Mesh。
  2. 将 Anthos Service Mesh 作为 Canary 控制平面与现有 Istio 控制平面一起安装,并准备工作负载。
  3. 在 Anthos Service Mesh 上测试工作负载,并重新标记命名空间以进行 Anthos Service Mesh Sidecar 注入。
  4. 访问并检查 Anthos Service Mesh 信息中心。
  5. 清理 Istio 工件或回滚到现有的 Istio 版本。

执行迁移前步骤

迁移前步骤会执行以下操作:

  • 它会验证项目和集群信息是否正确,以及安装的 Istio 版本与迁移是否兼容。

  • 它会备份默认网关的配置和当前 Istio 服务网格的标签。

  • 如果使用 --enable-dependencies 标志,它会代表您启用依赖项;否则,它会验证依赖项是否已启用。

迁移前脚本会在当前目录中创建一个名为 configuration_backup 的新文件夹(或覆盖现有文件夹)。

要执行迁移前的步骤,请运行以下命令:

依赖项

启用依赖项:

./migrate-to-asm pre-migrate \
    --cluster_location $CLUSTER_LOCATION \
    --cluster-name $CLUSTER_NAME \
    --project-id $PROJECT_ID \
    --enable-dependencies

无依赖项

请勿启用依赖项:

./migrate-to-asm pre-migrate \
    --cluster_location $CLUSTER_LOCATION \
    --cluster-name $CLUSTER_NAME \
    --project-id $PROJECT_ID

输出类似于以下内容:

    migrate-to-asm: Checking installation tool dependencies...
    migrate-to-asm: Checking for $PROJECT_ID...
    migrate-to-asm: Confirming cluster information for $PROJECT_ID/$LOCATION/$CLUSTER_NAME...
    migrate-to-asm: Confirming node pool requirements for $PROJECT_ID/$LOCATION/$CLUSTER_NAME...
    migrate-to-asm: Checking existing Istio version(s)...
    migrate-to-asm:   1.9.5
    migrate-to-asm: No version issues found.
    migrate-to-asm: Enabling required APIs...
    migrate-to-asm:
    migrate-to-asm: APIs enabled.
    migrate-to-asm: Enabling the service mesh feature...
    migrate-to-asm:
    migrate-to-asm: The service mesh feature is already enabled.
    migrate-to-asm: Enabling Stackdriver on $LOCATION/$CLUSTER_NAME...
    Updating $CLUSTER_NAME...
    .........................done.
    Updated [https://container.googleapis.com/v1/projects/$PROJECT_ID/zones/$LOCATION/clusters/$CLUSTER_NAME].
    To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/$LOCATION/$CLUSTER_NAME?project=$PROJECT_ID
    migrate-to-asm:
    migrate-to-asm: Stackdriver enabled.
    migrate-to-asm: Querying for core/account...
    migrate-to-asm: Binding user@example.com to cluster admin role...
    migrate-to-asm:
    migrate-to-asm:
    migrate-to-asm: Successfully bound to cluster admin role.
    migrate-to-asm: Initializing meshconfig API...
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100     3    0     3    0     0      6      0 --:--:-- --:--:-- --:--:--     6
    migrate-to-asm:
    migrate-to-asm: Finished pre-migration!

安装 Anthos Service Mesh 并准备工作负载

此步骤将执行以下操作:

  • 它会检查是否存在 configuration_backup 文件夹,如果不存在,则会中止以确保迁移前工具成功运行。
  • 它会根据集群和网格配置的分析来安装和配置 Anthos Service Mesh 控制平面。
  • 它会使用自定义 IstioOperator 资源(如果已提供)。如果您使用 IstioOperator 资源配置了自定义网关或多个网关,请在此步骤中使用同一资源。

如需跳过分析并强制工具安装使用集群资源运行的非代管式控制平面,请在命令中添加 --no-mcp 标志。

安装 Anthos Service Mesh 时,您可以从以下三种路径中选择一种:

  • 选项 1:没有自定义 IstioOperator 资源。您可以安装没有自定义资源的 Anthos Service Mesh。使用此选项将安装 Istio 的默认配置,并更新默认 istio-ingressgateway

  • 方法 2:具备--no-gateways选项。在安装没有自定义 IstioOperator 资源的 Anthos Service Mesh 时,您还可以使用 --no-gateways 选项不更新默认的 istio-ingressgateway。如果您使用此选项,则必须在安装后手动升级网关。

  • 选项 3:使用自定义 IstioOperator 资源。您可以使用自定义 IstioOperator 资源安装 Anthos Service Mesh。如果您使用自定义 IstioOperator 资源部署了 Istio,我们建议您在安装 Anthos Service Mesh 时使用相同的 IstioOperator 资源。

要安装 Anthos Service Mesh,请运行以下命令之一:

选项 1

升级默认的 istio-ingressgateway

 ./migrate-to-asm install-asm \
     --cluster_location $CLUSTER_LOCATION \
     --cluster-name $CLUSTER_NAME \
     --project-id $PROJECT_ID

选项 2

不升级默认的 istio-ingressgateway

 ./migrate-to-asm install-asm \
     --cluster_location $CLUSTER_LOCATION \
     --cluster-name $CLUSTER_NAME \
     --project-id $PROJECT_ID \
     --no-gateways

选项 3

使用自定义 IstioOperator 资源升级网关:

 export ISTIO_OPERATOR_FILEPATH=PATH_OF_ISTIO_OPERATOR_YAML_FILE

 ./migrate-to-asm install-asm \
     --cluster_location $CLUSTER_LOCATION \
     --cluster-name $CLUSTER_NAME \
     --project-id $PROJECT_ID \
     --custom-overlay ${ISTIO_OPERATOR_FILEPATH}

输出类似于以下内容:

 migrate-to-asm: Checking installation tool dependencies...
 migrate-to-asm: Checking for $PROJECT_ID...
 migrate-to-asm: Fetching/writing Google Cloud credentials to kubeconfig file...
 Fetching cluster endpoint and auth data.
 kubeconfig entry generated for $CLUSTER_NAME.
 migrate-to-asm:
 migrate-to-asm: Verifying connectivity (20s)...
 migrate-to-asm: kubeconfig set to $PROJECT_ID/$LOCATION/$CLUSTER_NAME...
 migrate-to-asm: Configuring kpt package...
 asm/
 set 20 field(s) of setter "gcloud.container.cluster" to value "$CLUSTER_NAME"
 asm/
 set 28 field(s) of setter "gcloud.core.project" to value "$PROJECT_ID"
 asm/
 set 2 field(s) of setter "gcloud.project.projectNumber" to value "42"
 asm/
 set 5 field(s) of setter "gcloud.project.environProjectNumber" to value "42"
 asm/
 set 20 field(s) of setter "gcloud.compute.location" to value "$LOCATION"
 asm/
 set 1 field(s) of setter "gcloud.compute.network" to value "$PROJECT_ID-default"
 asm/
 set 6 field(s) of setter "anthos.servicemesh.rev" to value "asm-1102-2"
 asm/
 set 5 field(s) of setter "anthos.servicemesh.tag" to value "1.10.2-asm.2"
 asm/
 set 4 field(s) of setter "anthos.servicemesh.hubTrustDomain" to value "$PROJECT_ID.svc.id.goog"
 asm/
 set 2 field(s) of setter "anthos.servicemesh.hub-idp-url" to value "https://container.googleapis.com/v1/projects/$PROJECT_ID/locations/$LOCATION/clusters/$CLUSTER_NAME"
 asm/
 set 4 field(s) of setter "anthos.servicemesh.trustDomainAliases" to value "$PROJECT_ID.svc.id.goog"
 migrate-to-asm: Configured.
 migrate-to-asm: Installing Anthos Service Mesh control plane...
 migrate-to-asm:
 - Processing resources for Istio core.
 ✔ Istio core installed
 - Processing resources for Istiod.
 - Processing resources for Istiod. Waiting for Deployment/istio-system/istiod-asm-1102-2
 ✔ Istiod installed
 - Processing resources for CNI, Ingress gateways.
 - Processing resources for CNI, Ingress gateways. Waiting for Deployment/istio-system/istio-ingressgateway
 ✔ CNI installed
 - Processing resources for Ingress gateways. Waiting for Deployment/istio-system/istio-ingressgateway
 ✔ Ingress gateways installed
 - Pruning removed resources
 migrate-to-asm:
 migrate-to-asm:
 namespace/asm-system created
 customresourcedefinition.apiextensions.k8s.io/canonicalservices.anthos.cloud.google.com configured
 role.rbac.authorization.k8s.io/canonical-service-leader-election-role created
 clusterrole.rbac.authorization.k8s.io/canonical-service-manager-role configured
 clusterrole.rbac.authorization.k8s.io/canonical-service-metrics-reader unchanged
 serviceaccount/canonical-service-account created
 rolebinding.rbac.authorization.k8s.io/canonical-service-leader-election-rolebinding created
 clusterrolebinding.rbac.authorization.k8s.io/canonical-service-manager-rolebinding unchanged
 clusterrolebinding.rbac.authorization.k8s.io/canonical-service-proxy-rolebinding unchanged
 service/canonical-service-controller-manager-metrics-service created
 deployment.apps/canonical-service-controller-manager created
 deployment.apps/canonical-service-controller-manager condition met
 migrate-to-asm:
 migrate-to-asm:
 migrate-to-asm: *******
 migrate-to-asm: Control plane installation complete!

重新注入工作负载并检查应用行为

Anthos Service Mesh 控制平面现已准备好处理工作负载,但现有 Istio 控制平面仍在管理现有工作负载。如需迁移这些工作负载,需要使用 Anthos Service Mesh 修订版本标签重新标记当前针对 Istio 注入标签的 Kubernetes 命名空间。然后,您需要重启这些命名空间中的工作负载。您可以使用工具手动执行此操作(请参阅第 1 步中的“备注”)或一步。

重新标签步骤会执行以下操作:

  • 它会查找当前使用 Istio 注入标签的所有命名空间。
  • 它使用 istio.io/rev=asm-1102-2 重新标记这些命名空间。
  • 它会重启命名空间中的工作负载。

如需重新注入工作负载,请按照以下步骤操作:

  1. 通过运行以下命令重新标记所有启用 Istio 的命名空间并重启工作负载:

     ./migrate-to-asm relabel \
         --cluster_location $CLUSTER_LOCATION \
         --cluster-name $CLUSTER_NAME \
         --project-id $PROJECT_ID
    

    输出类似于以下内容:

    migrate-to-asm: Checking installation tool dependencies...
    migrate-to-asm: Checking for $PROJECT_ID...
    migrate-to-asm: Fetching/writing Google Cloud credentials to kubeconfig file...
    Fetching cluster endpoint and auth data.
    kubeconfig entry generated for $CLUSTER_NAME.
    migrate-to-asm:
    migrate-to-asm: Verifying connectivity (20s)...
    migrate-to-asm: kubeconfig set to $PROJECT_ID/$LOCATION/$CLUSTER_NAME...
    ******
    migrate-to-asm: Installation of Anthos Service Mesh has completed. Migration will continue
    migrate-to-asm: by relabeling and restarting workloads in the following namespaces:
    migrate-to-asm:     namespace/default
    migrate-to-asm:
    Continue with migration? (Y/n)Y
    migrate-to-asm: Relabeling namespace/default...
    namespace/default labeled
    migrate-to-asm: Restarting workloads in namespace/default and waiting for them to become available (max 5 min)...
    deployment.apps/frontend restarted
    deployment.apps/backend restarted
    deployment.apps/frontend condition met
    deployment.apps/backend condition met
    migrate-to-asm: *******
    migrate-to-asm: Finished restarting workloads!
    
  2. 等待所有 Deployment 重启,然后通过运行以下命令检查数据平面版本:

    istioctl version
    

    输出类似于以下内容:

    client version: 1.8.0
    pilot version: 1.9.5
    istiod version: 1.10.2-asm.2
    data plane version: 1.10.2-asm.2 (14 proxies)
    
  3. 重启后验证应用是否正常运行。

访问 Anthos Service Mesh 信息中心

在本部分中,您将转到 Anthos Service Mesh 信息中心,并确保收到所有 Service 的黄金信号。您还应能够查看应用拓扑。

  1. 在 Google Cloud 控制台中,转到 Anthos Service Mesh 页面。

    转到 Anthos Service Mesh

  2. 您应该能够查看 Service 的指标和拓扑。

如需详细了解 Anthos Service Mesh 信息中心,请参阅在 Google Cloud 控制台中探索 Anthos Service Mesh

完成迁移

在完成迁移之前,请确保您的所有应用都能正常运行。完成迁移后,您无法回滚到现有的 Istio 版本。完成迁移会执行以下步骤:

  • 它可验证集群中的所有正在运行的代理是否正在使用 Anthos Service Mesh。
  • 它会从集群中移除未使用的 Istio 组件。此步骤不可逆。

如需完成向 Anthos Service Mesh 的迁移,请运行以下命令:

 ./migrate-to-asm finalize \
     --cluster_location $CLUSTER_LOCATION \
     --cluster-name $CLUSTER_NAME \
     --project-id $PROJECT_ID
输出内容类似如下:
migrate-to-asm: Checking installation tool dependencies...
migrate-to-asm: Checking for asm-scriptaro-oss...
migrate-to-asm: All proxies running Anthos Service Mesh!
Remove previous control plane resources? (Y/n)
migrate-to-asm: ****
migrate-to-asm: Previous Istio control plane has been removed.

回滚到现有的 Istio 版本

运行回滚步骤,以使用先前的 Istio 注入标签为命名空间重新添加标签,重启工作负载,以及回滚网关更改。之后,该工具会移除集群中部署的任何 Anthos Service Mesh 组件。

您需要手动还原迁移前步骤启用的任何依赖项。

如需回滚到 Istio,请运行以下命令:

 ./migrate-to-asm rollback \
     --cluster_location $CLUSTER_LOCATION \
     --cluster-name $CLUSTER_NAME \
     --project-id $PROJECT_ID
输出内容类似如下:
migrate-to-asm: Checking installation tool dependencies...
migrate-to-asm: Checking for $PROJECT_ID...
******
migrate-to-asm: Rolling back migration by relabeling and restarting workloads
migrate-to-asm: in the following namespaces:
migrate-to-asm:     namespace/default
migrate-to-asm:
Continue with rollback? (Y/n)
migrate-to-asm: Relabeling namespace/default...
namespace/default labeled
migrate-to-asm: Restarting workloads in namespace/default and waiting for them to become available (max 5 min)...
deployment.apps/frontend restarted
deployment.apps/backend restarted
deployment.apps/frontend condition met
deployment.apps/backend condition met
migrate-to-asm: *******
migrate-to-asm: Finished restarting workloads!
service/istio-ingressgateway configured
deployment.apps/istio-ingressgateway configured
There are still 14 proxies pointing to the control plane revision asm-1102-2
istio-ingressgateway-66c85975d-2gt8c.istio-system
istio-ingressgateway-66c85975d-jdd96.istio-system
...
frontend-685dcb78d6-9l45j.default
If you proceed with the uninstall, these proxies will become detached from any control plane and will not function correctly.

Removed HorizontalPodAutoscaler:istio-system:istio-ingressgateway.
Removed HorizontalPodAutoscaler:istio-system:istiod-asm-1102-2.
...
Removed ClusterRoleBinding::mdp-controller.
✔ Uninstall complete
namespace "asm-system" deleted
migrate-to-asm: ****
migrate-to-asm: Anthos Service Mesh has been uninstalled from the cluster.

附录

创建安装 Istio 的 GKE 集群:

在本部分中,您将部署一个启用了 Istio 的 GKE 集群。您可以使用专用集群或非专用 GKE 集群。专用 GKE 集群必须具有公共 GKE 端点。您还将验证 Istio 安装。

如果您已有 GKE 集群,则可以跳过创建步骤,并确保您有权访问使用 KUBECONFIG 文件的集群。本指南使用的上下文在变量 ${CLUSTER_1_CTX} 中定义。您可以将集群的上下文设置为此变量。

  1. 创建本指南中使用的环境变量:

    # Enter your project ID
    export PROJECT_ID=PROJECT_ID
    gcloud config set project ${PROJECT_ID}
    export PROJECT_NUM=$(gcloud projects describe ${PROJECT_ID} --format='value(projectNumber)')
    export CLUSTER_NAME=GKE_CLUSTER_NAME
    export CLUSTER_LOCATION=GKE_CLUSTER_REGION_OR_ZONE
    export CLUSTER_CTX=gke_${PROJECT_ID}_${CLUSTER_LOCATION}_${CLUSTER_NAME}
    export ISTIO_VERSION=ISTIO_VERSION # Must be versions 1.7 through 1.10 and must be of the form major.minor.patch, for example 1.7.4 or 1.9.5
    
  2. 创建已启用 Istio 的 GKE 集群(这是一个专用集群)。您还可以使用非专用 GKE 集群执行这些步骤。

    区域级集群

    gcloud container clusters create ${CLUSTER_NAME} \
        --project ${PROJECT_ID} \
        --zone ${CLUSTER_LOCATION} \
        --machine-type "e2-standard-4" \
        --num-nodes "4" --min-nodes "2" --max-nodes "5" \
        --enable-ip-alias --enable-autoscaling
    

    区域级集群

    gcloud container clusters create ${CLUSTER_NAME} \
        --project ${PROJECT_ID} \
        --region ${CLUSTER_LOCATION} \
        --machine-type "e2-standard-4" \
        --num-nodes "4" --min-nodes "2" --max-nodes "5" \
        --enable-ip-alias --enable-autoscaling
    
  3. 确认集群为 RUNNING

     gcloud container clusters list
    

    输出类似于以下内容:

    NAME      LOCATION    MASTER_VERSION    MASTER_IP      MACHINE_TYPE   NODE_VERSION      NUM_NODES  STATUS
    gke-east  us-east1-b  1.19.10-gke.1600  34.73.171.206  e2-standard-4  1.19.10-gke.1600  4          RUNNING
    
  4. 连接到该集群:

    区域级集群

    gcloud container clusters get-credentials ${CLUSTER_NAME} \
        --zone ${CLUSTER_LOCATION}
    

    区域级集群

    gcloud container clusters get-credentials ${CLUSTER_NAME} \
        --region ${CLUSTER_LOCATION}
    

请务必在最后取消设置 KUBECONFIG 变量。

安装 Istio

在本部分中,您需要将 Istio 1.7 版部署到 GKE 集群。

  1. 下载 Istio:

    curl -L https://istio.io/downloadIstio | ISTIO_VERSION=${ISTIO_VERSION} TARGET_ARCH=x86_64 sh -
    
  2. 使用 istioctl 命令行工具安装 Istio。从下列选项中选择一项:

    • 选项 1:没有自定义 IstioOperator 资源
    • 选项 2:使用自定义 IstioOperator 资源

    选项 1

    不使用自定义 IstioOperator 资源:

    ./istio-${ISTIO_VERSION}/bin/istioctl install --set profile=default -y
    

    输出类似于以下内容:

    ✔ Istio core installed
    ✔ Istiod installed
    ✔ Ingress gateways installed
    ✔ Installation complete
    

    选项 2

    使用自定义 IstioOperator 资源时:

    cat <<EOF > istio-operator.yaml
    apiVersion: install.istio.io/v1alpha1
    kind: IstioOperator
    metadata:
     name: istio-operator
    spec:
     components:
       base:
         enabled: true
       ingressGateways:
       - enabled: true
         k8s:
           env:
           - name: TERMINATION_DRAIN_DURATION_SECONDS
             value: "10"
           hpaSpec:
             maxReplicas: 10
             metrics:
             - resource:
                 name: cpu
                 targetAverageUtilization: 80
               type: Resource
             minReplicas: 2
           resources:
             limits:
               cpu: "4"
               memory: 8Gi
             requests:
               cpu: "2"
               memory: 4Gi
           service:
             ports:
             - name: status-port
               port: 15021
               targetPort: 15021
             - name: http2
               port: 80
               targetPort: 8080
             - name: https
               port: 443
               targetPort: 8443
             - name: tls
               port: 15443
               targetPort: 15443
         name: istio-ingressgateway
       - enabled: true
         k8s:
           env:
           - name: TERMINATION_DRAIN_DURATION_SECONDS
             value: "10"
           hpaSpec:
             maxReplicas: 10
             minReplicas: 2
           resources:
             limits:
               cpu: "4"
               memory: 8Gi
             requests:
               cpu: "2"
               memory: 4Gi
           service:
             ports:
             - name: status-port
               port: 15021
               targetPort: 15021
             - name: http2
               port: 80
               targetPort: 8080
             - name: https
               port: 443
               targetPort: 8443
             - name: tls
               port: 15443
               targetPort: 15443
         label:
           istio: istio-api-ingressgateway
         name: istio-api-ingressgateway
     meshConfig:
       defaultConfig:
         tracing:
           sampling: 1
           zipkin:
             address: jaeger-collector.observability.svc.cluster.local:9411
       enableTracing: true
    EOF
    
    ./istio-${ISTIO_VERSION}/bin/istioctl install -f istio-operator.yaml -y
    

    输出类似于以下内容:

    ✔ Istio core installed
    ✔ Istiod installed
    ✔ Ingress gateways installed
    ✔ Installation complete
    
  3. 确保已部署并运行 Istio 服务和 Pod:

    kubectl --context=${CLUSTER_CTX} -n istio-system get services,pods
    

    输出类似于以下内容:

    选项 1

    不使用自定义 IstioOperator 资源:

    NAME                           TYPE           CLUSTER-IP     EXTERNAL-IP         PORT(S)                                                      AGE
    service/istio-ingressgateway   LoadBalancer   10.64.5.113    <pending>           15021:31285/TCP,80:31740/TCP,443:30753/TCP,15443:31246/TCP   33s
    service/istiod                 ClusterIP      10.64.15.184   <none>              15010/TCP,15012/TCP,443/TCP,15014/TCP,853/TCP                45s
    
    NAME                                        READY   STATUS    RESTARTS   AGE
    pod/istio-ingressgateway-6f44d6745b-22q9h   1/1     Running   0          34s
    pod/istiod-b89f5cc6-nhsrc                   1/1     Running   0          48s
    

    选项 2

    使用自定义 IstioOperator 资源时:

    NAME                               TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)                                                      AGE
    service/istio-api-ingressgateway   LoadBalancer   10.100.0.84    104.196.26.108   15021:32489/TCP,80:30083/TCP,443:30565/TCP,15443:30705/TCP   76s
    service/istio-ingressgateway       LoadBalancer   10.100.3.221   34.139.111.125   15021:30966/TCP,80:31557/TCP,443:31016/TCP,15443:31574/TCP   75s
    service/istiod                     ClusterIP      10.100.13.72   <none>           15010/TCP,15012/TCP,443/TCP,15014/TCP                        86s
    
    NAME                                            READY   STATUS    RESTARTS   AGE
    pod/istio-api-ingressgateway-79978ddc65-hslbv   1/1     Running   0          61s
    pod/istio-api-ingressgateway-79978ddc65-z92w8   1/1     Running   0          77s
    pod/istio-ingressgateway-fb47c4859-pkdn7        1/1     Running   0          60s
    pod/istio-ingressgateway-fb47c4859-t2pfq        1/1     Running   0          77s
    pod/istiod-9445656d7-fxk9j                      1/1     Running   0          89s
    

部署 Online Boutique

在本部分中,您需要将名为 Online Boutique 的基于微服务的示例应用部署到 GKE 集群。Online Boutique 部署在启用了 Istio 的命名空间中。您需要验证应用是否正常工作,以及 Istio 是否正在向每个 Pod 注入 Sidecar 代理。

如果您已有具有应用的现有集群,则可以跳过创建新命名空间和部署 Online Boutique。您可以安装相同步骤处理安装 Anthos Service Mesh 并准备工作负载中的所有命名空间。

  1. 将 Online Boutique 部署到 GKE 集群:

    kpt pkg get \
    https://github.com/GoogleCloudPlatform/microservices-demo.git/release \
    online-boutique
    
    kubectl --context=${CLUSTER_CTX} create namespace online-boutique
    kubectl --context=${CLUSTER_CTX} label namespace online-boutique istio-injection=enabled
    
    kubectl --context=${CLUSTER_CTX} -n online-boutique apply -f online-boutique
    
  2. 等待所有 Deployment 准备就绪:

    kubectl --context=${CLUSTER_CTX} -n online-boutique wait --for=condition=available --timeout=5m deployment adservice
    kubectl --context=${CLUSTER_CTX} -n online-boutique wait --for=condition=available --timeout=5m deployment checkoutservice
    kubectl --context=${CLUSTER_CTX} -n online-boutique wait --for=condition=available --timeout=5m deployment currencyservice
    kubectl --context=${CLUSTER_CTX} -n online-boutique wait --for=condition=available --timeout=5m deployment emailservice
    kubectl --context=${CLUSTER_CTX} -n online-boutique wait --for=condition=available --timeout=5m deployment frontend
    kubectl --context=${CLUSTER_CTX} -n online-boutique wait --for=condition=available --timeout=5m deployment paymentservice
    kubectl --context=${CLUSTER_CTX} -n online-boutique wait --for=condition=available --timeout=5m deployment productcatalogservice
    kubectl --context=${CLUSTER_CTX} -n online-boutique wait --for=condition=available --timeout=5m deployment shippingservice
    kubectl --context=${CLUSTER_CTX} -n online-boutique wait --for=condition=available --timeout=5m deployment cartservice
    kubectl --context=${CLUSTER_CTX} -n online-boutique wait --for=condition=available --timeout=5m deployment loadgenerator
    kubectl --context=${CLUSTER_CTX} -n online-boutique wait --for=condition=available --timeout=5m deployment recommendationservice
    
  3. 确保每个 Pod 有两个容器:应用容器和 Istio 自动注入 Pod 的 Istio 边车代理:

    kubectl --context=${CLUSTER_CTX} -n online-boutique get pods
    

    输出类似于以下内容:

    NAME                                     READY   STATUS    RESTARTS   AGE
    adservice-7cbc9bd9-t92k4                 2/2     Running   0          3m21s
    cartservice-d7db78c66-5qfmt              2/2     Running   1          3m23s
    checkoutservice-784bfc794f-j8rl5         2/2     Running   0          3m26s
    currencyservice-5898885559-lkwg4         2/2     Running   0          3m23s
    emailservice-6bd8b47657-llvgv            2/2     Running   0          3m27s
    frontend-764c5c755f-9wf97                2/2     Running   0          3m25s
    loadgenerator-84cbcd768c-5pdbr           2/2     Running   3          3m23s
    paymentservice-6c676df669-s779c          2/2     Running   0          3m25s
    productcatalogservice-7fcf4f8cc-hvf5x    2/2     Running   0          3m24s
    recommendationservice-79f5f4bbf5-6st24   2/2     Running   0          3m26s
    redis-cart-74594bd569-pfhkz              2/2     Running   0          3m22s
    shippingservice-b5879cdbf-5z7m5          2/2     Running   0          3m22s
    
  4. 您还可以从任一 Pod 检查 Sidecar Envoy 代理版本,以确认您部署了 Istio 1.4 版 Envoy 代理:

    export FRONTEND_POD=$(kubectl get pod -n online-boutique -l app=frontend --context=${CLUSTER_CTX} -o jsonpath='{.items[0].metadata.name}')
    kubectl --context=${CLUSTER_CTX} get pods ${FRONTEND_POD} -n online-boutique -o json | jq '.status.containerStatuses[].image'
    

    输出类似于以下内容:

    "docker.io/istio/proxyv2:1.7.4"
    "gcr.io/google-samples/microservices-demo/frontend:v0.3.4"
    
  5. 通过导航到 istio-ingressgateway Service IP 地址的 IP 地址来访问应用:

    kubectl --context=${CLUSTER_CTX} -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}'
    

后续步骤