Search code examples
dockerkong

Kong cannot find sample flask container in tutorial


I started following this docker/KONG installation tutorial where they create a network called "kong-net" and fire up the KONG and postgresql containers.

Then I jumped to this docker/kong tutorial the registers a sample flask container as an API in KONG..

I did not see any thing alarming while configuring the KONG container with the flask service and associated routes.

The sample flask container seems to work fine:

curl http://localhost:5055/api/v1/test1
curl http://localhost:5055/api/v1/test2

I get the expected result:

{"message":"first end point test1 is called","status_code":200}

The results of these commands look good: curl -i -X POST --url http://localhost:8001/services/ --data 'name=testApi' --data 'url=http://localhost:5055' curl http://localhost:8001/routes | json_pp

Everything is great until I get this command to test KONG:

curl -i -X GET --url http://localhost:8000/api/v1/test1 --header 'Host: localhost'

I think KONG is supposed to forward this to the sample flask container.

Instead I see this error:

HTTP/1.1 502 Bad Gateway
Date: Wed, 08 May 2019 18:20:00 GMT
Content-Type: text/plain; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: kong/1.1.2
X-Kong-Upstream-Latency: 1
X-Kong-Proxy-Latency: 35
Via: kong/1.1.2

An invalid response was received from the upstream server

In the logs for the KONG container I see this:

2019/05/08 16:56:57 [error] 38#0: *167134 connect() failed (111: 
Connection refused) while connecting to upstream, client: 
172.19.0.1, server: kong, request: "GET /api/v1/test1 HTTP/1.1", 
upstream: "http://127.0.0.1:5055/api/v1/test1", host: "localhost"
172.19.0.1 - - [08/May/2019:16:56:57 +0000] "GET /api/v1/test1 
HTTP/1.1" 502 69 "-" "curl/7.59.0"

It looks like KONG cannot see localhost:5055.

I'm worried about that network the first tutorial had me create.

I tried stopping, rebuilding and re-running the flask container with this command (so the flask was part of the network too):

docker run -d --name flask  --network=kong-net -p 5055:5055  flask:test

Alas, this did not help. Same error!

When I type

docker network inspect kong-net

I now see that the flask container is part of kong-net. Is this necessary?

I tried this command and it worked:

docker exec -ti kong sh -c "curl http://172.19.0.4:5055/api/v1/test1 "
{"message":"first end point test1 is called","status_code":200}

I'm doing all this with Windows10/cygwin-bash/docker18.09.2 with docker/kubernetes turned on.

Questions:

  1. Does the sample flask app need to part of the kong-net?
  2. The tutorial seems be saying that kong should be able to see 127.0.0.1:5055. Is this correct? Is the tutorial wrong?
  3. How can I make this command work?

    curl -i -X GET --url http://localhost:8000/api/v1/test1 --header 'Host: localhost'


Solution

  • When Kong is installed as a docker container 'localhost' means loopback address of the Kong container - NOT the host. Endpoints registered in Kong should be resolvable from the Kong container.

    So, you can change your registration to use actual IP and Port of the backend service as reachable by Kong container, so, if your backend is also a docker container and its port 5055 is mapped to port 15055 on the host, then registration in Kong should use host IP and port 15055.