Install Elasticsearch 5 on CentOS 7.x

Elasticsearch is a distributed storage and real-time search engine.
  • Distributed storage – you just need to setup and add Elasticsearch nodes, it’ll keep the data distributed on the cluster nodes. The distributed-ness makes data durable and highly-available too.
  • Real-time search engine – You can get to query the data the moment it’s been written.
Due to the above 2 attributes you have been listening and reading about Elasticsearch, wherever there’s a discussion of real-time data analysis. It’d not be an overstatement to say technologies like Elasticsearch set the foundation for any efficient and reliable search engine.

Step 0 – Pre-requisite

Elasticsearch is built using Java, and requires a recent version of Java installed i.e. Java 8. If you have a CentOS 7.x Everything (one with the GUI), it already has Java 8 installed:
Output of java -version:
[nahmed@localhost ~]$ java -version
openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-b15)
OpenJDK 64-Bit Server VM (build 25.111-b15, mixed mode)
in case you are using CentOS 7 Minimal you may need to install it – here’s a guide for installing Java 8 on CentOS 7.x – Install Java 8 on CentOS 7.
Note: The same JVM version should be used on all Elasticsearch nodes and clients, in case of cluster setup.

Step 1 – Elasticsearch Installation

Download the RPM
It’s a good practice to download packages in your /opt directory:
The exact output:
[nahmed@localhost opt]$ sudo wget
--2016-11-30 23:03:50--
Resolving (,,, ...
Connecting to (||:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 32866827 (31M) [application/octet-stream]
Saving to: ‘elasticsearch-5.0.2.rpm’100%[===============================================================================>] 32,866,827   834KB/s   in 49s2016-11-30 23:04:40 (660 KB/s) - ‘elasticsearch-5.0.2.rpm’ saved [32866827/32866827]
Import the PGP key – for download integrity verification
rpm --import
Install the Elasticsearch rpm
rpm --install elasticsearch-5.0.2.rpm
The exact output:
[nahmed@localhost opt]$ sudo rpm --install elasticsearch-5.0.2.rpm
Creating elasticsearch group... OK
Creating elasticsearch user... OK
### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
### You can start elasticsearch service by executing
sudo systemctl start elasticsearch.service

Step 2 – Starting Elasticsearch

Reload the system daemon
systemctl daemon-reload
Enable the Elasticsearch

To automatically start the service on system reboots

systemctl enable elasticsearch.service
Finally, start the Elasticsearch service
systemctl start elasticsearch.service
The exact output:
[nahmed@localhost opt]$ sudo systemctl daemon-reload
[nahmed@localhost opt]$ sudo systemctl enable elasticsearch.service
Created symlink from /etc/systemd/system/ to /usr/lib/systemd/system/elasticsearch.service.
[nahmed@localhost opt]$ sudo systemctl start elasticsearch.service

Step 3 – Verifying if Elasticsearch is running

Test if the Elasticsearch is up and running, you can do so by sending an HTTP request to localhost port 9200, Simply hit the following url using any browser of your choice:
If you’re using a minimal CentOS 7.x (no GUI), simply execute the following command:
curl -X GET 'http://localhost:9200'
The response will be as follows:
"name" : "H5fcpdg",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "Nub7-l2sRE-TNQPeH8JrZg",
"version" : {
"number" : "5.0.2",
"build_hash" : "f6b4951",
"build_date" : "2016-11-24T10:07:18.101Z",
"build_snapshot" : false,
"lucene_version" : "6.2.1"
"tagline" : "You Know, for Search"
The exact response:
Elasticsearch success response

Step 4 – Using Elasticsearch

To get started with Elasticsearch you can use the provided API, mainly provides the native Java api, and the HTTP/JSON RESTful api, which can be accessed over the HTTP i.e. using curl command-line tool, and for simple GET request the usual browser will do the job.
Elasticsearch’s RESTful API makes the basic CRUD operations possible i.e. Create (POST), Read (GET), Update (PUT), and Delete (DELETE).
Adding data to Elasticsearch
curl -X POST 'http://localhost:9200/devopspy/helloworld/1' -d '{ "message": "Hello World!" }'
The exact output
[nahmed@localhost ]$ curl -X POST 'http://localhost:9200/devopspy/helloworld/1' -d '{ "message": "Hello World!" }'
Above we have sent an HTTP POST request to the Elasticsearch server, for adding the data provided after ‘-d‘ flag, where:
  • devopspy is the index of the data in Elasticsearch.
  • helloworld is the type.
  • 1 is the id of our entry under the above index and type.
Retrieving the just added data:
curl -X GET 'http://localhost:9200/devopspy/helloworld/1'
The exact output:
[nahmed@localhost]$ curl -X GET 'http://localhost:9200/devopspy/helloworld/1'
{"_index":"devopspy","_type":"helloworld","_id":"1","_version":1,"found":true,"_source":{ "message": "Hello World!" }}
Response in better format using ‘pretty‘ flag:
[nahmed@localhost]$ curl -X GET 'http://localhost:9200/devopspy/helloworld/1?pretty'
"_index" : "devopspy",
"_type" : "helloworld",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"message" : "Hello World!"

Leave a Reply

Your email address will not be published. Required fields are marked *