Elasticsearch 对于分片的处理

July 24th, 2020 by JasonLe's Tech Leave a reply »

elk在我们的生产环境中有广泛的应用,最近发现elk查询速度缓慢,最后查阅了大量资料,发现分片数量对查询性能有巨大影响,但是由于是线上系统,所以对于数据的处理比较谨慎。

1. 创建共享目录
在三个节点上创建共享目录/mnt/backup/,使三个节点的es用户都可以读写该目录。(推荐使用nfs创建共享目录)

2. 修改ES配置
在elasticsearch.yml中加入一行: path.repo: [“/home/backup/”],修改后重启es集群。

3. 创建备份仓库

PUT /_snapshot/fs_backup
{
  "type": "fs",
  "settings": {
    "location": "/home/app/elk_data",
    "compress": true
  }
}

PUT /_snapshot/fs_backup/snapshot_indicator_value?wait_for_completion=true
{
  "indices": "xxxxxx",
  "ignore_unavailable": true,
  "include_global_state": true
}

由于这个是一个耗时的操作,所以我们可以通过 GET _cat/tasks?detailed 查看备份状态。备份完毕以后,我们使用reindex进行分片,yyyy必须是事先创建好

{
...
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1
  }
...
}

 

POST _reindex?slices=auto&refresh
{
  "source": {
    "index": "xxxx"
  },
  "dest": {
    "index" : "yyyy"
  }
}

这个reindex也是一个耗时操作,需要等待执行完毕,执行完毕以后,使用alias别名,访问xxxx即可,通过迁移,发现api耗时降低一半,用户体验明显变好了。

DELETE xxxx
GET /_aliases
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "yyyy",
        "alias": "xxxx"
      }
    }
  ]
}

参考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/snapshots-register-repository.html