• 如何查看集群的当前状态 curl -sXGET ‘http://localhost:9200/_cluster/health?pretty’ 或者 sh index.sh health

es API查询的时候指定分片 查看es分片数量_离线

cluster_name:集群名称

status: 集群状态,green代表所有打开的索引的全部分片全部是正常状态,yellow表示部分分片的副本不活跃,red表示部分索引的主副分片全不活跃。
number_of_nodes: 在线的节点数。

number_of_data_nodes: 在线的数据节点数

active_primary_shards: 活跃的主分片数量

active_shards: 活跃的分片数量,包括主分片和副本。

relocating_shards: 正在移动的分片数量。

initializing_shards: 正在初始化的分片的数量。

unassigned_shards: 未分配的分片数量

delayed_unassigned_shards: 由于节点离线导致延迟分配的分片数量。

active_shards_percent_as_number: 所有活跃分片/打开的所有索引的分片总数。

  • hot_threads
    curl -sXGET ‘http://localhost:9200/_nodes/hot_threads?pretty’, 该api返回当前集群线程在处理什么任务。在进行问题定位的时候会用到。只要了解即可,不需要去分析输出的内容
  • 更新集群设置api(cluster update settings)
    ES提供了丰富的动态参数,方便修改集群运行时特性,修改分为持久(persistent)和临时(transient),persistent修改会写入集群的全局状态,进程重启仍然起作用。transient修改只是改变当前运行实例,进程重启后失效。
    transient修改方法为:curl -XPUT ‘http://localhost:9200/_cluster/settings?pretty’ -d ‘{“transient”:{“dynamic.parma”:“value”}}’
    persistent修改方法为:curl -XPUT ‘http://localhost:9200/_cluster/settings?pretty’ -d ‘{“persistent”:{“dynamic.parma”:“value”}}’
    常用的修改如下所示:
#修改每个节点并发recovery的主分片数
curl -sXPUT "http://localhost:9200/_cluster/settings" -d '{"transient":{"cluster.routing.allocation.node_initial_primaries_recoveries": "100"}}'
#修改每个节点并发recovery的副本数量,如果机器资源充足,修改较大的值有利于提高集群恢复的进度。recovery主要受限于网络带宽。
curl -sXPUT "http://localhost:9200/_cluster/settings" -d '{"transient":{"cluster.routing.allocation.node_concurrent_recoveries":"40"}}'
#
#禁用启用集群的查询服务。在某些时候,由于大量的查询占用了很多资源,导致集群恢复严重缓慢,禁用查询可以提高恢复速速。警告:禁用后所有查询无法完成,需要在相关人认可的情况下执行该操作。
curl -sXPUT "http://localhost:9200/_cluster/settings" -d '{"transient":{"cluster.search.enabled":"false"}}'
curl -sXPUT "http://localhost:9200/_cluster/settings" -d '{"transient":{"cluster.search.enabled":"true"}}'
#
#禁止分片划分到某个节点或者某个机器,出现磁盘故障或者下线机器时会用到,执行后,分片会从对应的节点和机器移走,根据数据量大小,可能需要一定的时间才能移完
curl -sXPUT "http://localhost:9200/_cluster/settings" -d '{"persistent":{"cluster.routing.allocation.exclude._name":"es.10.208.41.16.7"}}'
curl -sXPUT "http://localhost:9200/_cluster/settings" -d '{"persistent":{"cluster.routing.allocation.exclude._ip":"10.203.159.107"}}'
  • 更新索引设置api(index update setting)
    每个索引可以有不同的设置,控制索引的很多特性,下面列举经常用到的一些可以动态设置的索引参数。该api支持索引名称的模糊匹配,同时对多个索引进行设置。
#将2018年6月13号的全部索引的该属性设置为2,该设置可以解决由于某个索引单个节点上的分片数量超过指定的值,而变成yellow状态的问题。比如初始设置是1,主副共20个分片,此时集群内只有18个节点,则由于某些节点上只能分配超过1才能是索引green,修改该值为2即可。
curl -sXPUT "http://localhost:9200/*2018.06.13*/_settings" -d '{"index.routing.allocation.total_shards_per_node":2}'
 
#修改索引的refresh_interval
curl -sXPUT "http://localhost:9200/*2017.09.19/_settings" -d '{"index.refresh_interval":"120s"}'
 
#将索引的shard里的多个段合并成1个段,段越少,占用的资源越少,有利于提高查询速度
curl -XPOST 'http://localhost:9200/netops-syslog2018.07.19/_forcemerge?max_num_segments=1'
 
#修改由于节点离线,重新分片的时机,下面命令的意思是如果由于节点离线导致分片成为unassigned,120小时以后才会将分片分配到其他节点。常用单位有m--分钟,h--小时,
curl -XPUT "http://localhost:9200/$index/_settings" -d '{"settings":{"index.unassigned.node_left.delayed_timeout":"120h"}}'
  • 索引相关操作api
#关闭索引
curl -XPOST 'http://localhost:9200/$index/_close'
 
#批量关闭索引,比如关闭2018年6月份所有索引
curl -XPOST 'http://localhost:9200/*2018.06*/_close'
 
#打开索引
curl -XPOST 'http://localhost:9200/$index/_open'
 
#批量打开索引, 比如关闭2018年6月全部索引
curl -XPOST 'http://localhost:9200/*2018.06*/_open'
 
#关闭全部yellow索引, 其他状态类似
curl -sXGET 'http://localhost:9200/_cat/indices' | grep yellow | awk '{print $3}' |while read index; do curl -XPOST "http://localhost:9200/$index/_close"; done
 
#删除索引
curl -XDELETE 'http://localhost:9200/$index'
 
#批量删除索引,比如删除2016年的全部索引。 WARNING:一定要看清楚了,确认三遍没问题再回车
curl -XDELETE 'http://localhost:9200/*2016*/'
 
#创建索引,创建dmz-dns-2018.07.12索引, 系统会根据template创建索引
curl -XPUT 'http://localhost:9200/dmz-dns-2018.07.12'