Elasticsearch is a free and open-source analytics engine used for storing, searching, and analyzing big volumes of data in real-time. It is written in Java and based on Apache Lucene. It is known for its speed, scalability, and powerful set of features. You can use it to monitor application performance, logging, and log analytics.
In this tutorial, we will show you how to install ElasticSearch on Rocky Linux 8.
Prerequisites
- A server running RockyLinux 8.
- A root password is configured on the server.
Install Java
ElasticSearch is based on Java. So Java must be installed in your server. You can install it using the following command:
dnf install java-11-openjdk-devel -y
Once the Java is installed, verify the Java installation using the following command:
java -version
You should see the following output:
openjdk version "11.0.12" 2021-07-20 LTS OpenJDK Runtime Environment 18.9 (build 11.0.12+7-LTS) OpenJDK 64-Bit Server VM 18.9 (build 11.0.12+7-LTS, mixed mode, sharing)
Install ElasticSearch
By default, ElasticSearch is not included in the Rocky Linux 8. So you will need to create an ElasticSearch repo.
First, import the ElasticSearch GPG key with the following command:
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
Next, create an ElasticSearch repo with the following command:
nano /etc/yum.repos.d/elasticsearch.repo
Add the following lines:
[elasticsearch-7.x] name=Elasticsearch repository for 7.x packages baseurl=https://artifacts.elastic.co/packages/7.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md
Save and close the file then install the ElasticSearch package with the following command:
dnf install elasticsearch -y
Once ElasticSearch is installed, you can proceed to the next step.
Configure ElasticSearch
By default, ElasticSearch main configuration file is located at /etc/elasticsearch/elasticsearch.yml. You can edit it with the following command:
nano /etc/elasticsearch/elasticsearch.yml
Define your node name, cluster name, data path, and network host as shown below:
cluster.name: Elastic Cluster node.name: rockylinux path.data: /var/lib/elasticsearch network.host: 127.0.0.1
Save and close the file then start the ElasticSearch service and enable it to start at system reboot:
systemctl start elasticsearch
systemctl enable elasticsearch
You can verify the status of the ElasticSearch with the following command:
systemctl status elasticsearch
You should see the following output:
? elasticsearch.service - Elasticsearch Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2021-08-09 04:34:34 UTC; 8s ago Docs: https://www.elastic.co Main PID: 5247 (java) Tasks: 62 (limit: 11411) Memory: 1.1G CGroup: /system.slice/elasticsearch.service ??5247 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=1> ??5412 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller Aug 09 04:33:29 RockyLinux8 systemd[1]: Starting Elasticsearch... Aug 09 04:34:34 RockyLinux8 systemd[1]: Started Elasticsearch.
Verify ElasticSearch
At this point, ElasticSearch is started and listening on port 9200. You can check it with the following command:
ss -antpl | grep 9200
You should see the following output:
LISTEN 0 128 [::ffff:127.0.0.1]:9200 *:* users:(("java",pid=5247,fd=283))
You can also verify the ElasticSearch with the following command:
curl -X GET 'http://localhost:9200'
You should get the following output:
{ "name" : "rockylinux", "cluster_name" : "Elastic Cluster", "cluster_uuid" : "NuDPakHARaOJOMyi6ABQwA", "version" : { "number" : "7.14.0", "build_flavor" : "default", "build_type" : "rpm", "build_hash" : "dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1", "build_date" : "2021-07-29T20:49:32.864135063Z", "build_snapshot" : false, "lucene_version" : "8.9.0", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" }
How to Use ElasticSearch
You can use the Curl command to add data to the ElasticSearch as shown below:
curl -H 'Content-Type: application/json' -X POST 'http://localhost:9200/tutorial/blog/1' -d '{ "message": "My first blog!" }'
You should see the following output:
{"_index":"tutorial","_type":"blog","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
You can now retrieve your data using the GET request:
curl -X GET 'http://localhost:9200/tutorial/blog/1'
You should see the following output:
{"_index":"tutorial","_type":"blog","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":{ "message": "My first blog!" }}
To retrieve the data in human-readable format, run the following command:
curl -X GET 'http://localhost:9200/tutorial/blog/1?pretty'
You should get the following output:
{ "_index" : "tutorial", "_type" : "blog", "_id" : "1", "_version" : 1, "_seq_no" : 0, "_primary_term" : 1, "found" : true, "_source" : { "message" : "My first blog!" } }
Conclusion
In the above guide, you learned how to install and use ElasticSearch on Rocky Linux 8. You can now easily add, read, delete, and update data in Elasticsearch.