들어가며
기존 레거시 APM 서버에서 로깅이 정상적으로 수집되지 않는 에러가 발생하여 확인해보던 중 elasticsearch cluster status 를 확인해보니 red 임을 확인하였습니다.
그때 해결 과정을 정리한 글입니다.
🚨 Unassigned shards 발생 원인 및 해결방법
$ curl -X GET "localhost:9200/_cat/health?pretty&v"
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1696402762 06:59:22 dorito red 1 1 15 15 0 0 10 0 - 60.0%
이는 primary shards 중 unassigned 된 것이 있다는 의미입니다.
- 원인
- node 수에 비해 지나치게 많은 인덱스가 생성된 경우
- replica shard 수량을 너무 크게 설정하여 할당할 node가 부족한 경우
- shard 데이터 유실 발생
- 디스크 가용공간 부족
- Cluster중 ES 버전 불일치
- 해결방법
- node 수를 늘리거나 불필요한 인덱스를 제거
- replica 수 변경으로 샤드 할당에 실패한 것일 수도 있으니 replica 수 조정 (싱글 노드인 경우 개수 0으로 수정)
- node 를 지정하여 직접 할당
- elasticsearch cluster 상태 확인
$ curl -X GET "localhost:9200/_cluster/health?filter_path=status,*_shards,cluster_name&pretty"
[ec2-user@ip-172-31-9-33 ~]$ curl -X GET "localhost:9200/_cluster/health?filter_path=status,*_shards,cluster_name&pretty"
{
"cluster_name" : "dorito",
"status" : "red",
"active_primary_shards" : 10,
"active_shards" : 10,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 128,
"delayed_unassigned_shards" : 0
}
- status 설명
- green - 모든 shard 할당완료
- yellow - primary shard는 전부 할당 완료하였으나 replica shard 할당 실패. 이때 node 장애가 발생하면 데이터 유실이 발생 할 수 있음.
- red - 할당못한 primary shard 존재, 부분 데이터 사용 불가, node 구동때 일시적으로 발생할수도 있음.
- 샤드 상태 조회
# 전체 shard 사용 조회 URI
$ curl -X GET "localhost:9200/_cat/shards?v=true&s=i&pretty"
# UNASSIGNED 된 값과 이유를 보여주는 URI
$ curl -X GET "localhost:9200/_cat/shards?v=true&h=index,shard,prirep,state,node,unassigned.reason&s=state&s=i&pretty" | grep UNASSIGNED
Step 1. 불필요한 index 삭제
출력된 샤드 리스트를 보면, index 가 필요이상으로 많이 생성된 것을 확인 할 수 있습니다.
(예: apm-7.11.1-error-000001 ~ apm-7.11.1-error-000011 까지 생성되어있고, 그 중 1개만 정상 작동 상태임을 확인)
불필요한 중복된 index들을 삭제해줍니다.
$ curl -XDELETE http://localhost:9200/index_name
- 이때, 에러는 다음과 같은 의미를 나타냅니다.
- INDEX_CREATED: This state will show when API for creating an index introduces the problem.
- CLUSTER_RECOVERED: This state will show when full data restoration is performed for the cluster.
- INDEX_REOPENED: This state will show when an index is enabled or disabled.
- DANGLING_INDEX_IMPORTED: This state will show when result of dangling index is not imported.
- NEW_INDEX_RESTORED: This state will show when data is restored from the snapshot to a new index.
- EXISTING_INDEX_RESTORED: This state will show when data is restored from the snapshot to a disabled index.
- REPLICA_ADDED: This state will show when Replica shards are added explicitly.
- ALLOCATION_FAILED: This state will show when shard assignment fails.
- NODE_LEFT: This state will show when the node that carries the shard is located outside of the cluster.
- REINITIALIZED: This state will show when incorrect operations (such as use of the shadow replica shard) exist in the process from moving the shard to the shard initialization.
- REROUTE_CANCELLED: This state will show when the assignment is canceled because the routing is canceled explicitly.
- REALLOCATED_REPLICA: This indicates that a better replica location will be used, and the existing replica assignment is canceled. As a result, the shard is unassigned.
지금 상황은 CLUSTER_RECOVERED
이므로 클러스터에 대해 전체 데이터 복원이 수행되고 나서 제대로 할당되지 않았음을 의미합니다.
할당 상태 확인
[ec2-user@ip-172-31-9-33 ~]$ curl -XGET localhost:9200/_cluster/allocation/explain?pretty
{
"index" : "apm-7.11.1-profile-000002",
"shard" : 0,
"primary" : true,
"current_state" : "unassigned",
"unassigned_info" : {
"reason" : "CLUSTER_RECOVERED",
"at" : "2023-09-27T06:48:17.403Z",
"last_allocation_status" : "no_valid_shard_copy"
},
"can_allocate" : "no_valid_shard_copy",
"allocate_explanation" : "cannot allocate because a previous copy of the primary shard existed but can no longer be found on the nodes in the cluster",
"node_allocation_decisions" : [
{
"node_id" : "zddMUnHXSZuAiZtm7rbkhA",
"node_name" : "ip-172-31-9-33.ap-northeast-2.compute.internal",
"transport_address" : "172.31.9.33:9300",
"node_attributes" : {
"ml.machine_memory" : "16509018112",
"xpack.installed" : "true",
"transform.node" : "true",
"ml.max_open_jobs" : "20",
"ml.max_jvm_size" : "4294967296"
},
"node_decision" : "no",
"store" : {
"found" : false
}
}
]
}
Step 2. Replica shard 수량을 너무 크게 설정하여 할당할 node가 부족한 경우 발생하므로 Replica 수를 0 으로 수정
shard는 primary shard 와 replica shard로 나뉩니다.
index 내의 데이터(document)는 특정수량의 primary shard에 할당되어 각 node에 저장되며 primary shard를 몇 개 가져갈지는 number_of_shards
설정으로 결정됩니다.
그리고 각 primary shard는 특정수량의 복사본인 replica shard를 가지고 있습니다.
replica 수량은 number_of_replicas
설정으로 결정됩니다.
shard 수량은 보통은 index를 생성하거나 index template을 만들때 지정합니다.
여기서 알아야 할 하나의 규칙은 특정 primary shard의 replica는 해당 primary shard와 다른 node에 저장되어야 합니다.
N(node수량) >= R(replica수량) + 1
이는 replica 수량이 node 수와 같거나 클때 할당할 수 없다는 의미입니다.
- 문제 원인
- replica 수가 node 수보다 많아 같은 node에 같은 샤드가 할당된 경우 발생합니다.
- 특히, single node에서
number_of_replicas
를 default 값인 1로 설정한 경우 에러가 발생합니다.
- 해결 방법: replica 수를 0으로 바꾸고, 모든 샤드를 재할당한 후 다시 replicas 수를 조정합니다.
$ curl -XGET "localhost:9200/_cluster/allocation/explain?pretty"
위 URI 를 요청하면 출력되는 값은 다음과 같습니다.
[ec2-user@ip-172-31-9-33 ~]$ curl -XGET localhost:9200/_cluster/allocation/explain?pretty
{
"index" : "apm-7.11.1-span-000011",
"shard" : 0,
"primary" : false,
"current_state" : "unassigned",
"unassigned_info" : {
"reason" : "CLUSTER_RECOVERED",
"at" : "2023-09-27T06:48:17.400Z",
"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" : "zddMUnHXSZuAiZtm7rbkhA",
"node_name" : "ip-172-31-9-33.ap-northeast-2.compute.internal",
"transport_address" : "172.31.9.33:9300",
"node_attributes" : {
"ml.machine_memory" : "16509018112",
"xpack.installed" : "true",
"transform.node" : "true",
"ml.max_open_jobs" : "20",
"ml.max_jvm_size" : "4294967296"
},
"node_decision" : "no",
"deciders" : [
{
"decider" : "same_shard",
"decision" : "NO",
"explanation" : "a copy of this shard is already allocated to this node [[apm-7.11.1-span-000011][0], node[zddMUnHXSZuAiZtm7rbkhA], [P], s[STARTED], a[id=bgbujhsPSySHJunB4odX4Q]]"
}
]
}
]
}
cannot allocate because allocation is not permitted to any of the nodes
a copy of this shard is already allocated to this node [[apm-7.11.1-span-000011][0], node[zddMUnHXSZuAiZtm7rbkhA], [P], s[STARTED], a[id=bgbujhsPSySHJunB4odX4Q]]
에러 로그를 확인할 수 있습니다.
아래와 같이 설정해줍니다.
# replica 수 0으로 변경
curl -XPUT \
"http://localhost:9200/_settings" \
-H 'Content-Type: application/json' \
-d '{
"index" : {
"number_of_replicas" : 0
}
}'
# replica 수 확인
curl "localhost:9200/_cluster/stats?pretty" | grep "replication" # 결과가 "replication" : 0.0, ... 이면 성공
# 자동 할당 설정 활성화
curl -XPUT 'http://localhost:9200/_cluster/settings' \
-H 'Content-Type: application/json' \
-d '{
"transient" : {
"cluster.routing.allocation.enable" : "all"
}
}'
# 샤드 재할당
curl -XPOST "http://localhost:9200/_cluster/reroute?retry_failed"
# unassigned shards가 존재하는지 확인
$ curl "https://localhost:9200/_cat/shards" | grep "UNASSIGNED"
- 참고 문서
- elasticsearch = cannot allocate because allocation is not permitted to any of the nodes - Stack Overflow
- Red or yellow cluster status | Elasticsearch Guide [8.10] | Elastic
- Elasticsearch Shard할당 실패원인 및 해결방법,“X of Y shards failed” - Geuni's Blog
- unassigned shards 발생 원인 및 해결방법 | 의사줌치 (a3magic3pocket.github.io)
Step 3. 샤드 데이터가 더 이상 클러스터에 존재하지 않은 경우 발생하므로 노드를 직접 할당하여 해결
[로그 보기]
대부분 index 는 작동되는데, 여전히 일부 index 는 UNASSIGNED 에러 발생 및 cluster 상태 = red 문제가 발생함- 샤드 데이터가 더 이상 클러스터에 존재하지 않은 경우 상시 업데이트 인덱스의 기본 샤드 0이 할당 해제됩니다.
{ "acknowledged": true, "state": { "cluster_uuid": "20kpk897SxqFyGPMOfALww", "version": 507069, "state_uuid": "RxWjRoKbQmmRDp45tQYNPA", "master_node": "zddMUnHXSZuAiZtm7rbkhA", "blocks": {}, "nodes": { "zddMUnHXSZuAiZtm7rbkhA": { "name": "ip-172-31-9-33.ap-northeast-2.compute.internal", "ephemeral_id": "oC0ndMkAQeqtYQ6hXpJOiQ", "transport_address": "172.31.9.33:9300", "attributes": { "ml.machine_memory": "16509018112", "xpack.installed": "true", "transform.node": "true", "ml.max_open_jobs": "20", "ml.max_jvm_size": "4294967296" } } }, "routing_table": { "indices": { ".ds-.slm-history-5-2023.09.26-000006": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": ".ds-.slm-history-5-2023.09.26-000006", "allocation_id": { "id": "I5Puhs7ARE6zdvdeuKnUPA" } } ] } }, ".ds-.slm-history-5-2023.06.21-000004": { "shards": { "0": [ { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".ds-.slm-history-5-2023.06.21-000004", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } } ] } }, "apm-7.11.1-transaction-000013": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "apm-7.11.1-transaction-000013", "allocation_id": { "id": "eVR_8c5PRquJ1D79Tnp5vQ" } } ] } }, ".kibana_2": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": ".kibana_2", "allocation_id": { "id": "PA6mGxr3TC2NcOvp17nDMw" } } ] } }, "apm-7.11.1-span-000011": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "apm-7.11.1-span-000011", "allocation_id": { "id": "bgbujhsPSySHJunB4odX4Q" } } ] } }, ".kibana_task_manager_1": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": ".kibana_task_manager_1", "allocation_id": { "id": "Jqx2zkCARUuk2TwBSXb5xQ" } } ] } }, ".ds-ilm-history-5-2023.09.26-000027": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": ".ds-ilm-history-5-2023.09.26-000027", "allocation_id": { "id": "RSmJk_XrTwKDOzX2J44bHQ" } } ] } }, ".ds-.slm-history-5-2023.07.21-000005": { "shards": { "0": [ { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".ds-.slm-history-5-2023.07.21-000005", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } } ] } }, "apm-7.11.1-profile-000011": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "apm-7.11.1-profile-000011", "allocation_id": { "id": "SEnrau4ERf6drgtnaMwI8g" } } ] } }, ".kibana-event-log-7.11.1-000001": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": ".kibana-event-log-7.11.1-000001", "allocation_id": { "id": "KenYxWfISbCG0IEFyznK1g" } } ] } }, ".async-search": { "shards": { "0": [ { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".async-search", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } } ] } }, ".kibana_1": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": ".kibana_1", "allocation_id": { "id": "ulmrpns-QYqwRwZZoiQFfg" } } ] } }, ".transform-notifications-000002": { "shards": { "0": [ { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".transform-notifications-000002", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } } ] } }, "apm-7.11.1-error-000011": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "apm-7.11.1-error-000011", "allocation_id": { "id": "KCih6NQGR5u0lbODRDPycg" } } ] } }, "apm-7.11.1-onboarding-2023.09.26": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "apm-7.11.1-onboarding-2023.09.26", "allocation_id": { "id": "Mm1cp3mrSU6laP10WgcK-w" } } ] } }, ".tasks": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": ".tasks", "allocation_id": { "id": "OVwcZy92QBWtGPlNdTO83A" } } ] } }, ".apm-agent-configuration": { "shards": { "0": [ { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".apm-agent-configuration", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } } ] } }, ".ds-ilm-history-5-2023.06.21-000025": { "shards": { "0": [ { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".ds-ilm-history-5-2023.06.21-000025", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } } ] } }, ".apm-custom-link": { "shards": { "0": [ { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".apm-custom-link", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } } ] } }, ".ds-ilm-history-5-2023.07.21-000026": { "shards": { "0": [ { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".ds-ilm-history-5-2023.07.21-000026", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } } ] } }, ".transform-internal-005": { "shards": { "0": [ { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".transform-internal-005", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } } ] } }, ".security-7": { "shards": { "0": [ { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".security-7", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } } ] } }, "apm-7.11.1-metric-000011": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "apm-7.11.1-metric-000011", "allocation_id": { "id": "_lYK99nsRNOpQnQaTnsVZw" } } ] } }, "apm-7.11.1-onboarding-2023.09.27": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "apm-7.11.1-onboarding-2023.09.27", "allocation_id": { "id": "QFagHbufRf-YTZ3JEebqTw" } } ] } }, "filebeat-7.12.1-2023.09.27-000001": { "shards": { "0": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "filebeat-7.12.1-2023.09.27-000001", "allocation_id": { "id": "bxH-HL5PQjWolXAWE7OmRQ" } } ] } } } }, "routing_nodes": { "unassigned": [ { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".ds-.slm-history-5-2023.06.21-000004", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } }, { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".ds-.slm-history-5-2023.07.21-000005", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } }, { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".async-search", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } }, { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".transform-notifications-000002", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } }, { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".apm-agent-configuration", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } }, { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".ds-ilm-history-5-2023.06.21-000025", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } }, { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".apm-custom-link", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } }, { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".ds-ilm-history-5-2023.07.21-000026", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } }, { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".transform-internal-005", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } }, { "state": "UNASSIGNED", "primary": true, "node": null, "relocating_node": null, "shard": 0, "index": ".security-7", "recovery_source": { "type": "EXISTING_STORE", "bootstrap_new_history_uuid": false }, "unassigned_info": { "reason": "CLUSTER_RECOVERED", "at": "2023-09-27T10:18:26.507Z", "delayed": false, "allocation_status": "no_valid_shard_copy" } } ], "nodes": { "zddMUnHXSZuAiZtm7rbkhA": [ { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": ".ds-.slm-history-5-2023.09.26-000006", "allocation_id": { "id": "I5Puhs7ARE6zdvdeuKnUPA" } }, { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "apm-7.11.1-transaction-000013", "allocation_id": { "id": "eVR_8c5PRquJ1D79Tnp5vQ" } }, { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": ".kibana_2", "allocation_id": { "id": "PA6mGxr3TC2NcOvp17nDMw" } }, { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "apm-7.11.1-span-000011", "allocation_id": { "id": "bgbujhsPSySHJunB4odX4Q" } }, { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": ".kibana_task_manager_1", "allocation_id": { "id": "Jqx2zkCARUuk2TwBSXb5xQ" } }, { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": ".ds-ilm-history-5-2023.09.26-000027", "allocation_id": { "id": "RSmJk_XrTwKDOzX2J44bHQ" } }, { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "apm-7.11.1-profile-000011", "allocation_id": { "id": "SEnrau4ERf6drgtnaMwI8g" } }, { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": ".kibana-event-log-7.11.1-000001", "allocation_id": { "id": "KenYxWfISbCG0IEFyznK1g" } }, { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": ".kibana_1", "allocation_id": { "id": "ulmrpns-QYqwRwZZoiQFfg" } }, { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "apm-7.11.1-error-000011", "allocation_id": { "id": "KCih6NQGR5u0lbODRDPycg" } }, { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "apm-7.11.1-onboarding-2023.09.26", "allocation_id": { "id": "Mm1cp3mrSU6laP10WgcK-w" } }, { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": ".tasks", "allocation_id": { "id": "OVwcZy92QBWtGPlNdTO83A" } }, { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "apm-7.11.1-metric-000011", "allocation_id": { "id": "_lYK99nsRNOpQnQaTnsVZw" } }, { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "apm-7.11.1-onboarding-2023.09.27", "allocation_id": { "id": "QFagHbufRf-YTZ3JEebqTw" } }, { "state": "STARTED", "primary": true, "node": "zddMUnHXSZuAiZtm7rbkhA", "relocating_node": null, "shard": 0, "index": "filebeat-7.12.1-2023.09.27-000001", "allocation_id": { "id": "bxH-HL5PQjWolXAWE7OmRQ" } } ] } } } }
- 문제 원인
- 복제본이 (초기 인덱싱 프로세스의 속도를 높이는 데 사용되는 기술)이 없는 노드에서 생성되는 경우
- 데이터가 복제되기 전에 노드가 클러스터를 떠나는 경우
- 기본 인덱스가 글로벌 클러스터 상태 파일에서 샤드를 감지하지만 클러스터에서 샤드의 데이터를 찾을 수 없는 경우
- 노드가 재부팅하는 동안 문제가 발생하는 경우어떤 이유로 이 프로세스가 실패하면(예: 노드의 스토리지가 어떤 식으로든 손상된 경우) 샤드가 할당되지 않은 상태로 남아있을 수 있습니다.
- 일반적으로 노드가 클러스터에 대한 연결을 다시 시작하면 디스크에 있는 샤드에 대한 정보를 기본 노드에 전달하고, 기본 노드는 해당 샤드를 "할당되지 않음"에서 "할당/시작됨"으로 전환합니다.
1. 할당되지 않은 index 를 확인합니다.
$ curl -XGET 'localhost:9200/_cat/shards?h=index,shard,prirep,state,unassigned.reason' | grep UNASSIGNED
2. cluster 에서 할당되지 않은 주요 shard index가 무엇인지 확인합니다.
$ curl -X GET "localhost:9200/_cluster/allocation/explain?pretty"
[ec2-user@ip-172-31-9-33 ~]$ curl -X GET "localhost:9200/_cluster/allocation/explain?pretty" -H 'Content-Type: application/json' -d'
> {
> "index": ".security-7",
> "shard": 0,
> "primary": true
> }
> '
{
"index" : ".security-7",
"shard" : 0,
"primary" : true,
"current_state" : "unassigned",
"unassigned_info" : {
"reason" : "CLUSTER_RECOVERED",
"at" : "2023-09-27T06:48:17.401Z",
"last_allocation_status" : "no_valid_shard_copy"
},
"can_allocate" : "no_valid_shard_copy",
"allocate_explanation" : "cannot allocate because a previous copy of the primary shard existed but can no longer be found on the nodes in the cluster",
"node_allocation_decisions" : [
{
"node_id" : "zddMUnHXSZuAiZtm7rbkhA",
"node_name" : "ip-172-31-9-33.ap-northeast-2.compute.internal",
"transport_address" : "172.31.9.33:9300",
"node_attributes" : {
"ml.machine_memory" : "16509018112",
"xpack.installed" : "true",
"transform.node" : "true",
"ml.max_open_jobs" : "20",
"ml.max_jvm_size" : "4294967296"
},
"node_decision" : "no",
"store" : {
"found" : false
}
}
]
}
cannot allocate because a previous copy of the primary shard existed but can no longer be found on the nodes in the cluster
이 오류의 의미는 이것이 기본 샤드이고 복제본 샤드가 없기 때문에 데이터 손실로 인해 기본 샤드를 재할당할 수 없다는 것입니다.
- 해결 방안
- 해당 인덱스를 다시 색인
- 백업에서 인덱스를 반환
- 원본 노드를 복구하여 클러스터에 다시 넣기(기본 샤드를 강제로 할당하지 않아야 함)
- 샤드를 활성 데이터 노드 중 하나에 수동으로 재할당하고 원본 데이터 소스 또는 백업을 사용하여 누락된 데이터를 다시 색인 (가장 안전)
후자(기본 샤드 강제 할당)를 사용하기로 결정한 경우, 주의할 점은 "빈" 샤드를 할당하게 된다는 것입니다.
원본 Primary shard 데이터가 포함된 노드가 나중에 클러스터에 다시 조인할 경우, 새로 생성된(텅 빈 상태인) Primary shard가 데이터의 "최신" 버전으로 간주되기 때문에 해당 데이터를 덮어쓰게 됩니다.
이 작업을 진행하기 전에 대신 할당을 다시 시도하면 해당 샤드에 저장된 데이터를 보존할 수 있습니다.
따라서 UNASSIGNED 된 값들을 직접 노드 재할당을 해줍니다.
노드 명 확인
curl -X GET "localhost:9200/_cat/nodes?pretty&v"
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
172.31.9.33 17 78 64 2.62 2.84 2.86 cdhilmrstw * ip-172-31-9-33.ap-northeast-2.compute.internal
아래에서 다시 index 를 검색합니다.
$ curl -XGET 'localhost:9200/_cat/shards?h=index,shard,prirep,state,unassigned.reason' | grep UNASSIGNED
Cluster reroute API | Elasticsearch Guide [7.17] | Elastic 문서를 참고하여 직접 노드를 할당해줍니다.
할당되지 않은 기본 샤드를 강제로 할당하려는 경우, allocate_empty_primary
플래그를 사용하여 할당할 수 있습니다.
curl -X POST "localhost:9200/_cluster/reroute?pretty" -H 'Content-Type: application/json' -d'
{
"commands": [
{
"allocate_empty_primary": {
"index": "할당되지 않은 index 이름", "shard": 0,
"node": "ip-172-31-9-33.ap-northeast-2.compute.internal",
"accept_data_loss": true
}
}
]
}
'
작업을 마치면, cluster 상태를 확인합니다.
$ curl -X GET "localhost:9200/_cat/health?pretty&v"
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1696415436 10:30:36 dorito green 1 1 25 25 0 0 0 0 - 100.0%
green 이면 해결되었습니다.
(Optional
) 추가적으로 아래 URI 에서 index 상태를 확인합니다.
$ curl -X GET "localhost:9200/_cat/indices?pretty&s=i&v"
에러가 발생했을 때는 red 상태인 인덱스가 존재하고, 해당 인덱스에는 pri.store.size 등 값이 존재하지 않았는데, 값이 할당된 것을 확인할 수 있습니다.
- 참고문서
- RED Elasticsearch Cluster? Panic no longer | Elastic Blog
- Cluster reroute API | Elasticsearch Guide [7.17] | Elastic
- How to manually change the Elasticsearch's allocation of shards | DevCoops
- How to Resolve Unassigned Shards in Elasticsearch | Datadog (datadoghq.com)
[마치며]
이 에러가 난 근본적인 원인을 파악해보자면, 기존에 ELK stack EC2 인스턴스 서버 내에서 인덱스 생명 주기가 제대로 안되어 불필요한 자료가 삭제가 되지않고 있었습니다.
개인적인 가설로는 가상메모리 용량 부족으로 디스크 스왑이 일어나서 Node 가 손상이 된 것으로 사료됩니다.
결론적으로는 이 에러 해결 이후에 또 다른 에러가 발생하여서 새로 서버를 구축하는 방향으로 해결하였습니다.
만약 Elastic 을 사용하여 APM 로깅 인프라를 구축한 케이스라면 에러가 발생한 관련 인덱스를 관할하는 서비스가 무엇인지 알아보고 재설치를 해도 무방할 것 같습니다.
'Infra > ELK stack' 카테고리의 다른 글
[Elasticsearch] Elasticsearch 기본 구성 이해하기 (0) | 2023.10.25 |
---|