Elasticsearch is an open source full text search and analytics engine tool used in storing, searching and analysing big volumes of data real time. It supports RESTful operations i.e you can use HTTP methods in combisnation with a HTTP URI to manipulate data. This guide demonstrates how to install and configure Elasticsearch on Rocky and AlmaLinux.
Elastic search is popular due to its amazing features such as:
- Scalability and resiliency.
- Easy to use.
- REST APIs.
- Has improved security.
- Automatic node recovery.
- Automatic data balancing.
For Cluster setup setup checkout: Setup Three Node Elasticsearch Cluster on Rocky Linux 8 Using Ansible
Step 1: Install Java runtime
One of the dependencies that Elasticsearch uses is Java. In this guide, we will install Java OpenJDK 17.
sudo dnf install java-17-openjdk-devel
Dependency tree:
...
Transaction Summary
================================================================================
Install 3 Packages
Total download size: 43 M
Installed size: 177 M
Is this ok [y/N]: y
Verify the installed version.
$ java -version
openjdk version "17.0.11" 2024-04-16 LTS
OpenJDK Runtime Environment (Red_Hat-17.0.11.0.9-3) (build 17.0.11+9-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-17.0.11.0.9-3) (build 17.0.11+9-LTS, mixed mode, sharing)
Step 2: Install Elasticsearch
First install GPG key fro Elasticsearch rpm packages.
sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
Second, create a yum repository for Elasticsearch as below:
sudo vi /etc/yum.repos.d/elasticsearch.repo
In the file, paste the below lines:
- Elasticsearch 7.x
[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
- Elasticsearch 8.x
[elastic-8.x]
name=Elastic repository for 8.x packages
baseurl=https://artifacts.elastic.co/packages/8.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
With the created repository file, we an now install Elasticsearch using the below command:
sudo dnf install elasticsearch
Dependency tree:
...
Transaction Summary
================================================================================
Install 1 Package
Total download size: 312 M
Installed size: 520 M
Is this ok [y/N]: y
Step 3: Configure Elasticsearch
With a complete installation, we edit the configurations in /etc/elasticsearch/elasticsearch.yml
sudo vi /etc/elasticsearch/elasticsearch.yml
Set the node.name and cluster.name, they are set by finding these lines and uncomment them then edit them as below.
Edit the file as below:
cluster.name: my new cluster
node.name: node-1
path.data: /var/lib/elasticsearch
network.host: 127.0.0.1
You can also specifiy a different path.data by uncommenting the path.data
and changing the directory as
path.data: /path/to/media
Start and enable Elasticsearch service
sudo systemctl enable --now elasticsearch
Check the status of the service;
systemctl status elasticsearch
Step 4: Test Elasticsearch installation
Now we test and see if the installed Elasticsearch is working. Elasticsearch should be running on port 9200. Test this using the curl command as below.
- Elasticsearch 7
curl -X GET 'http://localhost:9200'
- Elasticsearch 8
sudo curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://localhost:9200
With the above response, Elasticserach is working properly on port 9200
Step 5: Using Elasticsearch
Here we will add we add some data and use manual queries.
Using CURL, we will add this entry:
curl -H 'Content-Type: application/json' -X POST 'http://localhost:9200/tutorial/helloworld/1' -d '{ "message": "Hello World!" }'
Output:
{"_index":"tutorial","_type":"helloworld","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1
We sent a HTTP POST
request to the Elasticsearch server with the URI tuitorial/helloworld/1. There are parameters in the URI, tuitorial is the index of the data, helloworld is the type and 1 is the ID of the entry
Now lets retrieve the entry using HTTP GET
request
curl -X GET 'http://localhost:9200/tutorial/helloworld/1'
Output:
{"_index":"tutorial","_type":"helloworld","_id":"1","_version":3,"_seq_no":2,"_primary_term":1,"found":true,"_source":{ "message": "Hello World!" }
We can modify the existing entry using HTTP PUT
as below
curl -H 'Content-Type: application/json' -X PUT 'localhost:9200/tutorial/helloworld/1?pretty' -d '
{
"message": "Welcome Back!"
}'
Output:
{
"_index" : "tutorial",
"_type" : "helloworld",
"_id" : "1",
"_version" : 4,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
From the output, we have modified the message to “Welcome Back!” and the version number now increases to 2
The output of GET can be changed to a more readable/human-language by using the pretty
argument
curl -X GET 'http://localhost:9200/tutorial/helloworld/1?pretty'
Output:
{
"_index" : "tutorial",
"_type" : "helloworld",
"_id" : "1",
"_version" : 4,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"message" : "Welcome Back!"
}
}
Explore More with CloudSpinx
Looking to streamline your tech stack? At CloudSpinx, we deliver robust solutions tailored to your needs.
Learn more about how we can support your journey with CloudSpinx.
Check out our other articles:
- How To Install Kubernetes Cluster on Proxmox VE
- Deploy Kubernetes Cluster on Oracle Linux 8 with Kubeadm
- Deploy k0s Kubernetes Cluster on Ubuntu 24.04|22.04|20.04|18.04