无法使用docker容器的ip地址连接到该容器

oalqel3c  于 2021-10-10  发布在  Java
关注(0)|答案(2)|浏览(499)

我正在使用docker compose启动一些容器,这些容器形成了solr和zookeeper节点的集群。
这是我的撰写文件(取自此处):

version: '3.8'
services:
  solr1:
    image: solr:8.7.0
    container_name: solr1
    ports:
     - "8981:8983"
    environment:
      - ZK_HOST=zoo1:2181,zoo2:2181,zoo3:2181
    networks:
      - solr
    depends_on:
      - zoo1
      - zoo2
      - zoo3

  solr2:
    image: solr:8.7.0
    container_name: solr2
    ports:
     - "8982:8983"
    environment:
      - ZK_HOST=zoo1:2181,zoo2:2181,zoo3:2181
    networks:
      - solr
    depends_on:
      - zoo1
      - zoo2
      - zoo3

  solr3:
    image: solr:8.7.0
    container_name: solr3
    ports:
     - "8983:8983"
    environment:
      - ZK_HOST=zoo1:2181,zoo2:2181,zoo3:2181
    networks:
      - solr
    depends_on:
      - zoo1
      - zoo2
      - zoo3

  zoo1:
    image: zookeeper:3.6.2
    container_name: zoo1
    restart: always
    hostname: zoo1
    ports:
      - 2181:2181
      - 7001:7000
    environment:
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
      ZOO_4LW_COMMANDS_WHITELIST: mntr, conf, ruok
      ZOO_CFG_EXTRA: "metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider metricsProvider.httpPort=7000 metricsProvider.exportJvmInfo=true"
    networks:
      - solr

  zoo2:
    image: zookeeper:3.6.2
    container_name: zoo2
    restart: always
    hostname: zoo2
    ports:
      - 2182:2181
      - 7002:7000
    environment:
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
      ZOO_4LW_COMMANDS_WHITELIST: mntr, conf, ruok
      ZOO_CFG_EXTRA: "metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider metricsProvider.httpPort=7000 metricsProvider.exportJvmInfo=true"
    networks:
      - solr

  zoo3:
    image: zookeeper:3.6.2
    container_name: zoo3
    restart: always
    hostname: zoo3
    ports:
      - 2183:2181
      - 7003:7000
    environment:
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
      ZOO_4LW_COMMANDS_WHITELIST: mntr, conf, ruok
      ZOO_CFG_EXTRA: "metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider metricsProvider.httpPort=7000 metricsProvider.exportJvmInfo=true"
    networks:
      - solr

networks:
  solr:

如果我跑 docker compose ls ,我看到项目正在正确运行:

NAME                STATUS
solr-cluster        running(6)

这是 docker container ls :

CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS          PORTS                                                                                                                NAMES
d5333378e78c   25b74ae1e488   "docker-entrypoint.s…"   23 hours ago   Up 52 minutes   0.0.0.0:8983->8983/tcp, :::8983->8983/tcp                                                                            solr3
5fac27cd0443   25b74ae1e488   "docker-entrypoint.s…"   23 hours ago   Up 52 minutes   0.0.0.0:8982->8983/tcp, :::8982->8983/tcp                                                                            solr2
db37ef90ca98   25b74ae1e488   "docker-entrypoint.s…"   23 hours ago   Up 52 minutes   0.0.0.0:8981->8983/tcp, :::8981->8983/tcp                                                                            solr1
b8b55694e281   a72350516291   "/docker-entrypoint.…"   23 hours ago   Up 52 minutes   2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:2183->2181/tcp, :::2183->2181/tcp, 0.0.0.0:7003->7000/tcp, :::7003->7000/tcp   zoo3
10885eafe4e8   a72350516291   "/docker-entrypoint.…"   23 hours ago   Up 52 minutes   2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:2182->2181/tcp, :::2182->2181/tcp, 0.0.0.0:7002->7000/tcp, :::7002->7000/tcp   zoo2
558f8574c036   a72350516291   "/docker-entrypoint.…"   23 hours ago   Up 52 minutes   2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, :::2181->2181/tcp, 8080/tcp, 0.0.0.0:7001->7000/tcp, :::7001->7000/tcp   zoo1

如果我尝试使用浏览器使用主机导航solr节点 localhost (例如。 http://localhost:8983/ ),容器使用solr admin接口正确响应。
但是,如果我尝试使用浏览器访问同一容器,但从其ip访问(例如。 http://172.23.0.7:8983/ 哪里 172.23.0.7 知识产权是什么 solr3 容器)我得到一个连接超时。如果我尝试ping该ip,我也会收到连接超时。
为什么会发生这种情况?两个都不应该吗 localhost172.23.0.7 主持人工作?
我需要从容器的ip访问容器,因为solr节点使用其ip向zookeeper注册。因此,由于我无法从容器的ip访问容器,因此当我尝试通过solrj API以编程方式连接到容器时,会出现connectiontimeout异常:

public static void main(String[] args)
{
    List<String> zkNodes = new ArrayList<>(3);

    zkNodes.add("localhost:2181");
    zkNodes.add("localhost:2182");
    zkNodes.add("localhost:2183");

    SolrClient solrClient = new CloudSolrClient.Builder(zkNodes, Optional.empty())
        .withConnectionTimeout(10000)
        .withSocketTimeout(10000)
        .build();

    try
    {
        CollectionAdminRequest.listCollections(solrClient);
    }
    catch (IOException | SolrServerException e)
    {
        e.printStackTrace();
    }
}

这是我得到的例外:

org.apache.solr.client.solrj.SolrServerException: IOException occurred when talking to server at: http://172.23.0.7:8983/solr
    at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:695)
    at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:266)
    at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:248)
    at org.apache.solr.client.solrj.impl.LBSolrClient.doRequest(LBSolrClient.java:370)
    at org.apache.solr.client.solrj.impl.LBSolrClient.request(LBSolrClient.java:298)
    at org.apache.solr.client.solrj.impl.BaseCloudSolrClient.sendRequest(BaseCloudSolrClient.java:1157)
    at org.apache.solr.client.solrj.impl.BaseCloudSolrClient.requestWithRetryOnStaleState(BaseCloudSolrClient.java:918)
    at org.apache.solr.client.solrj.impl.BaseCloudSolrClient.request(BaseCloudSolrClient.java:850)
    at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:214)
    at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:231)
    at org.apache.solr.client.solrj.request.CollectionAdminRequest.listCollections(CollectionAdminRequest.java:2690)
    at Main.main(Main.java:28)
Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to 172.23.0.7:8983 [/172.23.0.7] failed: connect timed out
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to 172.23.0.7:8983 [/172.23.0.7] failed: connect timed out
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:374)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
    at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:571)
    ... 11 more
Caused by: java.net.SocketTimeoutException: connect timed out
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
    ... 21 more

我不知道这是否有帮助,但无论如何我在windows主机上,所以我不能使用 --network host Docker 的旗帜。

ovfsdjhp

ovfsdjhp1#

这个 172.x.x.x IP是仅可在docker网络(即solr和zookeeper节点之间)内访问的IP。
当您从外部连接到zookeeper时,zookeeper不知道有“外部”,它只知道其网络中solr服务器的ip并将其发送回您。但这些IP无法从“外部”访问。
一种解决方法是直接连接到solr服务器,而不是使用zookeeper(URL如 localhost:8981HttpSolrClient ).
或者,您需要告诉solr服务器将外部ip“呈现”给zookeeper。

kgsdhlau

kgsdhlau2#

我认为solar示例的广告地址是docker示例的ip,端口是默认的,而不是重定向的。您应该在POD中注册docker端口,而不是solr端口。
也许solr.xml中的solrcloud部分会有所帮助,但不确定。。。
https://solr.apache.org/guide/8_8/format-of-solr-xml.html

相关问题