[EFK로 서버 로그 수집하기] Opensearch(Elasticsearch)로 로그 저장하기

Hyunjun Kim·2025년 5월 27일
0

Data_Engineering

목록 보기
84/153

4. Opensearch(Elasticsearch)로 로그 저장하기

강의에서는 Opensearch, Open Dashboard - 2.4.0 버전을 사용한다.

4.1 Opensearch 설치하기

이 강의에서는 기본적인 설치만 다룬다. 더 자세한 설치와 설정 방법은 매뉴얼을 참고.
기본설치는 다음 특징을 갖는다.

  • single mode
  • 권한이 없다
  • tls 사용안함

설정을 Open Dashboard에서 편하게 할 수 있으므로, 5.1 Open Dashboard 설치하기도 함께 진행한다.

설치하기 전에는 항상 아래 내용이 설치되었는지 확인한다.

sudo apt update
sudo apt install build-essential -y

4.1.1 Opensearch 다운로드

다운로드 링크 에서 자신의 환경에 맞는 파일을 다운로드 받는다.

  • 실습
    • Opensearch
    • Linux
    • x64 / tar.gz

4.1.2 unpack

# x64
wget https://artifacts.opensearch.org/releases/bundle/opensearch/2.4.0/opensearch-2.4.0-linux-x64.tar.gz

# ARM64
wget https://artifacts.opensearch.org/releases/bundle/opensearch/2.4.0/opensearch-2.4.0-linux-arm64.tar.gz
# x64
tar -xvf opensearch-2.4.0-linux-x64.tar.gz

# ARM64
tar -xvf opensearch-2.4.0-linux-arm64.tar.gz
cd opensearch-2.4.0
export OPENSEARCH_HOME=$(pwd)
  • 이하에서 $OPENSEARCH_HOME 사용.

주요 디렉토리

  • bin : 실행파일들의 위치
  • config : 설정 파일의 위치. 부팅하기 전에 설정을 완료해야한다.
  • data : opensearch 가 데이터를 저장하는 위치
  • jdk : 내장 JDK. Host 에 JDK가 설치되어있지 않으면 사용한다.
  • logs : log 디렉토리
  • plugins : plugin 의 위치

4.1.3 important system settings

DB종류들은 system 세팅이 필요하다.
아래 내용은 실습을 위한 간단한 세팅이므로, 고성능이 필요하다면 따로 검토해야한다.

  1. Disable memory paging and swapping performance on the host to improve performance.
sudo swapoff -a

  1. (선택) Increase the number of memory maps available to OpenSearch.
# Edit the sysctl config file
sudo vi /etc/sysctl.conf

# Add a line to define the desired value
# or change the value if the key exists,
# and then save your changes.
vm.max_map_count=262144

# Reload the kernel parameters using sysctl
sudo sysctl -p

# Verify that the change was applied by checking the value
cat /proc/sys/vm/max_map_count

4.1.4 Opensearch가 사용하는 기본 포트들

Port numberOpenSearch component
443OpenSearch Dashboards in AWS OpenSearch
Service with encryption in transit (TLS)
5601OpenSearch Dashboards
9200OpenSearch REST API
9250Cross-cluster search
9300Node communication and transport
9600Performance Analyzer

4.1.5 설정

1. Open opensearch.yml

vi $OPENSEARCH_HOME/config/opensearch.yml

2. Add the following lines.

# Bind OpenSearch to the correct network interface. Use 0.0.0.0
# to include all available interfaces or specify an IP address
# assigned to a specific interface.
network.host: 0.0.0.0

# Unless you have already configured a cluster, you should set
# discovery.type to single-node, or the bootstrap checks will
# fail when you try to start the service.
discovery.type: single-node

# If you previously disabled the security plugin in opensearch.yml,
# be sure to re-enable it. Otherwise you can skip this setting.
# plugins.security.disabled: true

3. Save your changes and close the file.

4. Specify initial and maximum JVM heap sizes.

  • a. Open jvm.options.
vi $OPENSEARCH_HOME/config/jvm.options
  • b. JVM heap 사이즈의 초기값과, max 값을 정한다. 자신의 EC2 서버 메모리의 절반을 추천한다. 특별한 요구사항이 없다면 xms와 xmx는 같은 값을 가지는 것이 모니터링 하고 관리하기 좋다.
-Xms128m
-Xmx128m
  • 실습용 EC2의 용량에 맞추어 조절한다.

5. Specify the location of the included JDK.

export OPENSEARCH_JAVA_HOME=$OPENSEARCH_HOME/jdk

4.1.6 plugin 설치하기(선택)

Opensearch의 다양한 기능들은 plugin 방식으로 개발되어있다. Opensearch 기본은 데이터 저장과 인덱싱 방법을 제공하는 것이고, 그것을 활용하는 로직은 plugin 에 구현되어 있다.

플러그인 매뉴얼 ( + 지원가능한 플러그인 리스트)

다음 명령어들을 활용한다. sudo 권한이 있어야 한다. 내부적으로 maven 을 이용해서 설치한다.

  • bin/opensearch-plugin list
  • bin/opensearch-plugin install $plugin-name
  • bin/opensearch-plugin remove $plugin-name

설치한 플러그인은 $OPENSEARCH_HOME/plugins 디렉토리에 설치된다.


4.1.7 플러그인 제거

원활한 실습을 위해서 security 관련 플러그인을 제거한다.

bin/opensearch-plugin remove opensearch-security
bin/opensearch-plugin remove opensearch-security-analytics

4.1.8 실행

$OPENSEARCH_HOME 에서
./bin/opensearch 로 실행한다.


확인

curl -X GET http://$IP:9200

결과

{
	"name" : $YOUR_HOSTNAME,
	"cluster_name" : "opensearch",
	"cluster_uuid" : "pS8BqBFcTiyQBr62148MxA",
	"version" : {
		"distribution" : "opensearch",
		"number" : "2.4.0",
		"build_type" : "tar",
		"build_hash" : 		"744ca260b892d119be8164f48d92b8810bd7801c",
		"build_date" : "2022-11-15T04:42:29.671309257Z",
		"build_snapshot" : false,
		"lucene_version" : "9.4.1",
		"minimum_wire_compatibility_version" : "7.10.0",
		"minimum_index_compatibility_version" : "7.0.0"
	},
"tagline" : "The OpenSearch Project: https://opensearch.org/"
}

4.1.9 systemctl 로 system service 로 등록하기

설정파일

sudo vi /etc/systemd/system/opensearch.service

내용

[Unit]
Description=OpenSearch
Wants=network-online.target
After=network-online.target

[Service]
Type=forking
RuntimeDirectory=data

WorkingDirectory=$YOUR_OPENSEARCH_HOME
ExecStart=$YOUR_OPENSEARCH_HOME/bin/opensearch -d

User=$YOUR_USER
Group=$YOUR_USER
StandardOutput=journal
StandardError=inherit
LimitNOFILE=65535
LimitNPROC=4096
LimitAS=infinity
LimitFSIZE=infinity
TimeoutStopSec=0
KillSignal=SIGTERM
KillMode=process
SendSIGKILL=no
SuccessExitStatus=143
TimeoutStartSec=75

[Install]
WantedBy=multi-user.target
  • $YOUR_OPENSEARCH_HOME 을 자신의 경로에 맞게 수정한다.
  • $YOUR_USER 도 자신이 사용할 유저에 맞게 수정한다. $YOUR_OPENSEARCH_HOME 에 대한 소유권한 ( chown )이
    $YOUR_USER에게 있어야 한다.

등록

sudo systemctl daemon-reload
sudo systemctl enable opensearch.service

systemctl로 opensearch 시작

sudo systemctl start opensearch.service
ps -ef | grep opensearch

로그 확인

tail -f $OPENSEARCH_HOME/logs/opensearch.log
profile
Data Analytics Engineer 가 되

0개의 댓글