Krakend 无法从 consul (Docker) 获取正在运行的服务

发布于 2025-01-15 23:51:11 字数 1676 浏览 5 评论 0原文

我使用 Consul 作为 SD,当我运行多个实例时,我想让网关识别它​​们以实现负载平衡。 如果 Krakend 配置的硬编码主机一切正常,

我将逐步执行以下操作: https://www.krakend.io/docs/backends/service-discovery/

发送

挖掘@127.0.0.1 -p 8600 user-ms.service.consul SRV 也给了我回复(在领事日志上也看到我已经提出了请求)

但是当我通过 krakenD 发出请求时,我得到“没有可用的主机”,并且根据日志,请求没有发送到领事

这里是docker-compose 文件:

services:
  postgres:
     image: postgres:13
     restart: 'always'
     environment:
       - POSTGRES_DB=user-db
       - POSTGRES_PASSWORD=password
       - POSTGRES_USER=user
     ports:
       - "5432:5432"

 consul:
   image: consul:latest
   restart: 'always'

   ports:
     - '8500:8500'
     - '8600:8600/tcp'
     - '8600:8600/udp'

 krakend_gateway:
   image: devopsfaith/krakend:2
   volumes:
     - ./krakend.json:/krakend.json
   ports:
     - "1234:1234"
     - "8080:8080"
     - "8090:8090"

   command: [ "run", "-d", "-c", "/krakend.json" ]

 user-ms:
   build: user-ms/
   platform: linux/arm64
   restart: 'always'
   depends_on: [consul-server , krakend_gateway]
   ports:
     - '8082'

还有非常简单的 Krakend.json 配置文件

{
  "version": 3,
  "timeout": "3000ms",
  "cache_ttl": "300s",
  "host" : "localhost:8080",
  "endpoints": [
    {
      "endpoint": "/users",
      "backend": [
        {
          "url_pattern": "/api/v1/users",
          "sd" : "dns",
          "host" : [
            "user-ms.service.consul.srv"
          ],
          "disable_host_sanitize": true
        }
      ]
    }
  ]
}

,我已经尝试了很多东西,但仍然得到相同的响应,将很高兴获得任何帮助。谢谢

I am using Consul as SD and while I am running multiple instances I want make gateway recognize them all to load balance.
In case of hardcoded host for Krakend config everything works fine

I was going step by step with this: https://www.krakend.io/docs/backends/service-discovery/

Sending

dig @127.0.0.1 -p 8600 user-ms.service.consul SRV
gives me response as well (on consul logs a also see that I've made a request)

But when I am making request via krakenD I am getting "no hosts available" and according to logs request wasn't send to consul

Here is a docker-compose file:

services:
  postgres:
     image: postgres:13
     restart: 'always'
     environment:
       - POSTGRES_DB=user-db
       - POSTGRES_PASSWORD=password
       - POSTGRES_USER=user
     ports:
       - "5432:5432"

 consul:
   image: consul:latest
   restart: 'always'

   ports:
     - '8500:8500'
     - '8600:8600/tcp'
     - '8600:8600/udp'

 krakend_gateway:
   image: devopsfaith/krakend:2
   volumes:
     - ./krakend.json:/krakend.json
   ports:
     - "1234:1234"
     - "8080:8080"
     - "8090:8090"

   command: [ "run", "-d", "-c", "/krakend.json" ]

 user-ms:
   build: user-ms/
   platform: linux/arm64
   restart: 'always'
   depends_on: [consul-server , krakend_gateway]
   ports:
     - '8082'

And pretty simple Krakend.json config file

{
  "version": 3,
  "timeout": "3000ms",
  "cache_ttl": "300s",
  "host" : "localhost:8080",
  "endpoints": [
    {
      "endpoint": "/users",
      "backend": [
        {
          "url_pattern": "/api/v1/users",
          "sd" : "dns",
          "host" : [
            "user-ms.service.consul.srv"
          ],
          "disable_host_sanitize": true
        }
      ]
    }
  ]
}

I've tried a lot of thing , but still get the same response , will be glad for any help with this. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

已下线请稍等 2025-01-22 23:51:11

当我通过 krakenD 发出请求时,我收到“没有可用的主机”,并且根据日志,请求未发送给领事

您的请求失败的原因是因为 Consul 的 DNS 服务器侦听非标准端口 8600。Kraken 不似乎支持配置自定义 DNS IP 和非标准端口。它使用 /etc/resolv.conf 中底层主机配置的名称服务器解析 SRV 请求,并期望可通过端口 53 访问这些 IP。

以下是一个 Docker 撰写文件,其中包含以下更改以允许Kraken 针对 Consul 解析 DNS。

# docker-compose.yaml
---
services:
  postgres:
    image: postgres:13
    restart: 'always'
    environment:
      - POSTGRES_DB=user-db
      - POSTGRES_PASSWORD=password
      - POSTGRES_USER=user
    ports:
      - "5432:5432"
    networks:
      - vpcbr

  consul:
    image: consul:latest
    restart: 'always'
    environment:
      CONSUL_LOCAL_CONFIG: |
        {
          "recursors": [
            "8.8.8.8",
            "8.8.4.4"
          ],
          "dns_config": {
            "recursor_strategy": "random"
          },
          "ports": {
            "dns": 53
          },
          "services": [
            {
              "name": "user-ms",
              "address": "192.0.2.20",
              "port": 8082
            }
          ]
        }
    networks:
      vpcbr:
        ipv4_address: 192.0.2.10
    ports:
      - '8500:8500'
      - '8600:53/tcp'
      - '8600:53/udp'

  krakend_gateway:
    image: devopsfaith/krakend:2
    command: [ "run", "-d", "-c", "/krakend.json" ]
    dns: 192.0.2.10
    volumes:
      - ./krakend.json:/krakend.json
    ports:
      - "1234:1234"
      - "8080:8080"
      - "8090:8090"
    networks:
      - vpcbr

  user-ms:
    build: user-ms/
    platform: linux/arm64
    restart: 'always'
    depends_on: [consul, krakend_gateway]
    networks:
      vpcbr:
        ipv4_address: 192.0.2.20
    ports:
      - '8082'

networks:
  vpcbr:
    driver: bridge
    ipam:
      config:
      - subnet: 192.0.2.0/24

Docker compose 更改

  1. 容器已部署到单独的专用网络 vpcbr 上,IP 范围为 192.0.2.0/24。这样就可以将静态 IP 分配给 Consul 容器。
  2. 已从 vpcbr 网络为 consul 容器分配了静态 IP 192.0.2.10。
  3. 其余容器已配置为从 vpcbr 获取动态 IP。
  4. krakend_gateway 容器已配置为使用 192.0.2.10 (Consul) 进行上游 DNS 解析。

Consul 更改

  1. Consul 已配置了 user-ms 服务的静态注册。或者,您可以使用 Registrator 之类的程序将容器从 Docker 动态注册到 Consul 中。
  2. Consul 已更新为侦听端口 53 而不是 8600。Consul
  3. 还配置了一组上游 DNS 递归器,以便它可以解析非 .consul TLD 的 DNS 查询。

Kraken 更改

  1. Kraken 文档中的示例 DNS 主机名对于与 Consul 集成不正确。应删除 .srv 后缀,因为 Consul 仅处理 .consul 顶级域下记录的查询。
{
  "version": 3,
  "timeout": "3000ms",
  "cache_ttl": "300s",
  "host": "localhost:8080",
  "endpoints": [
    {
      "endpoint": "/users",
      "backend": [
        {
          "url_pattern": "/api/v1/users",
          "sd": "dns",
          "host": [
            "user-ms.service.consul"
          ],
          "disable_host_sanitize": true
        }
      ]
    }
  ]
}

通过此配置,我可以看到 Kraken 成功根据 Consul DNS 解析 user-ms.service.consul 域。

when I am making request via krakenD I am getting "no hosts available" and according to logs request wasn't send to consul

The reason your request is failing is because Consul's DNS server listens on the non-standard port of 8600. Kraken does not appear to support configuring a custom DNS IP and non-standard port. It uses the underlying host's configured nameservers in /etc/resolv.conf resolve SRV requests, and expects these IPs to be reachable over port 53.

The following is a Docker compose file that includes the following changes to allow Kraken to resolve DNS against Consul.

# docker-compose.yaml
---
services:
  postgres:
    image: postgres:13
    restart: 'always'
    environment:
      - POSTGRES_DB=user-db
      - POSTGRES_PASSWORD=password
      - POSTGRES_USER=user
    ports:
      - "5432:5432"
    networks:
      - vpcbr

  consul:
    image: consul:latest
    restart: 'always'
    environment:
      CONSUL_LOCAL_CONFIG: |
        {
          "recursors": [
            "8.8.8.8",
            "8.8.4.4"
          ],
          "dns_config": {
            "recursor_strategy": "random"
          },
          "ports": {
            "dns": 53
          },
          "services": [
            {
              "name": "user-ms",
              "address": "192.0.2.20",
              "port": 8082
            }
          ]
        }
    networks:
      vpcbr:
        ipv4_address: 192.0.2.10
    ports:
      - '8500:8500'
      - '8600:53/tcp'
      - '8600:53/udp'

  krakend_gateway:
    image: devopsfaith/krakend:2
    command: [ "run", "-d", "-c", "/krakend.json" ]
    dns: 192.0.2.10
    volumes:
      - ./krakend.json:/krakend.json
    ports:
      - "1234:1234"
      - "8080:8080"
      - "8090:8090"
    networks:
      - vpcbr

  user-ms:
    build: user-ms/
    platform: linux/arm64
    restart: 'always'
    depends_on: [consul, krakend_gateway]
    networks:
      vpcbr:
        ipv4_address: 192.0.2.20
    ports:
      - '8082'

networks:
  vpcbr:
    driver: bridge
    ipam:
      config:
      - subnet: 192.0.2.0/24

Docker compose changes

  1. The containers have been deployed onto a separate private network, vpcbr, with the IP range of 192.0.2.0/24. This is so that a static IP can be assigned to the Consul container.
  2. The consul container has been issued a static IP of 192.0.2.10 from the vpcbr network.
  3. The remaining containers have been configured to obtain a dynamic IP from vpcbr.
  4. The krakend_gateway container has been configured to use 192.0.2.10 (Consul) for upstream DNS resolution.

Consul changes

  1. Consul has been configured with a static registration for the user-ms service. Alternatively, you can use a program like Registrator to dynamically register containers from Docker into Consul.
  2. Consul has been updated to listen on port 53 instead of 8600.
  3. Consul has also been configured with a set of upstream DNS recursors so that it can resolve DNS queries for non .consul TLDs.

Kraken changes

  1. The example DNS hostname in Kraken's documentation is incorrect for integrating with Consul. The .srv suffix should be removed as Consul only handles queries for records under the .consul top-level domain.
{
  "version": 3,
  "timeout": "3000ms",
  "cache_ttl": "300s",
  "host": "localhost:8080",
  "endpoints": [
    {
      "endpoint": "/users",
      "backend": [
        {
          "url_pattern": "/api/v1/users",
          "sd": "dns",
          "host": [
            "user-ms.service.consul"
          ],
          "disable_host_sanitize": true
        }
      ]
    }
  ]
}

With this configuration, I can see that Kraken successfully resolves the user-ms.service.consul domain against Consul DNS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文