Linux上搭建ElasticSearch和集群部署

  • 1. ElasticSearch单节点部署
  • 1.1 安装步骤
  • 1.2. 可能出现的错误以及对应的解决方法
  • could not find java in bundled JDK at xxxxxx
  • ERROR: [1] bootstrap checks failed [1]: max number of threads [3766] for user [testuser] is too low, increase to at least [4096]
  • 2. ElasticSearch集群部署


1. ElasticSearch单节点部署

1.1 安装步骤

  • 解压ES到指定的目录下

tar -zxvf elasticsearch-7.8.1-linux-x86_64.tar.gz -C 指定的目录

修改解压elasticsearch文件名 mv elasticsearch-7.8.1-linux-x86_64 es7

  • 创建用户:ES不允许root用户直接运行,需要创建新用户

新增用户 useradd xxx

设置密码 passwd xxxx

删除用户再新增 userdel -r xxxx

将解压之后的文件夹所有者权限赋给新建的用户 chown -R username:username 解压后的目录/es7

  • 修改配置文件:config/elasticsearch.yml
cluster.name: es-cluster
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["node-1"]

修改系统配置文件/etc/security/limits.conf

# 设置每个进程可以打开的文件数的限制
es - nproc 4096        # 设置线程数为4096 
es soft nofile 65536
es hard nofile 65536

修改/etc/security/limits.d/20-nproc.conf

# 设置每个进程可以打开的文件数的限制
es soft nofile 65536
es hard nofile 65536

修改/etc/sysctl.conf

一个进程可以拥有的VMA数量设置为655360(默认为65536)
vm.max_map_count=655360

使用sysctl -p重新加载

  • 测试启动
    ES不允许使用root用户直接运行,切换到新用户再次执行
    使用Postman发送GET请求查看是否成功启动ElasticSearch

1.2. 可能出现的错误以及对应的解决方法

could not find java in bundled JDK at xxxxxx

  1. 注意安装好的JDk已经配置好了环境变量,可以检查/etc/profile文件
  2. linux es 创建数据 linux部署es_linux es 创建数据

  3. 一定不要把JDK安装到某一个用户下,可以是在任何root用户文件夹下(我之前安装在home/huzhen目录下…)
  4. linux es 创建数据 linux部署es_postman_02

  5. 在切换到新用户启动ES之前,一定不要忘记将es7的权限赋给创建的新用户
    chown -R username:username 解压后的目录/es7

ERROR: [1] bootstrap checks failed [1]: max number of threads [3766] for user [testuser] is too low, increase to at least [4096]

  1. 注意虚拟机的内存大小一定要大于1.5G
  2. 设置当前启动ElasticSearch的用户,能开启的线程数一定要≥4096

2. ElasticSearch集群部署

  • 将软件分发到其他虚拟机上:xsync es解压文件/
  • 同样在每个虚拟机上需要创建新用户进行访问(与单节点配置相同)
  • 修改配置文件/config/elasticsearch.yml:每个虚拟机上的配置,需要修改为对应的network.host和node.name
# ES集群配置
cluster.name: cluster-es
node.name: node-1003
node.master: true
node.data: true
network.host: xxxx
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
http.max_content_length: 200mb
# es7.x后新增配置 初始化一个新的集群时需要选举master
cluster.initial_master_nodes: ["node-1001"]
# 节点发现配置
discovery.seed_hosts: ["linux1:9300", "linux2:9300", "linux3:9300"]
gateway.recover_after_nodes: 2
network.tcp.keep_alive: true
network.tcp.no_delay: true
transport.tcp.compress: true
# 集群内同时启动的数据任务数默认为2个
cluster.routing.allocation.cluster_concurrent_rebalance: 16
# 添加或者删除节点并发恢复的线程个数 默认4个
cluster.routing.allocation.node_concurrent_recoveries: 16
# 初始化数据恢复 并发恢复线程的个数 默认为4个
cluster.routing.allocation.node_initial_primaries_recoveries: 16

修改每个虚拟机上的配置文件/etc/security/limits.conf和/etc/security/limits.d/20-nproc.conf(与单节点部署相同配置即可)

  • 依次开启三个ES服务器
  • linux es 创建数据 linux部署es_java_03

    linux es 创建数据 linux部署es_linux_04

    linux es 创建数据 linux部署es_linux_05

  • 使用Postman发送GET请求测试是否启动成功
  • linux es 创建数据 linux部署es_java_06

  • 如果启动报错java.nio.file.AccessDeniedException:xxxxx

那就切换到root用户重新将es文件权限赋给所创建的新用户

chown -R user:user es文件目录