2017-06-22 66 views
1

(首先,我知道Can not connect to elasticsearch container in docker。我的問題仍然存在。)如何使用TransportClient連接到官方Docker鏡像中運行的ElasticSearch?

我在ElasticSearch上踢輪胎。

我已經run the official Docker image from the command line as described in the official documentation,指定cluster.nameelasticsearch(文檔聲稱是默認的,但檢查發現它實際上是docker-cluster默認):

$ docker run -p 9200:9200 -p 9300:9300 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" -e "xpack.security.enabled=false" -e "cluster.name=elasticsearch" docker.elastic.co/elasticsearch/elasticsearch:5.4.2 

你會注意到,我已經disabled the X-Pack security, following official documentation

你會注意到,我一直都暴露端口9200和端口9300

http://localhost:9200/_cat/health指向瀏覽器的結果是:

1498166019 21:13:39 docker-cluster yellow 1 1 3 3 0 0 3 0 - 50.0% 

...不自信地填寫我,但是按照official documentation運行時,我想這就是你所得到的結果。

無論如何,接下來,使用Java,我已經建立了Client像這樣:

this.client = new PreBuiltTransportClient(Settings.builder() 
              .put("cluster.name", "elasticsearch") 
              .put("client.transport.sniff", true) 
              .build()) 
    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); 

你會注意到我指定127.0.0.1作爲主機名(匹配transport.host財產)和9300作爲該端口(匹配暴露的端口)。

然後我運行:this.client.prepareGet("argle", "bargle", "1").get();。我期待看到某種「嘿,假人,argle不存在」的錯誤。

相反,這會導致可怕的:

NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{q00tH2RKTlCkXut03lYHOg}{127.0.0.1}{127.0.0.1:9300}]] 

我在做什麼錯?官方文檔的哪部分是不正確的?

回答

1

官方文檔希望您將transport.host Docker環境變量設置爲127.0.0.1。這需要設置爲0.0.0.0

所以連接到用於測試目的的官方ElasticSearch多克爾圖像,你需要這樣運行:

$ docker run -p 9200:9200 -p 9300:9300 -e "http.host=0.0.0.0" -e "transport.host=0.0.0.0" -e "xpack.security.enabled=false" docker.elastic.co/elasticsearch/elasticsearch:5.4.2 

羣集名稱將是,相反的是文檔告訴你 - docker-cluster(不elasticsearch )。這樣就意味着,相反的是文檔告訴你,你需要建立你的Java客戶端這樣的:

this.client = new PreBuiltTransportClient(Settings.builder() 
              .put("cluster.name", "docker-cluster") 
              .build()) 
    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); 

此外,必須client.transport.sniff設置爲true如果使用此配置將其設置爲true,則會得到原始異常。

0

在我的情況下,「xpack.security.enabled = false」是解決方案。

elasticsearch: 
    image: docker.elastic.co/elasticsearch/elasticsearch:5.4.1 
    ports: 
    - 9200:9200 
    - 9300:9300 
    container_name: elasticsearch 
    ulimits: 
    memlock: 
     soft: -1 
     hard: -1 
    mem_limit: 1g 
    environment: 
    - cluster.name=docker-cluster 
    - node.name=one 
    - bootstrap.memory_lock=false 
    - xpack.security.enabled=false 
    - "ES_JAVA_OPTS=-Xms512m -Xmx512m" 
    - network.publish_host=192.168.99.100 
    - transport.publish_port=9300 
    volumes: 
    - /usr/share/elasticsearch/data 

elasticsearch2: 
    image: docker.elastic.co/elasticsearch/elasticsearch:5.4.1 
    ports: 
    - 9201:9200 
    - 9301:9300 
    container_name: elasticsearch2 
    ulimits: 
    memlock: 
     soft: -1 
     hard: -1 
    mem_limit: 1g 
    environment: 
    - cluster.name=docker-cluster 
    - node.name=two 
    - bootstrap.memory_lock=false 
    - xpack.security.enabled=false 
    - "ES_JAVA_OPTS=-Xms512m -Xmx512m" 
    - network.publish_host=192.168.99.100 
    - transport.publish_port=9301 
    - "discovery.zen.ping.unicast.hosts=192.168.99.100" 
    - "discovery.zen.minimum_master_nodes=2" 
    volumes: 
    - /usr/share/elasticsearch/data 
相關問題