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

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

前提条件

  • 已完成使用ASM远程控制面降低推送延迟中所有步骤,其中cluster-1表示云上ACK集群(由ASM托管控制面管理),cluster-2表示非ACK集群(由ASM远程控制面管理)。

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

  • 配置集群网络和东西向网关,并在cluster-1cluster-2中部署httpbinsleep应用。具体操作,请参见使用ASM跨集群网格代理实现多集群跨网络互通

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

  1. 使用cluster-1kubeconfig执行以下命令,将cluster-1中的httpbin应用缩容至0以方便测试。

    kubectl scale deployment httpbin --replicas 0
  2. 使用cluster-1kubeconfig执行以下命令,从cluster-1中的sleep访问httpbin应用。

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

    预期输出:

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

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

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

  1. 使用以下YAMLcluster-1中创建ClusterRoleClusterRoleBinding资源。cluster-2的远程控制面会使用这个ClusterRole来获取cluster-1中的服务信息。

    展开查看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. 请确保已经配置好了kubectlcontext,并且cluster-1对应的context名称为cluster-1,cluster-2对应的context名称为cluster-2。

  3. 安装istioctl工具,请确保安装的istioctl版本与ASM版本对应。将工作目录切换到解压后的istio文件夹后,执行以下命令。

    # 以下<YOUR_CLUSTER1_ID>替换为cluster-1的集群id
    bin/istioctl create-remote-secret \
    --context=cluster-1 \
    --name=<YOUR_CLUSTER1_ID> | \
    kubectl apply -f - --context=cluster-2
    本步骤执行完成后,cluster-2istio-system命名空间下会生成一个对应的secret,secret中存储有连接cluster-1所用到的访问凭证,请妥善管理该secret的权限,确保该secret不会泄露。
  4. 使用cluster-1kubeconfig执行以下命令,将httpbin应用副本修改为1。

    kubectl scale deployment httpbin --replicas 1
  5. 使用cluster-2kubeconfig执行以下命令,将httpbin应用副本修改为0。

    kubectl scale deployment httpbin --replicas 0
  6. 使用cluster-2kubeconfig执行以下命令,测试从cluster-2中访问cluster-1httpbin应用。

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

    预期输出:

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