Search

Logstash 메모리 힙 정리

생성일
2024/08/13 14:18
Category
Devops 엔지니어
태그
Monitoring
OpenSearch
Kubernetes

배경

Logstash 의 메모리 힙에 대해 정리 한다.

내용

Logstash 는 평상시에는 괜찮지만 트래픽이 폭발적으로 늘어날때는 감당하기 어려울 정도로 쿠버네티스 워크로드 시스템을 불안정하게 한다.
이번에 제대로 느꼈기에 정리 해본다.
우선 정상적이던 Logstash Pod 가 주기적으로 Restart 되고 Filebeat 들도 재정신이 아닐 경우가 있다.
그럼 Logstash 에서는 아래 로그를 뿜는다.
[2023-05-30T06:05:38,242][INFO ][org.logstash.beats.BeatsHandler][log_a][6c037f95073bced7aef876e744d85cbf5e37452f53c3ec8654a375b26f7ac944] [local: 10.150.73.37:5044, remote: 10.150.70.84:57592] Handling exception: java.lang.OutOfMemoryError: Cannot reserve 180355072 bytes of direct buffer memory (allocated: 899103116, limit: 1073741824) (caused by: java.lang.OutOfMemoryError: Cannot reserve 180355072 bytes of direct buffer memory (allocated: 899103116, limit: 1073741824)) [2023-05-30T06:05:38,242][WARN ][io.netty.channel.DefaultChannelPipeline][log_a][6c037f95073bced7aef876e744d85cbf5e37452f53c3ec8654a375b26f7ac944] An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception. java.lang.OutOfMemoryError: Cannot reserve 180355072 bytes of direct buffer memory (allocated: 899103116, limit: 1073741824)
Bash
복사
에러 로그에서 중요한건 OutOfMemoryError = OOM 이다.
"java.lang.OutOfMemoryError: Cannot reserve 180355072 bytes of direct buffer memory”
라는 오류로 인해 메모리 부족 문제가 발생하고 있는 거다.
쿠버네티스의 워커 노드 스펙 최대 리소스에 가까울수록 OOM 이 발생될 확률이 높다.

해결 방법

Logstash 에 더 많은 메모리 할당 : JVM(Java Virtual Machine) 에 할당된 메모리를 늘려주면 된다. Logstash 는 JVM 위에서 실행되며 JVM 의 힙 메모리 크기 는 Logstash 의 메모리 사용량을 결정한다.따라서 Logstash 에 더 많은 메모리를 할당하려면 JVM 의 힙 메모리 크기를 늘려야 한다.
‘LS_JAVA_OPTS’ 환경 변수를 설정
예를 들어 JVM 에 2GB 의 힙 메모리를 할당하려면 다음과 같이 설정할 수 있다.
LS_JAVA_OPTS="-Xmx2g -Xms2g"
Bash
복사
Deployment 에서는 ENV 로 설정 가능하다.
env: - name: OPENSEARCH_ENDPOINT value: .xxxap-northeast-2.es.amazonaws.com - name: OPENSEARCH_USERNAME valueFrom: secretKeyRef: name: opensearch-user key: username - name: OPENSEARCH_PASSWORD valueFrom: secretKeyRef: name: opensearch-passwd key: password - name: LS_JAVA_OPTS value: "-Xmx2g -Xms2g"
YAML
복사

힙 메모리 확인 방법

적용 한 후에 확인하는 방법은 간단하다.
Logstash Pod 에 쉘 접속 후 아래 명령어로 확인 할 수 있다.
logstash@logstash-548d659475-rdhnv:~$ echo $LS_JAVA_OPTS -Xmx2g -Xms2g
Bash
복사