Ran
Ran

Reputation: 502

Can't connect to ElasticSearch Docker cluster which run on local virtual machine using Java API

I have Ubuntu running as a virtual machine using Oracle VM. In this VM in run elasticsreach docker using this command:

sudo docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 elasticsearch

In Oracle VM machine network setting I added this port forwarding:

Host Guest

127.0.0.1:9200 --> 127.0.0.1:9200 127.0.0.1:9300 --> 127.0.0.1:9300

When I try to send http request using chrome to the address 127.0.0.1:9200 and 127.0.0.1:9300 I get a response with no problem.

When I try to connect using Java Client API I get this exception:

[DEBUG] org.elasticsearch.client.transport - [Rattler] adding address [[#transport#-1][ran-pc][inet[/127.0.0.1:9300]]]
[DEBUG] org.elasticsearch.transport.netty - [Rattler] connected to node [[#transport#-1][ran-pc][inet[/127.0.0.1:9300]]]
[DEBUG] org.elasticsearch.client.transport - [Rattler] failed to connect to discovered node [[Ringmaster][LB14-DlsS-2XIDwduUj5sA][c367b60225b2][inet[/172.17.0.2:9300]]]
org.elasticsearch.transport.ConnectTransportException: [Ringmaster][inet[/172.17.0.2:9300]] connect_timeout[30s]
    at org.elasticsearch.transport.netty.NettyTransport.connectToChannels(NettyTransport.java:825) ~[elasticsearch-1.6.0.jar:na]
    at org.elasticsearch.transport.netty.NettyTransport.connectToNode(NettyTransport.java:758) ~[elasticsearch-1.6.0.jar:na]
    at org.elasticsearch.transport.netty.NettyTransport.connectToNode(NettyTransport.java:731) ~[elasticsearch-1.6.0.jar:na]
    at org.elasticsearch.transport.TransportService.connectToNode(TransportService.java:216) ~[elasticsearch-1.6.0.jar:na]
    at org.elasticsearch.client.transport.TransportClientNodesService$NodeSampler.validateNewNodes(TransportClientNodesService.java:331) [elasticsearch-1.6.0.jar:na]
    at org.elasticsearch.client.transport.TransportClientNodesService$SniffNodesSampler.doSample(TransportClientNodesService.java:503) [elasticsearch-1.6.0.jar:na]
    at org.elasticsearch.client.transport.TransportClientNodesService$NodeSampler.sample(TransportClientNodesService.java:315) [elasticsearch-1.6.0.jar:na]
    at org.elasticsearch.client.transport.TransportClientNodesService.addTransportAddresses(TransportClientNodesService.java:174) [elasticsearch-1.6.0.jar:na]
    at org.elasticsearch.client.transport.TransportClient.addTransportAddresses(TransportClient.java:268) [elasticsearch-1.6.0.jar:na]
    at infastructure.documents.ElasticSearchDocumentStore.lambda$Connect$2(ElasticSearchDocumentStore.java:104) [classes/:na]
    at infastructure.documents.ElasticSearchDocumentStore$$Lambda$5/673384643.run(Unknown Source) [classes/:na]
    at com.nurkiewicz.asyncretry.AsyncRetryExecutor.lambda$doWithRetry$4(AsyncRetryExecutor.java:55) [asyncretry-0.0.6.jar:na]
    at com.nurkiewicz.asyncretry.AsyncRetryExecutor$$Lambda$6/3886839.call(Unknown Source) [asyncretry-0.0.6.jar:na]
    at com.nurkiewicz.asyncretry.SyncRetryJob.run(SyncRetryJob.java:23) [asyncretry-0.0.6.jar:na]
    at com.nurkiewicz.asyncretry.RetryJob.run(RetryJob.java:95) [asyncretry-0.0.6.jar:na]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_45]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_45]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_45]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_45]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_45]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_45]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_45]
Caused by: java.net.ConnectException: Connection timed out: no further information: /172.17.0.2:9300
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_45]
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_45]
    at org.elasticsearch.common.netty.channel.socket.nio.NioClientBoss.connect(NioClientBoss.java:152) ~[elasticsearch-1.6.0.jar:na]
    at org.elasticsearch.common.netty.channel.socket.nio.NioClientBoss.processSelectedKeys(NioClientBoss.java:105) ~[elasticsearch-1.6.0.jar:na]
    at org.elasticsearch.common.netty.channel.socket.nio.NioClientBoss.process(NioClientBoss.java:79) ~[elasticsearch-1.6.0.jar:na]
    at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:337) ~[elasticsearch-1.6.0.jar:na]
    at org.elasticsearch.common.netty.channel.socket.nio.NioClientBoss.run(NioClientBoss.java:42) ~[elasticsearch-1.6.0.jar:na]
    at org.elasticsearch.common.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108) ~[elasticsearch-1.6.0.jar:na]
    at org.elasticsearch.common.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42) ~[elasticsearch-1.6.0.jar:na]
    ... 3 common frames omitted

As you can see it connect successfully to 127.0.0.1:9300, but then it get the node ip (172.17.0.2) and of course the connection to it failed (There is no ping from my windows that run the VM to this ip).

I tried to disable the firewall and add this fort forwarding:

172.17.0.2:9300 --> 172.17.0.2:9300

or

172.17.0.2:9300 --> 10.0.0.15:9300

(10.0.0.15 is the ip of the VM) but no success.

I understand the problem is in port forwarding, but how can I solve it?

I looked on this issue, but I don't find here an answer, and guess it another problem.

Upvotes: 2

Views: 1436

Answers (1)

Eyal.Dahari
Eyal.Dahari

Reputation: 770

The problem is that Elasticsearch cluster is located on a different network which is the network Docker had created.

One way to solve it is using SSH tunneling on your local machine to that network and forward from local a port to remote 9300.

Here is an example on how how to define SSH tunneling and a sample code on how to connect to Elasticsearch using JAVA client API.

Upvotes: 1

Related Questions