Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch and Testcontainers for testing "None of the configured nodes are available"

I try to write test using elasticsearch container. I run it with https://www.testcontainers.org/ library. That's my configuration:

@ClassRule
public static GenericContainer elasticContainer =
        new GenericContainer("docker.elastic.co/elasticsearch/elasticsearch:5.3.0")
                .withExposedPorts(9300, 9200)
                .withEnv("xpack.security.enabled", "false")
                .withEnv("transport.host", "127.0.0.1")
                .withEnv("http.host", "0.0.0.0");

And I got an exception:

org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{9fuJUZYWS6O6IgLGOgJDaA}{localhost}{127.0.0.1:32792}]

I reconfigured my ports for test and 9200 port is available (on the port that mapped by testcontainers) - I checked it by curl. But 9300 is not.

Does anybody knows how to fix transport host problem?

like image 254
knalx Avatar asked Apr 10 '17 12:04

knalx


2 Answers

The problem was with elastic search container - not with testcontainers lib.

I have found solution here https://github.com/olivere/elastic/issues/57#issuecomment-88697714

Transport client can't resolve ElasticSearch node in container.

Final code is:

@ClassRule
public static GenericContainer elasticContainer =
        new FixedHostPortGenericContainer("docker.elastic.co/elasticsearch/elasticsearch:5.3.0")
                .withFixedExposedPort(9200, 9200)
                .withFixedExposedPort(9300, 9300)
                .waitingFor(Wait.forHttp("/")) // Wait until elastic start
                .withEnv("xpack.security.enabled", "false")
                .withEnv("network.host", "_site_")
                .withEnv("network.publish_host", "_local_");

Also if you want just start ElasticSearch in docker and use 9300 (transport port) run this:

docker run  -p 9300:9300 -p 9200:9200 -e "xpack.security.enabled=false"  -e "network.host=_site_" -e "network.publish_host=_local_"  docker.elastic.co/elasticsearch/elasticsearch:5.3.0
like image 56
knalx Avatar answered Sep 23 '22 22:09

knalx


For anyone coming to this question now, please note that as of Testcontainers 1.10.1, we have Elasticsearch as an official module in the Testcontainers library. You should hopefully find this much easier than 'rolling your own' using GenericContainer!

Usage is documented here.

like image 32
Richard North Avatar answered Sep 24 '22 22:09

Richard North