zl程序教程

您现在的位置是:首页 >  系统

当前栏目

Linux(centos7)如何部署ElasticSearch7.6.2单节点跟集群(es部署指南)

2023-09-11 14:16:27 时间

原创Linux(centos7)部署ElasticSearch7.6.2集群跟单节点

目录

这篇文章将涉及到如何在centos7的linux物理机上面部署单点es跟多节点的es集群,在文章的末尾会举出一些集群部署失败的原因以及解决方案。

一、单节点es搭建部署

1、创建你想要下载安装es的目录

mkdir /usr/demo


cd /usr/demo

2、下载es压缩包
依次执行以下命令进行下载es7.6.2的压缩包(如果没有wget命令,需要额外先运行yum install -y wget命令):

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-linux-x86_64.tar.gz


tar zxvf elasticsearch-7.6.2-linux-x86_64.tar.gz

解压出来的目录名称是elasticsearch-7.6.2,我们进入这个目录下的config目录

cd /usr/demo/elasticsearch-7.6.2/config

3、配置es
我们把它自带的elasticsearch.yml删掉,因为里面有很多注释,影响我们配置

rm -rf elasticsearch.yml

然后我们用命令创建并编辑新的elasticsearch.yml

vi elasticsearch.yml

把以下配置项赋值给elasticsearch.yml:

cluster.name: “es1c”
node.name: “node-1”
network.host: 0.0.0.0
http.cors.allow-origin: “*”
discovery.type: single-node

4、调整linux的一些系统配置
修改/etc/security/limits.conf文件 增加配置

vi /etc/security/limits.conf 

在文件最后,增加如下配置(注意*号不要丢):

* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096

接着修改/etc/sysctl.conf文件

vi /etc/sysctl.conf

接着命令

sysctl -p

5、创建非root用户
由于es运行不能在root用户下,所以需要创建非root用户,我们创建名称为es的用户,设置密码为123456,依次运行以下命令:

useradd es -p 123456

把/usr/demo/elasticsearch-7.6.2目录下的所有文件的权限赋值给es这个用户

chown -R es /usr/demo/elasticsearch-7.6.2

6、运行测试
切换到es名称的用户

su es

进入运行命令目录

cd /usr/demo/elasticsearch-7.6.2/bin

运行

bash elasticsearch

测试,在浏览器输入 对应的Ip:9200,进行访问。我这边是在另一台虚拟机上发curl命令测试

[root@vagrant1 /]# curl http://192.168.56.25:9200
{
  "name" : "node-1",
  "cluster_name" : "es1c",
  "cluster_uuid" : "ufVbkcyuRcqWR9ZxUlUfEA",
  "version" : {
    "number" : "7.6.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
    "build_date" : "2020-03-26T06:34:37.794943Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

二、3个节点es集群搭建部署

3节点的每一台机子上面的es的部署基本跟单节点是一样的,主要区别在于elasticsearch.yml文件有很大的区别,跟单节点一样的东西这里就不赘述了,这边主要给出三台机子的elasticsearch.yml的配置:
第一台机子的elasticsearch.yml如下:

cluster.name: es3cluster
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["192.168.56.22","192.168.56.23", "192.168.56.24"]
cluster.initial_master_nodes: ["node-1",node-2","node-3"]
network.publish_host: 192.168.56.22
http.cors.enabled: true
http.cors.allow-origin: "*"

第二台机子的elasticsearch.yml如下:

cluster.name: es3cluster
node.name: node-2
network.host: 0.0.0.0
http.port: 9200
node.master: true
discovery.seed_hosts: ["192.168.56.22","192.168.56.23", "192.168.56.24"]
cluster.initial_master_nodes: ["node-1",node-2","node-3"]
network.publish_host: 192.168.56.23
http.cors.enabled: true
http.cors.allow-origin: "*"

第三台机子的elasticsearch.yml如下:

cluster.name: es3cluster
node.name: node-3
network.host: 0.0.0.0
http.port: 9200
node.master: true
discovery.seed_hosts: ["192.168.56.22","192.168.56.23", "192.168.56.24"]
cluster.initial_master_nodes: ["node-1",node-2","node-3"]
network.publish_host: 192.168.56.24
http.cors.enabled: true
http.cors.allow-origin: "*"

这三个文件主要的区别在于node.name不能一样,以及network.publish_host要根据自己当前机子的ip,其他的配置都是一样的

三、部署失败的4个原因

1、 防火墙没有关闭,也就是http访问端口9200、集群节点之间的数据通讯tcp端口9300、组播udp端口54328三个端口被防火墙关了。
解决方案:
查看防火墙状态:

systemctl status firewalld

查看指定端口开放情况
查看端口是否打开

 firewall-cmd --zone=public --query-port=9200/tcp

如果是yes就是已经开放
开启指定端口

 firewall-cmd --zone=public --add-port=9200/tcp --permanent
 firewall-cmd --zone=public --add-port=9300/tcp --permanent
 firewall-cmd --zone=public --add-port=54328/udp --permanent

关闭防火墙

 systemctl stop firewalld

永久关闭防火墙

 systemclt disable firewalld

在es运行的时候,查看是否已经开放端口

 netstat -tunlp | grep 9200

如果有显示某个java进程在监听9200,说明有开放
2. 集群的名称cluster.name配置的不一样
3. 有的会出现没有配置network.publish_host,无法正常组成集群的现象,所以加上这个network.publish_host的配置项就可以了,注意这个是跟当前机子的IP一致
4. 没有配置cluster.initial_master_nodes

推荐文章:其他博主的es文章