ElasticSearch Install & Configuration

1. Environment

2. Install

  • 将安装包通过SFTP上传至Centos7 服务器中指定目录,这里放在/opt目录下

    cd /opt
    ls -l
    
  • 创建安装目录

    mkdir -p /usr/local/es/
    mkdir -p /usr/local/es/data
    mkdir -p /usr/local/es/logs
    
  • 解压至安装目录

    tar -zxvf elasticsearch-6.7.1.tar.gz -C /usr/local/es/
    
  • 进入config文件夹下编辑elasticsearch.yml

    cd /usr/local/es/elasticsearch-6.7.1/config
    vi elasticsearch.yml
    
  • 编辑elasticsearch.yml

    # Use a descriptive name for the node:
    #
    node.name: master
    
    # Path to directory where to store the data (separate multiple locations by comma):
    #
    path.data: /usr/local/es/data
    #
    # Path to log files:
    #
    path.logs: /usr/local/es/logs
    
    # Set the bind address to a specific IP (IPv4 or IPv6):
    #
    network.host: 10.10.10.20
    #
    # Set a custom port for HTTP:
    #
    http.port: 19200
    transport.tcp.port: 19300
    transport.tcp.compress: true
    
  • 设置 max_map_count文件包含限制一个进程可以拥有的VMA(虚拟内存区域)的数量

    vi /etc/sysctl.conf
    
  • 在最后面追加下面内容

    vm.max_map_count=655360
    
  • 保存后,执行

    sysctl -p
    
  • 修改每个进程最大同时打开文件数为65536否则会报错( max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

    #每个进程最大同时打开文件数太小 通过下面2个命令查看当前数量
    ulimit -Hn
    ulimit -Sn
    
    vi /etc/security/limits.conf
    
  • 修改内容为

    *               soft    nofile          65536
    *               hard    nofile          65536
    
  • 修改成功后,root用户重新登录!重新登录!重新登录!

  • 由于 elasticsearch不能以root用户启动, 否则会报错( can not run elasticsearch as root ), 所以需要创建centos系统用户

    #创建用户es
    adduser es
    #设置es密码
    passwd es
    #密码 ZBg8wzvM
    #设置文件权限
    chown -R es /usr/local/es/
    
  • 切换用户

    su es
    
  • 启动ElasticSearch

    cd /usr/local/es/elasticsearch-6.7.1/bin
    ./elasticsearch -d
    
  • 查看ElasticSearch是否启动成功

    ps -ef | grep elasticsearch
    curl http://10.10.10.20:19200
    
  • 查看端口

    netstat -lnpt
    
  • 查看日志

    cat /usr/local/es/logs/elasticsearch.log
    
  • 设置ElasticSearch自动启动

    vi /lib/systemd/system/elasticsearch.service
    
  • 写入以下脚本

    [Unit]
    Description=elasticsearch
    [Service]
    LimitNOFILE=100000
    LimitNPROC=100000
    ExecStart=/usr/local/es/elasticsearch-6.7.1/bin/elasticsearch
    User=es
    Group=es
    [Install]
    WantedBy=multi-user.target
    
  • 给脚本赋权限

    chmod +x /usr/lib/systemd/system/elasticsearch.service
    
  • 添加到开机启动任务

    systemctl daemon-reload
    systemctl start elasticsearch
    systemctl enable elasticsearch
    
  • 如果出现错误可以使用如下命令查看日志

    sudo journalctl -u elasticsearch.service
    
  • 重启机器,检测elasticsearch是否自启

    ps -ef | grep elasticsearch
    curl http://10.10.10.20:19200
    

3. Configuration

4. Configuration 防火墙

  • 查看elasticsearch使用端口19200、19300

    netstat -lnpt
    
  • 开放端口,这里使用19200、19300端口

    firewall-cmd --permanent --zone=public --add-port=19200/tcp
    firewall-cmd --permanent --zone=public --add-port=19300/tcp
    
  • 重启防火墙

    firewall-cmd --reload
    
  • 查看防火墙,已开放端口列表

    firewall-cmd --list-ports