CentOS 安装 Elasticsearch 集群

来自Linux78|wiki

准备安装 修改系统 hosts vi /etc/hosts 192.168.11.1 sky-00 192.168.11.2 sky-01 192.168.11.3 sky-02 192.168.11.4 sky-03 192.168.11.5 sky-04 192.168.11.6 sky-05 192.168.11.7 sky-06 角色分配 主机名 角色 内存分配 sky-00 Master 4G sky-01 Master 8G sky-02 Master+Data 12G sky-03 Data 12G sky-04 Data 12G sky-05 Data 12G sky-06 Data 12G 创建 ES 用户 adduser elastic passwd elastic 创建 ES 数据和日志目录 cd /data/ mkdir elastic cd elastic mkdir data # 创建数据目录 mkdir log # 创建日志目录 chown -R elastic /data/elastic/ # 修改拥有着 调整文件句柄数以及可用进程数 Elasticsearch 要求其可用的文件句柄至少为 65536,同时要求其进程数限制至少为 2048,可用按照下面的指令进行修改。

分别对应以下两个报错信息:

max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536] max number of threads [1024] for user [es] is too low, increase to at least [2048] vi /etc/security/limits.conf

  • soft nofile 100001
  • hard nofile 100002
  • soft nproc 4096
  • hard nproc 8192

elastic soft memlock unlimited elastic hard memlock unlimited 设置内核交换 为了避免不必要的磁盘和内存交换,影响效率,需要将 vm.swappiness 修改为 1(进行最少量的交换,而不禁用交换)或者 10(当系统存在足够内存时,推荐设置为该值以提高性能),其默认值为 60。

此外需要修改最大虚拟内存 vm.max_map_count 防止启动时报错:max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]。

vi /etc/sysctl.conf vm.swappiness = 1 vm.max_map_count = 262144 下载安装文件 mkdir /opt/downloads/ mkdir /opt/soft/ cd /opt/downloads/ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.1.tar.gz wget https://artifacts.elastic.co/downloads/kibana/kibana-6.5.1-linux-x86_64.tar.gz wget http://download.oracle.com/otn/java/jdk/xxxxxx/jdk-8u191-linux-x64.tar.gz tar -zxvf elasticsearch-6.5.1.tar.gz -C /opt/soft/ tar -zxvf jdk-8u191-linux-x64.tar.gz -C /opt/soft/ tar -zxvf kibana-6.5.1-linux-x86_64.tar.gz -C /opt/soft/ chown -R elastic /opt/soft/elasticsearch-6.5.1/ chown -R elastic /opt/soft/kibana-6.5.1/ 开始安装 配置 Java 环境 su elastic #切换到 elastic 用户 vi ~/.bashrc #只修改 elastic 用户自己的环境变量 export JAVA_HOME=/opt/soft/jdk1.8.0_191 export JRE_HOME=/opt/soft/jdk1.8.0_191/jre export CLASSPATH=.:/opt/soft/jdk1.8.0_191/lib:/opt/soft/jdk1.8.0_191/jre/lib export PATH=$PATH:/opt/soft/jdk1.8.0_191/bin:/opt/soft/jdk1.8.0_191/jre/bin 配置 ES 内存占用 cd /opt/soft/elasticsearch-6.5.1/config/ vi jvm.options -Xms4g # 请根据自己机器配置调整 -Xmx4g 配置 Elasticsearch 下面的配置已经过多个生产环境验证,具体设置值仅供参考,请务必根据实际情况进行调整。

  1. ---------------------------------- Cluster -----------------------------------
  2. 设置集群名

cluster.name: cluster-name

  1. ------------------------------------ Node ------------------------------------
  2. 设置节点名

node.name: node01

  1. 设置角色

node.master: true node.data: false node.ingest: true

  1. 设置机架信息
  2. node.attr.rack: r1
  3. ----------------------------------- Paths ------------------------------------
  4. 设置数据路径

path.data: /data/elastic/data

  1. 设置日志路径

path.logs: /data/elastic/log

  1. ----------------------------------- Memory -----------------------------------
  2. 设置内存锁定

bootstrap.memory_lock: true bootstrap.system_call_filter: false

  1. ---------------------------------- Network -----------------------------------
  2. 设置ip和端口

network.bind_host: sky-00 network.publish_host: 0.0.0.0 http.port: 9200

  1. 设置跨域访问

http.cors.enabled: true http.cors.allow-origin: "*" http.max_content_length: 500mb

  1. --------------------------------- Discovery ----------------------------------
  2. 设置zen发现范围(只需要填写主节点的 ip 即可)

discovery.zen.ping.unicast.hosts: ["sky-00", "sky-01", "sky-02"] discovery.zen.no_master_block: write discovery.zen.fd.ping_timeout: 10s

  1. 设置最小主节点个数,一般为:(master_node_count+1)/2

discovery.zen.minimum_master_nodes: 2

  1. ---------------------------------- Gateway -----------------------------------
  2. 设置在有4个节点后进行数据恢复

gateway.recover_after_nodes: 4 gateway.expected_nodes: 7 gateway.recover_after_time: 1m

  1. ---------------------------------- Various -----------------------------------
  2. 禁止通配符模式删除索引

action.destructive_requires_name: true indices.recovery.max_bytes_per_sec: 200mb indices.memory.index_buffer_size: 20%

  1. 默认开启全部类型脚本,可以通过下面配置进行限制
  2. script.allowed_types: inline
  3. script.allowed_contexts: search, update
  4. 关闭xpack的安全校验

xpack.security.enabled: false

  1. 开启 monitoring

xpack.monitoring.enabled: true xpack.monitoring.collection.enabled: true

  1. 设置 monitoring 写入信息

xpack.monitoring.exporters:

 sky:
   type: http
   host: ["sky-02", "sky-03", "sky-04", "sky-05", "sky-06"]
   # 设置 monitoring 索引格式,默认是 YYYY-MM-DD(按天新建)
   index.name.time_format: YYYY-MM
   headers:
     # 设置 Basic 认证信息(详见插件安装部分说明)
     Authorization: "Basic XXXXXXXXXXXXXXX"

安装插件 安装插件 推荐安装的插件有:

IK 中文分词插件 Readonlyrest 安全认证插件 elasticsearch-head 集群监控管理插件(chrome 插件)

命令安装。

$ES_HOME/bin/elasticsearch-plugin -install file:///data/downloads/elasticsearch-analysis-ik-6.5.1.zip $ES_HOME/bin/elasticsearch-plugin -install file:///data/downloads/readonlyrest-1.16.29_es6.5.1.zip 配置 Readonlyrest 安全认证 下面只简单介绍 Readonlyrest 的 Basic 认证,更高级的用法可以去官方网站查看,在 ES 安装目录的 conf 目录下新建文件 readonlyrest.yml,并添加下面内容。

readonlyrest:

   access_control_rules:
   - name: "Require HTTP Basic Auth"
     type: allow
     auth_key: 用户名:密码

启动 ES 全部安装完成后,即可使用 elastic 用户启动 ES。

  1. 默认 ES 不支持 root 用户启动

su elastic cd /opt/soft/elasticsearch-6.5.1/bin ./elasticsearch -d

  1. 在 Kibana 里面监控

在安装 ES 的时候,我们配置了 ES 的监控信息,这样我们就可以在 Kibana 中查看 ES 索引信息、node 信息等。

    1. 配置 Kibana

进入 Kibana 的解压目录下的 conf 文件夹,打开配置文件 kibana.yml。

  1. 配置 kibana ui 的端口

server.port: 5601

  1. 配置 kibana 访问 ip

server.host: "0.0.0.0"

  1. 设置 ES 地址

elasticsearch.url: "http://sky-00:9200"

  1. dashboards. Kibana creates a new index if the index doesn't already exist.
  2. kibana.index: ".kibana"
  3. 打开 kibana 时默认页面
  4. kibana.defaultAppId: "home"
  5. ES Basic 认证信息

elasticsearch.username: "用户名" elasticsearch.password: "密码"

  1. 设置时区信息
  2. i18n.locale: "en"
  3. 开启监控

xpack.monitoring.enabled: true

  1. 关闭 kibana 监控,默认为 true

xpack.monitoring.kibana.collection.enabled: false 对 Kibana 配置文件的说明 ES Basic 认证信息配置(在启动时对 Kibana 索引进行维护)完成后,登陆 kibana 时,依旧需要输入认证信息; 由于 kibana 的 monitoring 无法设置新建的索引的索引名(无法配置 index.name.time_format),这样 kibana 每天会新建一个索引,由于 kibana 只是作为管理查看工具,因此关闭了 kibana 监控; elasticsearch.url 该配置项无法设置多个 es 地址;如果你想实现类似负载均衡的功能,最简单的方法就是在 Kibana 机器上运行一个协调(Coordinating)节点。

监控界面 全部配置完成后,启动 kibana,打开 monitoring 即可开始监控 node、index 等。

设置索引模板 https://juejin.im/post/5bed17daf265da614e2ba73e