본문 바로가기
개발/ELK

[Elasticsearch] 클러스터 구축하기 - 1

by parkkingcar 2023. 8. 19.

 

 

클러스터 구축을 위해 엘라스틱서치 환경 설정 파일을 다루는 방법에 대해 알아보겠습니다. elasticsearch.ymljvm.options파일의 설정은 엘라스틱서치 8.9.1버전을 기준으로 작성하였습니다.

 

대부분의 설정이 주석으로 처리되어 있고, 해당 설정에 대한 간략한 설명이 주석으로 제공됩니다. 기본으로 제공되는 설정과 필수적으로 알아야 하는 여러가지 설정을 영역별로 살펴보겠습니다.

 

 

 

elasticsearch.yml 설정 파일

elasticsearch.yml 파일은 엘라스틱서치를 구성하기 위해 기본이 되는 환경 설정 파일입니다. 아래 코드는 엘라스틱서치를 처음 설치했을 때의 기본 설정입니다. 

 

elasticsearch.yml

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
#network.host: 192.168.0.1
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Allow wildcard deletion of indices:
#
#action.destructive_requires_name: false

 

 


Cluster 영역

Cluster 영역은 클러스터 전체에 적용되는 설정입니다.

 

'cluster.name' : 클러스터의 이름을 설정하는 항목입니다. 클러스터의 이름을 변경하려면 클러스터 내의 모든 노드를 재시작해야 하므로 처음부터 신중하게 설정해야 합니다. 기본값인 주석 처리 상태로 프로세스를 시작하면 elasticsearch라는 이름으로 자동 설정합니다.

 


Node 영역

Node 영역은 해당 노드에만 적용되는 설정입니다.

 

'node.name' : 노드의 이름을 설정하는 항목으로 노드의 이름은 클러스터 내에서 유일해야 합니다. 주석 처리된 상태로 시작하면 랜덤한 문자열로 자동으로 설정합니다.

 

'node.attr.rack' : 각 노드에 설정할 수 있는 커스텀 항목으로, 사용자 정의된 rack 값을 통해 HA 구성과 같이 샤드를 분배할 수 있는 기능입니다.

 


Path 영역

Path 영역은 데이터와 로그의 저장 위치와 관련된 설정입니다. Path 영역은 이전버전에서 주석처리가 없는 유일한 영역이었는데, 최근버전에서는 아마 엘라스틱서치가 설치된 곳을 기본으로 설정하는 것 같습니다.

 

'path.data' : 노드가 가지고 있을 문서들을 저장할 경로를 설정하는 항목입니다. 색인이 완료된 문서들은 세그먼트에 파일로 저장되는데 이 파일들이 위치하게 될 경로입니다.

 

'path.data' : 엘라스틱서치에서 발생하는 로그를 저장할 경로를 설정하는 항목입니다.

 


Memory 영역

Memory 영역에는 엘라스틱서치 프로세스에 할당되는 메모리 영역을 어떻게 관리할 것인지 간략하게 설정할 수 있습니다.

 

'bootstrap.memory_lock' : 시스템의 스왑 메모리 영역을 사용하지 않도록 하는 설정입니다. 이 설정을 통해 스왑 영역을 사용하지 않으면 성능을 보장할 수 있지만 시스템의 메모리가 부족한 경우에는 에러를 일으켜서 노드에 장애가 발생할 수 있습니다. 만약 위 설정을 활성화하고자 한다면 아래와 같이 추가적인 시스템 설정이 필요합니다.

 

아래 명령으로 설정파일을 수정합니다.

$ sudo vi /etc/security/limits.conf

elasticsearch  soft    memlock unlimited
elasticsearch  hard    memlock unlimited

 

만약 systemd로 프로세스를 시작한다면 아래 설정이 추가로 필요합니다.

$ sudo mkdir /etc/systemd/system/elasticsearch.service.d
$ sudo vi /etc/systemd/system/elasticsearch.service.d/override.conf

[Service]
LimitMEMLOCK=infinity

$ sudo systemctl daemon-reload

 


Network 영역

Network 영역은 외부와 통신할 때 사용하게 될 IP 주소를 설정하는 항목입니다. 

 

'network.host' : 엘라스틱서치 어플리케이션이 사용하게 될 IP 주소를 설정합니다. 127.0.0.1 과 같은 로컬 IP를 사용하면 서버 내부에서만 사용할 수 있고, 서버에서 사용하고 있는 IP를 사용하면 외부와 통신이 가능합니다. 0.0.0.0을 사용하면 두 가지 경우를 모두 사용할 수 있습니다.

 

'http.port' : 엘라스틱서치 어플리케이션이 사용하게 될 포트 번호를 설정합니다. 기본값으로 9200을 사용합니다.

 


Discovery 영역

Discovery 영역은 노드 간의 클러스터링을 위해 필요한 설정입니다.

 

'discovery.seed_hosts' : 노드가 클러스터의 다른 노드를 찾아 바인딩하는데 사용할 호스트 목록입니다. 기본값은 ["127.0.0.1", "[::1]"]인데, 이는 노드가 localhost의 다른 노드만 찾는다는 것을 의미합니다. 노드가 다른 머신에 있는 노드를 찾을 수 있도록 하려면 해당 머신의 호스트 이름 또는 IP 주소를 지정해야 합니다.

 

'cluster.initial_master_nodes' : 클러스터의 마스터 노드가 될 수 있는 후보를 설정합니다. 

 

 

 

 

jvm.options 설정 파일

엘라스틱서치는 자바로 만들어진 어플리케이션이기 때문에 힙 메모리, GC방식 등과 같은 JVM관련 설정이 필요합니다. 아래 코드는 엘라스틱서치를 처음 설치했을 때의 기본 설정입니다. 

 

jvm.options

################################################################
##
## JVM configuration
##
################################################################
##
## WARNING: DO NOT EDIT THIS FILE. If you want to override the
## JVM options in this file, or set any additional options, you
## should create one or more files in the jvm.options.d
## directory containing your adjustments.
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/8.9/jvm-options.html
## for more information.
##
################################################################



################################################################
## IMPORTANT: JVM heap size
################################################################
##
## The heap size is automatically configured by Elasticsearch
## based on the available memory in your system and the roles
## each node is configured to fulfill. If specifying heap is
## required, it should be done through a file in jvm.options.d,
## which should be named with .options suffix, and the min and
## max should be set to the same value. For example, to set the
## heap to 4 GB, create a new file in the jvm.options.d
## directory containing these lines:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/8.9/heap-size.html
## for more information
##
################################################################


################################################################
## Expert settings
################################################################
##
## All settings below here are considered expert settings. Do
## not adjust them unless you understand what you are doing. Do
## not edit them in this file; instead, create a new file in the
## jvm.options.d directory containing your adjustments.
##
################################################################

-XX:+UseG1GC

## JVM temporary directory
-Djava.io.tmpdir=${ES_TMPDIR}

# Leverages accelerated vector hardware instructions; removing this may
# result in less optimal vector performance
20:--add-modules=jdk.incubator.vector

## heap dumps

# generate a heap dump when an allocation from the Java heap fails; heap dumps
# are created in the working directory of the JVM unless an alternative path is
# specified
-XX:+HeapDumpOnOutOfMemoryError

# exit right after heap dump on out of memory error
-XX:+ExitOnOutOfMemoryError

# specify an alternative path for heap dumps; ensure the directory exists and
# has sufficient space
-XX:HeapDumpPath=data

# specify an alternative path for JVM fatal error logs
-XX:ErrorFile=logs/hs_err_pid%p.log

## GC logging
-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,level,pid,tags:filecount=32,filesize=64m

 


JVM heap size 영역

JVM에서 사용하게 될 힙 메모리의 크기를 설정하는 항목입니다. 

-Xms4g
-Xmx4g

 

JVM은 데이터를 저장하기 위해 힙 메모리 공간을 필요로 하는데, 이 값은 Xms로 최소값을, Xmx로 최댓값을 설정합니다. 만약 두 값을 다른값으로 설정하면 실행 시에는 Xms에 설정된 최솟값 정도의 크기만 확보했다가 요청이 늘어나서 더 많은 힙 메모리가 필요해지는 경우 Xmx에 설정된 최댓값 크기까지 메모리를 요청합니다. 일반적으로 두 값은 같은 값으로 설정하도록 권장합니다.

 


추가 설정 영역

 

 

-XX:+UseG1GC

G1(Garbage First) GC를 사용하도록 지정합니다. G1 GC는 Java의 가비지 컬렉션 알고리즘 중 하나로, 대규모 메모리 및 다중 CPU 코어를 활용하여 매우 빠른 GC를 제공합니다.

 

 

-Djava.io.tmpdir=${ES_TMPDIR}

임시 파일을 생성하는 디렉토리를 지정합니다. ${ES_TMPDIR}은 엘라스틱서치의 임시 디렉토리 경로를 나타냅니다.

 

-XX:+HeapDumpOnOutOfMemoryError 

Java 힙 메모리 부족 시 힙 덤프(메모리 덤프)를 생성하도록 설정합니다. 

 

-XX:+ExitOnOutOfMemoryError

Java 힙 메모리 부족 시 JVM을 종료하도록 설정합니다.

 

-XX:HeapDumpPath=data

힙 덤프 파일이 생성되는 경로를 설정합니다. data 디렉토리에 저장됩니다.

 

-XX:ErrorFile=logs/hs_err_pid%p.log

JVM 에러 로그 파일의 경로를 설정합니다. %p는 현재 프로세스 ID를 나타냅니다.

 

-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,level,pid,tags:filecount=32,filesize=64m

GC 관련 정보를 logs/gc.log 파일에 기록하도록 설정합니다.

 

 

 

 

 

 

참고자료

 

Configuring Elasticsearch | Elasticsearch Guide [8.9] | Elastic

We no longer recommend using transient cluster settings. Use persistent cluster settings instead. If a cluster becomes unstable, transient settings can clear unexpectedly, resulting in a potentially undesired cluster configuration. See the Transient settin

www.elastic.co

 

2.3.2 elasticsearch.yml - Elastic 가이드북

실행중인 각각의 elasticsearch 노드들을 구분할 수 있는 노드의 이름을 설정할 수 있습니다. 설정하지 않으면 노드명은 7.0 버전 부터는 호스트명, 6.x 이하 버전에서는 프로세스 UUID의 첫 7글자가 노

esbook.kimjmin.net

 

 

 

이 글은 '기초부터 다지는 ElasticSearch 운영 노하우 - 박상헌, 강진우'를 기반으로 작성하였습니다.

댓글