consul_kv

    Thanks to @fatman-x guy, who developed this module, called , and its worker process data flow is below:

    Add following configuration in conf/config.yaml :

    And you can config it in short by default value:

    1. discovery:
    2. consul_kv:
    3. servers:
    4. - "http://127.0.0.1:8500"

    The keepalive has two optional values:

    • true, default and recommend value, use the long pull way to query consul servers
    • false, not recommend, it would use the short pull way to query consul servers, then you can set the fetch_interval for fetch interval

    Dump Data

    When we need reload apisix online, as the consul_kv module maybe loads data from CONSUL slower than load routes from ETCD, and would get the log at the moment before load successfully from consul:

    1. http_access_phase(): failed to set upstream: no valid upstream node

    The dump has three optional values now:

    • path, the dump file save path
      • support relative path, eg: logs/consul_kv.dump
      • support absolute path, eg: /tmp/consul_kv.bin
      • make sure the dump file’s parent path exist
      • make sure the apisix has the dump file’s read-write access permission,eg: chown www:root conf/upstream.d/
    • load_on_init, default value is true
      • if true, just try to load the data from the dump file before loading data from consul when starting, does not care the dump file exists or not
      • if false, ignore loading data from the dump file
      • Whether true or false, we don’t need to prepare a dump file for apisix at anytime
    • expire, unit sec, avoiding load expired dump data when load
      • default 0, it is unexpired forever
      • recommend 2592000, which is 30 days(equals 3600 * 24 * 30)

    Service register Key&Value template:

    1. Key: {Prefix}/{Service_Name}/{IP}:{Port}
    2. Value: {"weight": <Num>, "max_fails": <Num>, "fail_timeout": <Num>}

    The register consul key use upstreams as prefix by default. The http api service name called webpages for example, and you can also use webpages/oneteam/hello as service name. The api instance of node’s ip and port make up new key: <IP>:<Port>.

    Now, register nodes into consul:

    In some case, same keys exist in different consul servers. To avoid confusion, use the full consul key url path as service name in practice.

    L7

    Here is an example of routing a request with a URL of “/*“ to a service which named “ and use consul_kv discovery client in the registry :

    1. $ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
    2. {
    3. "uri": "/*",
    4. "upstream": {
    5. "service_name": "http://127.0.0.1:8500/v1/kv/upstreams/webpages/",
    6. "type": "roundrobin",
    7. "discovery_type": "consul_kv"
    8. }
    9. }'
    1. {
    2. "node": {
    3. "value": {
    4. "priority": 0,
    5. "update_time": 1612755230,
    6. "upstream": {
    7. "discovery_type": "consul_kv",
    8. "service_name": "http://127.0.0.1:8500/v1/kv/upstreams/webpages/",
    9. "hash_on": "vars",
    10. "pass_host": "pass"
    11. },
    12. "id": "1",
    13. "uri": "/*",
    14. "create_time": 1612755230,
    15. "status": 1
    16. },
    17. "key": "/apisix/routes/1"
    18. }
    19. }

    You could find more usage in the apisix/t/discovery/consul_kv.t file.

    L4

    Consul_kv service discovery also supports use in L4, the configuration method is similar to L7.

    1. $ curl http://127.0.0.1:9180/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
    2. {
    3. "remote_addr": "127.0.0.1",
    4. "upstream": {
    5. "scheme": "tcp",
    6. "service_name": "http://127.0.0.1:8500/v1/kv/upstreams/webpages/",
    7. "type": "roundrobin",
    8. "discovery_type": "consul_kv"
    9. }
    10. }'

    You could find more usage in the apisix/t/discovery/stream/consul_kv.t file.

    It also offers control api for debugging.

    For example:

    1. # curl http://127.0.0.1:9090/v1/discovery/consul_kv/dump | jq
    2. {
    3. "config": {
    4. "fetch_interval": 3,
    5. "timeout": {
    6. "wait": 60,
    7. "connect": 6000,
    8. "read": 6000
    9. },
    10. "prefix": "upstreams",
    11. "weight": 1,
    12. "servers": [
    13. "http://172.19.5.30:8500",
    14. "http://172.19.5.31:8500"
    15. ],
    16. "keepalive": true,
    17. "default_service": {
    18. "host": "172.19.5.11",
    19. "port": 8899,
    20. "metadata": {
    21. "fail_timeout": 1,
    22. "weight": 1,
    23. "max_fails": 1
    24. }
    25. },
    26. "skip_keys": [
    27. "upstreams/myapi/gateway/apisix/"
    28. ]
    29. },
    30. "services": {
    31. {
    32. "host": "127.0.0.1",
    33. "port": 30513,
    34. "weight": 1
    35. },
    36. {
    37. "host": "127.0.0.1",
    38. "port": 30514,
    39. "weight": 1
    40. }
    41. ],
    42. "http://172.19.5.30:8500/v1/kv/upstreams/1614480/grpc/": [
    43. {
    44. "host": "172.19.5.51",
    45. "port": 50051,
    46. "weight": 1
    47. }
    48. ],
    49. "http://172.19.5.30:8500/v1/kv/upstreams/webpages/": [
    50. {
    51. "host": "127.0.0.1",
    52. "port": 30511,
    53. "weight": 1
    54. },
    55. {
    56. "host": "127.0.0.1",
    57. "port": 30512,
    58. "weight": 1
    59. }
    60. ]
    61. }
    62. }

    It offers another control api for dump file view now. Maybe would add more api for debugging in future.

    1. GET /v1/discovery/consul_kv/show_dump_file
    1. curl http://127.0.0.1:9090/v1/discovery/consul_kv/show_dump_file | jq
    2. {
    3. "services": {
    4. "http://172.19.5.31:8500/v1/kv/upstreams/1614480/webpages/": [
    5. {
    6. "host": "172.19.5.12",
    7. "port": 8000,
    8. "weight": 120
    9. },
    10. {
    11. "host": "172.19.5.13",
    12. "port": 8000,
    13. "weight": 120
    14. }
    15. ]
    16. },
    17. "expire": 0,
    18. }