발단
테스트 서버의 상태를 모니터링 할 수 있는 도구를 찾던 도중 예전부터 관심있던 Prometheus 와 Grafana 를 도입하여 상태를 확인하고자 합니다. 추후 재사용 될 수 있으니 docker compose 를 통해 사용성을 높일 것입니다.
Prometheus
GitHub - prometheus/prometheus: The Prometheus monitoring system and time series database.
The Prometheus monitoring system and time series database. - prometheus/prometheus
github.com
프로메테우스는 오픈소스 모니터링 및 경고 도구로 특히 시계열 데이터(시간 기반의 정보, 예를들어 13:00 요청 갯수 100개, 13:01 요청 갯수 101 개, .. 등 시간에 따른 정보를 기록)를 수집하고 분석하는 데 중점을 두는 시스템입니다. 다양한 특징이 있고 현시점에 활발하게 사용되는 도구로 여기서는 메트릭(하드웨어나 소프트웨어에 대한 성능 측정을 위한 측정 수치)을 수집하는 도구로 사용할 것입니다.
Exporter
GitHub - prometheus/node_exporter: Exporter for machine metrics
Exporter for machine metrics. Contribute to prometheus/node_exporter development by creating an account on GitHub.
github.com
프로메테우스는 데이터 수집을 위해 Exporter 를 사용합니다. Exporter 는 프로메테우스가 메트릭 수집을 위해 사용하는 도구로 메트릭 정보를 os or application 으로 부터 수집하여 제공하는 역할을 합니다. 여기서는 프로메테우스 에서 공식적으로 제공하고 있는 node 로 구현된 exporter 를 사용하여 메트릭을 수집할 것입니다.
(이 글에서는 예제를 위주로 다루고자 하여 프로메테우스와 Exporter 의 자세한 설명은 공식 문서를 참고하여주세요.)
Grafana
GitHub - grafana/grafana: The open and composable observability and data visualization platform. Visualize metrics, logs, and tr
The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many mo...
github.com
그라파나는 오픈소스 데이터 시각화 및 모니터링 도구로, 다양한 데이터 소스를 연결하여 대시보드 형태로 데이터를 시각화하고 분석할 수 있게 해줍니다. Grafana 는 주로 시계열 데이터를 시각화하고 실시간 모니터링 및 알림 기능을 제공하는데 사용됩니다. 따라서, 데이터를 수집하여 시계열 정보로 기록하는 Promehteus 와 같이 사용할 것 입니다.
사용법
docker compose 를 사용하여 모니터링을 위한 3개의 컨테이너를 같이 구성하여 사용성을 높이고자 합니다.(윈도우즈 wsl2 로 테스트는 host os 가 windows 임으로 실제 linux 서버에서 테스트해야 동작합니다.)
파일 트리
.
└── tutorial-linux-monitoring-prometheus-grafana/
├── prometheus/
│ └── prometheus.yml
└── docker-compose.yml
prometheus/prometheus.yml
global:
scrape_interval: 20s # 데이터 수집 주기
scrape_configs:
- job_name: 'linux_server_info'
# metrics_path: '/metrics' # 메트릭을 수집할 경로를 커스텀 할 수 있음
static_configs:
- targets: ['host.docker.internal:9100'] # 메트릭을 제공받을 서버의 주소
- job_name: prometheus 에서 target check 에 명시될 대상 이름입니다.
- metrics_path: static_configs.targets 에 명시한 속성의 path 입니다.(ex, 위의 경우 host.docker.internal:9100/metrics 로 설정됩니다.)
- static_configs.targets: 메트릭 정보를 수집할 대상 URL 입니다.
docker-compose.yml
volumes:
prometheus-data: # 시계열 데이터 저장을 위해 사용됩니다.
grafana-data: # 대시보드 정보 기록을 위해 사용됩니다.
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
extra_hosts:
- "host.docker.internal:host-gateway" # 호스트 네트워크에 접근하기 위한 설정
volumes:
- prometheus-data:/prometheus
- ./prometheus:/etc/prometheus # prometheus.yml 설정 파일
command:
- "--config.file=/etc/prometheus/prometheus.yml"
- "--web.enable-lifecycle" # Prometheus 서버를 재시작하지 않고도 설정을 다시 읽을 수 있게 함
ports:
- "9090:9090"
restart: unless-stopped
grafana:
image: grafana/grafana:latest
container_name: grafana
environment:
- GF_SECURITY_ADMIN_USER=admin # grafana 사용자 계정
- GF_SECURITY_ADMIN_PASSWORD=admin # grafana 사용자 비밀번호
ports:
- "3000:3000"
restart: unless-stopped
volumes:
- grafana-data:/var/lib/grafana
linux_server_info:
image: quay.io/prometheus/node-exporter:latest
container_name: linux_server_info
command:
- '--path.rootfs=/host'
network_mode: host
pid: host # 컨테이너에서 호스트의 프로세스를 볼 수 있게 함
restart: unless-stopped
depends_on:
- prometheus
volumes:
- '/:/host:ro,rslave' # ro: 읽기전용, rslave: 마운트한 물리 경로의 것을 동기화만 하고 역동기화 하지 않음
- services.prometheus.command."--web.enable-lifecycle": prometheus.yml 을 수정한 뒤 재시작 없이 설정 파일을 reload 할 수 있는 API 를 호출할 수 있게 해줍니다.
- prometheus 컨테이너에 접속하여 `localhost:9090/-/reload` URL 을 POST 형식으로 요청하면 reload 됩니다.
명령어
실행
docker comopse up -d
- d 옵션은 백그라운에서 docker compose 가 실행될 수 있게 합니다.
종료
docker compose down
대시보드 UI 구축
[Monitoring] Prometheus 상태 확인과 Linux 서버의 Grafana dashboard 구축(feat. Grafana Dashboards Template)
발단 [Monitoring] Prometheus & Grafana 를 사용해 Linux 서버의 상태를 모니터링 해보자(feat. docker compose)발단 테스트 서버의 상태를 모니터링 할 수 있는 도구를 찾던 도중 예전부터 관심있던 Prometheus 와
atsky.tistory.com
해당 글에서 환경 구축에 따른 대시보드 UI 구축을 설명하고 있습니다.
'ETC > Monitoring' 카테고리의 다른 글
[Monitoring] Prometheus 상태 확인과 Linux 서버의 Grafana dashboard 구축(feat. Grafana Dashboards Template) (0) | 2025.01.14 |
---|