Krakend 无法从 consul (Docker) 获取正在运行的服务
我使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的请求失败的原因是因为 Consul 的 DNS 服务器侦听非标准端口 8600。Kraken 不似乎支持配置自定义 DNS IP 和非标准端口。它使用
/etc/resolv.conf
中底层主机配置的名称服务器解析 SRV 请求,并期望可通过端口 53 访问这些 IP。以下是一个 Docker 撰写文件,其中包含以下更改以允许Kraken 针对 Consul 解析 DNS。
Docker compose 更改
vpcbr
上,IP 范围为 192.0.2.0/24。这样就可以将静态 IP 分配给 Consul 容器。vpcbr
网络为consul
容器分配了静态 IP 192.0.2.10。vpcbr
获取动态 IP。krakend_gateway
容器已配置为使用192.0.2.10
(Consul) 进行上游 DNS 解析。Consul 更改
user-ms
服务的静态注册。或者,您可以使用 Registrator 之类的程序将容器从 Docker 动态注册到 Consul 中。.consul
TLD 的 DNS 查询。Kraken 更改
.srv
后缀,因为 Consul 仅处理.consul
顶级域下记录的查询。通过此配置,我可以看到 Kraken 成功根据 Consul DNS 解析
user-ms.service.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 changes
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.consul
container has been issued a static IP of 192.0.2.10 from thevpcbr
network.vpcbr
.krakend_gateway
container has been configured to use192.0.2.10
(Consul) for upstream DNS resolution.Consul changes
user-ms
service. Alternatively, you can use a program like Registrator to dynamically register containers from Docker into Consul..consul
TLDs.Kraken changes
.srv
suffix should be removed as Consul only handles queries for records under the.consul
top-level domain.With this configuration, I can see that Kraken successfully resolves the
user-ms.service.consul
domain against Consul DNS.