全部產品
Search
文件中心

Container Service for Kubernetes:使用ASM Serverless網關實現多叢集入口

更新時間:Aug 15, 2024

通過阿里雲Service MeshASM,您可以在同一VPC內的多個Kubernetes叢集上部署應用服務元件,並配置一個統一的Serverless入口網關代理多叢集的入口流量,從而提升整體服務的可用性、降低計算資源成本。

前提條件

步驟一:確認兩個叢集的互訪聯通性

預設情況下,同一個VPC下的兩個叢集,如果使用了企業安全性群組,預設能夠互相訪問。如果使用普通安全性群組或者兩個安全性群組訪問不通,則需要為彼此添加安全性群組訪問規則。具體操作,請參見添加安全性群組規則

步驟二:添加叢集到ASM執行個體並建立Serverless入口網關

將兩個叢集添加到ASM執行個體後,建立一個Serverless入口網關。

  1. 將兩個叢集添加到ASM執行個體。具體操作,請參見添加叢集到ASM執行個體

  2. 使用以下YAML,建立一個Serverless入口網關。具體操作,請參見建立入口網關服務

    展開查看Serverless入口網關YAML

    apiVersion: istio.alibabacloud.com/v1beta1
    kind: IstioGateway
    metadata:
      annotations:
        asm.alibabacloud.com/managed-by-asm: 'true'
      name: ingressgateway
      namespace: istio-system
    spec:
      gatewayType: ingress
      dnsPolicy: ClusterFirst
      externalTrafficPolicy: Local
      hostNetwork: false
      ports:
        - name: http
          port: 80
          protocol: TCP
          targetPort: 80
        - name: https
          port: 443
          protocol: TCP
          targetPort: 443
      replicaCount: 1
      resources:
        limits:
          cpu: '2'
          memory: 2G
        requests:
          cpu: 200m
          memory: 256Mi
      rollingMaxSurge: 100%
      rollingMaxUnavailable: 25%
      runAsRoot: true
      serviceType: LoadBalancer

步驟三:部署Bookinfo應用

為了示範ASM跨叢集的應用部署能力,Bookinfo應用的不同微服務分別部署在兩個叢集上。

  1. 使用以下內容,在m1c2叢集中建立bookinfo-m1c2.yaml。

    說明

    review-v3 deployment對應的功能是書評中顯示紅色星。

    展開查看Bookinfo應用YAML

    # Details service
    apiVersion: v1
    kind: Service
    metadata:
      name: details
      labels:
        app: details
        service: details
    spec:
      ports:
      - port: 9080
        name: http
      selector:
        app: details
    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: bookinfo-details
      labels:
        account: details
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: details-v1
      labels:
        app: details
        version: v1
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: details
          version: v1
      template:
        metadata:
          labels:
            app: details
            version: v1
        spec:
          serviceAccountName: bookinfo-details
          containers:
          - name: details
            image: docker.io/istio/examples-bookinfo-details-v1:1.15.0
            imagePullPolicy: IfNotPresent
            ports:
            - containerPort: 9080
    ---
    # Ratings service
    apiVersion: v1
    kind: Service
    metadata:
      name: ratings
      labels:
        app: ratings
        service: ratings
    spec:
      ports:
      - port: 9080
        name: http
      selector:
        app: ratings
    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: bookinfo-ratings
      labels:
        account: ratings
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: ratings-v1
      labels:
        app: ratings
        version: v1
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: ratings
          version: v1
      template:
        metadata:
          labels:
            app: ratings
            version: v1
        spec:
          serviceAccountName: bookinfo-ratings
          containers:
          - name: ratings
            image: docker.io/istio/examples-bookinfo-ratings-v1:1.15.0
            imagePullPolicy: IfNotPresent
            ports:
            - containerPort: 9080
    ---
    # Reviews service
    apiVersion: v1
    kind: Service
    metadata:
      name: reviews
      labels:
        app: reviews
        service: reviews
    spec:
      ports:
      - port: 9080
        name: http
      selector:
        app: reviews
    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: bookinfo-reviews
      labels:
        account: reviews
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: reviews-v1
      labels:
        app: reviews
        version: v1
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: reviews
          version: v1
      template:
        metadata:
          labels:
            app: reviews
            version: v1
        spec:
          serviceAccountName: bookinfo-reviews
          containers:
          - name: reviews
            image: docker.io/istio/examples-bookinfo-reviews-v1:1.15.0
            imagePullPolicy: IfNotPresent
            ports:
            - containerPort: 9080
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: reviews-v2
      labels:
        app: reviews
        version: v2
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: reviews
          version: v2
      template:
        metadata:
          labels:
            app: reviews
            version: v2
        spec:
          serviceAccountName: bookinfo-reviews
          containers:
          - name: reviews
            image: docker.io/istio/examples-bookinfo-reviews-v2:1.15.0
            imagePullPolicy: IfNotPresent
            ports:
            - containerPort: 9080
    ---
    # Productpage services
    apiVersion: v1
    kind: Service
    metadata:
      name: productpage
      labels:
        app: productpage
        service: productpage
    spec:
      ports:
      - port: 9080
        name: http
      selector:
        app: productpage
    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: bookinfo-productpage
      labels:
        account: productpage
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: productpage-v1
      labels:
        app: productpage
        version: v1
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: productpage
          version: v1
      template:
        metadata:
          labels:
            app: productpage
            version: v1
        spec:
          serviceAccountName: bookinfo-productpage
          containers:
          - name: productpage
            image: docker.io/istio/examples-bookinfo-productpage-v1:1.15.0
            imagePullPolicy: IfNotPresent
            ports:
            - containerPort: 9080
    ---
  2. 執行以下命令,在m1c2叢集中部署不包含review-v3 deployment的Bookinfo應用。

    kubectl apply -f bookinfo-m1c2.yaml
  3. 使用以下內容,在m1c1叢集中建立bookinfo-m1c1.yaml。

    展開查看YAML內容

    # Reviews service
    apiVersion: v1
    kind: Service
    metadata:
      name: reviews
      labels:
        app: reviews
        service: reviews
    spec:
      ports:
      - port: 9080
        name: http
      selector:
        app: reviews
    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: bookinfo-reviews
      labels:
        account: reviews
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: reviews-v3
      labels:
        app: reviews
        version: v3
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: reviews
          version: v3
      template:
        metadata:
          labels:
            app: reviews
            version: v3
        spec:
          serviceAccountName: bookinfo-reviews
          containers:
          - name: reviews
            image: docker.io/istio/examples-bookinfo-reviews-v3:1.15.0
            imagePullPolicy: IfNotPresent
            ports:
            - containerPort: 9080
    ---
    # Ratings service
    apiVersion: v1
    kind: Service
    metadata:
      name: ratings
      labels:
        app: ratings
        service: ratings
    spec:
      ports:
      - port: 9080
        name: http
      selector:
        app: ratings
  4. 執行以下命令,在m1c1叢集中部署review-v3和ratting service。

    kubectl apply -f bookinfo-m1c1.yaml

步驟四:添加虛擬服務和網關規則

  1. 在ASM執行個體的default命名空間下,建立一個名為bookinfo的虛擬服務。具體操作,請參見管理虛擬服務

    展開查看虛擬服務YAML

    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: bookinfo
    spec:
      hosts:
      - "*"
      gateways:
      - bookinfo-gateway
      http:
      - match:
        - uri:
            exact: /productpage
        - uri:
            prefix: /static
        - uri:
            exact: /login
        - uri:
            exact: /logout
        - uri:
            prefix: /api/v1/products
        route:
        - destination:
            host: productpage
            port:
              number: 9080
  2. 在ASM執行個體的default命名空間下,建立一個名為bookinfo-gateway的網關規則。具體操作,請參見管理網關規則

    展開查看網關規則YAML

    apiVersion: networking.istio.io/v1alpha3
    kind: Gateway
    metadata:
      name: bookinfo-gateway
    spec:
      selector:
        istio: ingressgateway # use istio default controller
      servers:
      - port:
          number: 80
          name: http
          protocol: HTTP
        hosts:
        - "*"
  3. 在瀏覽器地址欄,輸入http://{Serverless入口網關的IP地址}/productpage,並多次重新整理頁面。

    可以看到頁面出現reviews的3個版本,且比例接近1:1:1。雖然review-v3和其他服務不在同一個叢集中,也可以正常顯示。

(可選)步驟五:指定reviews總是使用v3版本

通過定義目標規則和虛擬服務,可以定義Bookinfo應用的微服務部署策略。本例中將指定Bookinfo總是使用review v3版本。

  1. 在ASM執行個體的default命名空間下,建立一個名為reviews的目標規則。具體操作,請參見管理虛擬服務

    展開查看目標規則YAML

    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
      name: reviews
    spec:
      host: reviews
      subsets:
      - name: v1
        labels:
          version: v1
      - name: v2
        labels:
          version: v2
      - name: v3
        labels:
          version: v3
  2. 在ASM執行個體的default命名空間下,建立一個名為reviews的虛擬服務。具體操作,請參見管理虛擬服務

    展開查看虛擬服務YAML

    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: reviews
    spec:
      hosts:
        - reviews
      http:
      - route:
        - destination:
            host: reviews
            subset: v3
  3. 在瀏覽器地址欄,輸入http://{Serverless入口網關的IP地址}/productpage,並多次重新整理頁面。

    可以看到reviews始終使用v3版本,即書評中為紅色星。書評用紅星