1 查看健康狀態
GET _cat/health?v
epoch timestamp cluster status node.total node.data shards
1531290005 14:20:05 elasticsearch green 1 1 2
pri relo init unassign pending_tasks
2 0 0 0 0
max_task_wait_time active_shards_percent
- 100.0%
status:green、yellow、red
green:每個索引的primary shard和replica shard都是active的
yellow:每個索引的primary shard都是active的,但部分的replica shard不是active的
red:不是所有的索引都是primary shard都是active狀態的。
2 檢查分片信息
查看索引的shard信息。
GET _cat/shards?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open test_index yu5HjAt0RS-qSFtrCj-emQ 5 1 0 0 1.1kb 1.1kb
3 設置磁盤限制
ES默認當磁盤空間不足15%時,會禁止分配replica shard。可以動態調整ES對磁盤空間的要求限制,命令如下:
PUT _cluster/settings
{
"transient": {
"cluster.routing.allocation.disk.watermark.low": "95%",
"cluster.routing.allocation.disk.watermark.high": "5gb"
}
}
注意:配置磁盤空間限制的時候,要求low必須比high大。可以使用百分比或gb的方式設置。且ES要求low至少滿足磁盤95%的容量。
此處配置的百分比都是磁盤的使用百分比,如85%,代表磁盤使用了85%后如何限制。配置的GB絕對值都是剩余空間多少。
low - 對磁盤空閑容量的最低限制。默認85%。
high - 對磁盤空閑容量的最高限制。默認90%。
如:low為50gb。high為10gb。則當磁盤空閑容量不足50gb時停止分配replica shard。當磁盤空閑容量不足10gb時,停止分配shard,並將應該在當前結點中分配的shard分配到其他結點中。
強調:red問題。因為ES中primary shard是主分片,要求必須全部活動才能正常使用。
4 查看索引信息
GET _cat/indices?v
health status index uuid pri rep docs.count
yellow open test_index 2PJFQBtzTwOUhcy-QjfYmQ 5 1 0
docs.deleted store.size pri.store.size
0 460b 460b
5 新增索引
PUT /test_index
在ES中,默認的創建索引的時候,會分配5個primary shard,並為每個primary shard分配一個replica shard。在ES中,默認的限制是:如果磁盤空間不足15%的時候,不分配replica shard。如果磁盤空間不足5%的時候,不再分配任何的primary shard。
創建索引時指定分片。
PUT /test_index
{
"settings":{
"number_of_shards" : 2,
"number_of_replicas" : 1
}
}
6 修改索引
注意:索引一旦創建,primary shard數量不可變化,可以改變replica shard數量。
PUT /test_index/_settings
{
"number_of_replicas" : 2
}
ES中對shard的分布是有要求的。有其內置的特殊算法。ES盡可能保證primary shard平均分布在多個節點上。Replica shard會保證不和他備份的那個primary shard分配在同一個節點上。
7 刪除索引
DELETE /test_index [, other_index]
8 總結:索引不可變
索引不可變的原因是倒排索引不可變。
倒排索引不可變的好處
不需要鎖,提升並發能力,避免鎖問題。數據不變,可以緩存在OS cache中(前提是cache足夠大)。filter cache始終在內存中,因為數據是不可變的。可以通過壓縮技術來節省CPU和IO的開銷。
倒排索引不可變的壞處
倒排索引不可變,導致了ES中的index不可變。只要index的結構發生任何變化,都必須重建索引。