入门示例

本文介绍如何快速将应用部署到Ambient Mesh模式。

前提条件

  • 已创建符合以下条件的Kubernetes集群。更多信息,请参见使用限制

    • 网络插件:从ASM 1.21版本开始, 兼容支持Terway和Flannel网络插件,kube-proxy代理支持iptables和IPVS模式。 同时, 支持Terway插件下的IPvlan模式以及NetworkPolicy能力。

    • 操作系统:支持Alibaba Cloud Linux 2和Alibaba Cloud Linux 3。

    • 如果需要在ACK Serverless集群、 Edge集群、注册集群中使用Ambient Mesh模式, 请提交工单获得技术支持。

  • 已创建启用Ambient Mesh模式的ASM实例。

    创建服务网格页面的数据面模式区域,选中启用Ambient Mesh模式,其他配置项请按照实际情况进行配置。具体操作,请参见创建ASM实例

  • 已为ASM实例添加ACK集群。具体操作,请参见添加集群到ASM实例

  • 已创建入口网关。具体操作,请参见创建入口网关

  • 已按照实际操作系统及平台,下载Istioctl服务网格调试工具。详细信息,请参见Istio

重要

在本操作文档中,您可能需要反复切换 Kubernetes 上下文(context)以操作数据面集群和控制面集群。为了避免误操作,请您在每次上下文切换时,务必确认当前上下文是否正确。您可以使用kubectx简化上下文切换的操作,具体步骤,请参见kubectx。您也可以通过开启通过数据面集群KubeAPI访问Istio资源,使用数据面集群KubeAPI直接操作控制面集群。

步骤一:部署示例应用

本文使用bookinfo作为示例应用。更多信息,请参见在ASM实例关联的集群中部署应用

ACK集群中部署示例应用

  1. 登录ASM控制台,在左侧导航栏,选择服务网格 > 网格管理

  2. 网格管理页面,单击目标实例名称,然后在左侧导航栏,选择网格实例 > 全局命名空间

  3. 全局命名空间页面的数据面模式列,单击default命名空间对应的切换为Ambient Mesh模式,然后在确认对话框,单击确定

  4. 部署Bookinfo应用。

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

      展开查看YAML内容

      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: registry-cn-hangzhou.ack.aliyuncs.com/acs/examples-bookinfo-details-v1:1.19.1
              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: registry-cn-hangzhou.ack.aliyuncs.com/acs/examples-bookinfo-ratings-v1:1.19.1
              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: registry-cn-hangzhou.ack.aliyuncs.com/acs/examples-bookinfo-reviews-v1:1.19.1
              imagePullPolicy: IfNotPresent
              env:
              - name: LOG_DIR
                value: "/tmp/logs"
              ports:
              - containerPort: 9080
              volumeMounts:
              - name: tmp
                mountPath: /tmp
              - name: wlp-output
                mountPath: /opt/ibm/wlp/output
            volumes:
            - name: wlp-output
              emptyDir: {}
            - name: tmp
              emptyDir: {}
      ---
      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: registry-cn-hangzhou.ack.aliyuncs.com/acs/examples-bookinfo-reviews-v2:1.19.1
              imagePullPolicy: IfNotPresent
              env:
              - name: LOG_DIR
                value: "/tmp/logs"
              ports:
              - containerPort: 9080
              volumeMounts:
              - name: tmp
                mountPath: /tmp
              - name: wlp-output
                mountPath: /opt/ibm/wlp/output
            volumes:
            - name: wlp-output
              emptyDir: {}
            - name: tmp
              emptyDir: {}
      ---
      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: registry-cn-hangzhou.ack.aliyuncs.com/acs/examples-bookinfo-reviews-v3:1.19.1
              imagePullPolicy: IfNotPresent
              env:
              - name: LOG_DIR
                value: "/tmp/logs"
              ports:
              - containerPort: 9080
              volumeMounts:
              - name: tmp
                mountPath: /tmp
              - name: wlp-output
                mountPath: /opt/ibm/wlp/output
            volumes:
            - name: wlp-output
              emptyDir: {}
            - name: tmp
              emptyDir: {}
      ---
      ##################################################################################################
      # 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:
            annotations:
              prometheus.io/scrape: "true"
              prometheus.io/port: "9080"
              prometheus.io/path: "/metrics"
            labels:
              app: productpage
              version: v1
          spec:
            serviceAccountName: bookinfo-productpage
            containers:
            - name: productpage
              image: registry-cn-hangzhou.ack.aliyuncs.com/acs/examples-bookinfo-productpage-v1:1.19.1
              imagePullPolicy: IfNotPresent
              ports:
              - containerPort: 9080
              volumeMounts:
              - name: tmp
                mountPath: /tmp
            volumes:
            - name: tmp
              emptyDir: {}
      ---
    2. 执行以下命令,在default命名空间中部署该示例应用。

      kubectl apply -f  bookinfo.yaml
  5. 部署Sleep应用。

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

      展开查看YAML内容

      apiVersion: v1
      kind: ServiceAccount
      metadata:
        name: sleep
      ---
      apiVersion: v1
      kind: Service
      metadata:
        name: sleep
        labels:
          app: sleep
          service: sleep
      spec:
        ports:
        - port: 80
          name: http
        selector:
          app: sleep
      ---
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: sleep
      spec:
        replicas: 1
        selector:
          matchLabels:
            app: sleep
        template:
          metadata:
            labels:
              app: sleep
          spec:
            terminationGracePeriodSeconds: 0
            serviceAccountName: sleep
            containers:
            - name: sleep
              image: curlimages/curl	registry-cn-hangzhou.ack.aliyuncs.com/ack-demo/curl:asm-sleep
              command: ["/bin/sleep", "infinity"]
              imagePullPolicy: IfNotPresent
              volumeMounts:
              - mountPath: /etc/sleep/tls
                name: secret-volume
            volumes:
            - name: secret-volume
              secret:
                secretName: sleep-secret
                optional: true
      ---

    2. 执行以下命令,在default命名空间中部署该示例应用。

      kubectl apply -f  sleep.yaml
  6. 部署Notsleep应用。

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

      展开查看YAML内容

      apiVersion: v1
      kind: ServiceAccount
      metadata:
        name: notsleep
      ---
      apiVersion: v1
      kind: Service
      metadata:
        name: notsleep
        labels:
          app: notsleep
          service: notsleep
      spec:
        ports:
        - port: 80
          name: http
        selector:
          app: notsleep
      ---
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: notsleep
      spec:
        replicas: 1
        selector:
          matchLabels:
            app: notsleep
        template:
          metadata:
            labels:
              app: notsleep
          spec:
            affinity:
              podAntiAffinity:
                preferredDuringSchedulingIgnoredDuringExecution:
                - weight: 100
                  podAffinityTerm:
                    labelSelector:
                      matchExpressions:
                      - key: app
                        operator: In
                        values:
                        - productpage
                    topologyKey: kubernetes.io/hostname 
            terminationGracePeriodSeconds: 0
            serviceAccountName: notsleep
            containers:
            - name: notsleep
              image: registry-cn-hangzhou.ack.aliyuncs.com/ack-demo/curl:asm-sleep
              command: ["/bin/sleep", "3650d"]
              imagePullPolicy: IfNotPresent
              volumeMounts:
              - mountPath: /etc/sleep/tls
                name: secret-volume
            volumes:
            - name: secret-volume
              secret:
                secretName: notsleep-secret
                optional: true
      ---
      
    2. 执行以下命令,在default命名空间中部署该示例应用。

      kubectl apply -f  notsleep.yaml

在ASM实例中部署网格资源

  1. 使用以下内容,创建bookinfo-gateway.yaml文件。

    YAML文件用于创建网关规则Gateway对象和虚拟服务VirtualService对象。

    展开查看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:
        - "*"
    ---
    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实例对应的KubeConfig环境下,执行以下命令,部署网格资源。

    kubectl apply -f bookinfo-gateway.yaml

验证基本功能

  1. 执行以下命令,设置入口网关的环境变量。

    export GATEWAY_HOST=istio-ingressgateway.istio-system
    export GATEWAY_SERVICE_ACCOUNT=ns/istio-system/sa/istio-ingressgateway
  2. 测试bookinfo应用程序,查看在有无网关的情况下能否正常运行。

    1. 执行以下命令:

      kubectl exec deploy/sleep -- curl -s "http://$GATEWAY_HOST/productpage" | grep -o "<title>.*</title>"

      预期输出:

      <title>Simple Bookstore App</title>
    2. 执行以下命令:

      kubectl exec deploy/sleep -- curl -s http://productpage:9080/ | grep -o "<title>.*</title>"

      预期输出:

      <title>Simple Bookstore App</title>
    3. 执行以下命令:

      kubectl exec deploy/notsleep -- curl -s http://productpage:9080/ | grep -o "<title>.*</title>"

      预期输出:

      <title>Simple Bookstore App</title>

      以上结果表明在有无网关下的情况下,bookinfo应用程序均可以正常运行。

步骤二:启用授权策略

将应用程序添加到Ambient Mesh后,您可以使用L4授权策略来保护应用程序访问。例如,可以根据客户端工作负载身份控制对服务的访问。

L4授权策略

  1. 使用以下内容,创建productpage-viewer.yaml。

    YAML文件用于定义授权策略,显式允许sleep应用和网关服务账户调用该productpage服务。

    apiVersion: security.istio.io/v1beta1
    kind: AuthorizationPolicy
    metadata:
     name: productpage-viewer
     namespace: default
    spec:
     selector:
       matchLabels:
         app: productpage
     action: ALLOW
     rules:
     - from:
       - source:
           principals:
           - cluster.local/ns/default/sa/sleep
           - cluster.local/ns/istio-system/sa/istio-ingressgateway
  2. 在ASM实例对应的KubeConfig环境下,执行以下命令,部署授权策略。

    kubectl apply -f productpage-viewer.yaml
  3. 验证授权策略是否生效。

    1. 执行以下命令:

      kubectl exec deploy/sleep -- curl -s "http://$GATEWAY_HOST/productpage" | grep -o "<title>.*</title>"

      预期输出:

      <title>Simple Bookstore App</title>
    2. 执行以下命令:

      kubectl exec deploy/sleep -- curl -s http://productpage:9080/ | grep -o "<title>.*</title>"

      预期输出:

      <title>Simple Bookstore App</title>
    3. 执行以下命令:

      kubectl exec deploy/notsleep -- curl -s http://productpage:9080/ | grep -o "<title>.*</title>"

      预期输出:

      command terminated with exit code 56

      以上结果表明授权策略生效。

L7授权策略

使用Kubernetes Gateway API,可以为bookinfo-productpage服务账户部署Waypoint代理,该代理用于productpage服务。任何流向productpage服务的流量都将由该7层代理路由。

  1. 执行以下命令,为bookinfo-productpage服务账户部署Waypoint代理。

    istioctl x waypoint apply --service-account bookinfo-productpage
  2. 执行以下命令,查看productpage的Waypoint代理状态。

    kubectl get gtw bookinfo-productpage -o yaml

    展开查看预期输出

    apiVersion: gateway.networking.k8s.io/v1beta1
    kind: Gateway
    metadata:
      annotations:
        gateway.istio.io/controller-version: "5"
        istio.io/for-service-account: bookinfo-productpage
      creationTimestamp: "2023-08-10T08:35:51Z"
      generation: 1
      name: bookinfo-productpage
      namespace: default
      resourceVersion: "7828921"
      uid: c085b788-a8fa-4a2c-8376-18d08689****
    spec:
      gatewayClassName: istio-waypoint
      listeners:
      - allowedRoutes:
          namespaces:
            from: Same
        name: mesh
        port: 15008
        protocol: HBONE
    status:
      conditions:
      - lastTransitionTime: "2023-08-10T08:35:51Z"
        message: Handled by Istio controller
        observedGeneration: 1
        reason: Accepted
        status: "True"
        type: Accepted
  3. 修改AuthorizationPolicy。

    1. 将productpage-viewer.yaml文件修改为如下内容,明确允许sleep和网关服务账户通过GET方式访问productpage服务,但不允许执行其他操作。

      apiVersion: security.istio.io/v1beta1
      kind: AuthorizationPolicy
      metadata:
       name: productpage-viewer
       namespace: default
      spec:
       selector:
         matchLabels:
           istio.io/gateway-name: bookinfo-productpage
       action: ALLOW
       rules:
       - from:
         - source:
             principals:
             - cluster.local/ns/default/sa/sleep
             - cluster.local/ns/istio-system/sa/istio-ingressgateway
         to:
         - operation:
             methods: ["GET"]
    2. 执行以下命令,重新部署AuthorizationPolicy。

      kubectl apply -f productpage-viewer.yaml
  4. 验证授权策略是否生效。

    1. 执行以下命令:

      kubectl exec deploy/sleep -- curl -s "http://$GATEWAY_HOST/productpage" -X DELETE

      预期输出:

       RBAC: access denied                                                                                                       
    2. 执行以下命令:

      kubectl exec deploy/notsleep -- curl -s http://productpage:9080/

      预期输出:

        RBAC: access denied
    3. 执行以下命令:

      kubectl exec deploy/sleep -- curl -s http://productpage:9080/ | grep -o "<title>.*</title>"

      预期输出:

       <title>Simple Bookstore App</title>

      以上结果表明授权策略生效。

步骤三:定义L7路由规则

  1. 执行以下命令,为reviews服务部署Waypoint代理,以便任何流向reviews服务的流量都将由Waypoint代理进行路由。

    istioctl x waypoint apply --service-account bookinfo-reviews
  2. 使用以下内容,创建reviews.yaml。

    配置流量路由以将90%的请求发送到reviews-v1,将10%的请求发送到reviews-v2。

    展开查看reviews.yaml

    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
      name: reviews
    spec:
      host: reviews
      trafficPolicy:
        loadBalancer:
          simple: RANDOM
      subsets:
      - name: v1
        labels:
          version: v1
      - name: v2
        labels:
          version: v2
      - name: v3
        labels:
          version: v3
    ---
    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: reviews
    spec:
      hosts:
        - reviews
      http:
      - route:
        - destination:
            host: reviews
            subset: v1
          weight: 90
        - destination:
            host: reviews
            subset: v2
          weight: 10
    
  3. 执行以下命令,部署DestinationRule。

    kubectl apply -f reviews.yaml
  4. 执行以下命令, 验证100个请求中是否约10%的流量流向reviews-v2。

    kubectl exec deploy/sleep -- sh -c "for i in \$(seq 1 100); do curl -s http://$GATEWAY_HOST/productpage | grep reviews-v.-; done"

    预期输出:

            <u>reviews-v1-5896f547f5-48zcn</u>
            <u>reviews-v1-5896f547f5-48zcn</u>
            <u>reviews-v2-5d99885bc9-qb5cv</u>
            <u>reviews-v1-5896f547f5-48zcn</u>
            <u>reviews-v1-5896f547f5-48zcn</u>
            <u>reviews-v1-5896f547f5-48zcn</u>
            <u>reviews-v1-5896f547f5-48zcn</u>
            <u>reviews-v1-5896f547f5-48zcn</u>
            <u>reviews-v1-5896f547f5-48zcn</u>
            <u>reviews-v1-5896f547f5-48zcn</u>

    预期输出表明L7路由规则生效。

步骤四:清理资源

执行以下命令,清理本文创建的资源对象。

istioctl x waypoint delete  --service-account bookinfo-productpage
istioctl x waypoint delete --service-account bookinfo-reviews
kubectl delete authorizationpolicy productpage-viewer