服务和路由

Kong Gateway管理员使用对象模型来定义其期望的流量管理策略。在该模型中,两个重要的对象是服务(services)和路由(routes)。服务和路由被协调地配置,以定义请求和响应在系统中经过的路由路径。

下面的高级概述显示请求到达路由(routes),并将其转发到服务(services),响应则沿相反路径进行返回:

服务和路由_第1张图片

服务是什么 

在Kong Gateway中,服务是对现有上游应用程序的抽象。服务可以存储插件配置、策略等一系列对象,并且它们可以与路由关联。

在定义一个服务时,管理员需要提供一个名称和与上游应用程序的连接信息。连接细节可以在url字段中以单个字符串的形式提供,也可以单独提供协议、主机、端口和路径的值。

服务与上游应用程序之间存在一对多的关系,这使得管理员能够创建复杂的流量管理行为。

路由是什么

 路由是指向上游应用程序内部资源的路径。将路由添加到服务中可以允许对底层应用程序进行访问。在Kong Gateway中,路由通常映射到通过Kong Gateway应用程序公开的端点。路由还可以定义匹配请求到相关服务的规则。因此,一个路由可以引用多个端点。基本的路由应该具有名称、路径或路径列表,并引用现有的服务。

您还可以使用以下选项配置路由:

  • 协议(Protocols):用于与上游应用程序进行通信的协议。
  • 主机(Hosts):与路由匹配的域名列表。
  • 方法(Methods):与路由匹配的HTTP方法。
  • 头部(Headers):请求头部中预期出现的值列表。
  • 重定向状态码(Redirect status codes):HTTPS状态码。
  • 标签(Tags):可选的一组字符串,用于对路由进行分组。

管理服务和路由 

以下教程将通过Kong Gateway管理员API来管理和测试服务和路由。Kong Gateway还提供其他配置管理选项,包括Kong Konnect和decK。

在本教程的这个部分中,您将完成以下步骤:

  • 创建一个指向httpbin API的服务,该API提供了对HTTP请求和响应进行测试的功能。
  • 通过提供一个URL路径来定义路由,客户端可以在运行中的Kong Gateway上访问该路径。
  • 使用新的httpbin服务来回显一个测试请求,帮助您了解Kong Gateway是如何代理API请求的。

管理服务

 1. 创建服务

要添加一个新的服务,请向Kong Gateway的Admin API的/services路由发送一个POST请求:

curl -i -s -X POST http://localhost:8001/services \
 --data name=example_service \
 --data url='http://httpbin.org'

这个请求指示Kong Gateway创建一个新的服务,映射到上游URL http://httpbin.org。

在我们的示例中,请求体包含两个字符串:

  • name: 服务的名称
  • url: 用于填充服务的主机、端口和路径属性的参数

如果您的请求成功,您将从Kong Gateway收到一个201的响应头,确认您的服务已创建,响应正文将类似于:

{
 "host": "httpbin.org",
 "name": "example_service",
 "enabled": true,
 "connect_timeout": 60000,
 "read_timeout": 60000,
 "retries": 5,
 "protocol": "http",
 "path": null,
 "port": 80,
 "tags": null,
 "client_certificate": null,
 "tls_verify": null,
 "created_at": 1661346938,
 "updated_at": 1661346938,
 "tls_verify_depth": null,
 "id": "3b2be74e-335b-4f25-9f08-6c41b4720315",
 "write_timeout": 60000,
 "ca_certificates": null
}

 没有在创建请求中显式提供的字段将根据当前Kong Gateway配置自动赋予默认值。

2. 查看服务配置

当您创建一个服务时,Kong Gateway会为它分配一个唯一的ID,如上面的响应中所示。id字段或创建服务时提供的名称可以用于在后续请求中标识该服务。这是服务的URL,其形式为/services/{服务名称或ID}。

要查看服务的当前状态,请向服务URL发送一个GET请求。

curl -X GET http://localhost:8001/services/example_service

 一个成功的请求将在响应正文中包含您的服务的当前配置,如以下片段所示:

{
 "host": "httpbin.org",
 "name": "example_service",
 "enabled": true,
 ...
}

 3. 更新服务

可以通过向服务URL发送PATCH请求来动态更新现有的服务配置。

要将服务的重试次数从5更改为6,请发送以下PATCH请求:

curl --request PATCH \
 --url localhost:8001/services/example_service \
 --data retries=6

 响应正文包含了完整的服务配置,包括更新后的值:

{
 "host": "httpbin.org",
 "name": "example_service",
 "enabled": true,
 "retries": 6,
 ...
}

 4. 列出服务

 您可以通过向基本的/services URL发送GET请求来列出所有当前的服务。

curl -X GET http://localhost:8001/services

Admin API文档提供了完整的服务更新规范。

您还可以通过在浏览器中导航到以下URL来查看Kong Manager UI中的服务配置:

Kong Manager OSS:http://localhost:8002/services
Kong Manager Enterprise:http://localhost:8002/default/services,其中default是工作区名称。

管理路由

1. 创建路由 

路由定义了Kong Gateway如何代理请求。您可以通过向服务URL发送POST请求来创建与特定服务关联的路由。

在/mock路径上配置一个新的路由,将流量定向到之前创建的example_service服务:

curl -i -X POST http://localhost:8001/services/example_service/routes \
 --data 'paths[]=/mock' \
 --data name=example_route

 如果路由成功创建,API将返回一个201响应代码和一个类似于以下的响应正文:

{
 "paths": [
   "/mock"
 ],
 "methods": null,
 "sources": null,
 "destinations": null,
 "name": "example_route",
 "headers": null,
 "hosts": null,
 "preserve_host": false,
 "regex_priority": 0,
 "snis": null,
 "https_redirect_status_code": 426,
 "tags": null,
 "protocols": [
   "http",
   "https"
 ],
 "path_handling": "v0",
 "id": "52d58293-ae25-4c69-acc8-6dd729718a61",
 "updated_at": 1661345592,
 "service": {
   "id": "c1e98b2b-6e77-476c-82ca-a5f1fb877e07"
 },
 "response_buffering": true,
 "strip_path": true,
 "request_buffering": true,
 "created_at": 1661345592
}

2. 查看路由配置

与服务一样,当您创建一个路由时,Kong Gateway会为其分配一个唯一的ID,如上面的响应中所示。id字段或创建路由时提供的名称可以用于在后续请求中标识路由。路由 URL 可以采用以下两种形式之一:

  • /services/{服务名称或ID}/routes/{路由名称或ID}
  • /routes/{路由名称或ID}

要查看example_route路由的当前状态,请向路由URL发送一个GET请求:

curl -X GET http://localhost:8001/services/example_service/routes/example_route

 响应正文包含了您的路由的当前配置:

{
 "paths": [
   "/mock"
 ],
 "methods": null,
 "sources": null,
 "destinations": null,
 "name": "example_route",
 "headers": null,
 "hosts": null,
 "preserve_host": false,
 "regex_priority": 0,
 "snis": null,
 "https_redirect_status_code": 426,
 "tags": null,
 "protocols": [
   "http",
   "https"
 ],
 "path_handling": "v0",
 "id": "189e0a57-205a-4f48-aec6-d57f2e8a9985",
 "updated_at": 1661347991,
 "service": {
   "id": "3b2be74e-335b-4f25-9f08-6c41b4720315"
 },
 "response_buffering": true,
 "strip_path": true,
 "request_buffering": true,
 "created_at": 1661347991
}

 3. 更新路由

与服务一样,可以通过向路由URL发送PATCH请求来动态更新路由。

标签是一个可选的字符串集合,可以与路由关联用于分组和过滤。您可以通过向服务端点发送PATCH请求并指定一个路由来分配标签。

通过为路由分配一个值为tutorial的标签来更新路由:

curl --request PATCH \
 --url localhost:8001/services/example_service/routes/example_route \
 --data tags="tutorial"

上面的示例使用了服务和路由名称字段作为路由URL。

如果成功应用了标签,响应正文将包含以下JSON值:

...
"tags":["tutorial"]
...

 4. 列出路由

Admin API还支持列出当前配置的所有路由:

curl http://localhost:8001/routes

 该请求将返回HTTP 200状态码和一个JSON响应正文,其中包含在此Kong Gateway实例上配置的所有路由的对象数组。您的响应应该如下所示:

{
 "next": null,
 "data": [
   {
     "paths": [
       "/mock"
     ],
     "methods": null,
     "sources": null,
     "destinations": null,
     "name": "example_route",
     "headers": null,
     "hosts": null,
     "preserve_host": false,
     "regex_priority": 0,
     "snis": null,
     "https_redirect_status_code": 426,
     "tags": [
       "tutorial"
     ],
     "protocols": [
       "http",
       "https"
     ],
     "path_handling": "v0",
     "id": "52d58293-ae25-4c69-acc8-6dd729718a61",
     "updated_at": 1661346132,
     "service": {
       "id": "c1e98b2b-6e77-476c-82ca-a5f1fb877e07"
     },
     "response_buffering": true,
     "strip_path": true,
     "request_buffering": true,
     "created_at": 1661345592
   }
 ]
}

Admin API文档中提供了管理路由对象的完整规范。

您还可以通过在浏览器中导航到以下URL来查看Kong Manager UI中的路由配置:http://localhost:8002/default/routes,其中default是工作区名称。

代理请求

Kong是一个API网关,它接收来自客户端的请求,并根据当前的配置将它们路由到适当的上游应用程序。使用之前配置的服务和路由,您现在可以通过http://localhost:8000/mock访问https://httpbin.org/。

默认情况下,Kong Gateway的Admin API在端口8001上监听管理请求,这有时被称为控制平面。客户端使用端口8000进行数据请求,这通常被称为数据平面。

Httpbin提供了一个/anything资源,它会将关于发送给它的请求的信息回复给客户端。通过Kong Gateway代理一个请求到/anything资源上。

curl -X GET http://localhost:8000/mock/anything

 您应该会看到类似以下的响应:

{
 "startedDateTime": "2022-08-24T13:44:28.449Z",
 "clientIPAddress": "172.19.0.1",
 "method": "GET",
 "url": "http://localhost/anything",
 "httpVersion": "HTTP/1.1",
 "cookies": {},
 "headers": {
   "host": "httpbin.org",
   "connection": "close",
   "accept-encoding": "gzip",
   "x-forwarded-for": "172.19.0.1,98.63.188.11, 162.158.63.41",
   "cf-ray": "73fc85d999f2e6b0-EWR",
   "x-forwarded-proto": "http",
   "cf-visitor": "{\"scheme\":\"http\"}",
   "x-forwarded-host": "localhost",
   "x-forwarded-port": "80",
   "x-forwarded-path": "/mock/anything",
   "x-forwarded-prefix": "/mock",
   "user-agent": "curl/7.79.1",
   "accept": "*/*",
   "cf-connecting-ip": "00.00.00.00",
   "cdn-loop": "cloudflare",
   "x-request-id": "1dae4762-5d7f-4d7b-af45-b05720762878",
   "via": "1.1 vegur",
   "connect-time": "0",
   "x-request-start": "1661348668447",
   "total-route-time": "0"
 },
 "queryString": {},
 "postData": {
   "mimeType": "application/octet-stream",
   "text": "",
   "params": []
 },
 "headersSize": 588,
 "bodySize": 0
}

你可能感兴趣的:(Kong,kong,gateway)