IT/Container&k8s

[k8s] EFK

Hayley Shim 2023. 10. 29. 00:29

안녕하세요. EFK를 활용한 로그 수집과 분석 과정을 진행하며 해당 내용을 이해하고 공유하기 위해 작성한 글입니다.

MSA 특징

https://microservices.io
  • 정의 : An architectural style that structures an application as a collection of services
  • 함께 작동하는, 독립적으로 배포 가능한 서비스는 비즈니스 도메인 중심의 모델링
  • 네트워크를 통해 서로 통신
  • 데이터베이스는 서비스 경계 내부에 숨겨져 있음
  • 정보 은폐의 원칙
  • 변경 사항을 독립적으로 변경하고 배포
  • 느슨한 결합 : 일부 구현은 이를 어렵게 함. 예) 데이터베이스 공유

Log 수집 분석의 필요성

  • 일반적으로 machine data는 비정형데이터인데 복잡하기 때문에 필요한 부분만 추출해서 정형화시키는 과정이 필요합니다.
  • 데이터는 휘발성이기 때문에 별도로 보관할 필요가 있습니다.

ElasticSearch

간단히 말해 비정형 데이터를 정형화 할 수 있는 도구입니다.

Elasticsearch Stack

Elasticsearch의 Architecture: Cluster 와 Node

Elasticsearch의 Architecture: Shard

Elasticsearch의 Architecture: Replica shards

EFK(Elasticsearch + Fluentd + Kibana)

  • 쿠버네티스 Node에 POD가 배포되는데, 이 POD에 여러 컨테이너가 존재하고 쿠버네티스는 POD가 실행이 안 되면 그 POD를 죽이고 새로 생성합니다.
  • 이때 죽은 POD에 있던 컨테이너가 남긴 로그는 사라지게 됩니다.
  • POD내의 컨테이너의 로그를 남기는 방법
  • > EFK 또는 ELK(Elasticsearch+Logstash+Kibana)

Elasticsearch와 검색엔진의 차이

  • SON 문서 기반 Elasticsearch는 내부적으로는 역파일 색인 구조로 데이터를 저장
  • Elasticsearch는 Rest API를 기본으로 지원하며 모든 데이터 조회, 입력, 삭제를 http 프로토콜을 통해 Rest API로 처리
  • Elasticsearch의 데이터들은 인덱스(Index) 라는 논리적인 집합 단위로 구성되며 서로 다른 저장소에 분산되어 저장하고 서로 다른 인덱스들을 별도의 커넥션 없이 하나의 질의로 묶어서 검색하고, 검색 결과 들을 하나의 출력으로 도출할 수 있는데, Elasticsearch의 이러한 특징을 멀티테넌시 라함

[참고] 역 인덱스 — Inverted Index

Fluent Bit/Fluentd

  • Fluent Bit은 CNCF의 Graduated Project인 Fluentd의 에코시스템 중 하나입니다. CNCF의 다른 Graduated Project로는 Kubernetes, containerd, etcd, helm, Prometheus 등이 있습니다.
  • 따라서 Fluent Bit은 Kubernetes, Prometheus와 같은 다른 CNCF 프로젝트와 함께 사 용하기 용이하며 Elastic.co의 시스템과도 통합을 지원합니다.
  • Fluentd는 Kubernetes와 같은 CNCF 스택이기 때문에 마이크로 서비스의 로깅에 더 적합하고, Ruby언어로 작성되어 있어 Logstash에 비해 메모리를 적게 사용하고 메시 지 손실이 없다고 알려져 있습니다.

Elasticsearch & Kibana Installation(비추천)

 

Fluentd 설치(비추천)

window 설치 경우, wsl 설치 전 Linux용 Windows 하위 시스템, Windows 하이퍼바이저 플랫폼, 가상 머신 플랫폼을 선택해줍니다.(Mac 설치 권장)

1. Increase the Maximum Number of File Descriptors$ ulimit -n 65535/etc/security/limits.conf file and reboot your machine:root soft nofile 65536
root hard nofile 65536
* soft nofile 65536
* hard nofile 655362. Optimize the Network Kernel Parameters/etc/sysctl.conf file:net.core.somaxconn = 1024
net.core.netdev_max_backlog = 5000
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_wmem = 4096 12582912 16777216
net.ipv4.tcp_rmem = 4096 12582912 16777216
net.ipv4.tcp_max_syn_backlog = 8096
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10240 65535
# If forward uses port 24224, reserve that port number for use as an ephemeral port.
# If another port, e.g., monitor_agent uses port 24220, add a comma-separated list of port numbers.
# net.ipv4.ip_local_reserved_ports = 24220,24224
net.ipv4.ip_local_reserved_ports = 24224
Use sysctl -p command or reboot your node3. Use sticky bit symlink/hardlink protectionFluentd sometimes uses predictable paths for dumping, writing files, and so on. This default settings for the protections are in /etc/sysctl.d/10-link-restrictions.conf, or /usr/lib/sysctl.d/50-default.conf or elsewhere. For symlink attack protection, check the following parameters are set to 1:fs.protected_hardlinks = 1 fs.protected_symlinks = 1Use sysctl -p command or reboot your node

Elasticsearch & Kibana Installation(추천)

  • Elasticsearch와 Kibana는 동일한 버전으로 설치해야합니다.
  • 설치환경 : ubuntu 20.04 LTS
 
 

Dockerfile

 

Command

docker-compose up -d

ElasticSearch 무료 체험

https://cloud.elastic.co/home

Elasticsearch + Kibana의 구성

  • documents의 집합 -> index
  • Elasticsearch는 데이터를 통해 column을 만들어 줌(비정형 데이터를 정형화)
EFK Container 확인

Kibana에 접속해봅니다.

Kibana 대시보드

Management > Kibana > Index Patterns 에서 설정해줍니다.

Index patterns

fluentd Index patterns을 생성해줍니다.

웹로그를 분석할 수 있다. 예로 웹방화벽 관련 로그 분석을 할 수 있습니다.

Nginx 앱을 설치해서 fluentd에 로그를 보내줍니다.

# Configuring Kibana Index Pattern

$ docker pull nginx:alpine

# Docker Container with Fluentd Log Driver
$ docker run --name nginx_container -d --log-driver=fluentd -p 8080:80 nginx:alpine
Kibana Discover Page

위와 같이 로그를 확인할 수 있습니다.

Kibana & Kubernetes Monitoring with EFK

아래와 같은 시스템 구성도로 EFK를 구성해봅니다.

Creating the cluster namespace /Headless Service

#1-efk-logging-ns.yamlkind: Namespace
apiVersion: v1
metadata:
  name: efk-logging#Headless Service
#2-elasticsearch-svc.yamlkind: Service
apiVersion: v1
metadata:
  name: elasticsearch
  namespace: efk-logging
  labels:
    app: elasticsearch
spec:
  selector:
    app: elasticsearch
  clusterIP: None
  ports:
    - port: 9200
      name: rest
    - port: 9300
      name: inter-node

Elasticsearch StatefulSet

 

Creating the Service & Kibana Deployment

 

Creating the FluentD DaemonSet

 

Launch Counter Pods & analyze log data

 

 

 

blog migration project

written in 2022.9.21

https://medium.com/techblog-hayleyshim/k8s-efk-be853e44898c

 
 
 
 

'IT > Container&k8s' 카테고리의 다른 글

[k8s] Debugging a k8s cluster  (0) 2023.10.29
[k8s] performance  (0) 2023.10.29
[k8s] Cloud Native PostgreSQL Operator  (1) 2023.10.29
[k8s] Percona Distribution for MongoDB Operator  (2) 2023.10.29
[k8s] Kafka & Strimzi Operator  (0) 2023.10.29