kong
kong & konga
下载
第1步:创建Docker网络
首先,创建一个Docker网络kong-net,这样你的服务可以在隔离的网络空间中通信。
1 | |
第2步:安装并启动PostgreSQL容器
运行以下命令来启动PostgreSQL容器,使用kong-net网络,并设置环境变量来配置数据库用户、密码和数据库名。
1 | |
-p 5432:5432: 将容器的5432端口映射到宿主机的5432端口,使得可以从宿主机访问PostgreSQL服务。-e: 设置环境变量,用于配置PostgreSQL数据库的用户名、数据库名和密码。
1 | |
--rm: 容器退出后自动清理容器文件系统。-e: 设置环境变量来配置Kong连接到PostgreSQL的细节。kong:latest kong migrations bootstrap: 使用最新的Kong镜像执行数据库迁移命令,准备数据库。
这一步指令的作用
- 创建表:Kong需要在PostgreSQL数据库中创建多个表,用来存储它的配置数据,比如服务(Services)、路由(Routes)、消费者(Consumers)、插件(Plugins)等的信息。
- 应用数据模式更新:随着Kong的版本更新,数据库模式(schema)也可能发生变化。迁移确保数据库模式与Kong的当前版本兼容。
- 准备数据库:此外,迁移还会执行一些检查和准备工作,确保Kong可以顺利地与数据库交互。
第3步:安装配置Kong
启动Kong容器
1 | |
- 8000: Kong的代理监听端口,用于接收API请求。
- 8443: Kong的代理SSL监听端口,用于接收API请求(通过HTTPS)。
- 8001: Kong的Admin API监听端口,用于配置Kong和查看状态信息。
- 8444: Kong的Admin API SSL监听端口,通过HTTPS提供同8001相同的功能。
查看docker网络
1docker network inspect kong-net
第4步:安装并启动PostgreSQL容器
1 | |
1 | |
第5步:启动Konga容器
最后一步是启动Konga容器,它将连接到同一个Docker网络内的PostgreSQL数据库。Konga提供了一个Web界面,使得管理Kong变得更加容易和直观。
1 | |
第6步:docker-compose
docker-compose
1 | |
路由
kong的路由主要涉及到两个组件,service和route
定义
service服务
就是我们自己定义的上游服务,通过Kong匹配到相应的请求要转发的地方, Service 可以与下面的Route进行关联,一个Service可以有很多Route,匹配到的Route就会转发到Service中, 当然中间也会通过Plugin的处理,增加或者减少一些相应的Header或者其他信息。
route路由
Route实体定义匹配客户端请求的规则. 每个路由都与一个服务相关联,而服务可能有多个与之相关联的路由. 每一个匹配给定路线的请求都将被提交给它的相关服务。
example
konga方式:
- 打开konga后台管理画面,打开Service
- 配置service: 添加
name=baidu-text,url=https://www.baidu.com - 配置route路由: 点击刚才配置好的service,然后点击routes,配置
name=baidu-route和paths=/myPath - 测试:浏览器访问
http://8.130.144.38:8000/myPath,或者输入curl http://localhost:8000/myPath
kong方式:
创建service服务
1
2
3
4curl -i -X POST \
--url http://localhost:8001/services/ \
--data 'name=baidu-test' \
--data 'url=http://www.baidu.com'为service添加route
1
2
3
4curl -i -X POST \
--url http://localhost:8001/services/baidu-test/routes \
--data 'name=baidu-route' \
--data 'paths[]=/myPath'验证是否成功
1
curl http://localhost:8000/myPath
身份认证(oauth2)
创建Service
创建一个Kong的Service Object指向上游的服务
1 | |
response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19{
"host": "httpbin.org",
"id": "33459a79-e284-4bb8-aa6f-65dafd456c6f",
"protocol": "http",
"read_timeout": 60000,
"tls_verify_depth": null,
"port": 80,
"updated_at": 1615001132,
"ca_certificates": null,
"created_at": 1615001132,
"connect_timeout": 60000,
"write_timeout": 60000,
"name": "oauth2-test",
"retries": 5,
"path": "/anything",
"tls_verify": null,
"tags": null,
"client_certificate": null
}
创建Route
1 | |
response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30{
"strip_path": true,
"tags": null,
"updated_at": 1615004204,
"destinations": null,
"headers": null,
"protocols": [
"http",
"https"
],
"methods": null,
"service": {
"id": "33459a79-e284-4bb8-aa6f-65dafd456c6f"
},
"snis": null,
"hosts": null,
"name": null,
"path_handling": "v0",
"paths": [
"/demo"
],
"preserve_host": false,
"regex_priority": 0,
"response_buffering": true,
"sources": null,
"id": "e804fef4-fa42-4f7e-be0c-bbe9b9999027",
"https_redirect_status_code": 426,
"request_buffering": true,
"created_at": 1615004204
}
在这之后我们可以通过
curl localhost:8000/demo来访问服务。
启用Oauth2插件
1 | |
response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40{
"created_at": 1615003048,
"id": "8bc8ed59-cdb4-4ac4-ab48-7719655cb9f3",
"tags": null,
"enabled": true,
"protocols": [
"grpc",
"grpcs",
"http",
"https"
],
"name": "oauth2",
"consumer": null,
"service": {
"id": "33459a79-e284-4bb8-aa6f-65dafd456c6f"
},
"route": null,
"config": {
"pkce": "lax",
"accept_http_if_already_terminated": false,
"reuse_refresh_token": false,
"token_expiration": 7200,
"mandatory_scope": true,
"enable_client_credentials": true,
"hide_credentials": false,
"enable_authorization_code": true,
"enable_implicit_grant": true,
"global_credentials": false,
"refresh_token_ttl": 1209600,
"enable_password_grant": true,
"scopes": [
"email",
"phone",
"address"
],
"anonymous": null,
"provision_key": "oauth2-demo-provision-key",
"auth_header_name": "authorization"
}
}
我们再次链接
curl localhost:8000/demo的时候,我们会得到HTTP/1.1 401 Unauthorized已经以下错误信息。这就说明Oauth2插件已经成功开启。
创建Consumer
1 | |
response
1
2
3
4
5
6
7{
"custom_id": null,
"created_at": 1615003502,
"id": "06d53376-8bfd-4bc7-aaaf-05c37316e7ef",
"tags": null,
"username": "oauth2-tester"
}
创建Oauth2 Credential (App)
然后我们会在这个consumer下创建Oauth2的身份凭证。在这里我也会使用自定义的 client_id 和 client_secret。如果留空,Kong会自动生成这两个变量。
如果使用Kong来生成身份凭证请切记不要添加
hash_secret=true在您的curl命令里面。
1 | |
response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16{
"created_at": 1615004674,
"id": "f602f09b-7b7e-4326-b236-2fa8d45badff",
"tags": null,
"name": "Oauth2 Demo App",
"client_secret": "$pbkdf2-sha512$i=10000,l=32$e3SNVIWRFt8PuBxjoL1ncQ$/hF26HS30QHopDLMzlZqC+zv0nt3m4YFokuW9eTma6Q",
"client_id": "oauth2-demo-client-id",
"redirect_uris": [
"http://localhost:8000/demo"
],
"hash_secret": true,
"client_type": "confidential",
"consumer": {
"id": "06d53376-8bfd-4bc7-aaaf-05c37316e7ef"
}
}
测试【等待完成】
身份认证(jwt)
创建service
1 | |
创建Route
1 | |
启用jwt插件
1 | |
尝试访问
curl http://localhost:8000/jwtresponse:{“message”:”Unauthorized”}
在 Kong 中创建用户
1 | |
创建jwt凭证(可以指定jwt的key和secret)
1 | |
查看用户的jwt信息
1 | |
然后生成jwt
携带jwt访问
1 | |