DNS_RESOLVER 如何使用 Kong?

How to use Kong with DNS_RESOLVER?

我在 docker 中使用 Kong,当我在没有“DNS_RESOLVER”的情况下启动时,它工作正常,这是命令

docker run -d --name kong --network=kong-net /
-e "KONG_DATABASE=postgres" -e "KONG_PG_HOST=kong-database" /
-e "KONG_PG_USER=kong" -e "KONG_PG_PASSWORD=kong"  /
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" /
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" -p 8000:8000 -p 8443:8443 -p 8001:8001 -p 8444:8444 kong:1.4.3-alpine

我想和 consul 一起使用 Kong,我发现我应该将 -e "KONG_DNS_RESOLVER=10.114.205.170:8600" 添加到 command.So 最后的命令是

docker run -d --name kong --network=kong-net /
-e "KONG_DATABASE=postgres" -e "KONG_PG_HOST=kong-database" /
-e "KONG_PG_USER=kong" -e "KONG_PG_PASSWORD=kong" -e "KONG_DNS_RESOLVER=10.114.205.170:8600" /
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" /
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" -p 8000:8000 -p 8443:8443 -p 8001:8001 -p 8444:8444 kong:1.4.3-alpine

10.114.205.170:8600是领事的DNS服务器。

但是Kong启动报错,报错日志为

2021/04/13 03:10:17 [crit] 41#0: *8 [lua] balancer.lua:540: could not obtain list of upstreams: [postgres] [cosocket] DNS resolution failed: dns server error: 2 server failure. Tried: ["(short)kong-database:(na) - cache-miss","kong-database:33 - cache-miss/scheduled/querying/dns server error: 2 server failure","kong-database:1 - cache-miss/scheduled/querying/dns server error: 2 server failure","kong-database:5 - cache-miss/scheduled/querying/dns server error: 2 server failure"], context: ngx.timer
...
2021/04/13 03:10:26 [error] 34#0: *253 [kong] kong.lua:42 [postgres] [cosocket] DNS resolution failed: dns server error: 2 server failure. Tried: ["(short)kong-database:(na) - cache-miss","kong-database:33 - cache-miss/scheduled/querying/dns server error: 2 server failure","kong-database:1 - cache-miss/scheduled/querying/dns server error: 2 server failure","kong-database:5 - cache-miss/scheduled/querying/dns server error: 2 server failure"], client: 172.18.0.1, server: kong_admin, request: "GET / HTTP/1.1", host: "127.0.0.1:8001"
...
2021/04/13 03:11:16 [error] 34#0: *1594 [lua] connector.lua:356: unable to clean expired rows from PostgreSQL database ([cosocket] DNS resolution failed: dns server error: 2 server failure. Tried: ["(short)kong-database:(na) - cache-miss","kong-database:33 - cache-miss/scheduled/querying/dns server error: 2 server failure","kong-database:1 - cache-miss/scheduled/querying/dns server error: 2 server failure","kong-database:5 - cache-miss/scheduled/querying/dns server error: 2 server failure"]), context: ngx.timer

我确定领事服务器工作正常。我在主机 运行 Kong 上进行了 consul 查询并得到了正确的响应

root@virtual-machine:/# dig @10.114.205.170 -p 8600 RdTestServiceProxy.service.consul
; <<>> DiG 9.10.3-P4-Ubuntu <<>> @10.114.205.170 -p 8600 RdTestServiceProxy.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 8122
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;RdTestServiceProxy.service.consul. IN  A

;; ANSWER SECTION:
RdTestServiceProxy.service.consul. 0 IN A       10.114.205.151

;; Query time: 1 msec
;; SERVER: 10.114.205.170#8600(10.114.205.170)
;; WHEN: Tue Apr 13 13:54:45 CST 2021
;; MSG SIZE  rcvd: 78

那么 Kong 有什么问题,如何启动 Kong dns_resolver?

最后我找到了 solution.In docker 启动命令 我使用 -e "KONG_PG_HOST=kong-database" 作为数据库的主机,它使用 docker 默认 DNS 服务器。

当我在命令中添加-e "KONG_DNS_RESOLVER=10.114.205.170:8600"时,它会替换默认的DNS-Server,所以Kong找不到数据库。

作为解决方案,我们可以将数据库服务添加到consul并用作KONG_PG_HOST=kong-database.service.consul