ElasticSearch Clustering setup

ElasticSearch is flexible and powerful open source, distributed real-time search and analytic engine. Using a simple set of APIs, it provides the ability for full-text search. Elastic search is freely available under the Apache 2 license, which provides most flexibility.

This article will help you for configuring ElasticSearch Multi Node Cluster on CentOS, RHEL, Ubuntu and Debian Systems. In ElasticSearch multi node cluster is just configuring multiple single node clusters with same cluster name in same network.

Network Scenerio

We have three server with following ips and host names. All server are running in same LAN and have full access to each other server using ip and hostname both.

  192.168.10.101  NODE_1
  192.168.10.102  NODE_2
  192.168.10.103  NODE_3

Verify Java (All Nodes)

Java is the primary requirement for installing ElasticSearch. So make sure you have Java installed on all nodes.

# java -version 

java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

If you don’t have Java installed on any node system, use one of following link to install it first.

Install Java 8 on CentOS/RHEL 7/6/5
Install Java 8 on Ubuntu

Download ElasticSearch (All Nodes)

Now download the latest ElasticSearch archive on all node systems from its official download page. At the time of last update of this article ElasticSearch 1.4.2 version is latest version available to download. Use following command to download ElasticSearch 1.4.2.

$ wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.2.tar.gz

Now extract ElasticSearch on all node systems.

$ tar xzf elasticsearch-1.4.2.tar.gz

Configure ElasticSearch

Now we need to setup ElasticSearch on all node systems. ElasticSearch uses “elasticsearch” as default cluster name. We recommend to change it as per your naming conversation.

$ mv elasticsearch-1.4.2 /usr/share/elasticsearch
$ cd /usr/share/elasticsearch

To change cluster named edit config/elasticsearch.yml file in each node and update following values. Node names are dynamically generated, but to keep a fixed user-friendly name change it also.

On NODE_1

Edit elasticsearch cluster configuration on NODE_1 (192.168.10.101) system.

$ vim config/elasticsearch.yml
  cluster.name: TecAdminCluster
  node.name: "NODE_1"

On NODE_2

Edit elasticsearch cluster configuration on NODE_2 (192.168.10.102) system.

$ vim config/elasticsearch.yml
  cluster.name: TecAdminCluster
  node.name: "NODE_2"

On NODE_3

Edit elasticsearch cluster configuration on NODE_3 (192.168.10.103) system.

$ vim config/elasticsearch.yml
  cluster.name: TecAdminCluster
  node.name: "NODE_3"

Install ElasticSearch-Head Plugin (All Nodes)

elasticsearch-head is a web front end for browsing and interacting with an Elastic Search cluster. Use the following command to install this plugin on all node systems.

$ bin/plugin --install mobz/elasticsearch-head

Starting ElasticSearch Cluster (All Nodes)

As the ElasticSearch cluster setup has been completed. Let start ElasticSearch cluster using following command on all nodes.

$ ./bin/elasticsearch &

By default elasticserch listen on port 9200 and 9300. So connect to NODE_1 on port 9200 like following url, You will see all three nodes in your cluster.

http://NODE_1:9200/_plugin/head

Verify Multi Node Cluster

To verify that cluster is working properly. Insert some data in one node and if the same data is available in other nodes, it means cluster is working properly.

Insert Data on NODE_1

To verify cluster create a bucket in NODE_1 and add some data.

$ curl -XPUT http://NODE_1:9200/mybucket
$ curl -XPUT 'http://NODE_1:9200/mybucket/user/rahul' -d '{ "name" : "Rahul Kumar" }'
$ curl -XPUT 'http://NODE_1:9200/mybucket/post/1' -d '
{
    "user": "rahul",
    "postDate": "01-16-2015",
    "body": "Adding Data in ElasticSearch Cluster" ,
    "title": "ElasticSearch Cluster Test"
}'

Search Data on All Nodes

Now search same data from NODE_2 and NODE_3 and check if same data is replicated to other nodes of cluster. As per above commands we have created a user named rahul and added some data there. So use following commands to search data associated with user rahul.

$ curl 'http://NODE_1:9200/mybucket/post/_search?q=user:rahul&pretty=true'
$ curl 'http://NODE_2:9200/mybucket/post/_search?q=user:rahul&pretty=true'
$ curl 'http://NODE_3:9200/mybucket/post/_search?q=user:rahul&pretty=true'

and you will get results something like below for all above commands.

{
  "took" : 69,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "mybucket",
      "_type" : "post",
      "_id" : "1",
      "_score" : 1.0,
      "_source":
{
    "user": "rahul",
    "postDate": "01-16-2015",
    "body": "Adding Data in ElasticSearch Cluster" ,
    "title": "ElasticSearch Cluster Test"
}
    } ]
  }
}

View Cluster Data on Web Browser

To view data on ElasticSearch cluster access of elasticsearch-head plugin using one of cluster ip at below url. Then click on Browser tab.

http://NODE_1:9200/_plugin/head/