0


kong网关

1. kong简介

kong网关基于nginx,但是比nginx更加强大,nginx一般用来实现反向代理和负载均衡,但是nginx无法实现动态配置,也就是说如果我们扩展了机器或者添加路由配置,需要手动修改nginx的配置文件并重启nginx才能生效。但是kong是可以实现动态配置的,如果我们在kong中注册service或者添加route,只需要调用kong的API即可,对应的会把相关配置持久化到postgresql数据库,不需要重启kong也能即时生效,实现动态配置。

另外,kong作为API网关,不仅仅能实现nginx具备的反向代理和负载均衡功能,还可以基于插件化实现更多功能,比如身份认证、请求限流、黑白名单配置等,都可以基于kong的插件非常方便的实现。网关一个最大的作用就是可以将一些通用的操作都提取到网关层来实现,让底层的后端服务只需要关注具体的业务即可。

kong虽然基于nginx,但是有些概念却和nginx略有差异,比如kong中有target、upstream、service、route几个概念。

upstream主要是和target做绑定,然后service又和upstream绑定,这样就可以实现负载均衡,比如一个upstream绑定了三台服务器,然后某个service又和这个upstream关联上,那么当请求来到service进行处理时,就会将请求均衡的分发到upstream下的多个target。

kong默认有四个端口,8000、8001、8443、8444。其中8000和8001是http请求的端口(常用);8443和8444是https请求的端口。

  • 8000/8443是外部请求访问的端口,即进行反向代理请求的端口
  • 8001/8444是kong admin的端口,即调用kong的api使用的端口

2. kong常用API

2.1. 举例

以下举例kong的常用API:
(jq命令的作用是将返回结果进行json格式化,可通过yum命令安装jq)

  1. 查看所有的upstream
  1. curl-XGET localhost:8001/upstreams | jq .
  1. {"next": null,
  2. "data":[{"hash_on_header": null,
  3. "name":"ApiServer",
  4. "algorithm":"round-robin",
  5. "id":"c6c38821-a08c-4c05-b282-f63c501240a0",
  6. "hash_on_cookie": null,
  7. "hash_on_cookie_path":"/",
  8. "host_header": null,
  9. "hash_fallback_header": null,
  10. "tags": null,
  11. "hash_fallback":"none",
  12. "healthchecks":{"active":{"https_verify_certificate": true,
  13. "http_path":"/",
  14. "https_sni": null,
  15. "type":"http",
  16. "healthy":{"successes":0,
  17. "http_statuses":[200,
  18. 302],
  19. "interval":0},
  20. "unhealthy":{"tcp_failures":0,
  21. "timeouts":0,
  22. "http_failures":0,
  23. "http_statuses":[429,
  24. 404,
  25. 500,
  26. 501,
  27. 502,
  28. 503,
  29. 504,
  30. 505],
  31. "interval":0},
  32. "timeout":1,
  33. "concurrency":10},
  34. "passive":{"healthy":{"successes":0,
  35. "http_statuses":[200,
  36. 201,
  37. 202,
  38. 203,
  39. 204,
  40. 205,
  41. 206,
  42. 207,
  43. 208,
  44. 226,
  45. 300,
  46. 301,
  47. 302,
  48. 303,
  49. 304,
  50. 305,
  51. 306,
  52. 307,
  53. 308]},
  54. "unhealthy":{"http_statuses":[429,
  55. 500,
  56. 503],
  57. "tcp_failures":0,
  58. "timeouts":0,
  59. "http_failures":0},
  60. "type":"http"},
  61. "threshold":0},
  62. "client_certificate": null,
  63. "hash_on":"none",
  64. "sl505
  65. ],
  66. "interval": 0
  67. },
  68. "timeout": 1,
  69. "concurrency": 10
  70. },
  71. "passive": {
  72. "healthy": {
  73. "successes": 0,
  74. "http_statuses": [
  75. 200,
  76. 201,
  77. 202,
  78. 203,
  79. 204,
  80. 205,
  81. 206,
  82. 207,
  83. 208,
  84. 226,
  85. 300,
  86. 301,
  87. 302,
  88. 303,
  89. 304,
  90. 305,
  91. 306,
  92. 307,
  93. 308
  94. ]
  95. },
  96. "unhealthy": {
  97. "http_statuses": [
  98. 429,
  99. 500,
  100. 503
  101. ],
  102. "tcp_failures": 0,
  103. "timeouts": 0,
  104. "http_failures": 0
  105. },
  106. "type": "http"
  107. },
  108. "threshold": 0
  109. },
  110. "client_certificate": null,
  111. "hash_on": "none",
  112. "slots": 10000,
  113. "created_at": 1660789714}]}
  1. 查看某个upstream下关联的target
  1. curl-XGET localhost:8001/upstreams/c6c38821-a08c-4c05-b282-f63c501240a0/targets | jq .
  1. {"next": null,
  2. "data":[{"id":"7d57a020-cec7-49c8-9dc6-387532e98c22",
  3. "tags": null,
  4. "target":"192.168.1.1:8080",
  5. "created_at":1666681580.184,
  6. "upstream":{"id":"c6c38821-a08c-4c05-b282-f63c501240a0"},
  7. "weight":100},
  8. {"id":"a64e44c2-50fe-49cb-8cc9-6df0265dd2e6",
  9. "tags": null,
  10. "target":"192.168.1.2:8080",
  11. "created_at":1660789905.537,
  12. "upstream":{"id":"c6c38821-a08c-4c05-b282-f63c501240a0"},
  13. "weight":100},
  14. {"id":"4f0f77d6-31cb-4f29-ae93-0f15c443d507",
  15. "tags": null,
  16. "target":"192.168.1.3:8080",
  17. "created_at":1660789812.133,
  18. "upstream":{"id":"c6c38821-a08c-4c05-b282-f63c501240a0"},
  19. "weight":100}]}
  1. 查看所有service
  1. curl -XGET localhost:8001/services | jq .
  1. {"next": null,
  2. "data":[{"connect_timeout":10000,
  3. "tls_verify": null,
  4. "read_timeout":10000,
  5. "name":"CA70B82CCB4E4BE494AC8E3DA6A17084",
  6. "tags": null,
  7. "ca_certificates": null,
  8. "created_at":1668656595,
  9. "updated_at":1668656595,
  10. "protocol":"http",
  11. "port":80,
  12. "id":"a976b162-1c1f-4200-b2c4-357ab6ece8e6",
  13. "client_certificate": null,
  14. "host":"ApiServer",
  15. "path":"/dataservice/customize/api",
  16. "write_timeout":10000,
  17. "retries":5,
  18. "tls_verify_depth": null
  19. }]}
  1. 查看某个service信息
  1. curl-XGET localhost:8001/services/a976b162-1c1f-4200-b2c4-357ab6ece8e6 | jq .
  1. {"connect_timeout":10000,
  2. "tls_verify": null,
  3. "read_timeout":10000,
  4. "name":"CA70B82CCB4E4BE494AC8E3DA6A17084",
  5. "tags": null,
  6. "ca_certificates": null,
  7. "created_at":1668656595,
  8. "updated_at":1668656595,
  9. "protocol":"http",
  10. "port":80,
  11. "id":"a976b162-1c1f-4200-b2c4-357ab6ece8e6",
  12. "client_certificate": null,
  13. "host":"ApiServer",
  14. "path":"/dataservice/customize/api",
  15. "write_timeout":10000,
  16. "retries":5,
  17. "tls_verify_depth": null
  18. }
  1. 查看所有route
  1. curl-XGET localhost:8001/routes | jq .
  1. {"next": null,
  2. "data":[{"protocols":["http",
  3. "https"],
  4. "name":"CA70B82CCB4E4BE494AC8E3DA6A17084",
  5. "snis": null,
  6. "tags": null,
  7. "request_buffering": true,
  8. "response_buffering": true,
  9. "created_at":1668656595,
  10. "updated_at":1668656595,
  11. "sources": null,
  12. "preserve_host": false,
  13. "destinations": null,
  14. "strip_path": false,
  15. "methods":["GET"],
  16. "id":"dfb4baec-aaa5-4463-992c-fa45bc3c68f4",
  17. "path_handling":"v0",
  18. "headers":{"API-ID":["CA70B82CCB4E4BE494AC8E3DA6A17084"]},
  19. "https_redirect_status_code":426,
  20. "regex_priority":0,
  21. "paths":["/test1"],
  22. "hosts": null,
  23. "service":{"id":"a976b162-1c1f-4200-b2c4-357ab6ece8e6"}}]}
  1. 查看某个route信息
  1. curl-XGET localhost:8001/routes/dfb4baec-aaa5-4463-992c-fa45bc3c68f4 | jq .
  1. {"protocols":["http",
  2. "https"],
  3. "name":"CA70B82CCB4E4BE494AC8E3DA6A17084",
  4. "snis": null,
  5. "tags": null,
  6. "request_buffering": true,
  7. "response_buffering": true,
  8. "created_at":1668656595,
  9. "updated_at":1668656595,
  10. "sources": null,
  11. "preserve_host": false,
  12. "destinations": null,
  13. "strip_path": false,
  14. "methods":["GET"],
  15. "id":"dfb4baec-aaa5-4463-992c-fa45bc3c68f4",
  16. "path_handling":"v0",
  17. "headers":{"API-ID":["CA70B82CCB4E4BE494AC8E3DA6A17084"]},
  18. "https_redirect_status_code":426,
  19. "regex_priority":0,
  20. "paths":["/test1"],
  21. "hosts": null,
  22. "service":{"id":"a976b162-1c1f-4200-b2c4-357ab6ece8e6"}}

kong网关对于每个请求的处理逻辑是:
请添加图片描述
基于上述kong命令示例查询出来的关于target、upstream、service、route信息理解kong处理请求的过程。假设现在调用一个请求是:

  1. http://localhost:8080/test1

,那么首先会根据

  1. /test1

(route)找到对应的service,即id为

  1. a976b162-1c1f-4200-b2c4-357ab6ece8e6

的service,由于该service的path还指定了

  1. "/dataservice/customize/api"

,所以请求地址变成:

  1. /dataservice/customize/api/test1

,然后再根据service绑定的upstream,即

  1. ApiServer

,找到

  1. ApiServer

这个upstream绑定的target,对应到后端服务是有三台服务器的,可以实现负载均衡,所以经过kong代理之后的实际请求地址变成

  1. http://192.168.1.1:8080/dataservice/customize/api/test1

,会由

  1. 192.168.1.1

这台服务器的后端服务进行处理,当然也可能是

  1. 192.168.1.2

  1. 192.168.1.3

这两台机器。

2.2. GET请求相关API(查看服务、查看路由等)

  1. # 1. 查看所有的upstreamcurl-XGET localhost:8001/upstreams
  2. # 2. 查看某个upstream下关联的targetcurl-XGET localhost:8001/upstreams/UPSTREAM_ID/targets
  3. # 3. 查看所有servicecurl-XGET localhost:8001/services
  4. # 4. 查看某个service详细信息curl-XGET localhost:8001/services/SERVICE_ID
  5. # 5. 查看所有routecurl-XGET localhost:8001/routes
  6. # 6. 查看某个route详细信息curl-XGET localhost:8001/routes/ROUTE_ID
  7. # 7. 查看已添加的kong插件curl-XGET localhost:8001/plugins
  8. # 8. 查看所有消费者curl-XGET localhost:8001/consumers
  9. # 9. 查看某个消费者详细信息curl-XGET localhost:8001/consumers/CONSUMER_ID

2.3. POST请求相关API(添加服务、注册路由等)

  1. # 1. 创建upstreamcurl-X POST localhost:8001/upstreams --data"name=test-upstream"# 2. 给某个upstream绑定多个targetcurl-X POST localhost:8001/upstreams/test-upstream/targets --data"target=192.168.1.1:8080"--data"weight=100"curl-X POST localhost:8001/upstreams/test-upstream/targets --data"target=192.168.1.2:8080"--data"weight=100"# 3. 添加service,并且指定该service关联的target(path参数可选)curl-X POST localhost:8001/services --data"name=test-service"--data"host=test-upstream"--data"path=/pms"# 4. 注册route到某个service下curl-X POST localhost:8001/services/test-service/routes --data"name=test-route"--data"paths[]=/test1"

3. kong添加官方插件

kong官方提供了丰富的插件,基于插件可以实现很多网关层的功能,例如身份认证、请求限流、黑白名单配置等。都可以基于kong的插件快速的实现。
kong插件的作用粒度可以是service、route、consumer或者全局生效。

关于kong官方提供的插件可参考官网:https://docs.konghq.com/hub/

3.1. 配置Basic Auth插件

Basic Auth插件是官方提供的身份认证插件,插件的作用粒度可以是service、route或者全局;即可以配置访问某个service/某个route或者经过kong代理的所有请求都需要进行身份认证。
配置Basic Auth插件可参考官方文档:https://docs.konghq.com/hub/kong-inc/basic-auth/

主要包含以下几个步骤:

  1. 为service/route/全局添加Basic Auth插件 注意:插件的名称需要从官网的插件库中获取
  1. # 在service上配置插件curl-X POST http://localhost:8001/services/SERVICE_NAME|SERVICE_ID/plugins \--data"name=basic-auth"\--data"config.hide_credentials=true"# 在route上配置插件curl-X POST http://localhost:8001/routes/ROUTE_NAME|ROUTE_ID/plugins \--data"name=basic-auth"\--data"config.hide_credentials=true"# 在全局上配置插件curl-X POST http://localhost:8001/plugins/ \--data"name=basic-auth"\--data"config.hide_credentials=true"
  1. 创建一个消费者
  1. curl-i-X POST http://localhost:8001/consumers/ \--data"username=Jason"
  1. 在该消费者下创建用户
  1. curl-X POST http://localhost:8001/consumers/Jason/basic-auth \--data"username=test"\--data"password=123456"
  1. 配置完成后,调用相关的API需要带上用户名密码或者在请求头加上Authorization参数

4. kong添加自定义插件

安装kong之后,系统会自带一些内置插件,插件的路径在

  1. /usr/local/share/lua/5.1/kong/plugins

目录下,每个插件是一个单独的文件夹,插件的开发都是基于lua语言编写实现的。

如果官方的插件不满足我们的需求,我们还可以自己开发插件,自己开发的插件主要就是实现两个脚本,handler.lua和schema.lua,然后放到一个文件夹(文件夹的命名就是自定义插件的名称),并拷贝到

  1. /usr/local/share/lua/5.1/kong/plugins

目录下,最后调用kong的API将自开发的插件添加到对应的service或者route即可。

自定义插件的开发可以参考官网:https://docs.konghq.com/gateway/latest/plugin-development/

补充关于正向代理和反向代理的区别:

标签: kong nginx 网关

本文转载自: https://blog.csdn.net/can_chen/article/details/127956204
版权归原作者 can_chen 所有, 如有侵权,请联系我们删除。

“kong网关”的评论:

还没有评论