1

I am working with a group of microservices developed with Java + Spring Boot and would like to use Consul and KrakenD inside a Docker compose.

My docker-compose file looks like this:

version: '3.8'

services:

  # Consul
  # Service discovery similar to Eureka but could keep configuration
  consul:
    image: consul
    container_name: consul
    command: agent -server -ui -node=server1 -bootstrap-expect=1 -client=0.0.0.0
    environment:
      - CONSUL_BIND_INTERFACE=eth0
    ports:
      - "8500:8500"
      - "8600:8600/udp"
      - "8600:8600/tcp"
    hostname: "consul"
    networks:
      - micros

  krakend:
    image: devopsfaith/krakend
    container_name: krakend
    volumes:
      - ./krakend:/etc/krakend
    ports:
      - "1234:1234"
      - "8080:8080"
      - "8090:8090"
    networks:
      - micros

  google-search-service:
    environment:
      - JAVA_OPTIONS=-Dlogging.level.net.leyba.googlesearch=INFO
    image: google-search-service
    container_name: google-search-service
    #ports:
    #  - "7000-7200"
    networks:
      - micros
    depends_on:
      consul:
        condition: service_started
#      krakend:
#        condition: service_started

networks:
  micros:

and the KrakenD config file looks like this:

{
  "version": 2,
  "extra_config": {
    "github_com/devopsfaith/krakend-gologging": {
      "level": "ERROR",
      "prefix": "[KRAKEND]",
      "syslog": false,
      "stdout": true,
      "format": "default"
    }
  },
  "timeout": "3000ms",
  "cache_ttl": "300s",
  "output_encoding": "json",
  "name": "googlesearch",
  "port": 8080,
  "endpoints": [
    {
      "endpoint": "/v1/googlesearch/{searchParam}",
      "method": "GET",
      "output_encoding": "json",
      "extra_config": {},
      "backend": [
        {
          "sd": "dns",
          "url_pattern": "/external-dbs/google/{searchParam}",
          "host": [
            "google-search-service.service.consul"
          ],
          "disable_host_sanitize": true
        }
      ]
    },
  ]
}

Whithout docker, the host google-search-service.service.consul is resolved through a resolver in my macOS, however it seems that could not be resolved inside the docker-compose network and therefore Krakend is not able to redirect API calls to the microservice because is not able to find Consul.

I am wondering if is there a way to config a resolver inside the docker-compose or to allow KrakendD to found Consul in any other way.

Thanks in advance J

antonof
  • 109
  • 1
  • 8

3 Answers3

1

If your Consul listens on 8600 port and you do not want to change that, then you can follow official documentation and this comment

/etc/systemd/resolved.conf.d/consul.conf

[Resolve]
DNS=127.0.0.1:8600
DNSSEC=false
Domains=~consul

/etc/systemd/resolved.conf.d/docker.conf

[Resolve]
DNSStubListener=yes
DNSStubListenerExtra=172.17.42.1

/etc/docker/daemon.json

{
  "bip": "172.17.42.1/16",
  "dns": ["172.17.42.1"]
}

Description for this approach with a bit more detail here: https://felix.ehrenpfort.de/notes/2022-06-22-use-consul-dns-interface-inside-docker-container/

How this works: consul.conf instructs systemd-resolv forward *.consul domain name lookups to Consul, which listens on 127.0.0.1:8600.

docker.conf instructs systemd-resolv additionally listen on 172.17.42.1:53 DNS queries

daemon.json instructs docker to add nameservers 172.17.42.1 line into /etc/resolv.conf

Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
0

extra-hosts in docker compose is the solution to your problem, you can make docker to resolve certain host names to to desired IP addresses.

check this thread for more info: Using --add-host or extra_hosts with docker-compose

Aref Riant
  • 582
  • 3
  • 14
0

You can configure Consul to listen on port 53 instead of 8600 for DNS, and then configure the other containers to use Consul for DNS resolution.

---
version: "3.8"
networks:
  micros:
    driver: bridge
    ipam:
      config:
        - subnet: 10.70.90.0/24
services:
  consul:
    image: consul:1.11.1
    container_name: consul
    environment:
      CONSUL_BIND_INTERFACE: eth0
      CONSUL_LOCAL_CONFIG: |
        {
          "client_addr": "0.0.0.0",
          "node": "server1",
          "ports": {
            "dns": 53
          },
          "recursors": [
            "8.8.8.8",
            "8.8.4.4"
          ],
          "server": true,
          "bootstrap_expect": 1,
          "ui_config": {
            "enabled": true
          }
        }
    hostname: consul
    networks:
      micros:
        ipv4_address: 10.70.90.5
    ports:
      - 8500:8500
      - 8600:53/tcp
      - 8600:53/udp

  krakend:
    image: devopsfaith/krakend
    container_name: krakend
    volumes:
      - ./krakend:/etc/krakend
    ports:
      - "1234:1234"
      - "8080:8080"
      - "8090:8090"
    dns: 10.70.90.5
    networks:
      - micros

  google-search-service:
    environment:
      - JAVA_OPTIONS=-Dlogging.level.net.leyba.googlesearch=INFO
    image: google-search-service
    container_name: google-search-service
    #ports:
    #  - "7000-7200"
    dns: 10.70.90.5
    networks:
      - micros
    depends_on:
      consul:
        condition: service_started
#      krakend:
#        condition: service_started
Blake Covarrubias
  • 2,138
  • 2
  • 6
  • 14