Build an elasticsearch cluster under Linux (CentOS7), super detailed

The elasticsearch database introduced by the recent work project, I was assigned to build an elasticsearch environment, this article will introduce how to build a nacos cluster

1. Environment preparation

Linux system : CentOS7-2009

三台主机:
192.168.64.70
192.168.64.71
192.168.64.72

elasticsearch : elasticsearch-7.6.2
JDK : JDK1.8 (ES needs to rely on JDK1.8 or above to run, please confirm that the machine has JDK1.8 or above installed before installing ES)

Elasticsearch download address: https://www.elastic.co/cn/downloads/past-releases#elasticsearch
insert image description here
After downloading, upload it to each Linux server. The author put it in the path of /usr/local/

2. Elasticsearch cluster construction

After uploading the elasticsearch compressed package, you can start building, perform the following operations

# 进入nacos上传目录
cd /usr/local/	
# 解压安装包
tar -zxvf elasticsearch-7.6.2-linux-x86_64.tar.gz
# 解压好后进入nacos配置目录
cd elasticsearch-7.6.2
# 创建data目录(data目录后来用来设置保存数据路径,如果要保存在其他路径,
# 需要创建该目录保证该目录存在,不然会启动报错)
mkdir data
# 创建目录好后进入config
cd config
# 编辑jvm.options设置合理参数,参数如下图
vim jvm.options

insert image description here
Modify the elasticsearch.yml configuration file after modification

# 编辑
vim elasticsearch.yml

Modify the following content, each machine must be edited

# 集群名称,同一集群下每台服务器的名称要一致否则会报错
cluster.name: my-elasticsearch
# 节点名称,集群下每台机的节点名称唯一(举例的机器节点名称设为node-1,其他分别为node-2,node-3)
node.name: node-1
# ES数据储存路径(保证该目录存在不存在则报错)
path.data: /usr/local/elasticsearch-7.6.2/data
# ES运行日志文件路径(保证该目录存在不存在则报错)
path.logs: /usr/local/elasticsearch-7.6.2/logs
# 外界访问ES ip地址(设置0.0.0.0表示可以通过该机器的任何ip访问)
network.host: 0.0.0.0
# ES的访问端口号(不设置则默认为9200)
http.port: 9200
# 集群下所有机器的访问ES的url集合
discovery.seed_hosts: ["192.168.64.70:9300", "192.168.64.71:9300", "192.168.64.72:9300"]
# 集群下所有节点集合
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]

insert image description here
At this point, the configuration of ES is completed, but it cannot be started yet, and some parameters of the system need to be modified.

# 编辑
vim /etc/security/limits.conf
# 添加以下内容
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

# 编辑
vim /etc/sysctl.conf
# 添加以下内容
vm.max_map_count = 262145
# 添加好后保存执行以下命令使配置生效
vim /etc/sysctl.conf

insert image description hereinsert image description here
Since ES cannot be started as root, we need to switch to another user, no other user can create one, execute the following command to create a user and start

# 添加群组(这里命令admin)
groupadd admin
# 添加用户到指定群组(useradd -g 群组名 用户名 -p 密码)
useradd -g admin admin -p admin
# 授予admin用户ES目录下所有编辑权限
chown -R admin:admin /usr/local/elasticsearch-7.6.2
# 切换到admin用户
su admin
# 切换成功后进入ES目录下的bin目录
cd /usr/local/elasticsearch-7.6.2/bin
# 启动ES(-d表示后台启动)
./elasticsearch -d

After startup, the following situations may appear. This information is recommended to use java11, which does not affect the operation of ES. You can ignore it.
insert image description here
After startup, you can access ES through ip:9200. If the following page appears, it means the startup is successful.
insert image description here

3. Elasticsearch is set to start automatically at boot

After elasticsearch is installed, we need to start it manually. If you need to set it to start automatically after booting, do the following:

# 进入init.d目录
cd /etc/init.d
# 编辑elasticsearch
vim elasticsearch
# 添加以下内容
----------------------------------------
#!/bin/bash
#chkconfig: 345 63 37
#description: elasticsearch
#processname: elasticsearch-7.6.2

export ES_HOME=/usr/local/elasticsearch-7.6.2     # elasticsearch所在目录

case $1 in
        start)
                su admin<<!        # admin对应的是启动elasticsearch的账号
                cd $ES_HOME
                ./bin/elasticsearch -d -p pid
                exit
!
                echo "elasticsearch is started"
                ;;
        stop)
                pid=`cat $ES_HOME/pid`
                kill -9 $pid
                echo "elasticsearch is stopped"
                ;;
        restart)
                pid=`cat $ES_HOME/pid`
                kill -9 $pid
                echo "elasticsearch is stopped"
                sleep 1
                su admin<<!    # 启动elasticsearch的账户/用户
                cd $ES_HOME
                ./bin/elasticsearch -d -p pid
                exit
!
                echo "elasticsearch is started"
        ;;
    *)
        echo "start|stop|restart"
        ;;
esac
exit 0

----------------------------------------------

insert image description here

After editing, execute the following command:

# 刚编辑的文件需要赋予权限
chmod -X elasticsearch 
# 添加到系统服务
chkconfig --add elasticsearch

At this time, ES has been added to the system service, which can be opened, closed, and restarted by the following commands.
Note: After the above operations are completed, the ES service must be closed when using the following commands, otherwise the command operation may fail.

# 开启服务
service elasticsearch start
# 停止服务
service elasticsearch stop
# 重启服务
service elasticsearch restart

Set up to start

chkconfig elasticsearch on

Guess you like

Origin blog.csdn.net/weixin_44947701/article/details/122709952