Search code examples
consulkong

How to use Kong with DNS_RESOLVER?


I'm using Kong with docker,when I start without "DNS_RESOLVER",it work fine,Here is the command

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

And I want to use Kong with consul,I found I should add -e "KONG_DNS_RESOLVER=10.114.205.170:8600" to the command.So the final command is

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 is the DNS SERVER of the consul.

But Kong start error and the error log is

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

I'm sure the consul server work fine. I did consul query on the host running Kong and got the right response

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

So what's wrong with the Kong,how to start Kong whit dns_resolver?


Solution

  • Finally I found the solution.In docker start command I use -e "KONG_PG_HOST=kong-database" as the host of the database,It use the docker default DNS-Server.

    When I add -e "KONG_DNS_RESOLVER=10.114.205.170:8600" to the command,It will replace the default DNS-Server,So Kong cannot find the database.

    As the solution,we can add the database service to the consul and use it as KONG_PG_HOST=kong-database.service.consul.