Prometheus Grafana with Docker

์žญ์žญ์ดยท2021๋…„ 4์›” 15์ผ
1

์•Œ๋ฉด ์œ ์šฉํ•œ ์ง€์‹

๋ชฉ๋ก ๋ณด๊ธฐ
3/5
post-thumbnail

Prometheus Grafana with Docker

๐ŸŽ ๋ชฉ์ฐจ

0. ๊ฐœ์š”

์ฃผ๊ธฐ์ ์œผ๋กœ ์Œ“์ด๋Š” metric data๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค.
ํ˜„ ์˜ˆ์ œ์—์„œ ํ™˜๊ฒฝ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • Metric data: Spring-boot
  • Time series DB: Prometheus
  • Monitoring tool: Grafana

Spring-boot์—์„œ metric data๋ฅผ ์ œ๊ณตํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.
(์ด ๋ถ€๋ถ„์ด ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ๊ฒ€์ƒ‰ํ•ด๋ณด์ž. ์–ด๋ ต์ง€ ์•Š๋‹ค ๐Ÿ˜…)

1. Pull docker image

Prometheus์™€ Grafana docker image๋ฅผ ์ค€๋น„ํ•œ๋‹ค.

docker pull prom/prometheus
docker pull grafana/grafana

mkdir prometheus-data grafana-data

2. Create prometheus yml

Prometheus์—์„œ ์ „๋‹ฌ๋ฐ›์„ spring-boot ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

# prometheus.yaml

global:
  scrape_interval:     15s 
  evaluation_interval: 15s 

scrape_configs:
  - job_name: 'Controller'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
    - targets: ['$SPRINGBOOT_IP:PORT']

3. Set defaults.ini

๊ทธ๋ผํŒŒ๋‚˜์—์„œ ์‚ฌ์šฉํ•  defaults.ini๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

# defaults.ini

allow_embedding = true

4. Create docker-compose.yml

Prometheus์™€ Grafana๋ฅผ ์‹คํ–‰ํ•  docker-compose.yaml์„ ์ž‘์„ฑํ•œ๋‹ค.

# docker-compose.yaml

version: '3'
services:
  prometheus:
    container_name: prometheus
    image: prom/prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - ./prometheus_data:/prometheus
    ports:
      - 9090:9090
  
  grafana:
    container_name: grafana
    image: grafana/grafana
    depends_on:
      - prometheus
    ports:
      - 9091:3000
    volumes:
      - ./defaults.ini:/usr/share/grafana/conf/defaults.ini
      - ./grafana_data:/var/lib/grafana
    restart: always

5. Start

docker-compose up -d

6. Grafana setting

Grafana์—์„œ Prometheus์˜ data๋“ค์„ ๊ฐ€์ ธ์˜ค๊ณ , template๋ฅผ ํ™œ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค.

  1. {host_url}:9091 ์ ‘์†
  2. Configure -> Data sources -> Add data source -> Prometheus ์„ ํƒ
  3. url: host.docker.internal:9090 ์ž…๋ ฅ ํ›„ Save&Test์œผ๋กœ ํ™•์ธ
  4. Create -> Import -> Upload JSON file -> jvm-micrometer_rev0.json ์„ ํƒ
  5. Prometheus -> ์ƒ์„ฑํ•œ data source ์„ ํƒ -> Import

jvm-micrometer_rev0.json

0๊ฐœ์˜ ๋Œ“๊ธ€