krakend connection refused 500

196 views
Skip to first unread message

Emo Leumassi

unread,
Sep 20, 2023, 12:02:18 PM9/20/23
to KrakenD Community
I have this `krakend.json`:
```
{
    "$schema": "https://www.krakend.io/schema/v2.4/krakend.json",
    "version": 3,
    "port": 8080,
    "host": ["http://localhost:8080"],
"endpoints": [
{
  "@comment": "Bla bla",
  "endpoint": "/app/{identification}",
  "backend": [
{
  "host": ["http://localhost:4802"],
  "url_pattern": "/app/{identification}"
}
  ]
}
]
}
```

my docker-compose.yml:
```
version: "3"
services:
  krakend_ce:
    # The :watch image restarts the service automatically when the configuration files change.
    # Do not use this image in production, it's meant to speed up your testing and development.
    image: devopsfaith/krakend:watch
    volumes:
      - .:/etc/krakend
    ports:
      - "1234:1234"
      - "8080:8080"
      - "8090:8090"
    command: ["run", "-d", "-c", "/etc/krakend/krakend.json"]
    depends_on:
      - myservice
  myservice:
    image: myservice:latest
    volumes:
      - ./config/application.yaml:/usr/application/config/application.yaml:ro
      - ./logs:/usr/application/logs
    ports:
      - "4802:4802"
```

Those containers:
```
docker ps
CONTAINER ID   IMAGE                       COMMAND                  CREATED          STATUS          PORTS                                                                                                                             NAMES
a8b301aed1c3   devopsfaith/krakend:watch   "/entrypoint.sh run …"   2 minutes ago    Up 6 seconds    0.0.0.0:1234->1234/tcp, :::1234->1234/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 0.0.0.0:8090->8090/tcp, :::8090->8090/tcp   myservice_krakend_ce_1
a1cd2e890510   myservice:latest         "java -Djava.securit…"   28 minutes ago   Up 28 minutes   0.0.0.0:4802->4802/tcp, :::4802->4802/tcp                                                                                         myservice_1
```

The log:
```
 docker logs -f a8b301aed1c3
Watching changes on files /etc/krakend/
Ignoring saves to file out.json
[00] Starting service
[00] Parsing configuration file: /etc/krakend/krakend.json
[00] 2023/09/20 12:29:45 KRAKEND INFO: Starting KrakenD v2.4.3
[00] 2023/09/20 12:29:45 KRAKEND DEBUG: [SERVICE: Gin] Debug enabled
[00] 2023/09/20 12:29:45 KRAKEND INFO: Starting the KrakenD instance
[00] 2023/09/20 12:29:45 KRAKEND DEBUG: [ENDPOINT: /app/:identification] Building the proxy pipe
[00] 2023/09/20 12:29:45 KRAKEND DEBUG: [BACKEND: /app/{{.identification}}] Building the backend pipe
[00] 2023/09/20 12:29:45 KRAKEND DEBUG: [ENDPOINT: /app/:identification] Building the http handler
[00] 2023/09/20 12:29:45 KRAKEND DEBUG: [ENDPOINT: /app/:identification][JWTSigner] Signer disabled
[00] 2023/09/20 12:29:45 KRAKEND INFO: [ENDPOINT: /app/:identification][JWTValidator] Validator disabled for this endpoint
[00] 2023/09/20 12:29:45 KRAKEND INFO: [SERVICE: Gin] Listening on port: 8080
[00] [GIN] 2023/09/20 - 12:30:07 | 200 |     122.211µs |      172.18.0.1 | GET      "/__health"
[00] [GIN] 2023/09/20 - 12:30:08 | 200 |      28.809µs |      172.18.0.1 | GET      "/__health"
[00] [GIN] 2023/09/20 - 12:30:09 | 200 |      53.791µs |      172.18.0.1 | GET      "/__health"
[00] 2023/09/20 12:30:16 KRAKEND ERROR: [ENDPOINT: /app/:identification] Get "http://localhost:4802/app/110033323312176": dial tcp 127.0.0.1:4802: connect: connection refused
[00] [GIN] 2023/09/20 - 12:30:16 | 500 |     703.485µs |      172.18.0.1 | GET      "/app/110033323312176"
[00] Error #01: Get "http://localhost:4802/app/110033323312176": dial tcp 127.0.0.1:4802: connect: connection refused
[00] 2023/09/20 12:30:16 KRAKEND ERROR: [ENDPOINT: /app/:identification] Get "http://localhost:4802/app/110033323312176": dial tcp 127.0.0.1:4802: connect: connection refused
[00] [GIN] 2023/09/20 - 12:30:16 | 500 |      541.71µs |      172.18.0.1 | GET      "/app/110033323312176"
[00] Error #01: Get "http://localhost:4802/app/110033323312176": dial tcp 127.0.0.1:4802: connect: connection refused
Watching changes on files /etc/krakend/
Ignoring saves to file out.json
[00] Starting service
[00] Parsing configuration file: /etc/krakend/krakend.json
[00] 2023/09/20 12:31:59 KRAKEND INFO: Starting KrakenD v2.4.3
[00] 2023/09/20 12:31:59 KRAKEND DEBUG: [SERVICE: Gin] Debug enabled
[00] 2023/09/20 12:31:59 KRAKEND INFO: Starting the KrakenD instance
[00] 2023/09/20 12:31:59 KRAKEND DEBUG: [ENDPOINT: /app/:identification] Building the proxy pipe
[00] 2023/09/20 12:31:59 KRAKEND DEBUG: [BACKEND: /app/{{.identification}}] Building the backend pipe
[00] 2023/09/20 12:31:59 KRAKEND DEBUG: [ENDPOINT: /app/:identification] Building the http handler
[00] 2023/09/20 12:31:59 KRAKEND DEBUG: [ENDPOINT: /app/:identification][JWTSigner] Signer disabled
[00] 2023/09/20 12:31:59 KRAKEND INFO: [ENDPOINT: /app/:identification][JWTValidator] Validator disabled for this endpoint
[00] 2023/09/20 12:31:59 KRAKEND INFO: [SERVICE: Gin] Listening on port: 8080
[00] 2023/09/20 12:33:15 KRAKEND ERROR: [ENDPOINT: /app/:identification] Get "http://localhost:4802/app/110033323312176": dial tcp 127.0.0.1:4802: connect: connection refused
[00] [GIN] 2023/09/20 - 12:33:15 | 500 |    1.070179ms |      172.18.0.1 | GET      "/app/110033323312176"
[00] Error #01: Get "http://localhost:4802/app/110033323312176": dial tcp 127.0.0.1:4802: connect: connection refused
[00] 2023/09/20 12:33:52 KRAKEND ERROR: [ENDPOINT: /app/:identification] Get "http://localhost:4802/app/110033323312176": dial tcp 127.0.0.1:4802: connect: connection refused
[00] [GIN] 2023/09/20 - 12:33:52 | 500 |     530.808µs |      172.18.0.1 | GET      "/app/110033323312176"
[00] Error #01: Get "http://localhost:4802/app/110033323312176": dial tcp 127.0.0.1:4802: connect: connection refused
```

I called: `http://localhost:8080/app/110033323312176` and i got an error.

Albert Lombarte

unread,
Sep 20, 2023, 12:36:02 PM9/20/23
to KrakenD Community, Emo Leumassi
Hi,

You cannot use "localhost" in the KrakenD container to access another container. It's not a KrakenD thing, is how Docker works. When you use localhost the resolution happens within the container, but you want to access another one.

With the docker compose you have attached, what you need to do is to change the configuration to  "host": ["http://myservice:4802"], so KrakenD accesses the other container.

You can access both applications from your computer as localhost because your port configuration is mapping them, but this is not what happens within the containers.

Best,
El dia dimecres, 20 de setembre de 2023 a les 18:02:18 UTC+2, Emo Leumassi va escriure:

ponmarimuthu s

unread,
Sep 20, 2023, 3:36:33 PM9/20/23
to KrakenD Community, Albert Lombarte, Emo Leumassi

In my case, it did not work even after adding the host with the service name in the Docker Compose file of the backend. I had to add both containers to the same Docker network, and then the KrakenD container was able to reach my backend with the hostname as the service name.

For multiple backend containers, I created individual networks for each so that KrakenD can communicate with the backend containers individually, but those containers should not communicate with each other.

/Pons

Reply all
Reply to author
Forward
0 new messages