实现远程控制面和托管控制面工作负载相互访问

本文主要介绍在使用了ASM远程控制面功能后,如何实现集群之间的互相访问。

重要
  • 本文依旧沿用使用ASM远程控制面降低推送延迟中的cluster-1和cluster-2,即cluster-1表示云上ACK集群(由ASM托管控制面管理),cluster-2表示非ACK集群(由ASM远程控制面管理)。请确保您已经完成了使用ASM远程控制面降低推送延迟中所有步骤。

  • 本文中的步骤涉及到多次切换kubeconfig,建议您将cluster-1和cluster-2的kubeconfig配置到同一个config文件中,并使用kubectl config use-context进行切换。您也可以使用kubecmkubectx来管理多个集群的kubeconfig。

步骤一:cluster-1访问cluster-2

  1. 配置集群网络和东西向网关。具体操作,请参见使用ASM东西向网关实现多集群跨网络互通

  2. 使用cluster-1的kubeconfig,部署httpbin和sleep应用。具体应用部署步骤,请参见部署httpbin应用

  3. 此时两个集群中都存在httpbin和sleep应用,ASM会将两个集群中的httpbin服务视为同一个服务,如果在cluster-1中使用sleep访问httpbin,请求将会平均分发到两个集群的httpbin工作负载上。

  4. 执行以下命令,将cluster-1中的httpbin服务缩容至0以方便测试。

    kubectl scale deployment httpbin --replicas 0
  5. 执行以下命令,从cluster-1中的sleep访问httpbin服务。

    kubectl exec deployment/sleep -it -- curl httpbin:8000/status/418

    预期输出:

        -=[ teapot ]=-
    
           _...._
         .'  _ _ `.
        | ."` ^ `". _,
        \_;`"---"`|//
          |       ;/
          \_     _/
            `"""`

    可以看到此时依然成功访问。该请求被cluster-2的httpbin服务处理,并且两者之间的通信使用mTLS进行了加密。

步骤二:测试cluster-2访问cluster-1

说明

本节操作步骤依赖于istiocli工具,请确保安装的istiocli版本与ASM版本对应。

由于远程控制面管理的工作负载默认不会发现被托管控制面管理的服务,本节将演示如何使cluster-2可以访问cluster-1中的服务。

  1. 使用以下内容,创建role.yaml。

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      labels:
        app: istio-reader
      name: istio-reader-clusterrole-istio-system
    rules:
    - apiGroups:
      - config.istio.io
      - security.istio.io
      - networking.istio.io
      - authentication.istio.io
      - rbac.istio.io
      - telemetry.istio.io
      - extensions.istio.io
      resources:
      - '*'
      verbs:
      - get
      - list
      - watch
    - apiGroups:
      - ""
      resources:
      - endpoints
      - pods
      - services
      - nodes
      - replicationcontrollers
      - namespaces
      - secrets
      verbs:
      - get
      - list
      - watch
    - apiGroups:
      - networking.istio.io
      resources:
      - workloadentries
      verbs:
      - get
      - watch
      - list
    - apiGroups:
      - networking.x-k8s.io
      - gateway.networking.k8s.io
      resources:
      - gateways
      - gatewayclasses
      verbs:
      - get
      - watch
      - list
    - apiGroups:
      - apiextensions.k8s.io
      resources:
      - customresourcedefinitions
      verbs:
      - get
      - list
      - watch
    - apiGroups:
      - discovery.k8s.io
      resources:
      - endpointslices
      verbs:
      - get
      - list
      - watch
    - apiGroups:
      - multicluster.x-k8s.io
      resources:
      - serviceexports
      verbs:
      - get
      - list
      - watch
      - create
      - delete
    - apiGroups:
      - multicluster.x-k8s.io
      resources:
      - serviceimports
      verbs:
      - get
      - list
      - watch
    - apiGroups:
      - apps
      resources:
      - replicasets
      verbs:
      - get
      - list
      - watch
    - apiGroups:
      - authentication.k8s.io
      resources:
      - tokenreviews
      verbs:
      - create
    - apiGroups:
      - authorization.k8s.io
      resources:
      - subjectaccessreviews
      verbs:
      - create
    - apiGroups: [""]
      resources: ["configmaps"]
      verbs: ["create", "get", "list", "watch", "update"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      labels:
        app: istio-reader
      name: istio-reader-clusterrole-istio-system
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: istio-reader-clusterrole-istio-system
    subjects:
    - kind: ServiceAccount
      name: istio-reader-service-account
      namespace: istio-system
  2. 将工作目录切换到解压后的istio文件夹后,执行以下命令,在cluster-1中创建ClusterRoleClusterRoleBinding,使cluster-2的远程控制面可以通过此ClusterRole来获取cluster-1中的服务信息。

    bin/istioctl create-remote-secret \
    --context=cluster-1 \
    --name=${cluster-1 ID} | \
    kubectl apply -f role.yaml --context=cluster-2
    说明

    本步骤执行完成后,cluster-2的istio-system命名空间下会生成一个对应的secret,secret中存储有连接cluster-1所用到的证书,请妥善保管此secret。

  3. 切换context到cluster-1,执行以下命令,将httpbin服务副本修改为1。

    kubectl scale deployment httpbin --replicas 1
  4. 切换context到cluster-2,执行以下命令,将httpbin服务副本修改为0。

    kubectl scale deployment httpbin --replicas 0
  5. 执行以下命令,测试从cluster-2中访问httpbin服务。

    kubectl exec deploy/sleep -it -- curl httpbin:8000/status/418

    预期输出:

        -=[ teapot ]=-
    
           _...._
         .'  _ _ `.
        | ."` ^ `". _,
        \_;`"---"`|//
          |       ;/
          \_     _/
            `"""`

    可以看到,可以正常访问。