이전 글
아파치 로그 모니터링 시스템 구축하기 - 개요(ELK)
아파치 로그 모니터링 시스템 구축하기 - Filebeat(ELK)
아파치 로그 모니터링 시스템 구축하기 - Logstash (ELK)
아파치 로그 모니터링 시스템 구축하기 - Logstash filter (ELK)
Elasticsearch란?
분산 문서 저장소로,분산형 RESTful 검색 및 분석 엔진 역할하며, 데이터를 중앙에 저장하여 빠르고 쉽게 검색하고 분석할 수 있도록 한다.
Elasticsearch Data
Elasticsearch는 JSON 문서로 직렬화된 데이터 구조를 저장한다. 클러스터에 여러 개의 Elasticsearch 노드가 있는 경우 , 저장된 문서는 클러스터 전체로 분산되어 있으며 어느 노드에서든 즉지 접근할 수 있다.
문서가 저장되면 인덱싱 되어 빠르게 검색 가능하다. Elasticsearch는 inverted index(역 색인) 라는 데이터 구조 사용하여 매우 빠르게 전체 텍스트 검색을 지원한다. inverted index는 모든 문서에 나타나는 모든 고유한 단어를 나열하고 각 단어가 나타나는 모든 문서를 식별한다.
인덱스는 문서 컬렉션으로, 각 문서는 데이터가 포함된 키-값 쌍인 필드 컬렉션이다. Elasticsearch는 모든 필드의 모든 데이터를 인덱싱하며, 각 인덱싱된 필드에는 전용 최적화된 데이터 구조가 있다.( 텍스트 필드 → 역인덱스 저장 , 숫자 및 지리 필드 → BKD 트리에 저장) 필드별 데이터 구조를 사용하여 검색 결과를 빠르게 찾게 한다.
Elasticsearch는 스키마 없이 사용할 수도 있다.
각 필드를 어떻게 처리할지 지정하지 않고도 문서를 인덱싱할 수 있다. 동적 매핑이 활성화되어 있다면 새로운 필드를 자동으로 감지하고 인덱스에 추가한다. 이는 데이터를 색인화하고 탐색하는 것을 쉽게 만들어준다. 자체 매핑을 정의할 수도 있다.
Searching data
간단한 REST API를 통해 클러스터 관리 및 데이터 색인, 검색할 수 있다.
Elasticsearch의 REST API는 구조화된 쿼리, 전체 텍스트 쿼리 및 두가지를 결합한 복잡한 쿼리를 지원한다.
구조화된 쿼리는 SQL 유형의 쿼리와 유사하다. 인덱스의 필드를 검색해서 일치 항목을 정렬할 수 있다.
전체 텍스트 쿼리는 쿼리 문자열과 일치하는 모든 문서를 찾아 관련성에 따라 정렬하여 검색 용어에 대한 일치 정도를 나타낸다.
그외에 집계를 이용하여 크기 , 평균값 등을 계산할 수 있다.
Elasticsearch 설치
wget <https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.13.2-linux-x86_64.tar.gz>
wget <https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.13.2-linux-x86_64.tar.gz.sha512>
shasum -a 512 -c elasticsearch-8.13.2-linux-x86_64.tar.gz.sha512
tar -xzf elasticsearch-8.13.2-linux-x86_64.tar.gz
Elasticsearch.yml
- Cluster의 이름과 노드를 설정한다.
- localhost에서 단일 노드 하나만 실행했기 때문에 network.host와 http.port는 기본 옵션 그대로 뒀다
- 외부 Elasticsearch 연결과 관련된 TLS을 설정해줬다.(Kibana와 Logstash의 연결 위해.. 그외의 외부접근 포함)
- ssl을 true로 설정해주고, 인증서 파일 위치를 알려준다.
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
...
#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically
# generated to configure Elasticsearch security features on 17-04-2024 03:44:16
#
# --------------------------------------------------------------------------------
# Enable security features
xpack.security.enabled: true
xpack.security.enrollment.enabled: true
# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
enabled: true
keystore.path: path/http.p12
cluster.initial_master_nodes: ["hostname"]
# Allow HTTP API connections from anywhere
# Connections are encrypted and require user authentication
http.host: 0.0.0.0
+) Elasticsearch TLS 글 작성
Index
생성
curl -X PUT "localhost:9200/<인덱스이름>?pretty"
제거
curl -X DELETE "localhost:9200/<인덱스이름>?pretty"
문서 조회
특정 문서 아이디로 조회 (기본)
- pretty는 출력할 때 예쁘게 정렬되어 출력된다
curl -X GET "localhost:9200/인덱스/_doc/문서아이디(_id)?pretty"
나같은 경우는, 보안때문에 인증서 파일과 유저 정보가 필요해서 추가했다.
curl -X GET -u 유저:비밀번호 "localhost:9200/인덱스/_doc/doc아이디(_id)?pretty" --cacert ca.pem
_source 필드에서 id 필드만 조회
curl -X GET "localhost:9200/인덱스/_doc/문서아이디(_id)?_source=*.id&pretty"
Query DSL를 이용해서 특정 필드 조건에 따라 조회
- match 블록 안에 조건의 기준이 되는 필드와 값을 적는다.
curl -X GET "localhost:9200/인덱스/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"필드이름": "찾고자 하는 값"
}
}
}
'
- 예를 들어 아래 코드는 tags 필드 값이 _grokparsefailure이 포함된 문서를 조회한다.
curl -X GET "localhost:9200/인덱스/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"tags": "_grokparsefailure"
}
}
}
'
문서 삭제
curl -X DELETE "localhost:9200/인덱스/_doc/문서아이디?pretty"
문서 수정
- 만약 없는 필드라면, 필드가 추가된다.
curl -X POST "localhost:9200/인덱스/_update/문서아이디?pretty" -H 'Content-Type: application/json' -d'
{
"doc": {
"필드이름": "값"
}
}
'
문서 추가
curl -X POST "localhost:9200/인덱스/_doc?pretty" -H 'Content-Type: application/json' -d '
{
"message": "10.0.15.216 - - [25/Apr/2024:14:56:07 +0000] \\"GET / HTTP/1.1\\" 403 45 \\"-\\" \\"curl/8.5.0\\"",
"host": {
"hostname": "",
"name": "",
"architecture": "x86_64",
"os": {
"name": "Amazon Linux",
"codename": "Amazon Linux",
"type": "linux",
"family": "redhat",
"platform": "amzn",
"version": "2023"
},
"id": "dklfjiowjowvowvwo",
"ip": [
"10.0.126.56",
],
"containerized": false
}
}
'
'ELK' 카테고리의 다른 글
아파치 로그 모니터링 시스템 구축하기 - Logstash (ELK) (1) | 2024.04.26 |
---|---|
아파치 로그 모니터링 시스템 구축하기 - Logstash filter (ELK) (1) | 2024.04.26 |
아파치 로그 모니터링 시스템 구축하기 - Filebeat(ELK) (0) | 2024.04.25 |
아파치 로그 모니터링 시스템 구축하기 - 개요(ELK) (0) | 2024.04.25 |