Elasticsearch维护方法汇总

集群健康状态

$ curl -XGET 127.0.0.1:9200/_cluster/health?pretty

green 绿灯,所有分片都正确运行,集群非常健康。
yellow 黄灯,所有主分片都正确运行,但是有副本分片缺失。这种情况意味着 ES 当前还是正常运行的,但是有一定风险。注意,在 Kibana4 的 server 端启动逻辑中,即使是黄灯状态,Kibana 4 也会拒绝启动,死循环等待集群状态变成绿灯后才能继续运行。
red 红灯,有主分片缺失。这部分数据完全不可用。而考虑到 ES 在写入端是简单的取余算法,轮到这个分片上的数据也会持续写入报错。

节点下线

集群中个别节点出现故障预警等情况,需要下线,也是 Elasticsearch 运维工作中常见的情况。如果已经稳定运行过一段时间的集群,每个节点上都会保存有数量不少的分片。这种时候通过 reroute 接口手动转移,就显得太过麻烦了。这个时候,有另一种方式:

$ curl -XPUT 127.0.0.1:9200/_cluster/settings -d '{
  "transient" :{
      "cluster.routing.allocation.exclude._ip" : "10.0.0.1"
   }
}'

Elasticsearch 集群就会自动把这个 IP 上的所有分片,都自动转移到其他节点上。等到转移完成,这个空节点就可以毫无影响的下线了。和 _ip 类似的参数还有 _host, _name 等。此外,这类参数不单是 cluster 级别,也可以是 index 级别。下一小节就是 index 级别的用例。

reroute 接口

reroute 接口支持五种指令:allocate_replica, allocate_stale_primary, allocate_empty_primary,move 和 cancel。
常用的一般是 allocate 和 move:
allocate_* 指令
因为负载过高等原因,有时候个别分片可能长期处于 UNASSIGNED 状态,我们就可以手动分配分片到指定节点上。默认情况下只允许手动分配副本分片(即使用 allocate_replica),所以如果要分配主分片,需要单独加一个 accept_data_loss 选项:

$ curl -XPOST 127.0.0.1:9200/_cluster/reroute -d '{
  "commands" : [ {
        "allocate_stale_primary" :
            {
              "index" : "logstash-2015.05.27", "shard" : 61, "node" : "10.19.0.77", "accept_data_loss" : true
            }
        }
  ]
}'

因为负载过高,磁盘利用率过高,服务器下线,更换磁盘等原因,可以会需要从节点上移走部分分片:

$ curl -XPOST 127.0.0.1:9200/_cluster/reroute -d '{
  "commands" : [ {
        "move" :
            {
              "index" : "logstash-2015.05.22", "shard" : 0, "from_node" : "10.19.0.81", "to_node" : "10.19.0.104"
            }
        }
  ]
}'

集群更新节点配置
刚上线的服务,最近一再的更新基础配置(硬盘)

  1. 关闭分片自动均衡
$ curl -XPUT 127.0.0.1:9200/_cluster/settings
{
    "transient" : {
        "cluster.routing.allocation.enable" : "none"
    }
}
  1. 升级重启该节点,并确认该节点重新加入到了集群中

  2. 其他节点重复第2步,升级重启。

  3. 最后所有节点配置更新完成后,重启集群的shard均衡

$ curl -XPUT 127.0.0.1:9200/_cluster/settings -d'
{
    "transient" : {
        "cluster.routing.allocation.enable" : "all"
    }
}'

设置磁盘空间水位线

$ curl -XPUT 127.0.0.1:9200/_cluster/settings

{

  "transient": {

    "cluster.routing.allocation.disk.watermark.low": "80%",

    "cluster.routing.allocation.disk.watermark.high": "90%",

  }

}

你可能感兴趣的:(Elasticsearch维护方法汇总)