elasticsearch 安装笔记

什么是 Elasticsearch

Elasticsearch是一个基于Lucene库的搜索引擎。它提供了一个分布式、支持多租户的全文搜索引擎,具有HTTP Web接口和无模式JSON文档。Elasticsearch是用Java开发的,并在Apache许可证下作为开源软件发布。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。

安装 JDK

  • Elasticsearch是java应用,故依赖JDK

    1
    yum install -y java-1.8.0-openjdk wget

安装 elasticsearch

  • 下载 elasticsearch 并解压,这里创建一个软连接,方便以后切换版本

    1
    2
    3
    4
    5
    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.7.2.tar.gz
    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.7.2.tar.gz.sha512
    shasum -a 512 -c elasticsearch-6.7.2.tar.gz.sha512
    tar -xzf elasticsearch-6.7.2.tar.gz -C /usr/share
    ln -s /usr/share/elasticsearch-6.7.2 /usr/share/elasticsearch
  • 若出现 shasum: command not found 报错信息,安装 perl-Digest-SHA 即可

    1
    yum install -y perl-Digest-SHA
  • 创建 elasticsearch 用户及组,为了安全考虑 elasticsearch 不能使用有 root 权限的用户进行启动

    1
    2
    groupadd elasticsearch
    useradd elasticsearch -g elasticsearch -p elasticsearch
  • 创建 elasticsearch 数据目录

    1
    mkdir -p /data/elasticsearch/{data,logs}
  • 授予 elasticsearch 用户目录访问权限

    1
    2
    chown -R elasticsearch:elasticsearch /usr/share/elasticsearch*
    chown -R elasticsearch:elasticsearch /data/elasticsearch
  • 动态调整内核的运行参数

    1
    2
    echo 'vm.max_map_count=262144' > /usr/lib/sysctl.d/elasticsearch.conf
    sysctl -p /usr/lib/sysctl.d/elasticsearch.conf
  • 修改相关配置

    • 修改 elasticsearch 配置

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      # vi /usr/share/elasticsearch/config/elasticsearch.yml

      cluster.name: dev
      node.name: node1
      path.data: /data/elasticsearch/data
      path.logs: /data/elasticsearch/logs
      network.host: 0.0.0.0
      http.port: 9200
      discovery.zen.ping.unicast.hosts: ["192.168.16.227","192.168.16.226","192.168.16.225"]
      # 集群为3个节点,故最小master数量为 ⌊nodes / 2⌋ + 1
      discovery.zen.minimum_master_nodes: 2
    • 修改 JVM 配置

      1
      2
      3
      4
      $ vi /usr/share/elasticsearch/config/jvm.options

      -Xms4g
      -Xmx4g
  • 添加 systemd 进程守护文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    $ vi /usr/lib/systemd/system/elasticsearch.service

    [Unit]
    Description=Elasticsearch
    Documentation=http://www.elastic.co
    Wants=network-online.target
    After=network-online.target

    [Service]
    RuntimeDirectory=elasticsearch
    PrivateTmp=true
    Environment=ES_HOME=/usr/share/elasticsearch
    Environment=PID_DIR=/data/elasticsearch
    WorkingDirectory=/usr/share/elasticsearch

    User=elasticsearch
    Group=elasticsearch

    ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet
    Restart=always

    # StandardOutput is configured to redirect to journalctl since
    # some error messages may be logged in standard output before
    # elasticsearch logging system is initialized. Elasticsearch
    # stores its logs in /var/log/elasticsearch and does not use
    # journalctl by default. If you also want to enable journalctl
    # logging, you can simply remove the "quiet" option from ExecStart.
    StandardOutput=journal
    StandardError=inherit

    # Specifies the maximum file descriptor number that can be opened by this process
    LimitNOFILE=262144

    # Specifies the maximum number of processes
    LimitNPROC=4096

    # Specifies the maximum size of virtual memory
    LimitAS=infinity

    # Specifies the maximum file size
    LimitFSIZE=infinity

    # Disable timeout logic and wait until process is stopped
    TimeoutStopSec=0

    # SIGTERM signal is used to stop the Java process
    KillSignal=SIGTERM

    # Send the signal only to the JVM rather than its control group
    KillMode=process

    # Java process is never killed
    SendSIGKILL=no

    # When a JVM receives a SIGTERM signal it exits with code 143
    SuccessExitStatus=143

    [Install]
    WantedBy=multi-user.target

    # Built for ${project.name}-${project.version} (${project.name})
  • 设置防火墙

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # 开启9200端口
    $ firewall-cmd --permanent --zone=public --add-port=9200/tcp

    # 关闭防火墙(不建议)
    $ systemctl stop firewalld.service
    $ systemctl disable firewalld.service

    # 重启防火墙
    $ firewall-cmd --reload

    # 开启防火墙
    $ systemctl start firewalld.service
  • 启动 elasticsearch

    1
    2
    3
    4
    5
    # 设置开机自启
    $ systemctl enable elasticsearch

    # 运行 elasticsearch
    $ systemctl start elasticsearch

参考文档

setzero wechat