traffic-split

    Note: The ratio between each upstream may not so accurate since the drawback of weighted round robin algorithm (especially when the wrr state is reset).

    Currently, in the configuration of weighted_upstreams.upstream, the unsupported fields are: service_name, discovery_type, checks, retries, retry_timeout, desc, scheme, labels, create_time and update_time. But you can use weighted_upstreams.upstream_id to bind the upstream object to achieve their functions.

    The traffic-split plugin is mainly composed of two parts: match and weighted_upstreams. match is a custom conditional rule, and weighted_upstreams is upstream configuration information. If you configure match and weighted_upstreams information, then after the match rule is verified, it will be based on the weight value in weighted_upstreams; the ratio of traffic between each upstream in the plugin will be guided, otherwise, all traffic will be directly Reach the upstream configured on route or service. Of course, you can also configure only the weighted_upstreams part, which will directly guide the traffic ratio between each upstream in the plugin based on the weight value in weighted_upstreams.

    Note: 1. In match, the expression in vars is the relationship of and, and the relationship between multiple vars is the relationship of or. 2. In the weighted_upstreams field of the plugin, if there is a structure with only weight, it means the upstream traffic weight value on route or service. Such as:

    Create a route and enable the traffic-split plugin. When configuring the upstream information of the plugin, there are two ways:

    1. Configure upstream information through the upstream attribute in the plugin.
    1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "uri": "/index.html",
    4. "plugins": {
    5. "traffic-split": {
    6. "rules": [
    7. {
    8. "weighted_upstreams": [
    9. {
    10. "upstream": {
    11. "name": "upstream_A",
    12. "type": "roundrobin",
    13. "nodes": {
    14. "127.0.0.1:1981":10
    15. },
    16. "timeout": {
    17. "connect": 15,
    18. "send": 15,
    19. "read": 15
    20. }
    21. },
    22. "weight": 1
    23. },
    24. {
    25. "weight": 1
    26. }
    27. ]
    28. }
    29. ]
    30. }
    31. },
    32. "upstream": {
    33. "type": "roundrobin",
    34. "nodes": {
    35. "127.0.0.1:1980": 1
    36. }
    37. }
    38. }'
    1. Use the upstream_id attribute in the plugin to bind upstream.
    1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "uri": "/index.html",
    4. "plugins": {
    5. "traffic-split": {
    6. "rules": [
    7. {
    8. "weighted_upstreams": [
    9. {
    10. "upstream_id": 1,
    11. "weight": 1
    12. },
    13. {
    14. "weight": 1
    15. }
    16. ]
    17. }
    18. ]
    19. }
    20. },
    21. "upstream": {
    22. "type": "roundrobin",
    23. "nodes": {
    24. "127.0.0.1:1980": 1
    25. }
    26. }
    27. }'

    The match rule part is missing, and the traffic is split according to the weight value configured by the weighted_upstreams in the plugin. Divide plugin's upstream and route's upstream according to the traffic ratio of 3:2, of which 60% of the traffic reaches the upstream of the 1981 port in the plugin, and 40% of the traffic reaches the default 1980 port on the route Upstream.

    1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "uri": "/index.html",
    4. "plugins": {
    5. "traffic-split": {
    6. "rules": [
    7. {
    8. "weighted_upstreams": [
    9. {
    10. "upstream": {
    11. "name": "upstream_A",
    12. "type": "roundrobin",
    13. "nodes": {
    14. "127.0.0.1:1981":10
    15. },
    16. "timeout": {
    17. "connect": 15,
    18. "send": 15,
    19. "read": 15
    20. }
    21. },
    22. "weight": 3
    23. },
    24. {
    25. "weight": 2
    26. }
    27. ]
    28. }
    29. ]
    30. }
    31. },
    32. "upstream": {
    33. "type": "roundrobin",
    34. "nodes": {
    35. }
    36. }
    37. }'

    Test plugin:

    There are 5 requests, 3 requests hit the upstream of port 1981 of the plugin, and 2 requests hit the upstream of port 1980 of route.

    1. $ curl http://127.0.0.1:9080/index.html -i
    2. HTTP/1.1 200 OK
    3. Content-Type: text/html; charset=utf-8
    4. hello 1980
    5. HTTP/1.1 200 OK
    6. Content-Type: text/html; charset=utf-8
    7. world 1981
    8. ......

    Get the match rule parameter through the request header (you can also get it through the request parameter or NGINX variable). After the match rule is matched, it means that all requests hit the upstream configured by the plugin, otherwise the request only hits the route configured upstream.

    1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "uri": "/index.html",
    4. "plugins": {
    5. "traffic-split": {
    6. "rules": [
    7. {
    8. "match": [
    9. {
    10. "vars": [
    11. ["http_release","==","new_release"]
    12. ]
    13. }
    14. ],
    15. "weighted_upstreams": [
    16. {
    17. "upstream": {
    18. "name": "upstream_A",
    19. "type": "roundrobin",
    20. "nodes": {
    21. "127.0.0.1:1981":10
    22. }
    23. }
    24. }
    25. ]
    26. }
    27. ]
    28. }
    29. },
    30. "upstream": {
    31. "type": "roundrobin",
    32. "nodes": {
    33. "127.0.0.1:1980": 1
    34. }
    35. }
    36. }'

    The rule of match is matched, and all requests hit the upstream port 1981 configured by the plugin:

    1. $ curl http://127.0.0.1:9080/index.html -H 'release: new_release' -i
    2. HTTP/1.1 200 OK
    3. Content-Type: text/html; charset=utf-8
    4. ......
    5. world 1981

    The match rule fails to match, and all requests hit the 1980 port upstream configured on the route:

    Multiple vars rules can be set in match. Multiple expressions in vars have an and relationship, and multiple vars rules have an or relationship; as long as one of the vars is required If the rule passes, the entire match passes.

    Example 1: Only one vars rule is configured, and multiple expressions in vars are in the relationship of and. In weighted_upstreams, the traffic is divided into 3:2 according to the value of weight, of which only the part of the weight value represents the proportion of upstream on the route. When match fails to pass, all traffic will only hit the upstream on the route.

    1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "uri": "/index.html",
    4. "plugins": {
    5. "traffic-split": {
    6. "rules": [
    7. {
    8. "match": [
    9. {
    10. "vars": [
    11. ["arg_name","==","jack"],
    12. ["http_user-id",">","23"],
    13. ["http_apisix-key","~~","[a-z]+"]
    14. ]
    15. }
    16. ],
    17. "weighted_upstreams": [
    18. {
    19. "upstream": {
    20. "name": "upstream_A",
    21. "type": "roundrobin",
    22. "nodes": {
    23. "127.0.0.1:1981":10
    24. }
    25. },
    26. "weight": 3
    27. },
    28. {
    29. "weight": 2
    30. }
    31. ]
    32. }
    33. ]
    34. }
    35. },
    36. "upstream": {
    37. "type": "roundrobin",
    38. "nodes": {
    39. "127.0.0.1:1980": 1
    40. }
    41. }
    42. }'

    The plugin sets the requested match rule and upstream with port 1981, and the route has upstream with port 1980.

    Test plugin:

    The match rule is successfully verified, and the upstream port of 1981 is hit.

    1. $ curl 'http://127.0.0.1:9080/index.html?name=jack' -H 'user-id:30' -H 'apisix-key: hello' -i
    2. HTTP/1.1 200 OK
    3. Content-Type: text/html; charset=utf-8
    4. ......
    5. world 1981

    The match rule fails to verify, and it hits the upstream of the default port of 1980.

    1. $ curl 'http://127.0.0.1:9080/index.html?name=jack' -H 'user-id:30' -H 'apisix-key: hello' -i
    2. HTTP/1.1 200 OK
    3. Content-Type: text/html; charset=utf-8
    4. ......
    5. hello 1980

    After 5 requests, the service of port 1981 was hit 3 times, and the service of port 1980 was hit 2 times.

    Example 2: Configure multiple vars rules. Multiple expressions in vars are and relationships, and multiple vars are or relationships. According to the weight value in weighted_upstreams, the traffic is divided into 3:2, where only the part of the weight value represents the proportion of upstream on the route. When match fails to pass, all traffic will only hit the upstream on the route.

    1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "uri": "/index.html",
    4. "plugins": {
    5. "traffic-split": {
    6. {
    7. "match": [
    8. {
    9. "vars": [
    10. ["arg_name","==","jack"],
    11. ["http_user-id",">","23"],
    12. ["http_apisix-key","~~","[a-z]+"]
    13. ]
    14. },
    15. {
    16. "vars": [
    17. ["http_user-id2","!",">","33"],
    18. ["http_apisix-key2","~~","[a-z]+"]
    19. ]
    20. }
    21. ],
    22. "weighted_upstreams": [
    23. {
    24. "upstream": {
    25. "name": "upstream_A",
    26. "type": "roundrobin",
    27. "nodes": {
    28. "127.0.0.1:1981":10
    29. }
    30. },
    31. "weight": 3
    32. },
    33. {
    34. "weight": 2
    35. }
    36. ]
    37. }
    38. ]
    39. }
    40. },
    41. "upstream": {
    42. "type": "roundrobin",
    43. "nodes": {
    44. "127.0.0.1:1980": 1
    45. }
    46. }
    47. }'

    Test plugin:

    1. $ curl 'http://127.0.0.1:9080/index.html?name=jack&name2=rose' -H 'user-id:30' -H 'user-id2:22' -H 'apisix-key: hello' -H 'apisix-key2: world' -i
    2. HTTP/1.1 200 OK
    3. Content-Type: text/html; charset=utf-8
    4. ......
    5. world 1981
    1. $ curl 'http://127.0.0.1:9080/index.html?name=jack&name2=rose' -H 'user-id:30' -H 'user-id2:22' -H 'apisix-key: hello' -H 'apisix-key2: world' -i
    2. HTTP/1.1 200 OK
    3. Content-Type: text/html; charset=utf-8
    4. ......
    5. hello 1980

    After 5 requests, the service of port 1981 was hit 3 times, and the service of port 1980 was hit 2 times.

    1. $ curl 'http://127.0.0.1:9080/index.html?name=jack' -H 'user-id:30' -H 'user-id2:22' -H 'apisix-key: hello' -H 'apisix-key2: world' -i
    2. HTTP/1.1 200 OK
    3. Content-Type: text/html; charset=utf-8
    4. ......
    5. hello 1980

    After 5 requests, the service of port 1981 was hit 3 times, and the service of port 1980 was hit 2 times.

    1. $ curl 'http://127.0.0.1:9080/index.html?name=jack' -i
    2. HTTP/1.1 200 OK
    3. Content-Type: text/html; charset=utf-8
    4. ......
    5. hello 1980

    By configuring multiple rules, we can achieve one-to-one correspondence between different matching rules and upstream.

    Example:

    When the request header x-api-id is equal to 1, it hits the upstream with port 1981; when x-api-id is equal to 2, it hits the upstream with port 1982; otherwise, it hits the upstream with port 1980 (the upstream response data is the corresponding port number).

    1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "uri": "/hello",
    4. "plugins": {
    5. "traffic-split": {
    6. "rules": [
    7. {
    8. "match": [
    9. {
    10. "vars": [
    11. ["http_x-api-id","==","1"]
    12. ]
    13. }
    14. ],
    15. "weighted_upstreams": [
    16. {
    17. "upstream": {
    18. "name": "upstream-A",
    19. "type": "roundrobin",
    20. "nodes": {
    21. "127.0.0.1:1981":1
    22. }
    23. },
    24. "weight": 3
    25. }
    26. ]
    27. },
    28. {
    29. "match": [
    30. {
    31. "vars": [
    32. ["http_x-api-id","==","2"]
    33. ]
    34. }
    35. ],
    36. "weighted_upstreams": [
    37. {
    38. "upstream": {
    39. "name": "upstream-B",
    40. "type": "roundrobin",
    41. "nodes": {
    42. "127.0.0.1:1982":1
    43. }
    44. },
    45. "weight": 3
    46. }
    47. ]
    48. }
    49. ]
    50. }
    51. },
    52. "upstream": {
    53. "type": "roundrobin",
    54. "nodes": {
    55. "127.0.0.1:1980": 1
    56. }
    57. }
    58. }'

    Test plugin:

    The request header x-api-id is equal to 1, hitting the upstream with the 1981 port.

    1. $ curl http://127.0.0.1:9080/hello -H 'x-api-id: 1'
    2. 1981

    The request header x-api-id is equal to 2, hitting the upstream with the 1982 port.

    1. $ curl http://127.0.0.1:9080/hello -H 'x-api-id: 2'
    2. 1982

    The request header x-api-id is equal to 3, the rule does not match, and it hits the upstream with port 1980.

    1. 1980