首頁>技術>

Elasticsearch系列

第4篇

Elasticsearch提供了大量了的Rest API用以操作相關功能,提供了極大的便利,掌握這些API是熟練地使用Elasticsearch的前提,這些API有Cluster APIs、Document APIs、Index APIs、Info API、Search Apis、Cat APIs等,本文主要講解Cluster APIs,透過Cluster APIs可以檢視叢集簡況、叢集狀態和統計資訊、叢集節點資訊等。

01

檢視叢集健康狀態(cluster health API)

使用helath API可以檢視叢集的整體健康情況,當有異常時及時告警,例如:

$ curl -X GET "http://127.0.0.1:9200/_cluster/health?pretty"
{  "cluster_name": "elasticsearch",  "status": "yellow",  "timed_out": false,  "number_of_nodes": 1,  "number_of_data_nodes": 1,  "active_primary_shards": 3,  "active_shards": 3,  "relocating_shards": 0,  "initializing_shards": 0,  "unassigned_shards": 3,  "delayed_unassigned_shards": 0,  "number_of_pending_tasks": 0,  "number_of_in_flight_fetch": 0,  "task_max_waiting_in_queue_millis": 0,  "active_shards_percent_as_number": 50.0}

還可以指定索引,檢視索引級別的健康情況,更加方便問題定位。

$ curl -X GET "http://127.0.0.1:9200/_cluster/health/index1,index2?pretty"
API請求引數API響應引數

響應結果中要特別關注statusunassigned_shardsnumber_of_pending_tasks這幾個指標,status代表叢集的狀態,只有green才是叢集完全健康的狀態,unassigned_shards表明了有多少分片沒有被分片,這是一個很重要的指標,正常情況下不應該存在未被分配的分片,number_of_pending_tasks則是等待中的任務數量,如果這個數值太大也需要特別關注。

02

檢視叢集狀態(cluster state API)

叢集狀態API提供了一個全面檢視叢集狀態資訊的方法,返回的響應內容很多,包括對映、節點、文件等元資料資訊,Elasticsearch提供了過濾器,可以根據需要獲取相應的內容。

$ curl -X GET "http://127.0.0.1:9200/_cluster/state?pretty"

注意:當叢集還在構建過程中,響應的cluster_uuid 欄位的值可能為na,叢集的狀態版本可能為-1

預設情況下,叢集狀態請求被路由到主節點,以確保返回最新的叢集狀態。出於除錯目的,您可以透過向查詢字串新增local=true來檢索特定節點的本地叢集狀態。

$ curl -X GET "http://127.0.0.1:9200/_cluster/state/{metrics}/{indices}?pretty"
indices指的是索引,使用逗號分隔metrics的值有以下選擇,可以使用多個值,用逗號分隔version叢集狀態版本master_nodemaster_node段的資訊,關於叢集主節點nodesnodes段的資訊,關於叢集中的節點routing_tablerouting_table段的資訊metadatametadata段的資訊,關於配置、對映等元資料資訊blocksblocks段的資訊_all包括所有的資訊

以訪問nodes段為例,訪問結果如下:

檢視叢集統計資訊(cluster state API)

叢集統計資訊API用於檢視叢集的各種統計彙總資訊,包括CPU、記憶體、儲存、文件統計等資訊。

$ curl -X GET "http://127.0.0.1:9200/_cluster/stats?human&pretty"
{  "_nodes" : {    "total" : 1,    "successful" : 1,    "failed" : 0  },  "cluster_name" : "elasticsearch",  "cluster_uuid" : "nhG2-6K6RU2kDreqsUZy6A",  "timestamp" : 1570199646793,  "status" : "yellow",  "indices" : {    "count" : 1,    "shards" : {      "total" : 3,      "primaries" : 3,      "replication" : 0.0,      "index" : {        "shards" : {          "min" : 3,          "max" : 3,          "avg" : 3.0        },        "primaries" : {          "min" : 3,          "max" : 3,          "avg" : 3.0        },        "replication" : {          "min" : 0.0,          "max" : 0.0,          "avg" : 0.0        }      }    },    "docs" : {      "count" : 0,      "deleted" : 0    },    "store" : {      "size" : "783b",      "size_in_bytes" : 783    },    "fielddata" : {      "memory_size" : "0b",      "memory_size_in_bytes" : 0,      "evictions" : 0    },    "query_cache" : {      "memory_size" : "0b",      "memory_size_in_bytes" : 0,      "total_count" : 0,      "hit_count" : 0,      "miss_count" : 0,      "cache_size" : 0,      "cache_count" : 0,      "evictions" : 0    },    "completion" : {      "size" : "0b",      "size_in_bytes" : 0    },    "segments" : {      "count" : 0,      "memory" : "0b",      "memory_in_bytes" : 0,      "terms_memory" : "0b",      "terms_memory_in_bytes" : 0,      "stored_fields_memory" : "0b",      "stored_fields_memory_in_bytes" : 0,      "term_vectors_memory" : "0b",      "term_vectors_memory_in_bytes" : 0,      "norms_memory" : "0b",      "norms_memory_in_bytes" : 0,      "points_memory" : "0b",      "points_memory_in_bytes" : 0,      "doc_values_memory" : "0b",      "doc_values_memory_in_bytes" : 0,      "index_writer_memory" : "0b",      "index_writer_memory_in_bytes" : 0,      "version_map_memory" : "0b",      "version_map_memory_in_bytes" : 0,      "fixed_bit_set" : "0b",      "fixed_bit_set_memory_in_bytes" : 0,      "max_unsafe_auto_id_timestamp" : -1,      "file_sizes" : { }    }  },  "nodes" : {    "count" : {      "total" : 1,      "data" : 1,      "coordinating_only" : 0,      "master" : 1,      "ingest" : 1    },    "versions" : [      "6.5.4"    ],    "os" : {      "available_processors" : 4,      "allocated_processors" : 4,      "names" : [        {          "name" : "Windows 10",          "count" : 1        }      ],      "mem" : {        "total" : "7.8gb",        "total_in_bytes" : 8482287616,        "free" : "2.5gb",        "free_in_bytes" : 2716463104,        "used" : "5.3gb",        "used_in_bytes" : 5765824512,        "free_percent" : 32,        "used_percent" : 68      }    },    "process" : {      "cpu" : {        "percent" : 0      },      "open_file_descriptors" : {        "min" : -1,        "max" : -1,        "avg" : 0      }    },    "jvm" : {      "max_uptime" : "3.4d",      "max_uptime_in_millis" : 294220612,      "versions" : [        {          "version" : "1.8.0_221",          "vm_name" : "Java HotSpot(TM) 64-Bit Server VM",          "vm_version" : "25.221-b11",          "vm_vendor" : "Oracle Corporation",          "count" : 1        }      ],      "mem" : {        "heap_used" : "238.8mb",        "heap_used_in_bytes" : 250430752,        "heap_max" : "1.9gb",        "heap_max_in_bytes" : 2112618496      },      "threads" : 46    },    "fs" : {      "total" : "88.1gb",      "total_in_bytes" : 94684311552,      "free" : "18.4gb",      "free_in_bytes" : 19831750656,      "available" : "18.4gb",      "available_in_bytes" : 19831750656    },    "plugins" : [ ],    "network_types" : {      "transport_types" : {        "netty4" : 1      },      "http_types" : {        "netty4" : 1      }    }  }}

還可以使用Node Filter過濾器進行內容過濾。關於Node Filter的內容請參考後面章節。

04

檢視叢集待處理任務(cluster pending task API)

此API用於返回叢集層面的已經提交但是未被執行的變更,例如建立索引、更新對映、分配分片等,一般情況下應該是空的,如果發現大量的pending task,則要檢查叢集是否正常。

$ curl -X GET "http://127.0.0.1s:9200/_cluster/pending_tasks?pretty"

結果如下:

{    "tasks": []}

05

分片重新分配(cluster reroute API)

reroute命令允許手動更改叢集中各個分片的分配,例如可以顯式地將分片從一個節點移動到另一個節點,可以顯式地將未分配的碎片分配給特定的節點。

curl -X POST "http:127.0.0.1:9200/_cluster/reroute?pretty" -H 'Content-Type: application/json' -d'{    "commands" : [        {            "move" : {                "index" : "test", "shard" : 0,                "from_node" : "node1", "to_node" : "node2"            }        },        {          "allocate_replica" : {                "index" : "test", "shard" : 1,                "node" : "node3"          }        }    ]}'
當routing.rebalance.enable設定為true,那麼reroute之後,Elasticsearch會重新進行叢集的平衡。當cluster.routing.allocation設定為false,叢集將禁用分片分配,只能使用reroute進行手工分配。

commands支援的引數有movecancelallocate_replica,其作用以及詳細引數如下:

move用於將一個活動的分片從一個節點移動到另外一個節點。支援的請求體引數有:cancel取消一個分片的分配,預設只能取消副本分片的分配,如果想要取消主分片的分配,需要指定allow_primary引數為true。支援的請求體引數有:allocate_replica分配一個未被分配的分片到某一個節點。支援的引數如下:

06

叢集配置(cluster reroute API)

獲取叢集配置獲取顯式設定的叢集配置:

獲取顯式設定的叢集配置:

更新叢集配置叢集配置的更新分為持久化的、臨時的更新,持久化的配置更新後集群重啟仍然生效,臨時的更新在叢集重啟後不生效。更新持久化配置:

更新臨時配置:

重置叢集配置重置叢集配置的只需要將配置項設定為null:

還可以使用萬用字元進行重置:

10
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 內部瘋傳,22個必考C++面試知識點總結(附答案解析)