How to connect to elasticsearch cluster with multiple nodes installed on Azure ? How do I get elasticsearch endpoint?

Viewed 419

I have installed 3 nodes of elastic search from Azure marketplace and any node can act as master node. Now how do I connect to the cluster ? If I had one node, I could simply use its IP with Port(9200) but here I have 3 nodes so how do I get the cluster endpoint ? Thanks

3 Answers

This how I did it and it worked well for me:

public class ElasticsearchConfig {

    private Vector<String> hosts;

    public void setHosts(String hostString) {
        if(hostString == null || hostString.trim().isEmpty()) {
            return;
        }

        String[] hostParts = hostString.split(",");
        this.hosts = new Vector<>();
        Collections.addAll(this.hosts, hostParts);
    }
}

public class ElasticClient {

    private final ElasticsearchConfig config;

    private RestHighLevelClient client;

    public ElasticClient(ElasticsearchConfig config) {
        this.config = config;
    }

    public void start() throws Exception {

        HttpHost[] httpHosts = new HttpHost[config.getHosts().size()];
        config.getHosts()
                .stream()
                .map(host -> new HttpHost(host.split(":")[0], Integer.valueOf(host.split(":")[1])))
                .collect(Collectors.toList())
                .toArray(httpHosts);

        client = new RestHighLevelClient(RestClient.builder(httpHosts));

        System.out.println("Started ElasticSearch Client");
    }

    public void stop() throws Exception {
        if (client != null) {
            client.close();
        }
        client = null;
    }
}

Set the ElasticsearchConfig as below:

ElasticsearchConfig config = new ElasticsearchConfig();
config.setHosts("ip1:port,ip2:port,ip3:port");

If all three nodes are part of same cluster than no need to specify all the nodes and even one single node ip is enough to connect to the cluster.

But above approach has some disadvantage and in small cluster with less workload its fine as in this case only one node which you configure in your elasticsearch client will act as co-ordinating node and can become a hot-spot in your cluster, its better to have all the nodes configured in your client so for every request any one node can act co-ordinating node and if you have huge workload you might also consider to have dedicated co-ordinating nodes for even better performance.

Hope this answer your question and I didn't provide the code snippet as don't know which language and client you are using and in your question I felt code is not issue but you want to understand the concept in detail.

Appreciate everyone's time and all those who replied; Turns out that by default, Azure marketplace copy of self managed ES, sets up only "Internal Load Balancer". I was able to get the cluster end point as soon as I configured "External Load Balancer". All set now.

Related