How To Install and Configure Elasticsearch on CentOS

Introduction

ElasticsearchΒ is a platform for the distributed search and analysis of data in real time. Elasticsearch is written in the Java programming language. Its popularity is due to its ease of use, powerful features, and scalability.

Elasticsearch supports RESTful operations. This means that you can use HTTP methods (GET,Β POST,Β PUT,Β DELETE, etc.) in combination with an HTTP URI (/collection/entry) to manipulate your data. The intuitive RESTful approach is both developer and user friendly, which is one of the reasons for Elasticsearch’s popularity.

Elasticsearch is free and open-source software with a solid company behind it β€” Elastic.

There are following steps for this:

  • Step 1 – Installing Java on CentOS
  • Step 2 β€” Downloading and Installing Elasticsearch on CentOS
  • Step 3 β€” Configuring Elasticsearch on CentOS
  • Step 4 β€” Testing Elasticsearch on CentOS

Let’s start with the process…

Step 1 β€” Installing Java on CentOS

Elasticsearch is written in the Java programming language. So our first step is to install a Java Runtime Environment (JRE) on your server. You will use the native CentOS OpenJDK package for the JRE.

  • Make a directory where you want to install Java. For global access (for all users) install it preferably in the directory /opt/java.
# mkdir /opt/java && cd /opt/java
  • Now it’s time to download Java (JDK) 9Β source tarball files for your system architecture by going to officialΒ Java downloadΒ page.

For reference, we have provided the source tarball file-name, please select and download these below mentioned file only.

jdk-16.0.2_linux-x64_bin.tar.gz

 

Alternatively, you may use wget command to download file directly into theΒ /opt/javaΒ directory as shown below.

# cd /opt/java
# wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/16.0.2+11/c2514751926b4512b076cc82f959763f/jdk-16.0.2_linux-x64_bin.tar.gz
  • Once file has been downloaded, you may extract the tarball usingΒ tar commandΒ as shown below.
# tar -zxvf jdk-16.0.2_linux-x64_bin.tar.gz
  • Next, move to the extracted directory and use commandΒ update-alternativesΒ to tell system where java and its executables are installed.
# cd jdk-16.0.2/
# update-alternatives --install /usr/bin/java java /opt/java/jdk-16.0.2/bin/java 100  
# update-alternatives --config java
Update Java Alternatives
  • Tell system to updateΒ javacΒ alternatives as:
# update-alternatives --install /usr/bin/javac javac /opt/java/jdk-16.0.2/bin/javac 100
# update-alternatives --config javac
Update Javac Alternatives
  • Similarly, updateΒ jarΒ alternatives as:
# update-alternatives --install /usr/bin/jar jar /opt/java/jdk-16.0.2/bin/jar 100
# update-alternatives --config jar
Update Jar Alternatives
  • Setting up Java Environment Variables.
# export JAVA_HOME=/opt/java/jdk-16.0.2/
# export JRE_HOME=/opt/java/jdk-16.0.2/jre
# export PATH=$PATH:/opt/java/jdk-16.0.2/bin:/opt/java/jdk-16.0.2/jre/bin
Set Java Environment Variables
  • Now You may verify the Java version again, to confirm.
# java -version

Suggested: If you are not usingΒ OpenJDKΒ (open source implementation of java), you may remove it as:

# yum remove openjdk-*      [On CentOs/RHEL]
# apt-get remove openjdk-*  [On Debian/Ubuntu]

 

Step 2 β€” Downloading and Installing Elasticsearch on CentOS

You can download Elasticsearch directly fromΒ elastic.coΒ inΒ zip,Β tar.gz,Β deb, orΒ rpmΒ packages. For CentOS, it’s best to use the nativeΒ rpmΒ package, which will install everything you need to run Elasticsearch.

At the time of this writing, the latest Elasticsearch version is 7.14.0.

From a working directory, download the program:

sudo rpm -ivh https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.14.0-x86_64.rpm

Elasticsearch will install inΒ /usr/share/elasticsearch/, with its configuration files placed inΒ /etc/elasticsearchΒ and its init script added inΒ /etc/init.d/elasticsearch.

To make sure Elasticsearch starts and stops automatically with the server, add its init script to the default runlevels:

sudo systemctl daemon-reload && sudo systemctl enable elasticsearch.service

With Elasticsearch installed, you will now configure a few important settings.

Step 3 β€” Configuring Elasticsearch on CentOS

Now that you have installed Elasticsearch and its Java dependency, it is time to configure Elasticsearch.

The Elasticsearch configuration files are in theΒ /etc/elasticsearchΒ directory. The ones we’ll review and edit are:

  • elasticsearch.ymlΒ β€” Configures the Elasticsearch server settings. This is where most options are stored, which is why we are mostly interested in this file.
  • jvm.optionsΒ β€” Provides configuration for the JVM such as memory settings.

The first variables to customize on any Elasticsearch server areΒ node.nameΒ andΒ cluster.nameΒ inΒ elasticsearch.yml. Let’s do that now.

As their names suggest,Β node.nameΒ specifies the name of the server (node) and the cluster to which the latter is associated. If you don’t customize these variables, aΒ node.nameΒ will be assigned automatically in respect to the server hostname. TheΒ cluster.nameΒ will be automatically set to the name of the default cluster.

TheΒ cluster.nameΒ value is used by the auto-discovery feature of Elasticsearch to automatically discover and associate Elasticsearch nodes to a cluster. Thus, if you don’t change the default value, you might have unwanted nodes, found on the same network, in your cluster.

Let’s start editing the mainΒ elasticsearch.ymlΒ configuration file.

Open it usingΒ nano or your preferred text editor:

sudo nano /etc/elasticsearch/elasticsearch.yml

Remove theΒ #Β character at the beginning of the lines forΒ node.nameΒ andΒ cluster.nameΒ to uncomment them, and then change their values. Your first configuration changes in theΒ /etc/elasticsearch/elasticsearch.ymlΒ file will look like this:

/etc/elasticsearch/elasticsearch.yml
...
node.name: "My First Node"
cluster.name: mycluster1
...
If your server only has 1GB of RAM, you must edit this setting.

OpenΒ jvm.options:

sudo nano /etc/elasticsearch/jvm.options

Now change theΒ XmsΒ andΒ XmxΒ values toΒ 512MB:

/etc/elasticsearch/jvm.options
...
-Xms512m
-Xmx512m
...
Save and exit the file.

Now start Elasticsearch for the first time:

sudo systemctl start elasticsearch.service

Allow some time for Elasticsearch to start before you attempt to use it. Otherwise, you may get a connection error.

 

Step 4 β€” Testing Elasticsearch on CentOS

By now, Elasticsearch should be running on port 9200. You can test this usingΒ curl, the command-line tool for client-side URL transfers.

To test the service, make aΒ GET request like this:

curl -X GET 'http://localhost:9200'

You will see the following response:

Output:

 

Congratulations !! If you see a similar response, Elasticsearch is working properly. If not, recheck the installation instructions and allow some time for Elasticsearch to fully start.

Happy Learning !!

Thank you !!