apisix根据源ip进行分流仅需两步

69 阅读1分钟

背景

为了给客户提供更稳定的产品,生产根据源ip进行灰度发布,产品公司内部访问最新版本产品,客户访问稳定版本

1. apisix服务需更改的配置

设置获取客户端ip

template/configmap.yaml

        real_ip_header: "X-Real-IP"    # http://nginx.org/en/docs/http/ngx_http_realip_module.html#real_ip_header
        real_ip_from:                  # http://nginx.org/en/docs/http/ngx_http_realip_module.html#set_real_ip_from
          - 127.0.0.1 
          - 'unix:'

注意:127.0.0.1 #这个配置是检测到这个ip,然后获取他的上一跳,定义为客户端的出口ip

2. apisix 路由配置

写入分流路由

curl http://apisix-admin.bugfix.svc.cluster.local:9180/apisix/admin/routes/6666 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '

{

  "uri": "/*",

  "hosts": ["fuwu.bugfix.wangwu.cn","*.wangwu.cn"],

  "priority": 2000,

  "vars": [

      ["uri", "~~", ".(htm|html|js|css|jpg|png|gif|ico|svg|json|map|mp4)$"]

  ],

  "plugins": {

    "redirect": {

        "http_to_https": true

    },

  "traffic-split": {

        "rules": [

          {

            "match": [

              {

                "vars": [

                   ["remote_addr", "in", ["110.19.19.100","10.1.19.120"]]

                ]

              }

            ],

            "weighted_upstreams": [

              {

                "upstream": {

                  "type": "roundrobin",

                  "nodes": {

                    "fuwu-b-svc.bugfix.svc.cluster.local:8080":1

                  }

                }

              }

            ]

          }

        ]

      }

  },

  "upstream":{

    "type": "roundrobin",

    "nodes":{

      "fuwu-svc.bugfix.svc.cluster.local:8080":1

    }

  }

}'

注意:

    1. 首先前面要有网关给X-Forward-X这个字段打上源ip,如loadbalance
    1. 要参考上篇文章配置解说里的:设置获取客户端ip

拓展知识

1. 获取自己出口ip

curl cip.cc

2. 官方文档地址

apisix.apache.org/zh/docs/api…