20210404 0. Elasticsearch 部署

环境信息

  • Linux:CentOS 7
  • Elasticsearch:7.12.0

单机安装过程

安装 Elasticsearch

  1. 安装在 /opt 目录

    cd /opt
    tar -xzvf elasticsearch-7.12.0-linux-x86_64.tar.gz
    mv elasticsearch-7.12.0 elasticsearch
    
  2. 修改配置文件 config/elasticsearch.yml

    # 取消注释
    node.name: node-1
    # 取消注释,修改为虚拟机 IP
    network.host: 192.168.181.133
    # 取消注释
    http.port: 9200
    # 取消注释,修改为单节点
    cluster.initial_master_nodes: ["node-1"]
    
  3. 按需修改配置文件 jvm.options

    -Xms512m
    -Xmx512m
    
  4. 添加 es 用户,es 默认 root 用户无法启动,需要改为其他用户

    useradd estest
    # 修改密码
    passwd estest
    # 改变es目录拥有者账号
    chown -R estest /opt/elasticsearch/
    
  5. 修改 /etc/sysctl.conf

    # 末尾添加
    vm.max_map_count=655360
    
    # 修改配置生效
    sysctl -p
    
  6. 修改 /etc/security/limits.conf

    # 末尾添加
    * soft nofile 65536
    * hard nofile 65536
    * soft nproc 4096
    * hard nproc 4096
    
  7. 切换用户,启动 ES

    su estest
    /opt/elasticsearch/bin/elasticsearch
    
  8. 浏览器访问测试: 192.168.181.133:9200

安装 Kibana

  1. 安装在 /opt 目录

    cd /opt
    tar -xzvf kibana-7.12.0-linux-x86_64.tar.gz
    mv kibana-7.12.0-linux-x86_64 kibana
    
  2. 改变 Kibana 目录拥有者,设置访问权限

    chown -R estest /opt/kibana/
    chmod -R 777 /opt/kibana/
    
  3. 修改配置文件,config/kibana.yml

    # 取消注释
    server.port: 5601
    server.host: "0.0.0.0"
    # The URLs of the Elasticsearch instances to use for all your queries.
    elasticsearch.hosts: ["http://192.168.181.133:9200"]
    
  4. 切换用户,启动 Kibana

    su estest
    /opt/kibana/bin/kibana
    
  5. 测试访问:192.168.181.133:5601

启动异常

报错日志:

  log   [20:51:02.436] [info][plugins-service] Plugin "osquery" is disabled.
  log   [20:51:02.686] [warning][config][deprecation] Config key [monitoring.cluster_alerts.email_notifications.email_address] will be required for email notifications to work in 8.0."
  log   [20:51:02.765] [fatal][root] [Error: EACCES: permission denied, stat '*/.i18nrc.json'] {
  errno: -13,
  code: 'EACCES',
  syscall: 'stat',
  path: '*/.i18nrc.json'
}

 FATAL  Error: EACCES: permission denied, stat '*/.i18nrc.json'

解决方法:

# 使用 root 用户启动
/opt/kibana/bin/kibana --allow-root

ES 集成 IK 分词器

在线安装

  1. 执行命令,下载并安装 IK 分词器

    /usr/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.0/elasticsearch-analysis-ik-7.12.0.zip
    
  2. 重启 ES 和 Kibana

离线安装

  1. 下载并安装 IK 分词器

    cd /opt/elasticsearch/plugins
    mkdir analysis-ik
    cd analysis-ik
    unzip elasticsearch-analysis-ik-7.12.0.zip
    rm -f elasticsearch-analysis-ik-7.12.0.zip
    
  2. 重启 ES 和 Kibana

  3. 测试,在 Kibana 的 web 端进行测试

    // ik_max_word (常用):会将文本做最细粒度的拆分
    POST _analyze
    {
      "analyzer": "ik_max_word",
      "text": "南京市长江大桥"
    }
    
    // ik_smart:会做最粗粒度的拆分
    POST _analyze
    {
      "analyzer": "ik_smart",
      "text": "南京市长江大桥"
    }
    

扩展、停用词典使用

  1. 进入到 config/analysis-ik/ (插件命令安装方式) 或 plugins/analysis-ik/config (安装包安装方式) 目录下

  2. 新增自定义词典 lagou_ext_dict.dic ,输入内容:

江大桥
  1. 新增自定义词典 lagou_stop_dict.dic ,输入内容:

    的
    了
    啊
    
  2. 将自定义的扩展词典文件添加到 IKAnalyzer.cfg.xml 配置中

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    <properties>
            <comment>IK Analyzer 扩展配置</comment>
            <!--用户可以在这里配置自己的扩展字典 -->
            <entry key="ext_dict">lagou_ext_dict.dic</entry>
             <!--用户可以在这里配置自己的扩展停止词字典-->
            <entry key="ext_stopwords">lagou_stop_dict.dic</entry>
            <!--用户可以在这里配置远程扩展字典 -->
            <!-- <entry key="remote_ext_dict">words_location</entry> -->
            <!--用户可以在这里配置远程扩展停止词字典-->
            <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
    </properties>
    
  3. 重启 Elasticsearch

集群安装过程

Elasticsearch 集群安装

操作系统 服务器ip 端口号 是否能成为主节点
centos7 192.168.181.133 9200
centos7 192.168.181.133 9201
centos7 192.168.181.133 9202
  1. 调整虚拟机内存到 3g 以上

  2. 安装 Elasticsearch

  3. 修改配置文件 elasticsearch.yml

    cluster.name: my-es #集群名称 ---
    node.name: node-1 # 节点名称
    node.master: true #当前节点是否可以被选举为master节点,是:true、否:false ---
    network.host: 0.0.0.0
    http.port: 9200
    transport.port: 9300 # ---
    #初始化一个新的集群时需要此配置来选举master
    cluster.initial_master_nodes: ["node-1","node-2","node-3"]
    #写入候选主节点的设备地址 ---
    discovery.seed_hosts: ["127.0.0.1:9300", "127.0.0.1:9301","127.0.0.1:9302"]
    #跨域相关
    http.cors.enabled: true
    http.cors.allow-origin: "*"
    

    修改完配置文件之后,一定要把之前的 data 目录下 node 数据删除

    rm -rf data/nodes/
    
  4. 节点复制

    cp elasticsearch/ elasticsearch1 -rf
    cp elasticsearch/ elasticsearch2 -rf
    

    修改部分配置:

    node.name: node-2
    http.port: 9201
    transport.port: 9301
    
    node.name: node-3
    http.port: 9202
    transport.port: 9302
    

    修改所有人:

    chown -R estest elasticsearch1
    chown -R estest elasticsearch2
    
  5. 启动节点

    su estest
    /opt/elasticsearch/bin/elasticsearch
    /opt/elasticsearch1/bin/elasticsearch
    /opt/elasticsearch2/bin/elasticsearch
    
  6. 简单验证,访问 http://192.168.181.133:9200/_cat/health?v

Elasticsearch Head 插件安装

  1. nodejs 安装

    cd /opt/
    wget https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-x64.tar.xz 
    tar xf node-v10.15.3-linux-x64.tar.xz
    mv node-v10.15.3-linux-x64 nodejs
    /opt/nodejs/bin/node -v
    # v10.15.3
    
    # 设置软链接
    ln -s /opt/nodejs/bin/npm /usr/local/bin/
    ln -s /opt/nodejs/bin/node /usr/local/bin/
    
  2. phantomjs 安装配置

    cd /opt/
    wget https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-linux-x86_64.tar.bz2
    yum install -y bzip2
    tar -jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
    mv phantomjs-2.1.1-linux-x86_64 phantomjs
    

    修改 /etc/profile

    vim /etc/profile
    
    # 添加内容
    export PATH=$PATH:/opt/phantomjs/bin
    #注意环境变量$Path移动在最前面
    
    source /etc/profile
    
  3. elasticsearch-head 安装

    npm install -g grunt-cli
    npm install grunt
    npm install grunt-contrib-clean
    npm install grunt-contrib-concat
    npm install grunt-contrib-watch
    npm install grunt-contrib-connect
    
    yum -y install git
    
    cd /opt/
    git clone git://github.com/mobz/elasticsearch-head.git
    cd /opt/elasticsearch-head
    npm install -g cnpm --registry=https://registry.npm.taobao.org
    
    # 安装失败或者启动失败可以在 elasticsearch-head 目录下重新执行安装
    npm install -g grunt-cli
    npm install grunt
    npm install grunt-contrib-clean
    npm install grunt-contrib-concat
    npm install grunt-contrib-watch
    npm install grunt-contrib-connect
    
  4. 启动 elasticsearch-head

    cd /opt/elasticsearch-head
    npm run start
    
  5. 测试,访问 http://192.168.181.133:9100 ,连接 http://192.168.181.133:9200/

  6. 在 Kibana 上创建索引库,然后在 head web 前端查看

    PUT /lagou-employee-index
    {
      "settings": {},
      "mappings": {
        "properties": {
          "name": {
            "type": "text"
          }
        }
      }
    }
    
    
    PUT /lagou-company-index
    {
      "settings": {
        "number_of_shards": "2",
        "number_of_replicas": "2"
      },
      "mappings": {
        "properties": {
          "name": {
            "type": "text"
          }
        }
      }
    }
    
原文地址:https://www.cnblogs.com/huangwenjie/p/14615706.html