Elasticsearch集群API管理集群

引言

Elasticsearch提供了大量了的Rest API用以操作Elastic search的相關(guān)功能,提供了極大的便利,掌握這些API是熟練地使用Elasticsearch的前提。

Elastic search提供的API有集群API、搜索API、索引API、文檔API、cat API、聚合API等,本文主要講解Elasticsearch API,使用集群API(下文稱為cluster API)可以查看集群簡(jiǎn)況狀況、集群狀態(tài)和統(tǒng)計(jì)信息、集群節(jié)點(diǎn)信息等內(nèi)容。

一、查看集群健康情況(health)

使用helath API可以查看集群的整體健康情況,當(dāng)有異常時(shí)及時(shí)告警,例如:

$ curl -X GET "localhost: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
}

甚至還可以指定索引,查看索引級(jí)別的健康情況,更加方便問題定位。

$ curl -X GET "localhost:9200/_cluster/health/test1,test2?pretty"

1.1 API請(qǐng)求參數(shù)

  • level

    值可以為cluster、indices、shards,用于返回不同層次的健康狀況信息,默認(rèn)為cluster。

  • wait_for_status

    值可以為green、yellow、red,此參數(shù)代表等待集群的狀態(tài)變?yōu)橹付ǖ臓顟B(tài)或者超時(shí)返回。

  • wait_for_no_relocating_shards

    true/fasle,代表是否等待不存在relocating的分片時(shí)返回,默認(rèn)false。

  • wait_for_no_initializing_shards

    true/fasle,代表是否等待不存在initializing的分片時(shí)返回,默認(rèn)false。

  • wait_for_active_shards

    數(shù)值,代表等待多少個(gè)分片活躍時(shí)存活,默認(rèn)為0,all代表等待所有的分片都為活躍的情況下返回。

  • wait_for_nodes

    數(shù)值,等待指定個(gè)數(shù)(記為N)的節(jié)點(diǎn)可達(dá)后返回,可以為>=N,<=N,>N,<N,也可以對(duì)應(yīng)使用ge(N),le(N),gt(N),lt(N)

  • wait_for_events

    給定一個(gè)優(yōu)先級(jí),等待指定優(yōu)先級(jí)的所有隊(duì)列事件被處理后返回。指定的優(yōu)先級(jí)可為immediate,urgent,high,normal,low,languid

  • timeout

    當(dāng)使用wait_for_XXX參數(shù)時(shí),指定的超時(shí)時(shí)間,默認(rèn)為30s

  • master_timeout

    連接master節(jié)點(diǎn)的超時(shí)時(shí)間,如果沒有設(shè)置,則與timeout保持一致。

  • local

    true/false,true代表從本地節(jié)點(diǎn)獲取相應(yīng)的信息,false代表從master節(jié)點(diǎn)獲取信息,默認(rèn)為false。

1.2 API響應(yīng)參數(shù)

結(jié)果如上所示,下面對(duì)返回值的重要字段進(jìn)行詳細(xì)解釋:

  • cluster_name

    集群的名稱,每個(gè)集群擁有一個(gè)唯一的名稱。

  • status

    集群的狀態(tài),分為green,yellow,red三種,green代表集群所有的主分片和副本分片都正常運(yùn)行,yellow代表所有的主分片都正常運(yùn)行,但是存在未正常分配的副本分片,red代表有主分片未能正常分配。

  • timed_out

    本次查詢是否超時(shí),值為true/false。

  • number_of_nodes

    集群擁有的節(jié)點(diǎn)數(shù)量。

  • number_of_data_nodes

    集群擁有的數(shù)據(jù)節(jié)點(diǎn)的數(shù)量。

  • active_primary_shards

    活動(dòng)的主分片

  • active_shards

    活動(dòng)的分片

  • relocating_shards

    顯示當(dāng)前正在從一個(gè)節(jié)點(diǎn)遷往其他節(jié)點(diǎn)的分片的數(shù)量。通常來說應(yīng)該是 0,不過在 Elasticsearch 發(fā)現(xiàn)集群不太均衡時(shí),該值會(huì)上漲。比如說:添加了一個(gè)新節(jié)點(diǎn),或者下線了一個(gè)節(jié)點(diǎn)。

  • initializing_shards

    是剛剛創(chuàng)建的分片的個(gè)數(shù)。比如,當(dāng)你剛創(chuàng)建第一個(gè)索引,分片都會(huì)短暫的處于 initializing 狀態(tài)。這通常會(huì)是一個(gè)臨時(shí)事件,分片不應(yīng)該長(zhǎng)期停留在 initializing 狀態(tài)。你還可能在節(jié)點(diǎn)剛重啟的時(shí)候看到 initializing 分片:當(dāng)分片從磁盤上加載后,它們會(huì)從 initializing 狀態(tài)開始。

  • unassigined_shards

    是已經(jīng)在集群狀態(tài)中存在的分片,但是實(shí)際在集群里又找不著。通常未分配分片的來源是未分配的副本。比如,一個(gè)有 5 分片和 1 副本的索引,在單節(jié)點(diǎn)集群上,就會(huì)有 5 個(gè)未分配副本分片。如果你的集群是 red 狀態(tài),也會(huì)長(zhǎng)期保有未分配分片(因?yàn)槿鄙僦鞣制?/p>

  • number_of_pending_tasks

    等待中的任務(wù)數(shù)量

  • number_of_in_flight_fetch

    進(jìn)行中的任務(wù)數(shù)量

  • task_max_waiting_in_queue_millis

    任務(wù)在隊(duì)列中等待的最長(zhǎng)時(shí)間。

  • active_shards_percent_as_number

    活動(dòng)的分片占所有分片的比例。

二、查看集群狀態(tài)

集群狀態(tài)API提供了一個(gè)全面查看集群狀態(tài)信息的方法,返回的響應(yīng)內(nèi)容很多,包括映射、節(jié)點(diǎn)、文檔等元數(shù)據(jù)信息,Elasticsearch提供了過濾器,可以根據(jù)需要獲取相應(yīng)的內(nèi)容。

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

注意:當(dāng)集群還在構(gòu)建過程中,響應(yīng)的cluster_uuid 字段的值可能為na,集群的狀態(tài)版本可能為-1

默認(rèn)情況下,集群狀態(tài)請(qǐng)求被路由到主節(jié)點(diǎn),以確保返回最新的集群狀態(tài)。出于調(diào)試目的,您可以通過向查詢字符串添加local=true來檢索特定節(jié)點(diǎn)的本地集群狀態(tài)。

$ curl -X GET "localhost:9200/_cluster/state/{metrics}/{indices}?pretty"
  • indices指的是索引,使用逗號(hào)分隔

  • metrics的值有以下選擇,可以使用多個(gè)值,用逗號(hào)分隔

    • version

      集群狀態(tài)版本

    • master_node

      master_node段的信息,關(guān)于集群主節(jié)點(diǎn)

    • nodes

      nodes段的信息,關(guān)于集群中的節(jié)點(diǎn)

    • routing_table

      routing_table段的信息

    • metadata

      metadata段的信息,關(guān)于配置、映射等元數(shù)據(jù)信息

    • blocks

      blocks段的信息

    • _all

      包括所有的信息

三、查看集群統(tǒng)計(jì)信息

集群統(tǒng)計(jì)信息API用于查看集群的各種統(tǒng)計(jì)匯總信息,包括CPU、內(nèi)存、存儲(chǔ)、文檔統(tǒng)計(jì)等信息。

$ curl -X GET "localhost: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過濾器進(jìn)行內(nèi)容過濾。關(guān)于Node Filter的內(nèi)容請(qǐng)參考后面章節(jié)。

四、等待集群任務(wù)

此API用于返回集群層面的已經(jīng)提交但是未被執(zhí)行的變更,例如創(chuàng)建索引、更新映射、分配分片等,一般情況下應(yīng)該是空的。

$ curl -X GET "localhost:9200/_cluster/pending_tasks?pretty"

五、集群重新路由

reroute命令允許手動(dòng)更改集群中各個(gè)碎片的分配。例如,可以顯式地將碎片從一個(gè)節(jié)點(diǎn)移動(dòng)到另一個(gè)節(jié)點(diǎn),可以取消分配,可以顯式地將未分配的碎片分配給特定的節(jié)點(diǎn)。

curl -X POST "localhost: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"
          }
        }
    ]
}
'
  • 當(dāng)routing.rebalance.enable設(shè)置為true,那么reroute之后,Elasticsearch會(huì)重新進(jìn)行集群的平衡。
  • 當(dāng)cluster.routing.allocation設(shè)置為false,集群將禁用分片分配,只能使用reroute進(jìn)行手工分配。
command參數(shù)有:
  • move

    用于將一個(gè)活動(dòng)的分片從一個(gè)節(jié)點(diǎn)移動(dòng)到另外一個(gè)節(jié)點(diǎn)。支持的請(qǐng)求體參數(shù)有:

    • index

      索引的名稱

    • shard

      分片的編號(hào)

    • from_node

      源節(jié)點(diǎn)

    • to_node

      目標(biāo)節(jié)點(diǎn)

  • cancel

    取消一個(gè)分片的分配,默認(rèn)只能取消副本分片的分片,如果想要取消主分片的分配,需要指定allow_primary參數(shù)為true。支持的請(qǐng)求體參數(shù)有:

    • index

      索引的名稱

    • shard

      分片的編號(hào)

    • allow_primary

      true/false,代表是否允許取消主分片的分配。

  • allocate_replica

    分配一個(gè)未被分配的分片到某一個(gè)節(jié)點(diǎn)。支持的參數(shù)如下:

    • index

      索引的名稱

    • shard

      分片的編號(hào)

    • node

      節(jié)點(diǎn)的名稱

六、更新集群配置

6.1 獲取集群配置

$ curl -X GET "localhost:9200/_cluster/settings?pretty"

6.2 更新集群配置

集群配置的更新分為持久化的、臨時(shí)的更新,持久化的配置更新后集群重啟仍然生效,臨時(shí)的更新在集群重啟后不生效。

更新持久化配置

$ curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
    "persistent" : {
        "indices.recovery.max_bytes_per_sec" : "50mb"
    }
}
'

更新臨時(shí)配置

$ curl -X PUT "localhost:9200/_cluster/settings?flat_settings=true&pretty" -H 'Content-Type: application/json' -d'
{
    "transient" : {
        "indices.recovery.max_bytes_per_sec" : "20mb"
    }
}
'

6.3 重置集群配置

重置集群配置的方式只需要將配置項(xiàng)設(shè)置為null

$ curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
    "transient" : {
        "indices.recovery.max_bytes_per_sec" : null
    }
}
'

6.4 使用通配符重置集群配置

$ curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
    "transient" : {
        "indices.recovery.*" : null
    }
}
'

6.5 集群配置順序

當(dāng)同時(shí)使用配置文件、配置API進(jìn)行配置更新時(shí),有效性遵循以下順序:

  1. 臨時(shí)集群配置
  2. 持久化集群配置
  3. elasticsearch.yml配置文件配置

一般建議本地配置放到elasticsearch.yml配置文件,集群相關(guān)的配置使用settings API。

七、獲取集群配置

7.1 獲取顯式設(shè)置的配置

$ curl -X GET "localhost:9200/_cluster/settings?pretty"

7.2 獲取默認(rèn)的配置

$ curl -X GET "localhost:9200/_cluster/settings?include_defaults=true&pretty"

更多內(nèi)容,微信關(guān)注“LiuXianSheng劉先生”。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容