前言

2.8 应用集成 InfluxDB 笔记中有记录一个无法连接 influxdb 容器的问题,这篇笔记做个深入分析。

小能手最近在学习 ChirpStack 项目,应该是最有影响力的 LoRaWAN 服务器开源项目。它组件丰富,代码可读性强,是个很好的学习资料。更多学习笔记,可点此查看

1 问题描述

appserver_1      | time="2020-02-07T10:24:34Z" level=error msg="integration/multi: integration *influxdb.Integration error" error="sending measurements error: http request error: Post http://localhost:8086/write?db=db0&precision=ms&rp=: dial tcp 127.0.0.1:8086: connect: connection refused"

我的主机上已经运行了 ChirpStack 的容器,根据应用需要又再开了一个 InfluxDB 容器,当在 chirpstack 的页面中配置 localhost:8086 为 InfluxDB 的连接地址时,提示 connection refused。

2 原因分析

docker 是一个虚拟环境,127.0.0.1 和 localhost 指的是虚拟环境内部,而不是外部宿主机,所以无法这样访问.

2.1 容器网络分析

# docker network ls
NETWORK ID          NAME                        DRIVER              SCOPE
02231307198a        bridge                      bridge              local
81b1d878dd80        host                        host                local
5284403f2094        lora-app-server_default     bridge              local
8de8416ca11a        lora_default                bridge              local
d4389dfb8f38        loraserver-docker_default   bridge              local
49d4a4343b5e        lorawan-stack-dev_default   bridge              local
194a422ad895        none                        null                local
lora-app-server_default
# docker network inspect lora-app-server_default
[
    {
        "Name": "lora-app-server_default",
        "Id": "5284403f20943bcca33e6fb7482aa1c5e714bbf05a66c5712500f35c901f1a5d",
        "Created": "2019-10-29T12:37:30.514317558+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "192.168.16.0/20",
                    "Gateway": "192.168.16.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "46e4075d8376bd54b2d59d330a2f9924a0dff32e080f53b26200ce7845a6f65c": {
                "Name": "lora-app-server_mosquitto_1",
                "EndpointID": "5677bc3efacb3454d2e70d490e8c6d2bc75469080d985645ceda500940311944",
                "MacAddress": "02:42:c0:a8:10:04",
                "IPv4Address": "192.168.16.4/20",
                "IPv6Address": ""
            },
            "92ee8b9f02e044c77765e723717788d67ea7434b222191e3bd9a019dc31fd17e": {
                "Name": "lora-app-server_postgres_1",
                "EndpointID": "0c032e0946d397ee09f14deac9cf0039c3a5bba99e8a88fcfe942a7661e322d8",
                "MacAddress": "02:42:c0:a8:10:02",
                "IPv4Address": "192.168.16.2/20",
                "IPv6Address": ""
            },
            "e4201031eb7e4d4a34a165f38b8871de1e6527c16cc1cf9021993d6ec0793b11": {
                "Name": "lora-app-server_redis_1",
                "EndpointID": "c0b595b4f7a7fa3848aa57c9b65b77158ee16ed0ed8a744e580f68f0dac5878f",
                "MacAddress": "02:42:c0:a8:10:03",
                "IPv4Address": "192.168.16.3/20",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
loraserver-docker_default
# docker network inspect loraserver-docker_default
[
    {
        "Name": "loraserver-docker_default",
        "Id": "d4389dfb8f38d6a023de63ca25d74c65a5b4b114fbb83a1b43263e376000a22b",
        "Created": "2020-02-05T14:23:44.985567025+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "192.168.64.0/20",
                    "Gateway": "192.168.64.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "0329aa3dbabb76ab7d94cd19ff0ff89a8c48cc45d710b8060800a177dc2dd214": {
                "Name": "loraserver-docker_appserver_1",
                "EndpointID": "1080f620260de45592ac7c824f2e7a2f68846de5764e2734cd2f0745718c326a",
                "MacAddress": "02:42:c0:a8:40:07",
                "IPv4Address": "192.168.64.7/20",
                "IPv6Address": ""
            },
            "0d8afb7896f6d5ad7516c8079b2b38f6c88f2beac13ccb887273f62cd2d10538": {
                "Name": "loraserver-docker_geoserver_1",
                "EndpointID": "4775a6fb238a594972d8fbac20175537dc4fd83572051283484522777f42ecc5",
                "MacAddress": "02:42:c0:a8:40:04",
                "IPv4Address": "192.168.64.4/20",
                "IPv6Address": ""
            },
            "4bb2a05b99aa24a524137bbc4df3bd7e19bc8e45edd558c3bfda1d0530fc5c0b": {
                "Name": "loraserver-docker_postgresql_1",
                "EndpointID": "8e5e035a17bca1b957dc9c2d00ef385118407ad04bff87da3ca2009bfe7eeee5",
                "MacAddress": "02:42:c0:a8:40:06",
                "IPv4Address": "192.168.64.6/20",
                "IPv6Address": ""
            },
            "5e5813773f6dbc0af8136e234df9f6fd9658488fcafdf57dfdafd57cb9198c76": {
                "Name": "loraserver-docker_gatewaybridge_1",
                "EndpointID": "9e1956f9ed7f5cd4ca1009788a811ec243428ca4b0787b621f402b06c827375b",
                "MacAddress": "02:42:c0:a8:40:08",
                "IPv4Address": "192.168.64.8/20",
                "IPv6Address": ""
            },
            "9b8a1f733e2b3e995eaac5fcdd759ea7f4926943e511f3754c66293e7910ce6e": {
                "Name": "loraserver-docker_loraserver_1",
                "EndpointID": "ffc5869e673c591776f07d7fc5ab95ab017c9773dd11aa403010c69551b373a0",
                "MacAddress": "02:42:c0:a8:40:05",
                "IPv4Address": "192.168.64.5/20",
                "IPv6Address": ""
            },
            "c984f4f533bd904469a6afe70224baade7fffa550b345580bfe911e38235581e": {
                "Name": "loraserver-docker_redis_1",
                "EndpointID": "d7ebab7f77eac0beb71f207b94c7432ab04092c2c5442eeeaa1d87c7783085a1",
                "MacAddress": "02:42:c0:a8:40:02",
                "IPv4Address": "192.168.64.2/20",
                "IPv6Address": ""
            },
            "cc5400f93f5454ee5033f37a8b8bd584a292429baaa7c9fd16e788bd34200407": {
                "Name": "loraserver-docker_mosquitto_1",
                "EndpointID": "d47e177c7785d3a0f9c0e70e707bfdfb7cbb056f1b785bfc136a503559d7d929",
                "MacAddress": "02:42:c0:a8:40:03",
                "IPv4Address": "192.168.64.3/20",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "loraserver-docker",
            "com.docker.compose.version": "1.24.1"
        }
    }
]

2.2 容器网络梳理

bridge 172.17.0.1

  • influxdb 172.17.0.2

lora-app-server_default 192.168.16.1

  • lora-app-server_mosquitto_1
  • lora-app-server_postgres_1
  • lora-app-server_redis_1

loraserver-docker_default 192.168.64.1

  • loraserver-docker_appserver_1
  • loraserver-docker_gatewaybridge_1
  • loraserver-docker_loraserver_1
  • loraserver-docker_geoserver_1
  • loraserver-docker_postgresql_1
  • loraserver-docker_mosquitto_1
  • loraserver-docker_redis_1

综上所述,loraserver-docker_appserver_1 与 influxdb 不在同一个网络中,因此无法通信。

具体原因可以查看 Docker 官网教程实践 自定义 bridge 网络

3 处理方案

方案1 网桥 ip 即为宿主机 ip

influxdb 容器有对外暴露出 8086 端口,因此可以直接通过宿主机IP及暴露端口来定位该容器。

宿主机 IP 为网桥 IP,也就是 Docker0 IP 地址,可参考:
https://nickjanetakis.com/blog/docker-tip-65-get-your-docker-hosts-ip-address-from-in-a-container

# ip a
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:5f:e8:7c:be brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever

因此我们通过 172.17.0.1:8086 即可定位到 influxdb 容器。我们在 chirpstack webui 中可以这样配置 InfluxDB integration API endpoint:

http://172.17.0.1:8086/write

方案2 将 influxdb 容器连接到相同网络中

# docker network connect loraserver-docker_default influxdb
# docker network inspect loraserver-docker_default 
[
    {
        "Name": "loraserver-docker_default",
        "Id": "d4389dfb8f38d6a023de63ca25d74c65a5b4b114fbb83a1b43263e376000a22b",
        "Created": "2020-02-05T14:23:44.985567025+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "192.168.64.0/20",
                    "Gateway": "192.168.64.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "0329aa3dbabb76ab7d94cd19ff0ff89a8c48cc45d710b8060800a177dc2dd214": {
                "Name": "loraserver-docker_appserver_1",
                "EndpointID": "1080f620260de45592ac7c824f2e7a2f68846de5764e2734cd2f0745718c326a",
                "MacAddress": "02:42:c0:a8:40:07",
                "IPv4Address": "192.168.64.7/20",
                "IPv6Address": ""
            },
            "0d8afb7896f6d5ad7516c8079b2b38f6c88f2beac13ccb887273f62cd2d10538": {
                "Name": "loraserver-docker_geoserver_1",
                "EndpointID": "4775a6fb238a594972d8fbac20175537dc4fd83572051283484522777f42ecc5",
                "MacAddress": "02:42:c0:a8:40:04",
                "IPv4Address": "192.168.64.4/20",
                "IPv6Address": ""
            },
            "4bb2a05b99aa24a524137bbc4df3bd7e19bc8e45edd558c3bfda1d0530fc5c0b": {
                "Name": "loraserver-docker_postgresql_1",
                "EndpointID": "8e5e035a17bca1b957dc9c2d00ef385118407ad04bff87da3ca2009bfe7eeee5",
                "MacAddress": "02:42:c0:a8:40:06",
                "IPv4Address": "192.168.64.6/20",
                "IPv6Address": ""
            },
            "5e5813773f6dbc0af8136e234df9f6fd9658488fcafdf57dfdafd57cb9198c76": {
                "Name": "loraserver-docker_gatewaybridge_1",
                "EndpointID": "9e1956f9ed7f5cd4ca1009788a811ec243428ca4b0787b621f402b06c827375b",
                "MacAddress": "02:42:c0:a8:40:08",
                "IPv4Address": "192.168.64.8/20",
                "IPv6Address": ""
            },
            "98b72943ab195564fb1a8465243bff96cbc7dc989a3a16dab5269afc835336f5": {
                "Name": "influxdb",
                "EndpointID": "b89b8104bcf27a29bc3a7b470206a4669e5513b37d09e888ab970425299a807c",
                "MacAddress": "02:42:c0:a8:40:09",
                "IPv4Address": "192.168.64.9/20",
                "IPv6Address": ""
            },
            "9b8a1f733e2b3e995eaac5fcdd759ea7f4926943e511f3754c66293e7910ce6e": {
                "Name": "loraserver-docker_loraserver_1",
                "EndpointID": "ffc5869e673c591776f07d7fc5ab95ab017c9773dd11aa403010c69551b373a0",
                "MacAddress": "02:42:c0:a8:40:05",
                "IPv4Address": "192.168.64.5/20",
                "IPv6Address": ""
            },
            "c984f4f533bd904469a6afe70224baade7fffa550b345580bfe911e38235581e": {
                "Name": "loraserver-docker_redis_1",
                "EndpointID": "d7ebab7f77eac0beb71f207b94c7432ab04092c2c5442eeeaa1d87c7783085a1",
                "MacAddress": "02:42:c0:a8:40:02",
                "IPv4Address": "192.168.64.2/20",
                "IPv6Address": ""
            },
            "cc5400f93f5454ee5033f37a8b8bd584a292429baaa7c9fd16e788bd34200407": {
                "Name": "loraserver-docker_mosquitto_1",
                "EndpointID": "d47e177c7785d3a0f9c0e70e707bfdfb7cbb056f1b785bfc136a503559d7d929",
                "MacAddress": "02:42:c0:a8:40:03",
                "IPv4Address": "192.168.64.3/20",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "loraserver-docker",
            "com.docker.compose.version": "1.24.1"
        }
    }
]

这样,我们可以通过容器名 influxdb 直接访问该容器,也可以通过容器 ip 192.168.64.9 来访问。

因此我们在 chirpstack webui 中可以这样配置 InfluxDB integration API endpoint:

http://influxdb:8086/write

或者:

http://192.168.64.9:8086/write

4 小结

这节笔记从 chirpstack 的 app_server 容器外接 influxdb 无法通过 localhost 连接的一个典型问题入手,分析了 chirpstack 所涉及的容器网络,针对问题给出了 网桥ip即为宿主机ip、将 influxdb 容器也接入相同容器网络 等两种解决方案。

END


Logo

尧米是由西云算力与CSDN联合运营的AI算力和模型开源社区品牌,为基于DaModel智算平台的AI应用企业和泛AI开发者提供技术交流与成果转化平台。

更多推荐