Category Archives: ElaticSearch

ElaticSearch

elasticsearch增加磁盘

基于EasticSearch-6.2.4

检查系统状态

1
2
3
curl -XGET "http://192.168.1.1:9002/_cat/indices"
curl -XGET "http://192.168.1.1:9002/_cat/nodes"
curl -XGET "http://192.168.1.1:9002/_cat/health"

修改配置文件,增加数据目录,多个以逗号分隔

1
2
# vim config/elasticsearch.yml
path.data: /data2/es/data,/data1/es/data

检查系统状态

1
2
3
curl -XGET "http://192.168.1.1:9002/_cat/indices"
curl -XGET "http://192.168.1.1:9002/_cat/nodes"
curl -XGET "http://192.168.1.1:9002/_cat/health"

为避免节点重启造成分片重新自动分配,需要设置集群中分片不自动分配

1
2
3
4
5
6
7
8
9
curl -X PUT \
  http://192.168.1.1:9002/_cluster/settings \
  -H 'Content-Type: application/json; charset=UTF-8' \
  -H 'cache-control: no-cache' \
  -d '{
  "transient" : {
      "cluster.routing.allocation.enable" : "none"
  }
}'

关闭节点

1
2
jps -l
kill xxxx

启动服务

1
./bin/elasticsearch -d

开启分片自动分配

1
2
3
4
5
6
7
8
9
curl -X PUT \
  http://192.168.1.1:9002/_cluster/settings \
  -H 'Content-Type: application/json; charset=UTF-8' \
  -H 'cache-control: no-cache' \
  -d '{
  "transient": {
    "cluster.routing.allocation.enable": "all"
  }
}'

查看分片及索引状态

1
2
3
4
5
# 查看未分配分片数
curl -XGET 'http://192.168.1.1:9002/_cat/shards' | grep UNASSIGNED   #查看未分配的索引分片
curl -XGET "http://192.168.1.1:9002/_cat/shards/lbs_geocoder_4307?v" #查看索引分片
curl -XGET "http://192.168.1.1:9002/_cat/shards?v"
curl -XGET "http://192.168.1.1:9002/_cat/shards?v" |grep UNASSIGNED |wc -l

当没有未分配分片时,检查系统状态

1
2
3
curl -XGET "http://192.168.1.1:9002/_cat/indices"
curl -XGET "http://192.168.1.1:9002/_cat/nodes"
curl -XGET "http://192.168.1.1:9002/_cat/health"

因为负载过高等原因,有时候个别分片可能长期处于 UNASSIGNED 状态时,可以手动进行分配;reroute 接口支持五种指令:allocate_replica, allocate_stale_primary, allocate_empty_primary,move 和 cancel;默认情况下只允许手动分配副本分片(即使用 allocate_replica),所以如果要分配主分片,需要单独加一个 accept_data_loss 选项

分配主分片

1
2
3
4
5
6
7
8
9
10
11
12
13
curl -X PUT \
  http://192.168.1.1:9002/_cluster/reroute \
  -H 'Content-Type: application/json; charset=UTF-8' \
  -H 'cache-control: no-cache' \
  -d '{
  "commands" : [ {
        "allocate_stale_primary" :
            {
              "index" : "index", "shard" : 4, "node" : "node56", "accept_data_loss" : true
            }
        }
  ]
}'

分配副本

1
2
3
4
5
6
7
8
9
10
11
12
13
curl -X PUT \
  http://192.168.1.1:9002/_cluster/reroute \
  -H 'Content-Type: application/json; charset=UTF-8' \
  -H 'cache-control: no-cache' \
  -d '{
  "commands" : [ {
        "allocate_replica" :
            {
              "index" : "index", "shard" : 4, "node" : "node56"
            }
        }
  ]
}'

磁盘空间不足报 [FORBIDDEN/12/index read-only / allow delete (api)] 错误后,进行增加磁盘操作或删除无用旧索引数据,完成操作需要修改索引的状态,ES不会主动更新,命令为:

1
PUT _settings { "index":{ "blocks":{ "read_only_allow_delete":"false" } } }

磁盘扩容后,可能需要修改报警的设置,默认值会在 85% 90% 95%的磁盘空间时报警

1
2
3
4
5
6
7
8
9
10
PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.allocation.disk.threshold_enabled" : true,
    "cluster.routing.allocation.disk.watermark.low": "80gb",
    "cluster.routing.allocation.disk.watermark.high": "40gb",
    "cluster.routing.allocation.disk.watermark.flood_stage":"20gb",
    "cluster.info.update.interval" : "1m"
  }
}