apisix icon indicating copy to clipboard operation
apisix copied to clipboard

request help: no host allowed while connecting to upstream

Open ccwxl opened this issue 5 years ago • 20 comments

Issue description

从nacos上发现服务.

  • nacos上的服务ip为主机名uaa-service, 一个为ip
  • 当负载均衡运行时报 failed to set server peer [uaa-service:20101] err: no host allowed while connecting to upstream
  • 造成当负载到为主机名的时候返回网关错误. 负载到ip的时候则正常.

Environment

  • apisix version (cmd: apisix version):
  • OS (cmd: uname -a):
  • OpenResty / Nginx version (cmd: nginx -V or openresty -V):
  • etcd version, if have (cmd: run curl http://127.0.0.1:9090/v1/server_info to get the info from server-info API):
  • apisix-dashboard version, if have:
  • the plugin runner version, if the issue is about a plugin runner (cmd: depended on the kind of runner):
  • luarocks version, if the issue is about installation (cmd: luarocks --version):

ccwxl avatar Oct 14 '21 06:10 ccwxl

Issue description

从nacos上发现服务.

  • nacos上的服务ip为主机名uaa-service, 一个为ip
  • 当负载均衡运行时报 failed to set server peer [uaa-service:20101] err: no host allowed while connecting to upstream

Environment

  • apisix version (cmd: apisix version):
  • OS (cmd: uname -a):
  • OpenResty / Nginx version (cmd: nginx -V or openresty -V):
  • etcd version, if have (cmd: run curl http://127.0.0.1:9090/v1/server_info to get the info from server-info API):
  • apisix-dashboard version, if have:
  • the plugin runner version, if the issue is about a plugin runner (cmd: depended on the kind of runner):
  • luarocks version, if the issue is about installation (cmd: luarocks --version):

image 如果使用上游类型是node, 那么主机名是没有问题的.

ccwxl avatar Oct 14 '21 06:10 ccwxl

Whether the hostname of uaa-service has been configured with DNS resolution on the APISIX node, you can test it on the APISIX node ping uaa-service

shuaijinchao avatar Oct 14 '21 07:10 shuaijinchao

配置了host了. 能ping通的

localhost:~ xxx$ ping uaa-service
PING uaa-service (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.084 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.293 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.128 ms
^C
--- uaa-service ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.084/0.168/0.293/0.090 ms

ccwxl avatar Oct 14 '21 08:10 ccwxl

use dig uaa-service and show the result, and proveide the environment info

tzssangglass avatar Oct 14 '21 10:10 tzssangglass

use dig uaa-service and show the result, and proveide the environment info

apisix version (cmd: apisix version): 2.10.0
OS (cmd: uname -a): macos
OpenResty / Nginx version (cmd: nginx -V or openresty -V):
etcd version, if have (cmd: run curl http://127.0.0.1:9090/v1/server_info to get the info from server-info API):

{ "version": "2.10.0", "hostname": "localhost", "etcd_version": "3.5.0", "up_time": 48, "id": "c1141ba7-70c4-41a9-8d9d-c54c70ac2912", "boot_time": 1634215727, "last_report_time": 1634215727 }

apisix-dashboard version, if have:
the plugin runner version, if the issue is about a plugin runner (cmd: depended on the kind of runner):
luarocks version, if the issue is about installation (cmd: luarocks --version):

复现方式

  • discovery config
discovery:                       # service discovery center
#  dns:
#    servers:
#      - "127.0.0.1:8600"         # use the real address of your dns server
 nacos:
  host:                        # it's possible to define multiple eureka hosts addresses of the same eureka cluster.
    - "http://nacos:[email protected]:8848"
  prefix: "/nacos/v1/"
  fetch_interval: 3    # default 30 sec
  weight: 10           # default 100
  timeout:
    connect: 2000       # default 2000 ms
    send: 2000          # default 2000 ms
    read: 5000          # default 5000 ms
  • nacos注册服务为 image
  • host 配置
localhost:apisix-2.10.0 xxx$ cat /etc/hosts
127.0.0.1 uaa-service uaa-service-host
  • 路由配置
{
  "uri": "/uaa/*",
  "name": "uaa服务",
  "methods": [
    "GET",
    "POST",
    "PUT",
    "DELETE",
    "PATCH",
    "HEAD",
    "OPTIONS",
    "CONNECT",
    "TRACE"
  ],
  "upstream_id": "376959049259811901",
  "status": 1
}
  • 上游服务
{
  "timeout": {
    "connect": 6,
    "send": 6,
    "read": 6
  },
  "type": "roundrobin",
  "scheme": "http",
  "discovery_type": "nacos",
  "pass_host": "pass",
  "name": "uaa2",
  "service_name": "uaa-service",
  "keepalive_pool": {
    "idle_timeout": 60,
    "requests": 1000,
    "size": 320
  }
}
  • 调用服务 image

  • 测试

localhost:apisix-2.10.0 xielongwang$ ping uaa-service-host
PING uaa-service (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.147 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.249 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.269 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.109 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.232 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.258 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.371 ms
64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.239 ms
^C
--- uaa-service ping statistics ---
8 packets transmitted, 8 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.109/0.234/0.371/0.074 ms
localhost:apisix-2.10.0 xielongwang$ dig uaa-service-host

; <<>> DiG 9.10.6 <<>> uaa-service-host
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 55283
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;uaa-service-host.		IN	A

;; Query time: 86 msec
;; SERVER: 192.168.31.1#53(192.168.31.1)
;; WHEN: Thu Oct 14 20:54:55 CST 2021
;; MSG SIZE  rcvd: 34

localhost:apisix-2.10.0 xielongwang$
  • 日志打印
2021/10/14 20:49:04 [error] 71097#853898: *2184 [lua] balancer.lua:353: run(): failed to set server peer [uaa-service-host:30101] err: no host allowed while connecting to upstream, client: 127.0.0.1, server: _, request: "GET /uaa/test/hello HTTP/1.1", host: "127.0.0.1:9080"
  • 相关 issues
    • https://github.com/openresty/lua-resty-core/issues/45
    • https://github.com/Kong/kong/issues/4786

ccwxl avatar Oct 14 '21 12:10 ccwxl

At present, the APISIX nacos plug-in does not perform dns resolution on the back-end host it gets (code location), so if the domain name is configured in nacos, it will be directly used as the parameter of balancer.set_current_peer, which will lead to no host allowed While connecting to upstream error. The configuration node on the Dashboard page will first do dns query and then give balancer.set_current_peer, so it can work. code location So the error you encountered is in line with expectations, we can further consider whether service discovery should support configuration in the form of domain names.

nic-6443 avatar Oct 14 '21 13:10 nic-6443

mark,same problem, register domain as nacos ip.

biubiue avatar Dec 01 '21 06:12 biubiue

i have resolved this issue related to eureka. how can i contributor?

TenYearsIn avatar Dec 29 '21 14:12 TenYearsIn

i have resolved this issue related to eureka. how can i contributor?

Can you start with a brief chat about how it was solved?

tzssangglass avatar Dec 30 '21 01:12 tzssangglass

i have resolved this issue related to eureka. how can i contributor?

Can you start with a brief chat about how it was solved?

I parse the file '/etc/hosts' with RegExp in the initial phase,So I can get 'hosts to ip' mapping . then I modify eureka code that get ip (the code is line 132 in /apisix/discovery/eureka/init.lua ), use mapping relations to get ip in high priority , if not, I use the eureka service ip oneself .

TenYearsIn avatar Dec 30 '21 01:12 TenYearsIn

I parse the file '/etc/hosts' with RegExp in the initial phase

Did you write the host and ip mappings in /etc/hosts beforehand?

So I can get 'hosts to ip' mapping . then I modify eureka code that get ip (the code is line 132 in /apisix/discovery/eureka/init.lua ), use mapping relations to get ip in high priority , if not, I use the eureka service ip oneself .

I have a suggestion for enhancing nacos/eureka

  1. determine whether the host is a domain or an ip
  2. if it's a domain, resolve the domain to an ip, overriding the host

ref: https://github.com/apache/apisix/blob/719c5388143d73cc1424d0a03ec51584a60e243e/apisix/init.lua#L166-L188

tzssangglass avatar Dec 30 '21 04:12 tzssangglass

Did you write the host and ip mappings in /etc/hosts beforehand?

yeah, i think this is General Situation

I have a suggestion for enhancing nacos/eureka determine whether the host is a domain or an ip

  1. determine whether the host is a domain or an ip
  2. if it's a domain, resolve the domain to an ip, overriding the host

nice~ so. i modify the code resolver .parse_domain that can get ip from ' host to ip ' mapping in high priority then use dns_parse ?

TenYearsIn avatar Dec 30 '21 05:12 TenYearsIn

Did you write the host and ip mappings in /etc/hosts beforehand?

yeah, i think this is General Situation

I have a suggestion for enhancing nacos/eureka determine whether the host is a domain or an ip

  1. determine whether the host is a domain or an ip
  2. if it's a domain, resolve the domain to an ip, overriding the host

nice~ so. i modify the code resolver .parse_domain that can get ip from ' host to ip ' mapping in high priority then use dns_parse ?

It seems that this is a more generic demands that we can use the /etc/hosts to change the DNS resolving.

tokers avatar Dec 30 '21 09:12 tokers

It seems that this is a more generic demands that we can use the /etc/hosts to change the DNS resolving.

Our dns resolution process also takes resolution information from /etc/hosts. So the key point of my suggestion is to use the existing dns resolution method, not just /etc/hosts, to resolve the domain name.

tzssangglass avatar Dec 30 '21 13:12 tzssangglass

nice~ so. i modify the code resolver .parse_domain that can get ip from ' host to ip ' mapping in high priority then use dns_parse ?

just try

tzssangglass avatar Dec 30 '21 13:12 tzssangglass

I get same error in custom plugin when rewrite upstream. Upstream data:

{
    "id": "ups-sys1-abcd",
    "create_time": 1643165083,
    "update_time": 1643190928,
    "nodes": [
        {
            "host": "example.com",
            "port": 80,
            "weight": 1
        }
    ],
    "type": "roundrobin",
    "hash_on": "vars",
    "scheme": "http",
    "pass_host": "pass",
    "name": "ups-sys1-abcd"
}

Route:

{
    "id": "1",
    "create_time": 1643186095,
    "update_time": 1643187397,
    "uri": "/get",
    "name": "",
    "methods": [
        "GET"
    ],
    "host": "sys1.com",
    "plugins": {
        "custom-plugin": {}
    },
    "upstream": {
        "nodes": [
            {
                "host": "httpbin.org",
                "port": 80,
                "weight": 1
            }
        ],
        "type": "roundrobin",
        "hash_on": "vars",
        "scheme": "http",
        "pass_host": "pass"
    },
    "status": 1
}

Custom plugin code:

function _M.rewrite(conf, ctx)
    -- rewrite by request header
    -- get upstream info
    local upstreams = core.config.fetch_created_obj("/upstreams")
    if not upstreams then
        return core.response.exit(502)
    end
    -- new upstream id by system name and tag
    local upstream_id = "ups-sys1-abcd"
    local ups_info = upstreams:get(tostring(upstream_id))
    if not ups_info then
        return
    end
    ups_info = ups_info.value
    upstream.set(ctx, ups_info.name, ctx.conf_version, ups_info)
end

error log:

apisix            | 2022/01/26 09:11:48 [error] 48#48: *456684 [lua] balancer.lua:353: run(): failed to set server peer [example.com:80] err: no host allowed while connecting to upstream, client: 172.18.0.1, server: _, request: "GET /get HTTP/1.1", host: "sys1.com"

elricli avatar Jan 26 '22 10:01 elricli

hi @drrrMikado, the cause and the solution of this problem are provided above. Since it is your own plugin, there is nothing I can do about it.

tzssangglass avatar Jan 26 '22 12:01 tzssangglass

hi @drrrMikado, the cause and the solution of this problem are provided above. Since it is your own plugin, there is nothing I can do about it.

Ok, Thanks.

elricli avatar Jan 27 '22 06:01 elricli

2.99.0上也遇到这个问题,目前有修复计划吗

zzong12 avatar Nov 07 '22 11:11 zzong12

is discovery to get the service address should be judged when it is not the ip, otherwise then resolve the dns/hosts ?

zzong12 avatar Nov 08 '22 08:11 zzong12