通过CR对同一K8s集群中多个应用进行泳道流量控制

EDAS升级了微服务治理的新模型,支持在单集群中针对复杂场景下的Java微服务应用,通过CR的方式实现全链路流量控制。EDAS中的全链路流量控制仅需通过三个自定义资源配置即可完成。

背景信息

image

针对流程图中各资源的解释如下:

  • LaneEntry:入口网关,负责接收流量的入口应用。

  • Lane:泳道,通过tag标签指定多个应用规划到泳道环境中,属于相同泳道的应用组成相同的环境分组。

  • LaneHTTPRoute:泳道入口流量规则。泳道入口流量规则将入口网关与目标泳道关联起来,描述了当流量经过入口网关时,如果其特征符合泳道入口流量规则的配置,则该流量将被转发至指定泳道的服务实例进行访问。

说明

本操作流程仅支持在相同集群内的应用进行全链路泳道流量控制,无法管理跨多个集群的应用。如果您在多集群环境中需要使用CR进行泳道流控管理,请在多个集群中选择一个操作集群来创建LaneEntryLaneHTTPRoute资源。分布在不同集群的应用可以通过Lane进行标签操作,而无需额外创建LaneEntryLaneHTTPRoute资源。

前提条件

操作步骤

应用A、B、C之间存在如下图所示的调用关系。在本示例中,还部署了应用B-grey和应用C-grey,它们与应用B和应用C提供相同的服务。在默认情况下(即未加入泳道),请求将在应用B、应用B-grey、应用C和应用C-grey之间进行轮询访问。

image

  1. 使用kubectl命令创建5个应用。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        cloudapp.alibabacloud.com/app: native-deploy-a
      name: native-deploy-a
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: native-deploy-a
      template:
        metadata:
          labels:
            app: native-deploy-a
        spec:
          containers:
            - name: group-1
              image: registry.cn-hangzhou.aliyuncs.com/edas_unified_test/sc-mq-a:headers
          imagePullSecrets:
            - name: test
    
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        cloudapp.alibabacloud.com/app: native-deploy-b
      name: native-deploy-b
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: native-deploy-b
      template:
        metadata:
          labels:
            app: native-deploy-b
        spec:
          containers:
            - name: group-1
              image: registry.cn-hangzhou.aliyuncs.com/edas_unified_test/sc-mq-b:headers
          imagePullSecrets:
            - name: test
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        cloudapp.alibabacloud.com/app: native-deploy-b-grey
      name: native-deploy-b-grey
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: native-deploy-b-grey
      template:
        metadata:
          labels:
            app: native-deploy-b-grey
        spec:
          containers:
            - name: group-1
              image: registry.cn-hangzhou.aliyuncs.com/edas_unified_test/sc-mq-b:headers
          imagePullSecrets:
            - name: test
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        cloudapp.alibabacloud.com/app: native-deploy-c
      name: native-deploy-c
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: native-deploy-c
      template:
        metadata:
          labels:
            app: native-deploy-c
        spec:
          containers:
            - name: group-1
              image: registry.cn-hangzhou.aliyuncs.com/edas_unified_test/sc-mq-c:headers
          imagePullSecrets:
            - name: test
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        cloudapp.alibabacloud.com/app: native-deploy-c-grey
      name: native-deploy-c-grey
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: native-deploy-c-grey
      template:
        metadata:
          labels:
            app: native-deploy-c-grey
        spec:
          containers:
            - name: group-1
              image: registry.cn-hangzhou.aliyuncs.com/edas_unified_test/sc-mq-c:headers
          imagePullSecrets:
            - name: test
  2. 访问应用A提供的接口,检查流量链路。

    for i in {1..20}; do curl http://127.0.0.1:8080/a?tag=zone ; sleep 0.3; echo ""; done

    image

    image

    说明

    经过验证,可以观察到在未配置泳道管理策略之前,应用B、应用B-grey、应用C及应用C-grey的实例均会被轮询访问。

  3. 创建泳道流控策略。

    apiVersion: cloudapp.alibabacloud.com/v1
    kind: LaneEntry
    metadata:
      name: lane-entry-demo
    spec:
      source: CLUSTER
      providerRef:
        name: native-deploy-a
        namespace: default
    ---
    apiVersion: cloudapp.alibabacloud.com/v1
    kind: Lane
    metadata:
      name: lane-sample
    spec:
      source: CLUSTER
      tag: grey
      enable: true
      targetRefs:
        - name: native-deploy-b-grey
          namespace: default
        - name: native-deploy-c-grey
          namespace: default
    ---
    apiVersion: cloudapp.alibabacloud.com/v1
    kind: LaneHTTPRoute
    metadata:
      name: lanehttproute-sample
    spec:
      parentRefs:
        - name: lane-entry-demo
      rules:
        - match:
            - path:
                type: Equal
                value: "/a"
              headers:
                - name: env
                  type: Equal
                  value: grey
              matchPolicy: AND
          backendRefs:
            - name: lane-sample

    在本示例中,应用A被指定为泳道入口网关,同时将应用B-grey和应用C-grey纳入泳道。设定为当进入网关的流量满足请求头env值为grey且访问/a路径时,将进入泳道访问泳道中的应用。

  4. 访问应用A提供的接口,分别验证符合流量规则的请求和不符合流量规则的请求,是否符合流控规则。

    image

    • 满足泳道规则的流量,将进入泳道访问应用B-grey和应用C-grey对应的实例。

    • 不满足泳道规则的流量,将访问应用B和应用C的实例。

  5. 在删除泳道流控规则配置后,访问应用A提供的接口,检查流量链路,发现流量控制效果已恢复,应用调用关系重新采用轮询访问方式。