<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Prabhjot Singh on Medium]]></title>
        <description><![CDATA[Stories by Prabhjot Singh on Medium]]></description>
        <link>https://medium.com/@prabhjot-singh?source=rss-e9e7da2452f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*o7LoPboYiyjL_bl_.jpg</url>
            <title>Stories by Prabhjot Singh on Medium</title>
            <link>https://medium.com/@prabhjot-singh?source=rss-e9e7da2452f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 16:55:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@prabhjot-singh/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Setup a multi node production ready Elasticsearch cluster]]></title>
            <link>https://prabhjot-singh.medium.com/setup-a-multi-node-production-ready-elasticsearch-cluster-8504955f5d10?source=rss-e9e7da2452f------2</link>
            <guid isPermaLink="false">https://medium.com/p/8504955f5d10</guid>
            <category><![CDATA[big-data]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[elk]]></category>
            <category><![CDATA[high-availability]]></category>
            <dc:creator><![CDATA[Prabhjot Singh]]></dc:creator>
            <pubDate>Sat, 15 May 2021 04:41:13 GMT</pubDate>
            <atom:updated>2021-05-15T12:42:04.053Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*20yTn7K6c6vcmEdgwAgjSA.png" /></figure><p>Elasticsearch’s distributed architecture makes it possible to search and analyze huge volumes of data in near real time. It allows you to start with one machine and scale to hundreds. Elasticsearch makes it easy to run a full-featured search cluster, though running it at scale still requires a considerable level of expertise.</p><p>Creating, maintaining and scaling an Elasticsearch cluster will be a task that will eat up quite a lot of your time. Elasticsearch is an extremely powerful search and analysis engine that can also be used for variety of tasks such as auto-completions and autosuggestions.</p><p>It should be noted that each Elasticsearch setup will likely differ from one another depending on multiple factors, including the number of available servers, workload on them, the amount of indexed data and hardware specifications. This tutorial will provide some information on how to set up a production level Elasticsearch cluster from scratch, and will add some operational tips and best practices to help you get started.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/0*1sTYPGQWvY20RJ8d.jpeg" /><figcaption>Figure a shows an Elasticsearch cluster consisting of three primary shards with one replica each. All these shards together form an Elasticsearch index and each shard is a Lucene index itself. Figure b demonstrates the logical relationship between Elasticsearch index, shards, Lucene index and documents.</figcaption></figure><h3>Basics of sharding and replication:</h3><ul><li><strong>shard</strong> — Because Elasticsearch is a distributed search engine, an index is usually split into elements known as shards that are distributed across multiple nodes. Elasticsearch automatically manages the arrangement of these shards. It also rebalances the shards as necessary, so users need not worry about the details.</li><li><strong>replica</strong> — <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.8/indices-create-index.html"><strong>In the most recent versions</strong></a> (ES 7.x), by default, Elasticsearch creates 1 primary shard and 1 replica for each index. In the earlier versions, the default number of shards was 5 and the number of replicas was 1 per index.</li></ul><p>Allocating multiple shards and replicas is the essence of the design for distributed search capability, providing for high availability and quick access in searches against the documents within an index. The main difference between a primary and a replica shard is that only the primary shard can accept indexing requests. Both replica and primary shards can serve querying requests.</p><p><strong>The ideal shard count per node should be 1 primary shard per node</strong>. So, for a cluster with 3 data nodes, the ideal primary shard count should be 3. And the recommened replication factor on production clusters is 2. If the size of each shard increases 50GB, then you probably need to bump up the number of shards by 2–3 times (not more than that), or increase the count of data nodes.</p><h3>Types of Nodes in Elasticsearch:</h3><p>There are two common types of Elasticsearch nodes: <strong>master</strong> and <strong>data</strong>. Master nodes perform cluster-wide actions, such as managing indices and determining which data nodes should store particular data shards. Data nodes hold shards of your indexed documents, and handle CRUD, search, and aggregation operations. As a general rule, data nodes consume a significant amount of CPU, memory, and I/O.</p><p>By default, every Elasticsearch node is configured to be a “master-eligible” data node, which means they store data (and perform resource-intensive operations) and have the potential to be elected as a master node. For a small cluster, this is usually fine; a large Elasticsearch cluster which is production ready, however, should be configured with <strong>dedicated master nodes</strong> so that the master node’s stability can’t be compromised by intensive data node work.</p><p>Be aware that in order to get true high availability you need to have at least 3 master-eligible nodes (this ensures that your cluster will still be 100% operational even when losing one of the master-eligible nodes).</p><p>Therefore, <a href="https://www.elastic.co/">Elastic</a> always recommends to configure 3 or 5 nodes in your cluster as master-eligible nodes. You can configure that role via the node.master property in the elasticsearch.yml file. Setting it to true (default) allows that node to become master, while false will ensure that this node never ever will become master and also will not participate in the master election.</p><p>In this case, though, I will be showing you how to manually set up a cluster consisting of 3 servers overall (all of them would be master and data nodes), all running Ubuntu 18.04 on AWS EC2 instances created in the same VPC. We can setup a 5 node or a n node cluster in a similar fashion where n is greater than 3.</p><p>Note: For disaster recoverability, you need to setup cross cluster replication, since I don’t have experience in setting up cross cluster replication in elasticsearch, this might be a good resource for cross cluster replication:</p><p><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/xpack-ccr.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/xpack-ccr.html</a></p><p>Or disk backups, snapshotting can also be considered for disaster recoverability if minor downtime is acceptable.</p><p>So, lets start setting up our cluster now:</p><h3>Elasticsearch cluster setup process:</h3><p><strong>Step 1: Install Java</strong></p><p>Elasticsearch is built on Java and requires at least Java 8 (1.8.0_131 or later) to run. Our first step, therefore, is to install Java 8 on all the nodes in the cluster. Please note that the same version should be installed on all Elasticsearch nodes in the cluster.</p><p>Repeat the following steps on all the servers designated for your cluster.</p><p>First, update your system:</p><pre>sudo apt-get update</pre><p>Then, install Java with:</p><pre>sudo apt-get install default-jre</pre><p>Checking your Java version:</p><pre>java -version</pre><p>The output of the above command should list the following or something similar (versions could be different in your case):</p><pre>openjdk version &quot;1.8.0_151&quot;<br>OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)<br>OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)</pre><p><strong>Step 2: Install elasticsearch on all the servers</strong></p><p>Our next step is to install Elasticsearch. As before, repeat the steps in this section on all your servers. <strong>And make sure that the version of elasticsearch is same on all the servers. If already installed as a standalone deployment, you need to delete the contents of /var/lib/elasticsearch</strong> directory. Also, make sure that the ports 9200 and 9300 are accessible from all the 3 servers, to and fro.</p><p>First, you need to add Elastic’s signing key so that the downloaded package can be verified (skip this step if you’ve already installed packages from Elastic):</p><pre>wget -qO - <a href="https://artifacts.elastic.co/GPG-KEY-elasticsearch">https://artifacts.elastic.co/GPG-KEY-elasticsearch</a> | sudo apt-key add -</pre><p>We also need to install the apt-transport-https package:</p><pre>sudo apt-get install apt-transport-https</pre><p>The next step is to add the repository definition to your system:</p><pre>echo &quot;deb <a href="https://artifacts.elastic.co/packages/7.x/apt">https://artifacts.elastic.co/packages/7.x/apt</a> stable main&quot; | sudo tee /etc/apt/sources.list.d/elastic-7.x.list</pre><p>All that’s left to do is to update your repositories and install Elasticsearch:</p><pre>sudo apt-get update <br>sudo apt-get install elasticsearch</pre><p><strong>Step 3: Change /etc/elasticsearch/elasticsearch.yml on all the nodes</strong></p><p>You can copy paste the below snippet to the elasticsearch.yml file on all the three nodes:</p><pre>#This node would be master eligible:<br>node.master: true</pre><pre>#This node would be a data node as well:<br>node.data: true</pre><pre>#Name the ES cluster<br>cluster.name: ES_cluster</pre><pre>#The initial set of master-eligible nodes<br>cluster.initial_master_nodes: [&quot;172.29.22.19&quot;, &quot;172.29.22.20&quot;, &quot;172.29.22.21&quot;]</pre><pre>#Keep node name as the hostname of the server. Would be easier to track.<br>node.name: ip-172-29-22-19</pre><pre>#Where the data would be stored<br>path.data: /var/lib/elasticsearch</pre><pre>#Where the logs would be stored<br>path.logs: /var/log/elasticsearch</pre><pre>#Disabling swappingfor performance boost<br>bootstrap.memory_lock: true</pre><pre>#Enter the private IP of your node along with the localhost entry:<br>network.host: [127.0.0.1, 172.29.22.19]</pre><pre>#Private IPs of all your nodes:<br>discovery.seed_hosts: [&quot;172.29.22.19&quot;, &quot;172.29.22.20&quot;, &quot;172.29.22.21&quot;]</pre><pre>#To avoid split brain (N/2 + 1):<br>discovery.zen.minimum_master_nodes: 2</pre><pre># Require explicit names when deleting indices:<br>action.destructive_requires_name: true</pre><p>All the properties configured above are either a necessity or improve the performance of the elasticsearch cluster. I would recommend you study about each property in detail. I will try to explain all these properties in a separate blog.</p><p><strong>Step 4: Adjusting JVM heap size</strong></p><p>To ensure Elasticsearch has enough operational freedom, the default JVM heap size (min/max 1 GB) should be adjusted.</p><p>As a rule of the thumb, the maximum heap size should be set up to 50% of your RAM, but no more than 32GB (due to Java pointer inefficiency in larger heaps). Elastic also recommends that the value for maximum and minimum heap size be identical.</p><p>These value can be configured using the Xmx and Xms settings in the <em>jvm.options</em> file.</p><pre>sudo vim /etc/elasticsearch/jvm.options</pre><p>Change the following snippet in the above file:</p><pre>-Xms1g<br>-Xmx1g</pre><p>to (50% of your RAM):</p><pre>-Xms32g<br>-Xmx32g</pre><p><strong>Step 5: Disable Swapping</strong></p><p>Since you already added bootstrap.memory_lock: true in your elasticsearch.yml file to disable swapping, you also need to update the /etc/default/elasticsearch file to ensure the same can happen seamlessly:</p><p>Open the /etc/default/elasticsearch file:</p><pre>sudo vim /etc/default/elasticsearch</pre><p>Add the following configurations:</p><pre>MAX_LOCKED_MEMORY=unlimited</pre><p><strong>Step 6 : Increasing open file descriptor limit</strong></p><p>Another important configuration is the limit of open file descriptors. Since Elasticsearch makes use of a large amount of file descriptors, you must ensure the defined limit is enough otherwise you might end up losing data.</p><p>The common recommendation for this setting is 65,536 and higher.</p><p>Open the /etc/security/limits.conf file:</p><pre>sudo vim  /etc/security/limits.conf</pre><p>Add the following line to the file:</p><pre>- nofile 65536</pre><p><strong>Step 7: Start elasticsearch on all the nodes</strong></p><pre>service elasticsearch restart</pre><p>Check the status of elasticsearch on all the nodes.</p><pre>service elasticsearch status</pre><p>Elasticsearch should be active and running on all the nodes.</p><p>Don’t forget to check the cluster health as well.</p><p>This API can be used to see general info on the cluster and gauge its health:</p><pre>curl -XGET &#39;localhost:9200/_cluster/health?pretty&#39;</pre><p>Response:</p><pre>{<br>  &quot;cluster_name&quot; : &quot;ES_cluster&quot;,<br>  &quot;status&quot; : &quot;green&quot;,<br>  &quot;timed_out&quot; : false,<br>  &quot;number_of_nodes&quot; : 3,<br>  &quot;number_of_data_nodes&quot; : 3,<br>  &quot;active_primary_shards&quot; : 0,<br>  &quot;active_shards&quot; : 0,<br>  &quot;relocating_shards&quot; : 0,<br>  &quot;initializing_shards&quot; : 0,<br>  &quot;unassigned_shards&quot; : 0,<br>  &quot;delayed_unassigned_shards&quot; : 0,<br>  &quot;number_of_pending_tasks&quot; : 0,<br>  &quot;number_of_in_flight_fetch&quot; : 0,<br>  &quot;task_max_waiting_in_queue_millis&quot; : 0,<br>  &quot;active_shards_percent_as_number&quot; : 100.0<br>}</pre><p><strong>Other useful APIs:</strong></p><ol><li>Cluster State<strong>:</strong></li></ol><p>This API can be sued to see a detailed status report on your entire cluster. You can filter results by specifying parameters in the call URL.</p><pre>curl -XGET &#39;localhost:9200/_cluster/state?pretty&#39;</pre><p>2. Cluster Stats:</p><p>Extremely useful for monitoring performance metrics on your entire cluster:</p><pre>curl -XGET &#39;localhost:9200/_cluster/stats?human&amp;pretty&#39;</pre><p>3. Nodes stats:</p><pre>curl -XGET &#39;localhost:9200/_nodes/stats?pretty&#39;</pre><p>Congratulations, you just finished setting up your production ready elasticsearch cluster.</p><p>Please click the clap button if you found this post to be useful. Feel free to reach out to me in the responses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1019/0*CYIOpUdTM2eke9vx.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8504955f5d10" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>