在关联的集群上安装 Anthos Service Mesh

本指南介绍如何在 GKE Enterprise 关联集群上执行 Anthos Service Mesh 版本 1.6.14-asm.2 的全新安装。使用本指南可以在以下环境中安装 Anthos Service Mesh:

  • Kubernetes 版本 1.16 上的 Amazon Elastic Kubernetes Service (Amazon EKS)
  • Kubernetes 版本 1.16 上的 Microsoft Azure Kubernetes Service (Microsoft AKS)

安装将在集群上针对 asm-multicloud 配置文件启用支持的功能

准备工作

使用要求

  • 安装 Anthos Service Mesh 的用户集群至少具有 4 个 vCPU、15 GB 内存和 4 个副本。

  • 集群没有安装 Anthos Service Mesh 或开源 Istio。

  • 如需将服务端口纳入服务网格,必须为服务端口命名,并且名称必须包含以下语法的端口协议:name: protocol[-suffix],其中方括号表示必须以短划线开头的可选后缀。如需了解详情,请参阅为服务端口命名

限制

一个 Google Cloud 项目只能关联一个网格。

设置您的环境

在要安装 Anthos Service Mesh 的机器上,您需要以下工具。请注意,您只能在用户集群上安装 Anthos Service Mesh,不能在管理员集群上安装。

安装 Google Cloud CLI 后:

  1. 使用 Google Cloud CLI 进行身份验证:

    gcloud auth login
    
  2. 更新组件:

    gcloud components update
    
  3. 安装 kubectl

    gcloud components install kubectl
    
  4. 安装所需的 kpt 版本:

       curl -L https://github.com/GoogleContainerTools/kpt/releases/download/v0.39.2/kpt_linux_amd64 > kpt_0_39_2
       chmod +x kpt_0_39_2
       alias kpt="$(readlink -f kpt_0_39_2)"
    
  5. 将上下文切换到用户集群:

    kubectl config use-context CLUSTER_NAME
  6. 向您的用户账号(您的 Google Cloud 登录电子邮件地址)授予集群管理员权限。您需要这些权限,以便为 Anthos Service Mesh 创建必要的基于角色的访问权限控制 (RBAC) 规则:

    kubectl create clusterrolebinding cluster-admin-binding \
      --clusterrole=cluster-admin \
      --user=USER_ACCOUNT

准备安装 Anthos Service Mesh

    Linux

  1. 将 Anthos Service Mesh 安装文件下载到当前工作目录中:
    curl -LO https://storage.googleapis.com/gke-release/asm/istio-1.6.14-asm.2-linux-amd64.tar.gz
  2. 下载签名文件并使用 openssl 验证签名:
    curl -LO https://storage.googleapis.com/gke-release/asm/istio-1.6.14-asm.2-linux-amd64.tar.gz.1.sig
    openssl dgst -verify /dev/stdin -signature istio-1.6.14-asm.2-linux-amd64.tar.gz.1.sig istio-1.6.14-asm.2-linux-amd64.tar.gz <<'EOF'
    -----BEGIN PUBLIC KEY-----
    MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEWZrGCUaJJr1H8a36sG4UUoXvlXvZ
    wQfk16sxprI2gOJ2vFFggdq3ixF2h4qNBt0kI7ciDhgpwS8t+/960IsIgw==
    -----END PUBLIC KEY-----
    EOF

    预期输出为 Verified OK

  3. 将文件内容提取到文件系统上的任意位置。例如,如需将内容提取到当前工作目录,请运行以下命令:
    tar xzf istio-1.6.14-asm.2-linux-amd64.tar.gz

    该命令会在当前工作目录中创建一个名为 istio-1.6.14-asm.2 的安装目录,其中包含:

    • samples 目录中的示例应用。
    • 用于安装 Anthos Service Mesh 的 istioctl 命令行工具位于 bin 目录中。
    • Anthos Service Mesh 配置文件位于 manifests/profiles 目录中。

  4. Mac OS

  5. 将 Anthos Service Mesh 安装文件下载到当前工作目录中:
    curl -LO https://storage.googleapis.com/gke-release/asm/istio-1.6.14-asm.2-osx.tar.gz
  6. 下载签名文件并使用 openssl 验证签名:
    curl -LO https://storage.googleapis.com/gke-release/asm/istio-1.6.14-asm.2-osx.tar.gz.1.sig
    openssl dgst -sha256 -verify /dev/stdin -signature istio-1.6.14-asm.2-osx.tar.gz.1.sig istio-1.6.14-asm.2-osx.tar.gz <<'EOF'
    -----BEGIN PUBLIC KEY-----
    MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEWZrGCUaJJr1H8a36sG4UUoXvlXvZ
    wQfk16sxprI2gOJ2vFFggdq3ixF2h4qNBt0kI7ciDhgpwS8t+/960IsIgw==
    -----END PUBLIC KEY-----
    EOF

    预期输出为 Verified OK

  7. 将文件内容提取到文件系统上的任意位置。例如,如需将内容提取到当前工作目录,请运行以下命令:
    tar xzf istio-1.6.14-asm.2-osx.tar.gz

    该命令会在当前工作目录中创建一个名为 istio-1.6.14-asm.2 的安装目录,其中包含:

    • samples 目录中的示例应用。
    • 用于安装 Anthos Service Mesh 的 istioctl 命令行工具位于 bin 目录中。
    • Anthos Service Mesh 配置文件位于 manifests/profiles 目录中。

  8. Windows

  9. 将 Anthos Service Mesh 安装文件下载到当前工作目录中:
    curl -LO https://storage.googleapis.com/gke-release/asm/istio-1.6.14-asm.2-win.zip
  10. 下载签名文件并使用 openssl 验证签名:
    curl -LO https://storage.googleapis.com/gke-release/asm/istio-1.6.14-asm.2-win.zip.1.sig
    openssl dgst -verify - -signature istio-1.6.14-asm.2-win.zip.1.sig istio-1.6.14-asm.2-win.zip <<'EOF'
    -----BEGIN PUBLIC KEY-----
    MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEWZrGCUaJJr1H8a36sG4UUoXvlXvZ
    wQfk16sxprI2gOJ2vFFggdq3ixF2h4qNBt0kI7ciDhgpwS8t+/960IsIgw==
    -----END PUBLIC KEY-----
    EOF

    预期输出为 Verified OK

  11. 将文件内容提取到文件系统上的任意位置。例如,如需将内容提取到当前工作目录,请运行以下命令:
    tar xzf istio-1.6.14-asm.2-win.zip

    该命令会在当前工作目录中创建一个名为 istio-1.6.14-asm.2 的安装目录,其中包含:

    • samples 目录中的示例应用。
    • 用于安装 Anthos Service Mesh 的 istioctl 命令行工具位于 bin 目录中。
    • Anthos Service Mesh 配置文件位于 manifests/profiles 目录中。

  12. 确保您位于 Anthos Service Mesh 安装的根目录。
    cd istio-1.6.14-asm.2
  13. 为方便起见,请将 /bin 目录中的工具添加到 PATH:
    export PATH=$PWD/bin:$PATH

创建 istio-system 命名空间

为控制层面组件创建一个名为 istio-system 的命名空间:

kubectl create namespace istio-system

配置验证网络钩子

安装 Anthos Service Mesh 时,您可以在 istiod 上设置修订版本标签。您需要在验证网络钩子上设置相同的修订版本。

将以下 YAML 复制到名为 istiod-service.yaml 的文件中:

apiVersion: v1
kind: Service
metadata:
  name: istiod
  namespace: istio-system
  labels:
    istio.io/rev: asm-1614-2
    app: istiod
    istio: pilot
    release: istio
spec:
  ports:
    - port: 15010
      name: grpc-xds # plaintext
      protocol: TCP
    - port: 15012
      name: https-dns # mTLS with k8s-signed cert
      protocol: TCP
    - port: 443
      name: https-webhook # validation and injection
      targetPort: 15017
      protocol: TCP
    - port: 15014
      name: http-monitoring # prometheus stats
      protocol: TCP
  selector:
    app: istiod
    istio.io/rev: asm-1614-2

安装 Anthos Service Mesh

  1. 运行以下命令以使用 asm-multicloud 配置文件安装 Anthos Service Mesh。如果您要启用受支持的可选功能,请在以下命令行中添加 -f 和 YAML 文件名。 如需了解详情,请参阅启用可选功能

    istioctl install \
      --set profile=asm-multicloud \
      --set revision=asm-1614-2
    

    --set revision 参数会将格式为 istio.io/rev=asm-1614-2 的修订版本标签添加到 istiod。自动 Sidecar 注入器网络钩子使用修订版本标签将注入的 Sidecar 与特定 istiod 修订版本相关联。如需为命名空间启用 Sidecar 自动注入功能,您必须使用一个与 istiod 上的标签匹配的修订版本来为其添加标签。

  2. 配置验证网络钩子,使其能够使用修订版本标签查找 istiod 服务:

    kubectl apply -f istiod-service.yaml
    

    此命令会创建一个服务条目,可让验证网络钩子在配置应用之前自动检查配置。

自动双向 TLS(自动 mTLS)默认情况下处于启用状态。通过自动 mTLS,客户端边车代理会自动检测服务器是否具有 Sidecar。客户端 Sidecar 会将 mTLS 发送到具有 Sidecar 的工作负载,并将纯文本流量发送到没有 Sidecar 的工作负载。

检查控制层面组件

检查 istio-system 中的控制层面 Pod 是否已启动:

kubectl get pod -n istio-system

预期输出如下所示:

NAME                                   READY   STATUS      RESTARTS   AGE
istio-ingressgateway-cff9f5c7d-qg4ls   1/1     Running   0          7m5s
istio-ingressgateway-cff9f5c7d-vlkzb   1/1     Running   0          7m20s
istiod-66b587859c-886gx                1/1     Running   0          7m33s
istiod-66b587859c-dfs2j                1/1     Running   0          7m33s

如果该命令的输出显示一个或多个组件未处于 Running 状态,请参阅对关联集群上的 GKE Enterprise 进行问题排查,获取可运行以获取组件详细信息的脚本。

注入 Sidecar 代理

Anthos Service Mesh 使用边车代理来增强网络安全性、可靠性和可观测性。借助 Anthos Service Mesh,这些功能可以从应用的主容器中抽象出来,并在作为同一 Pod 中单独的容器提供的常见进程外代理中实现。

在部署工作负载之前,请务必配置边车代理注入,以便 Anthos Service Mesh 可以监控和保护流量。

安装 Anthos Service Mesh 之前在集群上运行的任何工作负载都需要注入或更新边车代理,以使其具有当前 Anthos Service Mesh 版本。在部署新的工作负载之前,请务必配置边车代理注入,以便 Anthos Service Mesh 可以监控和保护流量。

要启用 Sidecar 自动注入,请使用您在 istiod 上设置的同一修订版本来为命名空间添加标签。运行以下命令以显示 istiod 上的标签:

kubectl -n istio-system get pods -l app=istiod --show-labels

确认输出中包含修订版本标签 istio.io/rev=asm-1614-2。这是您用于启用自动注入的标签。您可以使用一个命令来启用自动注入,例如:

kubectl label namespace NAMESPACE istio-injection-istio.io/rev=asm-1614-2 --overwrite

其中,NAMESPACE 是应用服务的命名空间的名称;如果未显式创建命名空间,则为 default

如需了解详情,请参阅注入边车代理

部署示例应用

Anthos Service Mesh 提供预配置的入站流量网关 istio-ingressgateway,该网关可用于管理流向服务网格上运行的应用的入站流量。如需允许从集群外部(例如通过浏览器)访问应用,请执行以下操作:

  • 您需要获取 istio-ingressgateway 的外部 IP 地址。

  • 您的应用必须定义网关和 VirtualService 资源,类似于 Online Boutique 示例应用的 frontend-gateway.yaml

如需部署和运行示例应用,请执行以下操作:

  1. 下载示例:

    kpt pkg get \
    https://github.com/GoogleCloudPlatform/microservices-demo.git/release \
    microservices-demo
    
  2. 启用自动 Sidecar 注入:

    kubectl label namespace default istio-injection=enabled
    
  3. 将示例部署到集群:

    kubectl apply -f microservices-demo
    

如需获取 istio-ingressgateway 的外部 IP 地址,请运行以下命令:

  1. 创建主机密钥环境变量:

    Microsoft AKS

      export HOST_KEY="ip"
    

    Amazon EKS

      export HOST_KEY="hostname"
    
  2. 创建 INGRESS_HOST 环境变量:

    export INGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].'"$HOST_KEY"'}')
    
  3. 创建 INGRESS_PORT 环境变量:

    export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].port}')
    
  4. 通过浏览器访问应用。将以下网址中的 EXTERNAL_IP 替换为 $INGRESS_HOST:$INGRESS_PORT

    http://EXTERNAL_IP/

探索完毕后,请移除此示例:

kubectl delete -f microservices-demo