Best Steps To Install Elasticsearch on Rocky Linux 8

Posted on

Best Steps To Install Elasticsearch on Rocky Linux 8

Best Steps To Install Elasticsearch on Rocky Linux 8

In this guide, we want to teach you How To Install Elasticsearch on Rocky Linux 8. Elasticsearch is a modern search and analytics engine which is based on Apache Lucene. Completely open source and built with Java, Elasticsearch is a NoSQL database. That means it stores data in an unstructured way and that you cannot use SQL to query it. If you need help installing Elasticsearch on Rocky Linux 8, read on!

You can now proceed to the guide steps below on the Orcacore website to set up Elasticsearch on Rocky Linux 8.

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Rocky Linux 8.

1. Install Java for Elasticsearch Setup

Elasticsearch needs Java installed on your server. First, update your local package index with the following command:

sudo dnf update -y

To install the OpenJDK package on Rocky Linux 8, run the following command:

sudo dnf install java-11-openjdk-devel -y

Now verify that your Java installation was successful by printing its version:

java -version

In your output you will see:

**Output**
openjdk version "11.0.17" 2022-10-18 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.17.0.8-2.el8_6) (build 11.0.17+8-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.17.0.8-2.el8_6) (build 11.0.17+8-LTS, mixed mode, sharing)

2. Install Elasticsearch on Rocky Linux 8

As you know, the Elasticsearch package is not available in the default Rocky Linux repository. You need to install it from the Elasticsearch RPM repository. Let’s get Elasticsearch installed on Rocky Linux 8

Import Elasticsearch GPG Key

First, import the GPG key with the following command:

sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

Add Elasticsearch 8 Repository

Then, you need to create an Elasticsearch repository file with your favorite text editor, here we use vi text editor:

sudo vi /etc/yum.repos.d/elasticsearch.repo

Add the following content to the file:

[elasticsearch]
name=Elasticsearch 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=0
autorefresh=1
type=rpm-md

When you are done, save and close the file.

Now you can use the following command to install Elasticsearch on your Rocky Linux server:

sudo dnf install --enablerepo=elasticsearch elasticsearch -y

Verify your Elasticsearch installation by using the command below:

rpm -qi elasticsearch
Elasticsearch Rocky Linux 8

Manage Elasticsearch Service

At this point, you need to start and enable Elasticsearch to start on boot on Rocky Linux 8. To do this, use the following commands:

# sudo systemctl daemon-reload
# sudo systemctl enable elasticsearch.service
# sudo systemctl start elasticsearch.service

Verify that your service is active and running:

sudo systemctl status elasticsearch.service
**Output**
● elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vend>
   Active: **active** (**running**) since Tue 2022-11-08 05:06:43 EST; 55s ago
     Docs: https://www.elastic.co
 Main PID: 89849 (java)
    Tasks: 90 (limit: 23699)
   Memory: 2.4G
   CGroup: /system.slice/elasticsearch.service
...

By default, the Elasticsearch service listens to port 9200. You can confirm this using the command below:

ss -pnltu | grep 9200
**Output**
tcp   LISTEN 0      128                     *:**9200**            *:*    users:(("java",pid=89913,fd=414))

3. Configure Elasticsearch on Rocky Linux 8

At this point, you can make some basic configuration changes. Let’s configure Elasticsearch on Rocky Linux 8

Elasticsearch data is stored in the default directory location (/var/lib/elasticsearch). To edit the configuration files, you can find them in the directory location (/etc/elasticsearch). The Java start-up options can be configured in the (/etc/default/elasticsearch) configuration file.

If you want to set up a cluster, you need to modify the configuration file to allow remote connections.

By default, Elasticsearch listens on localhost. To change this, you need to open the configuration file with your favorite text editor, here we use vi:

sudo vi /etc/elasticsearch/elasticsearch.yml

Now find the network.host directive and uncomment it by removing the “#”.

You can replace it with your private IP address. Or you can change it to listen to all by entering 0.0.0.0:

network.host: 0.0.0.0

Also, uncomment the cluster.name attribute and specify your own cluster name:

cluster.name: my-application

Next, uncomment node.name: and specify the name of your node:

node.name: node-1

Finally, set the xpack.security.enabled security setting to false:

xpack.security.enabled: false

When you are done, save and close the file.

To apply the changes, restart Elasticsearch on Rocky Linux 8 with the following command:

sudo systemctl restart elasticsearch

4. Send an HTTP Request with Elasticsearch

At this point, you can verify your installation was successful by using the curl command to send an HTTP request to port 9200 on localhost:

curl -X GET "localhost:9200/"

In your output you will see:

Test Elasticsearch

You can use the curl command to use Elasticsearch.

5. Examples of Using Elasticsearch

Here are some examples of using Elasticsearch:

To delete an index, you can use the following command, here the name of the index is “sample”:

curl -X DELETE 'http://localhost:9200/samples'

You can list all indexes with:

curl -X GET 'http://localhost:9200/_cat/indices?v'

Also, you can list all docs in the index with the command below:

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

Conclusion

At this point, you have learned to Install and Configure Elasticsearch on Rocky Linux 8. Elasticsearch on Rocky Linux 8 is used for real-time search, analytics, and data storage. It helps in efficiently storing, searching, and analyzing large volumes of data.

Hope you enjoy it.

You may also like these articles:

Install and Configure Laravel on Rocky Linux 8

How To Install and Use Iptables on Rocky Linux 8

How To Install MysQL on Rocky Linux 9

Alternative Solutions for Installing Elasticsearch on Rocky Linux 8

While the provided guide offers a straightforward method to install Elasticsearch on Rocky Linux 8 using the official Elasticsearch repository, alternative approaches exist. Here are two different ways to achieve the same goal: using Docker and using Ansible.

1. Installing Elasticsearch with Docker

Docker provides a containerized environment, simplifying application deployment and management. Installing Elasticsearch within a Docker container offers several advantages, including isolation, portability, and reproducibility.

Explanation:

Docker containers package Elasticsearch and its dependencies into a single unit, ensuring consistency across different environments. This eliminates potential conflicts with system-level libraries or configurations. Docker also simplifies updates and rollbacks, as you can easily switch between different Elasticsearch versions by simply changing the Docker image.

Steps:

  1. Install Docker: If Docker is not already installed on your Rocky Linux 8 system, install it using the following commands:

    sudo dnf install docker -y
    sudo systemctl start docker
    sudo systemctl enable docker
  2. Pull the Elasticsearch Docker Image: Obtain the official Elasticsearch Docker image from Docker Hub:

    sudo docker pull docker.elastic.co/elasticsearch/elasticsearch:8.6.0

    Replace 8.6.0 with the desired Elasticsearch version.

  3. Run the Elasticsearch Container: Create and start the Elasticsearch container with appropriate settings:

    sudo docker run -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:8.6.0
    • -d: Runs the container in detached mode (background).
    • -p 9200:9200 -p 9300:9300: Maps ports 9200 and 9300 on the host to the container.
    • -e "discovery.type=single-node": Configures Elasticsearch to run in single-node mode for development purposes. For production deployments, a cluster setup is recommended.
  4. Verify the Installation: After the container starts, verify that Elasticsearch is running by accessing http://localhost:9200 in your web browser or using curl:

    curl -X GET "localhost:9200/"

You should see the same output as in the original guide, confirming a successful installation.

2. Installing Elasticsearch with Ansible

Ansible is an automation tool that allows you to define and execute infrastructure as code. Using Ansible to install Elasticsearch ensures consistency and repeatability across multiple servers.

Explanation:

Ansible playbooks define the desired state of your system, including the packages to install, configuration files to modify, and services to manage. By using Ansible, you can automate the entire Elasticsearch installation process, reducing the risk of human error and ensuring that all servers are configured identically.

Steps:

  1. Install Ansible: If Ansible is not already installed on your Rocky Linux 8 system, install it using the following commands:

    sudo dnf install ansible -y
  2. Create an Ansible Playbook: Create a file named elasticsearch.yml with the following content:

    ---
    - hosts: all
      become: true
      tasks:
        - name: Install Java
          dnf:
            name: java-11-openjdk-devel
            state: present
    
        - name: Import Elasticsearch GPG key
          rpm_key:
            state: present
            key: https://artifacts.elastic.co/GPG-KEY-elasticsearch
    
        - name: Add Elasticsearch repository
          yum_repository:
            name: elasticsearch
            description: Elasticsearch repository for 8.x packages
            baseurl: https://artifacts.elastic.co/packages/8.x/yum
            gpgcheck: yes
            gpgkey: https://artifacts.elastic.co/GPG-KEY-elasticsearch
            enabled: yes
            state: present
    
        - name: Install Elasticsearch
          dnf:
            name: elasticsearch
            state: present
    
        - name: Configure Elasticsearch (example: setting network.host)
          lineinfile:
            path: /etc/elasticsearch/elasticsearch.yml
            regexp: '^#network.host: '
            line: 'network.host: 0.0.0.0'
    
        - name: Start and enable Elasticsearch service
          systemd:
            name: elasticsearch
            state: started
            enabled: yes

    This playbook automates the steps outlined in the original guide, including installing Java, adding the Elasticsearch repository, installing the Elasticsearch package, configuring the network.host setting, and starting the Elasticsearch service.

  3. Run the Ansible Playbook: Execute the playbook using the ansible-playbook command:

    ansible-playbook elasticsearch.yml

    This command will connect to the target server(s) and execute the tasks defined in the playbook. Ensure you have configured SSH access to your target server(s) and that Ansible can authenticate properly.

  4. Verify the Installation: After the playbook completes, verify that Elasticsearch is running by accessing http://<your_server_ip>:9200 in your web browser or using curl:

    curl -X GET "<your_server_ip>:9200/"

Both Docker and Ansible offer viable alternatives to the manual installation process described in the original guide. Docker provides a containerized environment for simplified deployment, while Ansible automates the installation process for consistency and repeatability. Choosing the best approach depends on your specific needs and environment.

Leave a Reply

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