Cyberithub

How to Check Elasticsearch Cluster Health Status in Linux Using 3 Easy Steps

Advertisements

In this tutorial, we will understand the steps and queries required to check the Elasticsearch cluster health status. Sometimes you might have noticed that frontend app which was fetching data from Elasticsearch cluster suddenly not showing any data and it goes completely blank. Although the issue could be anywhere but just for the understanding we will see it from the Elasticsearch context where data might not be available because Elasticsearch cluster went down and are now unable to service any further requests. More on Elasticsearch Cluster Service official documentation.

The other issue that could have happened that Cluster might be up and running fine but data is missing from Elasticsearch cluster or is not available due to by mistake deletion. Well here we are more on looking from cluster perspective so we will ignore all the other possibilities and will take up those in later articles.

To understand more on this topic first you need to understand what is Elasticsearch, how it works, how to talk to Elasticsearch and what are the queries needs to be used to check the cluster status and other important things. We will see all of them one by one in below sections.

What is Elasticsearch

Elasticsearch is a free distributed, open source search and analytics engine built on top of Apache Lucene Library. It was developed in Java and is now the most popular analytics engine currently in use. It currently supports the client in many languages like C#, PHP, Python etc.

How Elasticsearch Works

Elasticsearch takes the idea of database to save data to the next level where it saves everything as a document collection of whose are known as indices. Each index can be further subdivided into smaller units called shards where each shard can act as a fully independent index. So a document saved in an index can be distributed among clusters and each index with multiple shards can be distributed among different nodes in a cluster.

How to Talk to Elasticsearch

Over the time there are many developments happen in this field but only few of them are supported and in use currently. Those are mentioned below.

  • HTTP Client: It is the most general way to connect and talk to the Elasticsearch.
  • Native Client: It is also one of the method used by few developers to talk to Elasticsearch.
  • Other Client: It is always possible to write your own plugin based on current environment and use it to run your Elasticsearch queries.

How to Check Elasticsearch Cluster Health Status in Linux Using 3 Easy Steps

Check Elasticsearch Cluster Health Status

Also Read: Concept of Data Encapsulation in Python Explained with Best Examples

Step 1: Check Elasticsearch Version

You can always verify the Elasticsearch version first by running curl -XGET 'http://localhost:9200' query from command line as shown below. It is just to check that Elasticsearch queries are running fine without any issue. By default Elasticsearch always runs on Port 9200 hence we are using this port in our query. As you can see from below output current Elasticsearch version is 6.6.1.

[root@localhost ~]# curl -XGET 'http://localhost:9200'
{
"name" : "books-data",
"cluster_name" : "books-data-cluster",
"cluster_uuid" : "i2mphs3gSVO8NqZqkpF6SQ",
"version" : {
"number" : "6.6.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}

NOTE:

Please note that here I am using root user to run all the below commands. You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.

Step 2: Check Elasticsearch Cluster Health Status

In the next step if you go and check the Elasticsearch cluster health status by running curl http://localhost:9200/_cluster/health?pretty query then it should show something like below where you can see the Cluster Name, Status, Number of Nodes, Active Shards, Active shards percentage etc. Here you can see that the status is currently green on the output which means your Elasticsearch cluster is up and running fine. In case you see the status as yellow or red then you need to further check the root cause for this.

Sometimes all the shards are not initialized or may be some of them got corrupted then status will either show yellow or red depends on the total number of shards allocated to the nodes. If it is showing yellow then it means atleast one primary shard and its replica are not allocated to the node and if it is showing red then replica shards for atleast one index are not allocated to the node. You can check more on How to Delete Elasticsearch Unassigned Shards in 4 Easy Steps to Know more about this issue.

[root@localhost ~]# curl http://localhost:9200/_cluster/health?pretty
{
"cluster_name" : "books-data-cluster",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 0,
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}

Step 3: Restart Elasticsearch Cluster Service

You can also restart the Elasticsearch service once to check if it resolves the cluster issue. There are multiple ways to restart the service depends on how you are using it. If you are using SysV init then you need to use service elasticsearch restart command to restart the service.

[root@localhost ~]# service elasticsearch restart

If you are using SystemD then you need to use systemctl restart elasticsearch command to restart the service.

[root@localhost ~]# systemctl restart elasticsearch

If you are using Supervisord then you need to use supervisorctl restart elasticsearch command to restart the service.

[root@localhost ~]# supervisorctl restart elasticsearch

 

 

 

 

Popular Recommendations:-

How to Delete Elasticsearch Red Status Indices in 3 Easy Steps

Popular 30 Elasticsearch Interview Questions and Answers[Recent-2020] For Beginners 

10 Popular Examples of sudo command in Linux(RedHat/CentOS 7/8)

9 useful w command in Linux with Examples

12 Most Popular rm command in Linux with Examples

8 Useful Linux watch command examples (RedHat/CentOS 7/8)

Leave a Comment