Linux搭建ElasticSearch集群环境

一、软件安装

1、软件下载

官方连接:https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-8-0

# 官网下载地址,可能会慢一点
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.8.0-linux-x86_64.tar.gz
# 华为云的镜像下载地址
wget https://repo.huaweicloud.com/elasticsearch/7.8.0/elasticsearch-7.8.0-linux-x86_64.tar.gz

2、解压

# 解压缩
tar -zxvf elasticsearch-7.8.0-linux-x86_64.tar.gz -C /soft
# 改名
mv elasticsearch-7.8.0 elasticsearch

二、创建用户

由于安全问题,Elasticsearch 不允许 root 用户直接运行,所以要创建新用户,在 root 用户中创建新用户。

# 新增 es 用户
useradd es 
# 为 es 用户设置密码
passwd es 
# 如果错了,可以删除再加
userdel -r es 
# 文件夹所有者
chown -R es:es /soft/elasticsearch 

三、修改配置文件

1、修改/opt/module/es/config/elasticsearch.yml 文件,分发文件

# 加入如下配置
# 集群名称
cluster.name: cluster-es
# 节点名称,每个节点的名称不能重复
node.name: node-1
# ip 地址,每个节点的地址不能重复
# 我试过在单机上部署三个es,前两个能正常集群,但是到第三个的时候显示内存不足,
# 原因是我的linux虚拟机内存只有1G,但是在正常工作中,我们需要配置多机部署,保证高可用性
network.host: linux1
# 是不是有资格主节点
node.master: true
node.data: true
http.port: 9200
# 通信端口
transport.tcp.port: 9300 
# head 插件需要这打开这两个配置
http.cors.allow-origin: "*"
http.cors.enabled: true
http.max_content_length: 200mb
# es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举 master
cluster.initial_master_nodes: ["node-1"]
# es7.x 之后新增的配置,节点发现 注意IP地址,注意端口号9300是节点间通讯地址不是9200
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

2、修改/etc/security/limits.conf

# 在文件末尾中增加下面内容
# 每个进程可以打开的文件数的限制
es soft nofile 65536
es hard nofile 65536

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

# 在文件末尾中增加下面内容
# 每个进程可以打开的文件数的限制
es soft nofile 65536
es hard nofile 65536
# 操作系统级别对每个用户创建的进程数的限制
* hard nproc 4096
# 注:* 带表 Linux 所有用户名称

4、修改/etc/sysctl.conf

# 在文件中增加下面内容
# 一个进程可以拥有的 VMA(虚拟内存区域)的数量,默认值为 65536
vm.max_map_count=655360

5、重新加载

sysctl -p

四、测试集群

分别启动三个es服务:

# 关闭防火墙
systemctl stop firewalld
# 使用es用户
su es
# 进入安装目录
cd /soft/elasticsearch
# 启动服务
bin/elasticsearch

访问http://10.70.20.112:9200/_cat/nodes,显示有三个节点,主节点是node-1,配置成功!

五、踩坑经验

  1. 如果三个es都放到一个虚拟机上,对于虚拟机内存小的可能会有内存不足的情况

  1. 之前我先做过一次集群,然后又做了一次三个虚拟机的集群,但是我直接把先前的es文件夹拷到了另外两台虚拟机,并按照以上步骤做了配置更新,但是我启动三个节点后三个节点没能集群联系起来,在执行http://10.70.20.112:9200/_cluster/allocation/explain查看详细的异常信息的时候显示:

org.elasticsearch.cluster.block.ClusterBlockException: blocked by: [SERVICE_UNAVAILABLE/1/state not recovered / initialized];

解决办法:把每个es的config目录下的elasticsearch.keystore文件移除,然后重启服务;如果不行可以试试再把data下的indices文件夹移除

  1. 执行http://10.70.20.112:9200/_cluster/health 报错MasterNotDiscoveredException: null,这个bug可能是因为在配置文件elasticsearch.conf中:

discovery.seed_hosts: ["linux1:9300","linux2:9300","linux3:9300"]

这一行的9300是各个节点见的通讯端口号,不能写9200!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值