ElasticSearch正确的重启方式
[toc]
# 你一直都用错了:Elasticsearch 安全完整的重启步骤指南
本文详细介绍了Elasticsearch集群的安全重启步骤,涵盖使用命令行curl和Kibana两种操作方式。从关闭集群自动均衡和写入权限开始,到通过SSH重启服务,最后恢复集群的均衡和写入功能,确保重启过程中数据的完整性和集群的稳定性。这些步骤对于维护Elasticsearch集群的高效运行至关重要。
- 命令行curl方式
- 关闭集群自动均衡和禁止集群写入
- 重启ES集群
- 打开集群自动均衡和开启集群写入
- Kibana执行
- 关闭集群自动均衡和禁止集群写入
- 重启ES集群
- 打开集群自动均衡和开启集群写入
# 命令行curl方式
# 1. 关闭集群自动均衡和禁止集群写入
首先,需要关闭集群的自动均衡功能,并禁止写入,以防止重启过程中数据不一致。
# 关闭集群自动均衡
curl -XPUT "http://集群任意IP:9200/_cluster/settings?pretty" -H 'Content-Type:application/json' -d '
{
"persistent": {
"cluster.routing.rebalance.enable": "none"
},
"transient": {
"cluster.routing.rebalance.enable": "none"
}
}'
# 检查集群自动均衡是否关闭
curl -XGET "http://集群任意IP:9200/_cluster/settings?pretty"
# 禁止集群写入
curl -XPUT "http://集群任意IP:9200/_cluster/settings?pretty" -H 'Content-Type:application/json' -d '
{
"persistent": {
"cluster.blocks.read_only": true
},
"transient": {
"cluster.blocks.read_only": true
}
}'
# 检查集群写入是否已经关闭
curl -XGET "http://集群任意IP:9200/_cluster/settings?pretty"
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
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
# 2. 重启ES集群
接下来,通过SSH命令重启Elasticsearch服务。
# 停止ES服务
ssh ip -C 'ps -ef|grep org.elasticsearch.bootstrap.Elasticsearch|grep -v grep|awk '\''{print $2}'\''|xargs kill -9'
# 启动ES服务
ssh ip -C 'su - es -c "cd /home/es/software/elasticsearch/bin; sh elasticsearch -d"'
1
2
3
4
5
2
3
4
5
# 3. 打开集群自动均衡和开启集群写入
在重启后,恢复集群的自动均衡功能和写入权限。
# 打开集群写入
curl -XPUT "http://集群任意IP:9200/_cluster/settings?pretty" -H 'Content-Type:application/json' -d '{"persistent": {"cluster.blocks.read_only": false}}'
curl -XPUT "http://集群任意IP:9200/_cluster/settings?pretty" -H 'Content-Type:application/json' -d '{"transient": {"cluster.blocks.read_only": false}}'
# 检查集群写入是否已经打开
curl -XGET "http://集群任意IP:9200/_cluster/settings?pretty"
# 打开集群自动均衡
curl -XPUT "http://集群任意IP:9200/_cluster/settings?pretty" -H 'Content-Type:application/json' -d '{"persistent": {"cluster.routing.rebalance.enable": "all"}}'
curl -XPUT "http://集群任意IP:9200/_cluster/settings?pretty" -H 'Content-Type:application/json' -d '{"transient": {"cluster.routing.rebalance.enable": "all"}}'
# 检查集群自动均衡是否打开
curl -XGET "http://集群任意IP:9200/_cluster/settings?pretty"
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# Kibana执行
# 1. 关闭集群自动均衡和禁止集群写入
使用Kibana的Dev Tools执行以下命令:
# 关闭集群自动均衡
PUT /_cluster/settings
{
"persistent": {
"cluster.routing.rebalance.enable": "none"
},
"transient": {
"cluster.routing.rebalance.enable": "none"
}
}
# 检查集群自动均衡是否关闭
GET /_cluster/settings?pretty
# 禁止集群写入
PUT /_cluster/settings
{
"persistent": {
"cluster.blocks.read_only": true
},
"transient": {
"cluster.blocks.read_only": true
}
}
# 检查集群写入是否已经关闭
GET /_cluster/settings?pretty
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
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
# 2. 重启ES集群
与命令行curl方式相同,使用SSH命令重启Elasticsearch服务。
# 3. 打开集群自动均衡和开启集群写入
在Kibana的Dev Tools中执行以下命令:
# 打开集群写入
PUT /_cluster/settings
{
"persistent": {
"cluster.blocks.read_only": false
}
}
PUT /_cluster/settings
{
"transient": {
"cluster.blocks.read_only": false
}
}
# 检查集群写入是否已经打开
GET /_cat/shards?v&pretty&s=ip:desc
# 打开集群自动均衡
PUT /_cluster/settings
{
"persistent": {
"cluster.routing.rebalance.enable": "all"
}
}
PUT /_cluster/settings
{
"transient": {
"cluster.routing.rebalance.enable": "all"
}
}
# 检查集群自动均衡是否打开
GET /_cluster/settings?pretty
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
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
在完成这些步骤后,您的Elasticsearch集群应该已经安全地重启,并且所有功能都已经恢复。
注意:在进行这些操作时,请确保您已经备份了所有重要数据,以防止不可预见的数据丢失。此外,操作前应当通知所有相关用户,因为重启过程可能会导致短暂的集群不可用。
上次更新: 2024/08/09, 21:00:04