docker -compose ๊ธฐ๋ฐ˜ ๋กœ์ปฌ DB ๋ชจ๋‹ˆํ„ฐ๋ง ๊ตฌ์ถ•(mysqld-exporter + prometheus + grafana)

2025. 1. 25. 00:56ยท๐Ÿ“‚ backend/test

[1] DB monitoring

DB metric ์„ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด ๋กœ์ปฌ ํ™˜๊ฒฝ์—์„œ  mysqld-exporter + prometheus + grapana ๋ฅผ ๊ธฐ๋ฐ˜ํ•œ DB monitoring ๊ตฌ์ถ•ํ•œ ๋‚ด์šฉ์— ๊ด€ํ•ด ๊ธฐ๋กํ•˜๊ณ ์ž ํ•œ๋‹ค.

 

(1) mysql-exporter

Prometheus์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋Š” MySQL/MariaDB์šฉ ๋ฉ”ํŠธ๋ฆญ ์ˆ˜์ง‘ ๋„๊ตฌ์ด๋‹ค. MySQL ์„œ๋ฒ„์˜ ์„ฑ๋Šฅ ๋ฐ ์ƒํƒœ ์ •๋ณด๋ฅผ Prometheus๊ฐ€ ์ˆ˜์ง‘ํ•  ์ˆ˜ ์žˆ๋„๋ก HTTP ์—”๋“œํฌ์ธํŠธ๋ฅผ ํ†ตํ•ด ๋…ธ์ถœํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. InnoDB, ๋ฒ„ํผ ํ’€, ํŠธ๋žœ์žญ์…˜, ์ฟผ๋ฆฌ ์บ์‹œ, ์—ฐ๊ฒฐ ์ƒํƒœ ๋“ฑ ๋‹ค์–‘ํ•œ ์ง€ํ‘œ ์ œ๊ณตํ•˜๋ฉฐ, Prometheus์™€ ์—ฐ๋™ ๊ฐ€๋Šฅํ•ด mysqld-exporter์˜ ๋ฉ”ํŠธ๋ฆญ์„ ๊ฐ€์ ธ์™€ ์‹œ๊ฐํ™” ๋ฐ ๊ฒฝ๊ณ  ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

(2) Prometheus

Prometheus๋Š” ์˜คํ”ˆ์†Œ์Šค ๋ชจ๋‹ˆํ„ฐ๋ง ๋ฐ ์•Œ๋žŒ ์‹œ์Šคํ…œ์œผ๋กœ, ๋ฉ”ํŠธ๋ฆญ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  ์ €์žฅํ•˜๋Š” ์‹œ๊ณ„์—ด(time-series) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ด๋‹ค. ์ฃผ๋กœ ์„œ๋ฒ„, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜, ์ปจํ…Œ์ด๋„ˆ ๋“ฑ์˜ ์„ฑ๋Šฅ์„ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๊ณ , ๋ฌธ์ œ ๋ฐœ์ƒ ์‹œ ์•Œ๋ฆผ์„ ๋ณด๋‚ด๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

 

1. ์ฃผ์š” ํŠน์ง•

โœ” ์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ ๊ธฐ๋ฐ˜ (์‹œ๊ฐ„์— ๋”ฐ๋ผ ๋ณ€ํ™”ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ ๋ฐ ๋ถ„์„)
โœ” Pull ๋ฐฉ์‹์˜ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ (exporter๋กœ๋ถ€ํ„ฐ ์ฃผ๊ธฐ์ ์œผ๋กœ ๋ฉ”ํŠธ๋ฆญ ๊ฐ€์ ธ์˜ค๊ธฐ)
โœ” PromQL (Prometheus Query Language) ์ง€์›
โœ” Grafana์™€ ์—ฐ๋™ ๊ฐ€๋Šฅ (๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™”)
โœ” Alertmanager๋ฅผ ํ™œ์šฉํ•œ ์•Œ๋žŒ ์„ค์ •

 

(3) Grafana

Grafana๋Š” ์˜คํ”ˆ์†Œ์Šค ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™” ๋ฐ ๋ชจ๋‹ˆํ„ฐ๋ง ๋„๊ตฌ์ด๋‹ค.Prometheus, InfluxDB, MySQL, Elasticsearch ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์†Œ์Šค๋กœ๋ถ€ํ„ฐ ๋ฉ”ํŠธ๋ฆญ์„ ๊ฐ€์ ธ์™€ ๋Œ€์‹œ๋ณด๋“œ ํ˜•ํƒœ๋กœ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

1. ์ฃผ์š” ํŠน์ง•

โœ” ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์†Œ์Šค ์ง€์› (Prometheus, MySQL, AWS CloudWatch ๋“ฑ)
โœ” ๋Œ€์‹œ๋ณด๋“œ ๊ธฐ๋ฐ˜ ์‹œ๊ฐํ™” (๊ทธ๋ž˜ํ”„, ์ฐจํŠธ, ํ…Œ์ด๋ธ” ๋“ฑ ์ง€์›)
โœ” ๊ฒฝ๊ณ (Alert) ๊ธฐ๋Šฅ (ํŠน์ • ์กฐ๊ฑด ์ถฉ์กฑ ์‹œ ์•Œ๋žŒ ์ „์†ก)
โœ” ๋‹ค์–‘ํ•œ ํ”Œ๋Ÿฌ๊ทธ์ธ ๋ฐ ํ…œํ”Œ๋ฆฟ ์ง€์›
โœ” ์‚ฌ์šฉ์ž ๊ถŒํ•œ ๊ด€๋ฆฌ ๋ฐ ๋Œ€์‹œ๋ณด๋“œ ๊ณต์œ  ๊ฐ€๋Šฅ

 

2. ์ฃผ์š” ๊ธฐ๋Šฅ

โœ” ๋Œ€์‹œ๋ณด๋“œ : ์—ฌ๋Ÿฌ ํŒจ๋„์„ ์กฐํ•ฉํ•ด ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™”

โœ” ํŒจ๋„(Panel) : ๊ทธ๋ž˜ํ”„, ํ…Œ์ด๋ธ”, ํžˆํŠธ๋งต ๋“ฑ ๋‹ค์–‘ํ•œ ์ฐจํŠธ ์ œ๊ณต

โœ” ํ…œํ”Œ๋ฆฟ ๋ณ€์ˆ˜ : ๋™์  ํ•„ํ„ฐ๋ง์„ ์œ„ํ•œ ๋ณ€์ˆ˜ ์ง€์›

โœ” ์•Œ๋žŒ(Alerting) :  ํŠน์ • ์กฐ๊ฑด ์ถฉ์กฑ ์‹œ Slack, Email ๋“ฑ์œผ๋กœ ์•Œ๋žŒ ์ „์†ก

โœ” ์‚ฌ์šฉ์ž ๊ด€๋ฆฌ : ์กฐ์ง๋ณ„ ์‚ฌ์šฉ์ž ๊ถŒํ•œ ๋ถ€์—ฌ ๊ฐ€๋Šฅ

โœ”ํ”Œ๋Ÿฌ๊ทธ์ธ ์ง€์› : ์ปค์Šคํ…€ ํŒจ๋„ ๋ฐ ๋ฐ์ดํ„ฐ ์†Œ์Šค ํ™•์žฅ ๊ฐ€๋Šฅ

 

 

[2] DB monitoring ๊ตฌ์ถ•

(1) docker compose.yml

๊ฐ ์ปจํ…Œ์ด๋„ˆ๋Š”  docker-network ์—์„œ ์„œ๋ธŒ๋„ท ํ•˜์œ„์˜ ipv4 ์„ ์„ ์–ธํ•˜๊ณ , bridge ์˜ต์…˜์„ ์„ ์–ธํ•ด ์ปจํ…Œ์ด๋„ˆ ๊ฐ„ ํ†ต์‹ ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ๊ตฌ์„ฑ

 

version: '3.8'
services:
  mysql:
    image: mysql:8.0
    ports:
      - "3306:3306"
    volumes:
      - ../concert_mysql:/var/lib/mysql
      - ./rdb/initdb.d:/docker-entrypoint-initdb.d
    environment:
      MYSQL_ROOT_PASSWORD: 'concert2025'
      MYSQL_DATABASE: concert_db
    command:
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
    networks:
      concert-network:
        ipv4_address: 172.10.0.2
  mysqld-exporter:
    image: prom/mysqld-exporter:latest
    volumes:
      - ./mysqld-exporter/.my.cnf:/cfg/.my.cnf
    depends_on:
      - mysql
    command:
      - --config.my-cnf=/cfg/.my.cnf
      - --mysqld.address=172.10.0.2:3306
    ports:
      - "9104:9104"
    networks:
      concert-network:
        ipv4_address: 172.10.0.5
  prometheus:
    image: prom/prometheus
    container_name: prometheus
    volumes:
      - ./prometheus/config/prometheus.yml:/etc/prometheus/prometheus.yml
      - ../grafana-data:/var/lib/grafana
    ports:
      - "9090:9090"
    command:
      - '--web.enable-lifecycle'
      - '--config.file=/etc/prometheus/prometheus.yml'
    networks:
      concert-network:
        ipv4_address: 172.10.0.6
volumes:
  grafana-data:
  mysql-data:
networks:
  concert-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.10.0.0/16
          gateway: 172.10.0.1

 

(2) mysqld-exporter ๋ฅผ ์œ„ํ•œ ์‚ฌ์šฉ์ž ์ถ”๊ฐ€

  • mysql container ๋ฅผ ์ƒ์„ฑ ํ›„, ์‚ฌ์šฉ์ž ์ƒ์„ฑ์„ ์ž๋™ํ™”ํ•˜๊ธฐ ์œ„ํ•ด init.sql ์„ docker-entrypoint-initdb.d ๋กœ ๋งˆ์šดํŠธ ์„ค์ •
# ์‚ฌ์šฉ์ž ์ƒ์„ฑ
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'concert2025' WITH MAX_USER_CONNECTIONS 3;

# ์ƒ์„ฑํ•œ ์œ ์ €์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ ์ ์ ˆํ•œ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ด์ค์‹œ๋‹ค.
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';

 

 

(3) mysql-exporter - .my.cnf ์„ค์ •

  • mysql-exporter ๊ฐ€ ๋ฉ”ํŠธ๋ฆญ์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์‚ฌ์šฉ์ž ๊ณ„์ •๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ
  • command option ์— --mysqld.address ์˜ต์…˜์„ ์„ ์–ธํ•ด ํ†ต์‹ ํ•  ์ฃผ์†Œ ์„ค์ • (mysql container Ipv4:port = 172.10.0.2:3306)
[client]
user=exporter
password=concert2025

 

 

์„ค์ •์„ ์™„๋ฃŒํ•˜๊ณ  docker-compose.yml ์„ ์‹คํ–‰ํ•˜๊ณ  localhost:9104/metrics ํ™•์ธํ•˜๊ธฐ

์œ„์™€ ๊ฐ™์ด metric ์ด ๋ณด์ด๋ฉด ์„ฑ๊ณต์ด๋‹ค.

 

 

(4) prometheus ์„ค์ •

mysqld-exporter ์— ์ „๋‹ฌ๋œ metric ์„ prometheus ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด prometheus.yml ์— mysqld-exporter ipv4(172.10.0.2) ์„ค์ •

global:
  scrape_interval: 15s
  scrape_timeout: 10s
  evaluation_interval: 15s
alerting:
  alertmanagers:
    - static_configs:
        - targets: []
      scheme: http
      timeout: 10s
      api_version: v2
scrape_configs:
  - job_name: prometheus
    honor_timestamps: true
    scrape_interval: 15s
    scrape_timeout: 10s
    metrics_path: /metrics
    scheme: http
    static_configs:
      - targets:
          - localhost:9090
  - job_name: mysqld-exporter
    scrape_interval: 3s
    static_configs:
      - targets:
          - 172.10.0.5:9104 # mysqld-exporter ipv4:port ์„ค์ •

 

(5) grafana - ์ ‘์†

  1. grafana container ์‹คํ–‰ ํ›„, localhost:3000 ์ ‘์†
  2. id/pw ์ž…๋ ฅํ•˜์—ฌ ๋กœ๊ทธ์ธ (default id/pw = admin/admin)

grafana ๋กœ๊ทธ์ธ ํ™”๋ฉด

 

(6) grafana - datasource ์„ค์ •

  • menu > Connections > add new connection > prometheus ์„ค์ •
  • Connection : http//172.10.0.6:9090 (prometheus ipv4:port ์„ค์ •)

 

(7) grafana - dashboard ์„ค์ •

  1. menu > Dashboards > import ํด๋ฆญ
  2. [grafana] MySQL Exporter Quickstart and Dashboard Json Download
    • https://grafana.com/grafana/dashboards/14057-mysql/
  3. Json file > Import dashboard - import via dashboard JSON model ์— ์ถ”๊ฐ€

 

 

 

(8) dashboard ํ™•์ธ

 

 

Reference

  • [dockerdocs] networks.ipam : https://docs.docker.com/reference/compose-file/networks/#ipam
  • [grafana] MySQL Exporter Quickstart and Dashboard :  https://grafana.com/grafana/dashboards/14057-mysql/
  • MySQL monitoring using Prometheus and Grafana | MySQL Exporter : https://www.youtube.com/watch?v=cjIb-lKeN5s

'๐Ÿ“‚ backend > test' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Testcontainers ์‚ฝ์งˆ1 : ํ…Œ์ŠคํŠธ ์ปจํ…Œ์ด๋„ˆ ์žฌ์‚ฌ์šฉ  (0) 2025.01.13
K6 tutorial  (0) 2024.12.04
nGrinder tutorial  (0) 2024.03.19
pinpoint simple summary  (0) 2024.03.08
'๐Ÿ“‚ backend/test' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • Testcontainers ์‚ฝ์งˆ1 : ํ…Œ์ŠคํŠธ ์ปจํ…Œ์ด๋„ˆ ์žฌ์‚ฌ์šฉ
  • K6 tutorial
  • nGrinder tutorial
  • pinpoint simple summary
cooper25
cooper25
  • cooper25
    dev cooper
    cooper25
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (84)
      • ๐Ÿ“‚ backend (34)
        • spring (19)
        • architecture (10)
        • test (5)
      • ๐Ÿ“‚ computer-science (5)
      • ๐Ÿ“‚ programming-language (12)
        • java (12)
      • ๐Ÿ“‚ infra (9)
        • mysql (4)
        • redis (2)
        • message-queue (3)
      • ๐Ÿ“‚ cloud (2)
        • aws (2)
      • ๐Ÿ“‚ frontend (1)
        • react (0)
      • ๐Ÿ“‚ education & lecture (16)
        • ์ธํ”„๋ผ ๊ณต๋ฐฉ (11)
        • ํ•ญํ”Œ ๋ฐฑ์—”๋“œ 7๊ธฐ (5)
      • ๐Ÿ“‚ ai (1)
        • claude (1)
      • ๐Ÿ“‚ etc (2)
        • ํšŒ๊ณ  (1)
        • ์ปจํผ๋Ÿฐ์Šค (1)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    JPA
    gof
    AWS
    spring camp 2025
    spring-batch
    ํšŒ๊ณ 
    spring AOP
    ์Šคํ”„๋ง์บ ํ”„ ํ›„๊ธฐ
    ๋ฐ์ดํ„ฐ ์ค‘์‹ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค๊ณ„
    mysql
    nGrinder
    ํ•ญํ•ดํ”Œ๋Ÿฌ์Šค
    spring
    Redisson
    ๋™์‹œ์„ฑ
    UUID
    ์ธํ”„๋ผ๊ณต๋ฐฉ
    kafka
    ๊ฐ€์ƒ ๋ฉด์ ‘ ์‚ฌ๋ก€๋กœ ๋ฐฐ์šฐ๋Š” ๋Œ€๊ทœ๋ชจ ์„ค๊ณ„
    react
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.6
cooper25
docker -compose ๊ธฐ๋ฐ˜ ๋กœ์ปฌ DB ๋ชจ๋‹ˆํ„ฐ๋ง ๊ตฌ์ถ•(mysqld-exporter + prometheus + grafana)
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”