Các Thao Tác Quản Lý Phổ Biến trong Elasticsearch

Kiểm tra trạng thái cụm

Sử dụng lệnh sau để kiểm tra trạng thái cụm Elasticsearch:

curl --user elastic:password -s -X GET "http://${ES_NODE}:9200/_cluster/health?pretty"

Kết quả trả về có dạng:

{
  "cluster_name" : "es",
  "status" : "yellow",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 204,
  "active_shards" : 204,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 181,
  "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" : 52.98701298701298
}

Các trạng thái chính:

  • red: Cụm không hoạt động do thiếu shard chính, thường xảy ra khi một chỉ mục có shard không được phân bổ.
  • yellow: Tất cả shard chính hoạt động, nhưng các replicas không đầy đủ. Trong trường hợp chạy đơn nút, do Elasticsearch mặc định tạo replicas, nhưng không thể phân bổ trên cùng một nút nên các replicas bị unassigned.

Phân tích nguyên nhân phân mảnh không được phân bổ

Để xác định lý do cụ thể phân mảnh không được phân bổ, sử dụng lệnh:

curl --user elastic:password -s -X GET "http://${ES_NODE}:9200/_cluster/allocation/explain?pretty"

Kết quả ví dụ:

{
  "note" : "No shard was specified in the explain API request, so this response explains a randomly chosen unassigned shard. There may be other unassigned shards in this cluster which cannot be assigned for different reasons. It may not be possible to assign this shard until one of the other shards is assigned correctly. To explain the allocation of other shards (whether assigned or unassigned) you must specify the target shard in the request to this API.",
  "index" : "2022-09-19",
  "shard" : 0,
  "primary" : false,
  "current_state" : "unassigned",
  "unassigned_info" : {
    "reason" : "CLUSTER_RECOVERED",
    "at" : "2022-10-24T06:43:35.247Z",
    "last_allocation_status" : "no_attempt"
  },
  "can_allocate" : "no",
  "allocate_explanation" : "cannot allocate because allocation is not permitted to any of the nodes",
  "node_allocation_decisions" : [
    {
      "node_id" : "Nd92ablSRhqhD7E04Q9URg",
      "node_name" : "elasticsearch-0",
      "transport_address" : "122.20.3.18:9300",
      "node_attributes" : {
        "ml.machine_memory" : "16656502784",
        "xpack.installed" : "true",
        "transform.node" : "true",
        "ml.max_open_jobs" : "512",
        "ml.max_jvm_size" : "1073741824",
        "tag" : "cold"
      },
      "node_decision" : "no",
      "deciders" : [
        {
          "decider" : "same_shard",
          "decision" : "NO",
          "explanation" : "a copy of this shard is already allocated to this node [[2022-09-19][0], node[Nd92ablSRhqhD7E04Q9URg], [P], s[STARTED], a[id=uFsMaIF8Rgq-BxrplZqSSQ]]"
        },
        {
          "decider" : "shards_limit",
          "decision" : "NO",
          "explanation" : "too many shards [1] allocated to this node for index [2022-09-19], index setting [index.routing.allocation.total_shards_per_node=1]"
        }
      ]
    }
  ]
}

Trong trường hợp này, thường cần xóa chỉ mục không cần thiết (cần thận trọng trong môi trường sản xuất).

Để liệt kê các chỉ mục có shard unassigned:

curl --user elastic:password -s -X GET "http://${ES_SERVER}:9200/_cat/shards?h=index,shard,prirep,state,unassigned.reason" | grep UNASSIGNED

Xóa chỉ mục cụ thể:

curl --user elastic:password -X DELETE "http://${ES_SERVER}:9200/${INDEX_NAME}"

Ví dụ trên cho thấy nguyên nhân do thiết lập mặc định index.routing.allocation.total_shards_per_node=1, giới hạn số shard trên một nút là 1. Điều này khiến không thể phân bổ thêm replicas, dẫn đến trạng thái yellow.

Các nguyên nhân phổ biến của phân mảnh unassigned:

  • INDEX_CREATED: Khi tạo chỉ mục mới qua API.
  • CLUSTER_RECOVERED: Sau khi khôi phục toàn bộ cụm.
  • INDEX_REOPENED: Khi mở hoặc đóng một chỉ mục.
  • DANGLING_INDEX_IMPORTED: Khi nhập chỉ mục bị treo.
  • NEW_INDEX_RESTORED: Khôi phục chỉ mục mới.
  • EXISTING_INDEX_RESTORED: Khôi phục chỉ mục đã đóng.
  • REPLICA_ADDED: Thêm replicas thủ công.
  • ALLOCATION_FAILED: Phân bổ thất bại.
  • NODE_LEFT: Nút chứa shard rời cụm.
  • REINITIALIZED: Khi shard chuyển từ trạng thái bắt đầu sang khởi tạo.
  • REROUTE_CANCELLED: Hủy lệnh reroute.
  • REALLOCATED_REPLICA: Tìm vị trí tốt hơn cho replicas, hủy phân bổ hiện tại.

Tìm kiếm dữ liệu trong chỉ mục

Sử dụng lệnh sau để xem dữ liệu trong một chỉ mục cụ thể:

curl --user elastic:password -s -X GET "http://${ES_SERVER}:9200/${INDEX_NAME}/_search" | jq .

Thẻ: Elasticsearch cluster-health shard-unassigned index-management allocation-deciders

Đăng vào ngày 5 tháng 6 lúc 22:38