이전에 시스템 메트릭 모니터링 환경을 간단히 구축하면서 프로메테우스(Prometheus)와 그라파나(Grafana)를 설치하여 시스템 메트릭에 대한 가벼운 대시보드를 구축해본 적이 있었다.
현재 ELK(Elasticsearch Logstash Kibana) Stack에 대해 공부하는 중, Filebeat와 Filebeat의 모듈들을 이용하여 Kibana에서 매우 간편하게 대시보드를 통한 시각화가 가능함을 확인해보았다.
추후에 모니터링 시스템을 구축하기 위해서 ELK 자체만을 사용하는 것이 가능하지만,
시스템 메트릭은 Prometheus & Grafana를 통해서,
시스템 로그는 ELK를 통해서 수집 및 모니터링 환경을 구축하고 싶을 것 같아 미리 아래의 아키텍처를 통해 연습삼아 환경을 구축해보고자 한다.
목표는 다음과 같다.
- Nginx를 통해 가상의 웹서비스를 제공하는 호스트(Service Node)에서 Filebeat와 Node Exporter를 통한 로그 및 시스템 메트릭 수집
- MGMT Node에서는 MySQL의 로그를 수집하고 시스템 메트릭을 수집
- 모든 로그는 Filebeat의 모듈들을 통해 ElasticSearch에게 전달되어 최종적으로 Kibana의 대시보드를 통해 시각화
- 모든 시스템 메트릭은 Prometheus로 모아져서 Grafana의 대시보드를 통해 시각화
로그 모니터링 환경은 ELK로, 시스템 메트릭 모니터링 환경은 Prometheus와 Grafana를 이용한 환경을 만드는 것이 목적이다.
Logstash는 지금 단계에선 구성하지 않고, Filebeat에서 직접 ElasticSearch로 로그를 전달하는 방법을 사용할 예정이다.
만약 Kibana와 Grafana에서 Dashboard에 대한 API를 제공받을 수 있다면, 두 개의 서비스의 대시보드를 하나의 페이지에 모아 띄워주는 방법까지도 도전해볼 계획이다.
공통 구성
시작하기에 앞서, 호스트들의 구성 환경에 대해 정리한다.
MGMT Node
- OS: ubuntu-20.04.6-desktop-amd64.iso
- CPU: 4
- Memory: 8GB
- Storage: 40GB
- root & user1 사용자 사용
Service Node
- OS: ubuntu-20.04.6-desktop-amd64.iso
- CPU: 2
- Memory: 4GB
- Storage: 20GB
- root & user1 사용자 사용
최초 세팅
sudo passwd root # 루트 비밀번호 재설정
sudo apt install -y curl git wget vim ssh # 기본적으로 필요한 패키지 설치
ufw disable # 방화벽 해제
systemctl enable ssh # ssh 활성화
모니터링 노드(MGMT) 구성 순서
MGMT Node의 구성은 아래와 같은 순서로 진행할 계획이다.
- Elasticsearch & Kibana 설치
- Filebeat 설치 - Kibana Log로 테스트
- Node Exporter & Prometheus 설치
- Grafana 설치
- Mysql 설치 및 Filebeat 모듈 구성
좀 과정이 많지만 천천히 수행해보자.
ElasticSearch & Kibana 설치
우선, ElasticSearch와 Kibana는 8버전이 아닌 7버전대를 설치하여 사용한다. 아래의 공식 문서를 통해 설치하는 방법을 확인할 수 있다.
Install Elasticsearch with Debian Package | Elasticsearch Guide [7.17] | Elastic
Startup timeouts with older systemd versions By default Elasticsearch sets the TimeoutStartSec parameter to systemd to 900s. If you are running at least version 238 of systemd then Elasticsearch can automatically extend the startup timeout, and will do so
www.elastic.co
이번 ELK 환경 구축에서 사용할 버전은 7.17.11 이다.
-------------- es, kibana 설치 ----------------------
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
sudo apt install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-7.x.list
sudo apt update
sudo apt install elasticsearch
sudo systemctl enable elasticsearch --now
sudo apt install kibana
sudo systemctl enable kibana --now
우선 Elastic 스택의 다운로드를 위한 공개인증 키를 기기에 등록하고, 의존성 패키지를 설치한다.
이후 7.17 버전을 위한 패키지 저장소를 추가한 후 ElasticSearch와 Kibana를 설치한다.
ElasticSearch와 Kibana의 설치가 완료된 이후 이를 Localhost 뿐만 아니라 Host OS에서도 접근 가능하도록 추가적인 포트 설정을 진행한다.
Kibana는 정상적으로 동작하므로, ElasticSearch를 확인하는데, 아래와 같은 경고 문구를 확인할 수 있었다.
위의 경로를 해결하기 위해선 ElasticSearch의 내장 보안 기능을 활성화해야하는데, 이를 수행하고자한다면 아래의 글을 참고할 수 있다. 블로그 멀티버스
[ELK]Elasticsearch built-in security features are not enabled. 경고(문제) 해결 - 사진 O
데스크탑에서 ELK 환경을 구성하던 도중 설치 이후 Kibana Dev Tools을 통해 ElasticSearch와의 연동이 정상적인지 확인하던 와중 아래와 같은 경고 메시지를 발견하였다. 해당 메시지를 확인해보면 다음
alive-wong.tistory.com
아무튼 본인은 위의 기본 보안 설정을 수행한 이후 구성 과정을 진행할 계획이니, 이 점을 고려해주시길 바란다.
Filebeat 설치 - Kibana Logs 연습
Filebeat를 설치하는 방법은 Kibana에서 확인 가능한 방법을 이용해보았다. Kibana 자체의 문서를 통해 Kibana Logs를 수집하는 예제(?)로 Filebeat의 설치 및 모듈 구성을 실시했다.
위의 사진으 흐름대로 선택하면 바로 위의 설치 방법을 안내해준다. 이를 정리해보면 아래와 같다.
1. Filebeat 다운로드 및 설치
curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.17.11-amd64.deb # 설치 파일 다운로드
sudo dpkg -i filebeat-7.17.11-amd64.deb # Filebeat 설치
2. 연결을 위한 설정 - 기본 보안 설정을 할 경우 elastic 사용자의 비밀번호를 입력하면 된다.
output.elasticsearch:
hosts: ["<es_url>"]
username: "elastic"
password: "<password>"
setup.kibana:
host: "<kibana_url>"
3. Kibana 모듈 활성화 및 구성
sudo filebeat modules enable kibana
4. Kibana Dashboard 반영 및 Filebeat 수집 시작
sudo filebeat setup -e # Kibana Dashboard 재구성
sudo filebeat -e # filebeat 실행
여기서 -e 옵션은 CLI 환경에서 로그를 살펴보겠다는 의미이다.
위와 같이 filebeat를 통해 정상적으로 kibana의 로그가 수집되었음을 확인할 수 있다.
추가적인 모듈 설정을 통해서 MySQL, Nginx의 로그를 가져오도록 설정만 수행하면 로그 수집이 가능한 상황이다.
Prometheus & Node Exporter 설치
Prometheus와 Node Exporter는 공식 페이지에서 가장 최신의 버전을 설치했고(각각 2.45.0, 1.6.0),
서비스 데몬이 없는 Prometheus와 Node Exporter에 대해서 서비스 제공을 위한 전용 사용자(prometheus)와 실행 바이너리 파일을 서비스 데몬에 등록하는 과정을 거쳐서 구성해주었다.
우선 Prometheus를 설치하고 서비스 데몬에 등록시키는 과정이다.
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
tar xvfz prometheus-*
sudo useradd -r -s /bin/false prometheus
sudo mkdir -p /usr/share/prometheus/bin
sudo mv prometheus-2.45.0.linux-amd64/prometheus /usr/share/prometheus/bin/
sudo mv prometheus-2.45.0.linux-amd64/promtool /usr/share/prometheus/bin/
sudo chown -R prometheus:prometheus /usr/share/prometheus
sudo chown prometheus:prometheus /usr/share/prometheus/bin/prometheus
sudo chown prometheus:prometheus /usr/share/prometheus/bin/promtool
sudo mkdir /var/lib/prometheus
sudo chown prometheus:prometheus /var/lib/prometheus
sudo cp prometheus-2.45.0.linux-amd64/prometheus.yml /etc/prometheus.yml
sudo chown prometheus:prometheus /etc/prometheus.yml
sudo tee /etc/systemd/system/prometheus.service << EOF
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/share/prometheus/bin/prometheus --config.file=/etc/prometheus.yml --storage.tsdb.path=/var/lib/prometheus
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
echo " - job_name: 'web'
static_configs:
- targets: ['192.168.1.101:9100']
- job_name: 'MGMT'
static_configs:
- targets: ['192.168.1.200:9100']" >> /etc/prometheus.yml
sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus
rm -rf prometheus-*
다음은 Node-Exporter를 설치하고 서비스 데몬에 등록시키는 과정이다.
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.0/node_exporter-1.6.0.linux-amd64.tar.gz
tar xvfz node_exporter-*
sudo mkdir -p /usr/share/node_exporter/bin
sudo chown prometheus:prometheus /usr/share/node_exporter/bin/
sudo mv node_exporter-1.6.0.linux-amd64/node_exporter /usr/share/node_exporter/bin/
sudo chown prometheus:prometheus /usr/share/node_exporter/bin/node_exporter
sudo tee /etc/systemd/system/node_exporter.service << EOF
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/share/node_exporter/bin/node_exporter
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=default.target
EOF
sudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporter
rm -rf node_exporter-*
전체 과정을 하나로 보여주기 위해 다소 길어졌는데, 큰 갈래는 아래와 같다.
- wget으로 파일 압축파일 다운로드 및 해제
- /usr/share 및에 실행 파일 이동
- 전용 사용자에게 해당 파일들의 권한 부여
- 서비스 데몬 생성
- 환경 설정 - Prometheus만
위의 과정을 수행해주면 Node Exporter는 기본 포트 9100에서, Prometheus는 기본 포트 9090에서 서비스를 제공한다. 아래와 같이 정상적으로 동작한다.
Grafana 설치
grafana 설치는 매우 간단해서 별도의 설명을 적진 않겠다. 아래는 공식 문서이다.
Install Grafana on Debian or Ubuntu | Grafana documentation
Enterprise Open source Install Grafana on Debian or Ubuntu This topic explains how to install Grafana dependencies, install Grafana on Linux Debian or Ubuntu, and start the Grafana server on your Debian or Ubuntu system. There are multiple ways to install
grafana.com
sudo apt-get install -y apt-transport-https
sudo apt-get install -y software-properties-common wget
sudo wget -q -O /usr/share/keyrings/grafana.key https://apt.grafana.com/gpg.key
echo "deb [signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
sudo apt-get update
sudo apt-get install grafana
Grafana 설치 이후 서비스 데몬을 재시작해주는 과정이 필요하다. 데몬 재시작하고 그라파나 서버를 활성화하자.
systemctl daemon-reload
systemctl restart grafana-server.service
systemctl status grafana-server.service
Grafana는 기본 포트 3000번에서 서비스를 제공한다. - 기본 계정 및 비밀번호 admin/admin이다.
MySQL 설치
MySQL 또한 별도의 설정 없이 apt 패키지 매니저를 이용하여 사용하였다.
sudo apt-get install mysql-server
설치 끝이다.
기본적으로 MySQL을 설치하면 error.log에 대해서만 생성하고 관리하는데, 추가적인 정보를 위해 slowlog를 수집하기 위한 설정을 진행했다.
이후 MySQL을 재실행하고, mysql 사용자의 /var/log/mysql 디렉토리에 권한을 부여하면 된다.
sudo systemctl restart mysql
sudo chown mysql.mysql /var/log/mysql
마지막으로 로그 위치를 확인해보면 다음과 같다.
결론
지금까지 모니터링 노드를 구성한 결과를 다이어그램으로 표현해보면 아래와 같다.
다음 절차는 Service Node를 구성하고,
Filebeat를 통한 로그 수집 & Kibana를 통한 모니터링, Prometheus를 통한 시스템 메트릭 수집 & Grafana를 통한 모니터링 환경을 구성해볼 계획이다.
'Infra > Etc' 카테고리의 다른 글
[ELK + Prometheus & Grafana] 로그 & 시스템 모니터링 환경 구축 - 3. 대시보드 통합 및 연결 (Ubuntu 20.04) (0) | 2023.07.06 |
---|---|
[ELK + Prometheus & Grafana] 로그 & 메트릭 모니터링 환경 구축 - 2. 서비스 노드 구성 및 완료 (Ubuntu 20.04) (0) | 2023.07.04 |
[ELK]Elasticsearch built-in security features are not enabled. 경고(문제) 해결 - 사진 O (0) | 2023.07.03 |
SSH KEY-PAIR 실습(CentOS7) (0) | 2023.06.10 |
SSH KEY-PAIR 개념(비대칭 키 인증 방식) (2) | 2023.05.20 |