Tag: Elasticsearch

  • How to Auto Restart ElasticSearh service on failure

    How to Auto Restart ElasticSearh service on failure

    If ElasticSearh service crash on your server, you can auto restart it by modifying systemd service file.

    systemctl edit elasticsearch
    

    This will open an editor. In the file editor, paste the following text.

    [Service]
    Restart=always
    

    Save and exit the editor. Restart elasticsearch service

    systemctl restart elasticsearch
    

    To verify ElasticSearch auto restart on failure, you can kill the process. To find the process ID, you can use the command “systemctl status elasticsearch”, it will show the current status and PID of ElasticSearch. After killing the process, check the status of ElasticSearch, you will see it auto-starting.

    Monitor using cronjob

    If you are not using systemd, you can use a cronjob to monitor ElasticSearch and restart in case of failure.

    Create a file

    mkdir /usr/serverok/
    vi /usr/serverok/elasticsearch.sh
    

    In the file, paste the following code

    #!/bin/bash
    # Author: ServerOK
    # Web: https://serverok.in
    # Auto restart ElasticSearch on failure.
    
    CMD_RESULT="$(netstat -lntp | grep java | grep 9200)"
    TIME_STAMP="$(date "+%Y-%m-%d %H:%M:%S")"
    
    if [[ ! $CMD_RESULT == *"LISTEN"* ]];
    then
        service elasticsearch restart
        echo -e "${TIME_STAMP} restarted\n"
    fi
    

    This script will check if port 9200 is open (elasticsearch default port). If you use a non-standard port, replace the port 9200 in the script with your elasticsearch port.

    Make the script executable

    chmod 755 /usr/serverok/elasticsearch.sh
    

    Create a cronjob

    crontab -e
    

    Add

    */5 * * * * /usr/serverok/elasticsearch.sh >> /var/log/sok-elasticsearch.log
    

    You can also use monit to auto restart Elastic Search. How to Auto restart ElasticSearch with monit

    Check ElasticSearch Process is running

    In the above method, we check if ElasticSearch listening on a port. Instead, you can check if the process is running or not. Use the following script to check if ElasticSearch process is running, if it crashed, then restart the service.

    #!/bin/bash
    # Author: ServerOK
    # Web: https://serverok.in
    # Auto restart ElasticSearch on crash.
    
    TIME_STAMP="$(date "+%Y-%m-%d %H:%M:%S")"
    CMD_RESULT="$(ps -ef | grep -v grep | grep elastic)"
    
    if [[ -z $CMD_RESULT ]];
    then
        systemctl restart elasticsearch
        echo -e "${TIME_STAMP} restarted\n"
    fi
    

    NOTE: when you use this method, make sure you don’t name the script with name elastic as the script itself shows when you check if elasticsearch is running. So name this script with some generic name, example monitor-es.sh

    Check ElasticSearch status with systemctl

    #!/bin/bash
    
    CMD_RESULT="$(systemctl status elasticsearch.service | grep 'Active' | egrep 'running|dead|failed' | awk '{print $2}' | sed 's/[()]//g')"
    TIME_STAMP="$(date "+%Y-%m-%d %H:%M:%S")"
    
    if [[ ! $CMD_RESULT == "running" ]];
    then
        systemctl restart elasticsearch
        echo -e "${TIME_STAMP} restarted\n"
    #    echo 'elasticsearch restarted' | mail -s 'elasticsearch restarted' [email protected]
    fi
    

    Back to ElasticSearch

  • How to Install Elasticsearch in Docker

    How to Install Elasticsearch in Docker

    To Install Elasticsearch with Docker, first, you need to install docker on your server with command

    wget -qO- https://get.docker.com/ | sh
    

    Docker images for Elasticsearch are available from

    https://hub.docker.com/_/elasticsearch

    You can see all available versions at

    https://hub.docker.com/_/elasticsearch?tab=tags&page=1&ordering=last_updated

    To create an elasticsearch container, run

    docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.5.2

    To run it in the background

    docker run --name sok-elasticsearch --restart=unless-stopped \
     -d -p 9200:9200 -p 9300:9300 \
     -e "xpack.security.enabled=false" \
     -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.5.2

    To verify if ElasticSearch works, run

    boby@sok-01:~$ curl localhost:9200
    {
      "name" : "730d385743b3",
      "cluster_name" : "docker-cluster",
      "cluster_uuid" : "V9XDIPXZQt-Tfgcy8hoIDg",
      "version" : {
        "number" : "7.5.2",
        "build_flavor" : "default",
        "build_type" : "docker",
        "build_hash" : "8bec50e1e0ad29dad5653712cf3bb580cd1afcdf",
        "build_date" : "2020-01-15T12:11:52.313576Z",
        "build_snapshot" : false,
        "lucene_version" : "8.3.0",
        "minimum_wire_compatibility_version" : "6.8.0",
        "minimum_index_compatibility_version" : "6.0.0-beta1"
      },
      "tagline" : "You Know, for Search"
    }
    boby@sok-01:~$ 

    Back to Elasticsearch

  • Install Elasticsearch 7 on Ubuntu for Magento 2.3

    To install Elasticsearch, add the key

    wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg

    Install apt-transport-https

    sudo apt-get install apt-transport-https

    Add apt repository

    echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-7.x.list

    install elasticsearch with apt

    apt-get update
    apt-get install elasticsearch

    Set elasticsearch to start on boot

    systemctl daemon-reload
    systemctl enable elasticsearch

    Elasticsearch can be started and stopped as follows:

    systemctl start elasticsearch
    systemctl stop elasticsearch

    Verify Elasticsearch is running with

    systemctl status elasticsearch

    or with

    curl localhost:9200

    if it fails to start, you can debug it with

    journalctl --unit elasticsearch

    Official install instructions at

    https://www.elastic.co/guide/en/elasticsearch/reference/8.3/deb.html

    See elasticsearch

  • elasticsearch insufficient memory

    When starting elasticsearch on CentOS 7 server, it failed with error. Checking the logs with

    journalctl -u elasticsearch
    

    Found following errors

    systemd[1]: Starting Elasticsearch...
    systemd-entrypoint[2438]: Exception in thread "main" java.lang.RuntimeException: starting java failed with [1]
    systemd-entrypoint[2438]: output:
    systemd-entrypoint[2438]: #
    systemd-entrypoint[2438]: # There is insufficient memory for the Java Runtime Environment to continue.
    systemd-entrypoint[2438]: # Native memory allocation (mmap) failed to map 16710107136 bytes for committing reserved memory.
    systemd-entrypoint[2438]: # An error report file with more information is saved as:
    systemd-entrypoint[2438]: # /var/log/elasticsearch/hs_err_pid2576.log
    systemd-entrypoint[2438]: error:
    systemd-entrypoint[2438]: OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000003dc000000, 16710107136, 0) failed; error='Not enough space' (errno=
    systemd-entrypoint[2438]: at org.elasticsearch.tools.launchers.JvmOption.flagsFinal(JvmOption.java:119)
    systemd-entrypoint[2438]: at org.elasticsearch.tools.launchers.JvmOption.findFinalOptions(JvmOption.java:81)
    systemd-entrypoint[2438]: at org.elasticsearch.tools.launchers.JvmErgonomics.choose(JvmErgonomics.java:38)
    systemd-entrypoint[2438]: at org.elasticsearch.tools.launchers.JvmOptionsParser.jvmOptions(JvmOptionsParser.java:135)
    systemd-entrypoint[2438]: at org.elasticsearch.tools.launchers.JvmOptionsParser.main(JvmOptionsParser.java:86)
    systemd[1]: elasticsearch.service: main process exited, code=exited, status=1/FAILURE
    systemd[1]: Failed to start Elasticsearch.
    systemd[1]: Unit elasticsearch.service entered failed state.
    systemd[1]: elasticsearch.service failed.
    

    To fix it, edit file

    /etc/elasticsearch/jvm.options
    

    At end of the file, add

    -Xms1g
    -Xmx1g
    

    Restart elasticsearch with

    systemctl restart elasticsearch
    

    Verify elasticsearch is running with

    systemctl restart elasticsearch
    curl localhost:9200
    

    See elasticsearch

  • Elasticsearch failed to start on CentOS 7

    Elasticsearch failed to start on CentOS 7

    After installing elasticsearch on CentOS 7, it failed to start with error

    [root@ns3160182 ~]# systemctl start elasticsearch.service
    
    Job for elasticsearch.service failed because the control process exited with error code. See "systemctl status elasticsearch.service" and "journalctl -xe" for details.
    [root@ns3160182 ~]# 
    [root@ns3160182 ~]# systemctl status elasticsearch.service
    ● elasticsearch.service - Elasticsearch
       Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: disabled)
       Active: failed (Result: exit-code) since Sat 2021-03-06 11:10:24 CET; 28s ago
         Docs: https://www.elastic.co
      Process: 10234 ExecStart=/usr/share/elasticsearch/bin/systemd-entrypoint -p ${PID_DIR}/elasticsearch.pid --quiet (code=exited, status=1/FAILURE)
     Main PID: 10234 (code=exited, status=1/FAILURE)
       CGroup: /system.slice/elasticsearch.service
    
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.cli.Command.main(Command.java:90)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd[1]: elasticsearch.service: main process exited, code=exited, status=1/FAILURE
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd[1]: Failed to start Elasticsearch.
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd[1]: Unit elasticsearch.service entered failed state.
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd[1]: elasticsearch.service failed.
    [root@ns3160182 ~]#
    

    On checking journalctl, i get

    [root@ns3160182 ~]# journalctl -u elasticsearch
    -- Logs begin at Sat 2021-02-13 16:48:02 CET, end at Sat 2021-03-06 11:12:02 CET. --
    Mar 06 11:10:01 ns3160182.ip-151-106-34.eu systemd[1]: Starting Elasticsearch...
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: fatal error in thread [main], exiting
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: java.lang.NoClassDefFoundError: Could not initialize class com.sun.jna.Native
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.systemd.Libsystemd.lambda$static$0(Libsystemd.java:34)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at java.base/java.security.AccessController.doPrivileged(AccessController.java:312)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.systemd.Libsystemd.(Libsystemd.java:33)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.systemd.SystemdPlugin.sd_notify(SystemdPlugin.java:126)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.systemd.SystemdPlugin.onNodeStarted(SystemdPlugin.java:137)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.node.Node.start(Node.java:902)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.bootstrap.Bootstrap.start(Bootstrap.java:317)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:402)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.cli.Command.main(Command.java:90)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd-entrypoint[10234]: at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd[1]: elasticsearch.service: main process exited, code=exited, status=1/FAILURE
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd[1]: Failed to start Elasticsearch.
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd[1]: Unit elasticsearch.service entered failed state.
    Mar 06 11:10:24 ns3160182.ip-151-106-34.eu systemd[1]: elasticsearch.service failed.
    [root@ns3160182 ~]# 
    

    To fix this, edit file

    vi /etc/sysconfig/elasticsearch
    

    At end of the file, add

    ES_TMPDIR=/var/log/elasticsearch
    

    Now elasticsearch will start

    systemctl start elasticsearch
    

    elasticsearch CentOS 7 error

    Verify service is running with

    systemctl status elasticsearch
    

    To confirm elasticsearch is running, use command

    curl localhost:9200
    

    See elasticsearch

  • Install elasticsearch on CentOS 7

    To install elasticsearch, first install java

    yum -y install java-1.8.0-openjdk
    

    Import key

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

    Create file

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

    Paste following

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

    Install elasticsearch

    yum install --enablerepo=elasticsearch elasticsearch
    

    Set elastic search to start on boot

    systemctl enable elasticsearch
    

    To start/stop/status

    systemctl stop elasticsearch.service
    systemctl start elasticsearch.service
    systemctl status elasticsearch.service
    

    To see logs

    tail -f /var/log/elasticsearch/elasticsearch.log
    

    ElasticSearch runs on port 9200

    [root@server ~]# netstat -lntp | grep java
    tcp6       0      0 127.0.0.1:9200          :::*                    LISTEN      1289/java           
    tcp6       0      0 ::1:9200                :::*                    LISTEN      1289/java           
    tcp6       0      0 127.0.0.1:9300          :::*                    LISTEN      1289/java           
    tcp6       0      0 ::1:9300                :::*                    LISTEN      1289/java           
    [root@server ~]# 
    

    If you get error starting elasticsearch, check Elasticsearch failed to start on CentOS 7

    If you have a previous installation, make sure you delete left over files before you install differnt version of elasticsearch. I removed following files/folders when i had to downgrade elasticsearch.

    /etc/sysconfig/elasticsearch.rpmsave
    /var/lib/elasticsearch
    /var/log/elasticsearch
    

    See elasticsearch

  • Install Elasticsearch 6 on Debian for Magento

    To install Elasticsearch for Magento on Debian, install Java 1.8 and apt-transport-https

    apt install -y openjdk-8-jdk-headless
    apt install -y apt-transport-https
    

    Add key

    wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | apt-key add -
    

    Add repository

    echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | tee -a /etc/apt/sources.list.d/elastic-6.x.list
    

    Install Elasticsearch

    apt update
    apt install -y elasticsearch
    

    Enable and start Elasticsearch

    systemctl enable elasticsearch
    systemctl restart elasticsearch