Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to connect to Dockerized elasticsearch via java-client

I setup a elasticsearch container with the OFFICIAL REPO elasticsearch docker image. Then run it with

docker run -dP elasticsearch

Easy and worked. The ps info is

CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                                              NAMES
658b49ed9551        elasticsearch:latest   "/docker-entrypoint.   2 seconds ago       Up 1 seconds        0.0.0.0:32769->9200/tcp, 0.0.0.0:32768->9300/tcp   suspicious_albattani  

And I can access the server with http-client via port 32769->9200

baihetekiMacBook-Pro:0 baihe$ curl 10.211.55.100:32769
{
  "status" : 200,
  "name" : "Scorpia",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "1.4.5",
    "build_hash" : "2aaf797f2a571dcb779a3b61180afe8390ab61f9",
    "build_timestamp" : "2015-04-27T08:06:06Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}

Now I need my JAVA-program to work with the dockerized elasticsearch. The java Node client can only connected to the elasticsearch through 32768->9300 (the cluster node talking port). So I config the transport client in my java like this

    Settings settings = ImmutableSettings.settingsBuilder()
            .put("client.transport.sniff", true)
            .put("client.transport.ignore_cluster_name", true).build();
    client = new TransportClient(settings);
    ((TransportClient) client)
    .addTransportAddress(new InetSocketTransportAddress(
            "10.211.55.100", 32768));

Then I get the following errors in the console:

Caused by: org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []
    at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:305)
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:200)
    at org.elasticsearch.client.transport.support.InternalTransportIndicesAdminClient.execute(InternalTransportIndicesAdminClient.java:86)
    at org.elasticsearch.client.support.AbstractIndicesAdminClient.exists(AbstractIndicesAdminClient.java:170)
    at org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder.doExecute(IndicesExistsRequestBuilder.java:53)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:91)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:65)
    at cct.bigdata.yellowbook.service.impl.ResourceServiceImpl.<init>(ResourceServiceImpl.java:49)
    at cct.bigdata.yellowbook.config.YellowBookConfig.resourceService(YellowBookConfig.java:21)
    at cct.bigdata.yellowbook.config.YellowBookConfig$$EnhancerBySpringCGLIB$$e7d2ff3e.CGLIB$resourceService$0(<generated>)
    at cct.bigdata.yellowbook.config.YellowBookConfig$$EnhancerBySpringCGLIB$$e7d2ff3e$$FastClassBySpringCGLIB$$72e3e213.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
    at cct.bigdata.yellowbook.config.YellowBookConfig$$EnhancerBySpringCGLIB$$e7d2ff3e.resourceService(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
    ... 31 common frames omitted

When I run the elasticsearch directly in the host. everything is all right.

I check all the dockerfile of elasticsearch on docker hub. It seems all of them simply do the followings:

EXPOSE 9200 9300

I wonder has anyone tried to do the similar things. Is the 9300 the normal TCP port or UDP port? Do I need to do some special thing to make it when running the container? Thanks!

like image 397
He Bai Avatar asked May 14 '15 07:05

He Bai


2 Answers

This works for me (in docker-compose.yml).

version: "2"
services:
    elasticsearch5:
        image: docker.elastic.co/elasticsearch/elasticsearch:5.5.3
        container_name: elasticsearch5
        environment:
            - cluster.name=elasticsearch5-cluster
            - http.host=0.0.0.0
            - network.publish_host=127.0.0.1
            - transport.tcp.port=9700
            - discovery.type=single-node
            - xpack.security.enabled=false
        ports:
            - "9600:9200"
            - "9700:9700"

Specifying network.publish_host and transport.tcp.port seems to do the trick. And sniff=true still works.

like image 73
user661312 Avatar answered Sep 29 '22 12:09

user661312


If you set "client.transport.sniff" to false it should work.

If you still want to use sniffing follow next instructions: https://github.com/olivere/elastic/wiki/Docker

Detailed discussion here: https://github.com/olivere/elastic/issues/57#issuecomment-88697714

like image 40
Sidorov Maxim Avatar answered Sep 29 '22 11:09

Sidorov Maxim