全部產品
Search
文件中心

Alibaba Cloud Service Mesh:實現遠端控制面和託管控制面工作負載相互訪問

更新時間:Aug 29, 2025

本文主要介紹在使用了ASM遠端控制面功能後,如何?叢集之間的互相訪問。

前提條件

步驟一:測試cluster-1訪問cluster-2

  1. 使用cluster-1的kubeconfig執行以下命令,將cluster-1中的httpbin應用縮容至0以方便測試。

    kubectl scale deployment httpbin --replicas 0
  2. 使用cluster-1的kubeconfig執行以下命令,從cluster-1中的sleep訪問httpbin應用。

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

    預期輸出:

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

    可以看到此時依然成功訪問。該請求被cluster-2的httpbin應用處理,並且兩者之間的通訊使用mTLS進行了加密。

步驟二:測試cluster-2訪問cluster-1

  1. 使用以下YAML在cluster-1中建立ClusterRole和ClusterRoleBinding資源。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. 請確保已經配置好了kubectl的context,並且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-2的istio-system命名空間下會產生一個對應的secret,secret中儲存有串連cluster-1所用到的訪問憑證,請妥善管理該secret的許可權,確保該secret不會泄露。
  4. 使用cluster-1的kubeconfig執行以下命令,將httpbin應用副本修改為1。

    kubectl scale deployment httpbin --replicas 1
  5. 使用cluster-2的kubeconfig執行以下命令,將httpbin應用副本修改為0。

    kubectl scale deployment httpbin --replicas 0
  6. 使用cluster-2的kubeconfig執行以下命令,測試從cluster-2中訪問cluster-1的httpbin應用。

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

    預期輸出:

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