文章

二进制部署ES集群

1. 准备文件

1
2
3
4
5
6
7
8
9
# 1. 下载、解压文件
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
mv elasticsearch-7.6.2 /usr/local/elasticsearch-7.6.2
注: 在创建ES启动专属用户之后授权此文件夹

# 2.配置jdk环境
Jdk: elasticsearch-7.6.2-linux-x86_64.tar.gz 包中自带的有JDK环境不需要下载
注意: es7.x之上jdk建议11版本, 当前我配置的jdk1.8.0也可以正常使用

2. 系统环境配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1. vim /etc/sysctl.conf
vm.max_map_count = 655360

2. vim /etc/security/limits.conf
* soft nofile 655360
* hard nofile 131072
* soft nproc 655350
* hard nproc 655350
* soft memlock unlimited
* hard memlock unlimited
注意此时memlock 为无限制,在 elasticsearch.yml 文件中设置 bootstrap.memory_lock: false

3. 配置java环境: JAVA_HOME为jdk所在路径
vim /root/.bash_profile
export JAVA_HOME=/usr/local/elasticsearch-7.6.2/jdk
export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin

4. 创建单独属于es的用户: 启动es不能使用root用户
useradd elastic
passwd elastic

5. 创建es存储数据和日志的文件夹并赋予权限: elasticsearch.yml 中会指定
mkdir -p /data/es/{data,logs}
chown -R elastic.elastic /data/es/

chown -R elastic.elastic /usr/local/elasticsearch-7.6.2

3. ES配置文件: elasticsearch.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Node-01:
cluster.name: ESCluster
node.name: Node-01
node.master: true
node.data: true
path.data: /data/es/data
path.logs: /data/es/logs
#http.host: 0.0.0.0
network.host: 172.17.3.17
http.port: 9200
transport.tcp.port: 9300
discovery.seed_hosts: ["172.17.3.17:9300","172.17.3.18:9300"]
#cluster.initial_master_nodes: ["172.17.3.17","172.17.3.18","172.17.3.19"]
cluster.initial_master_nodes: ["Node-01"]

# 已经弃用,当前为保持兼容还在,未来会直接删除
# 从7.0开始,es废弃了原来的zen discovery配置项,引入了新的"cluster.initial_master_nodes"配置项
#discovery.zen.ping.unicast.hosts: ["172.17.3.17:9300", "172.17.3.18:9300","172.17.3.19:9300"]
#discovery.zen.minimum_master_nodes: 3


action.destructive_requires_name: true
#action.auto_create_index: .security,.monitoring*,.watches,.triggered_watches,.watcher-history*

bootstrap.memory_lock: false

action.auto_create_index: true
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type

xpack.security.enabled: false
xpack.monitoring.enabled: true
xpack.graph.enabled: false
xpack.watcher.enabled: false
xpack.ml.enabled: false



Node-02:(与Node-01不同的地方)
node.name: Node-02
network.host: 172.17.3.18

Node-03:(与Node-01不同的地方)
node.name: Node-03
network.host: 172.17.3.19

4. 配置文件jvm.options

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 与原模块一样:需要改动一个地方不然会报错
8-13:-XX:+UseConcMarkSweepGC
# 改为
8-13:-XX:+UseG1GC

# jvm.options
# =================================================================================
## JVM configuration

################################################################
## IMPORTANT: JVM heap size
################################################################
##
## You should always set the min and max JVM heap
## size to the same value. For example, to set
## the heap to 4 GB, set:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms1g
-Xmx1g

################################################################
## Expert settings
################################################################
##
## All settings below this section are considered
## expert settings. Don't tamper with them unless
## you understand what you are doing
##
################################################################

## GC configuration
#8-13:-XX:+UseConcMarkSweepGC
8-13:-XX:+UseG1GC
8-13:-XX:CMSInitiatingOccupancyFraction=75
8-13:-XX:+UseCMSInitiatingOccupancyOnly

## G1GC Configuration
# NOTE: G1 GC is only supported on JDK version 10 or later
# to use G1GC, uncomment the next two lines and update the version on the
# following three lines to your version of the JDK
# 10-13:-XX:-UseConcMarkSweepGC
# 10-13:-XX:-UseCMSInitiatingOccupancyOnly
14-:-XX:+UseG1GC
14-:-XX:G1ReservePercent=25
14-:-XX:InitiatingHeapOccupancyPercent=30

## JVM temporary directory
-Djava.io.tmpdir=${ES_TMPDIR}

## heap dumps

# generate a heap dump when an allocation from the Java heap fails
# heap dumps are created in the working directory of the JVM
-XX:+HeapDumpOnOutOfMemoryError

# specify an alternative path for heap dumps; ensure the directory exists and
# has sufficient space
-XX:HeapDumpPath=data

# specify an alternative path for JVM fatal error logs
-XX:ErrorFile=logs/hs_err_pid%p.log

## JDK 8 GC logging
8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:logs/gc.log
8:-XX:+UseGCLogFileRotation
8:-XX:NumberOfGCLogFiles=32
8:-XX:GCLogFileSize=64m

# JDK 9+ GC logging
9-:-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m

5. 守护进程启动

1
2
3
4
5
6
7
8
9
10
11
12
[program: ESCluster]
directory=/usr/local/elasticsearch-7.6.2
command=/usr/local/elasticsearch-7.6.2/bin/elasticsearch -p /data/es/logs/elasticsearch.pid
startsec=10
autostart=true
startretries = 3
autorestart=true
redirect_stderr=true
user=elastic
stdout_logfile_maxbytes=100MB
stdout_logfile_backups=10
stdout_logfile=/data/es/logs/elasticsearch.log

6. 检测是否启动成功

1
2
3
4
5
6
7
8
9
10
访问集群中的哪个地址都可以,得到的结果都一样
curl 172.17.3.17:9200/_cat/nodes?pretty
curl 172.17.3.18:9200/_cat/nodes?pretty
curl 172.17.3.19:9200/_cat/nodes?pretty

172.17.3.18  6 96 1 0.02 0.02 0.05 dim * Node-02
172.17.3.17 10 97 1 0.00 0.01 0.06 dim - Node-01
172.17.3.19 20 96 1 0.20 0.13 0.20 dim - Node-03

注: 带 * 的是 主节点

7.部署过程中遇到的问题

1.2022-03-03 22:43:18,724 main ERROR RollingFileManager (/usr/share/elasticsearch/logs/ESCluster_deprecation.log) java.io.FileNotFoundException: /usr/share/elasticsearch/logs/ESCluster_deprecation.log (权限不够) java.io.FileNotFoundException: /usr/share/elasticsearch/logs/ESCluster_deprecation.log (权限不够)

1
2
3
4
对这个文件没有操作权限: /usr/share/elasticsearch/logs

1.没有此文件需要创建; 
2.需要授权用户: elastic

2.Exception in thread “main” 2022-03-03 22:30:03,746 main ERROR No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property ‘log4j2.debug’ to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2

SettingsException[Failed to load settings from [elasticsearch.yml]]; nested: JsonParseException[Duplicate field ‘path.logs’

1
elasticsearch.yml 配置文件里面格式出错,我开始时还一直以为Log4j某个模块出问题了,耗费了好长时间

3.warning: future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/jdk1.8.0_131/jre] does not meet this requirement

1
这个是一个警告,ES7.x之上建议使用jdk11,但是一般jdk1.8也可以正常使用, 可以使用elastic下载文件里面的jdk环境,官方推荐的

4. max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

1
最大打开进程数不够,需要修改系统限制: 如上《系统环境配置》/etc/security/limits.conf,改为此配置即可

5. max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

1
elasticsearch用户拥有的内存权限太小,至少需要262144;如上《系统环境配置》/etc/sysctl.conf,改为此配置即可

6. the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured

1
在7.x之上新版本的es中这三项至少配置一项, 如上 elasticsearch.yml 配置文件,配置文件中特别指出,一部分老的配置项暂时会有兼容,未来会不使用
本文由作者按照 CC BY 4.0 进行授权