๋ถ€ํ•˜ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด k6 + Prometheus + Grafana ์ž๋™ ํ™˜๊ฒฝ ๊ตฌ์„ฑํ•˜๊ธฐ (with Docker-compose)

2024. 4. 2. 16:43ใ†Backend

k6๋ฅผ ํ†ตํ•˜๋ฉด ๋ถ€ํ•˜ํ…Œ์ŠคํŠธ๋ฅผ js ๊ธฐ๋ฐ˜ ์Šคํฌ๋ฆฝํŠธ๋กœ ์†์‰ฝ๊ฒŒ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๋‹ค๋งŒ k6๋Š” ๋‹จ์ˆœ ํ…Œ์ŠคํŠธ ๋งŒ์„ ์ˆ˜ํ–‰ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๊ฐ€์ ์ธ ์‹œ๊ฐํ™” ๊ตฌ์„ฑ์„ ์ง„ํ–‰ํ•ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด ๊ตฌ๊ธ€๋ง์„ ํ•ด๋ณด๋ฉด, ๋Œ€๋ถ€๋ถ„์˜ ๋ ˆํผ๋Ÿฐ์Šค์—์„œ ์ˆ˜๋™์œผ๋กœ ์ด๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค. (Grafana UI์— ์ ‘์†ํ•ด์„œ Datasource, Dashboard๋ฅผ ์ง์ ‘ ๋“ฑ๋กํ•˜๋Š” ์‹)

๋”ฐ๋ผ์„œ ๋ถ€ํ•˜ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ชจ๋‘๊ฐ€ ๋ฐฉ๋ฒ•์„ ์ˆ™์ง€ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ, ๊ฐœ์ธ์ ์œผ๋กœ ์ด๋Š” ๋น„ํšจ์œจ์ ์ด๋ผ ์ƒ๊ฐํ•˜์—ฌ ๋ถ€ํ•˜ ํ…Œ์ŠคํŠธ + ์‹œ๊ฐํ™”๋ฅผ ํ•œ๋ฒˆ์— ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜๊ณ ์ž ํ•œ๋‹ค.

๊ฐœ์ธ์ ์œผ๋กœ ๋กœ์ปฌ PC์— ์ด๊ฒƒ์ €๊ฒƒ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•˜์ง€ ์•Š์•„์„œ, ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœํ•œ docker-compose ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.

 

Docker-compose.yml

version: "3.8"

services:
  grafana:
    image: grafana/grafana
    ports:
      - 3000:3000
    volumes:
      - ./grafana-provisioning:/etc/grafana/provisioning
      - ./grafana-dashboard:/dashboard
    depends_on:
      - prometheus
  prometheus:
    image: prom/prometheus
    ports:
      - 9090:9090
    command:
      - --web.enable-remote-write-receiver
      - --enable-feature=native-histograms
      - --config.file=/etc/prometheus/prometheus.yml
  k6:
    image: grafana/k6
    volumes:
      - ./k6-scripts:/scripts
    command: run -o experimental-prometheus-rw /scripts/stress.js
    environment:
      - K6_PROMETHEUS_RW_SERVER_URL=http://prometheus:9090/api/v1/write
      - K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM=true
    extra_hosts:
      - "host.docker.internal:host-gateway"
    depends_on:
      - prometheus

์ž‘์„ฑํ•œ docker-compose.yml ํŒŒ์ผ์€ ์œ„์™€ ๊ฐ™๋‹ค.

๋„์ปค ์ด๋ฏธ์ง€๋Š” ๋ชจ๋‘ ๊ณต์‹ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

๊ฐ ์„œ๋น„์Šค์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋ง๋ถ™์ด์ž๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

grafana ์„œ๋น„์Šค

grafana:
    image: grafana/grafana
    ports:
      - 3000:3000
    volumes:
      - ./grafana-provisioning:/etc/grafana/provisioning
      - ./grafana-dashboard:/dashboard
    depends_on:
      - prometheus

Grafana์— ์ ‘์†ํ•ด์„œ datasource, dashboard ์„ค์ •์„ ์ง์ ‘ํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด Grafana์˜ Provisioning ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ–ˆ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด ๋ณผ๋ฅจ์„ ํ†ตํ•ด provisioning ํŒŒ์ผ๊ณผ ๋Œ€์‹œ๋ณด๋“œ ์šฉ ํŒŒ์ผ์„ ๊ณต์œ ํ•ด์•ผ ํ•œ๋‹ค. ๊ฐ ํด๋” ๊ฒฝ๋กœ์™€ ํฌํ•จ๋˜์–ด์•ผ ํ•˜๋Š” ํŒŒ์ผ ๋‚ด์šฉ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

docker-compose.yml ๊ณผ ๊ฐ™์€ ๊ฒฝ๋กœ์— ๊ตฌ์„ฑํ•ด์•ผ ํ•  ํด๋”

// dashboards.yml
apiVersion: 1

providers:
  - name: "k6 Stress Test"
    orgId: 1
    folder: ""
    type: file
    disableDeletion: false
    updateIntervalSeconds: 10
    options:
      path: /dashboard/k6.json
      foldersFromFilesStructure: true
// prometheus.yml
apiVersion: 1

datasources:
  - name: Prometheus
    type: prometheus
    access: proxy
    url: http://prometheus:9090

์ถ”๊ฐ€์ ์œผ๋กœ k6.json ํŒŒ์ผ๋„ ์ž‘์„ฑํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ด๋Š” ์•„๋ž˜ ๋งํฌ์—์„œ ์šฐ์ธก์˜ Download JSON ๋ฒ„ํŠผ์„ ํ†ตํ•ด ๋‚ด๋ ค ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ๋ณต๋ถ™ํ•˜๋ฉด ๋œ๋‹ค.

 

k6 Prometheus (Native Histograms) | Grafana Labs

Thank you! Your message has been received!

grafana.com

prometheus ์„œ๋น„์Šค

prometheus:
    image: prom/prometheus
    ports:
      - 9090:9090
    command:
      - --web.enable-remote-write-receiver
      - --enable-feature=native-histograms
      - --config.file=/etc/prometheus/prometheus.yml

k6๋ฅผ ํ†ตํ•ด ํš๋“ํ•œ ๊ฒฐ๊ณผ๋ฅผ Prometheus์— ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Prometheus์˜ remote-write ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ ๊ณ ํ•ด์ƒ๋„  ์‹œ๊ฐํ™”๋ฅผ ์œ„ํ•ด์„œ๋Š” native-histograms๋„ ํ•„์š”ํ•˜๋‹ค. command์˜ ๋‚ด์šฉ๋“ค์€ ์ด๋“ค์„ ์„ค์ •ํ•œ ๋’ค Prometheus๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ๋‚ด์šฉ๋“ค์ด๋‹ค.

k6 ์„œ๋น„์Šค

k6:
    image: grafana/k6
    volumes:
      - ./k6-scripts:/scripts
    command: run -o experimental-prometheus-rw /scripts/stress.js
    environment:
      - K6_PROMETHEUS_RW_SERVER_URL=http://prometheus:9090/api/v1/write
      - K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM=true
    extra_hosts:
      - "host.docker.internal:host-gateway"
    depends_on:
      - prometheus

์œ„ ๊ตฌ์„ฑ์—์„œ k6๋Š” ๋กœ์ปฌ์— ๋‹ค์šด ๋ฐ›์•„๋„ ๋˜์ง€๋งŒ ๊ฐœ์ธ์ ์ธ ์„ ํ˜ธ์— ์˜ํ•ด ์ด ๋˜ํ•œ ๋„์ปค ์ปจํ…Œ์ด๋„ˆ๋กœ ์‹คํ–‰์‹œ์ผฐ๋‹ค. ์‹คํ–‰์‹œํ‚ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ปจํ…Œ์ด๋„ˆ์— ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ๋ณผ๋ฅจ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜๋ฉฐ ์•„๋ž˜์ฒ˜๋Ÿผ ์ž‘์„ฑ์ด ํ•„์š”ํ•˜๋‹ค.

docker-compose.yml๊ณผ ๋™์ผํ•œ ๊ฒฝ๋กœ์— k6-scripts ํด๋”๋ฅผ ๊ตฌ์„ฑ

stress.js ํŒŒ์ผ์—๋Š” ์‹ค์ œ ๋ถ€ํ•˜ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ๋‚ด์šฉ์ด ํฌํ•จ๋˜๋ฉด ๋œ๋‹ค.

์ด ๋•Œ extra_hosts ์˜ต์…˜์„ ์ค€ ์ด์œ ๋Š”, ๋ถ€ํ•˜ํ…Œ์ŠคํŠธ ๋กœ์ง์—์„œ localhost์— ๋„์›Œ์ง„ ์„œ๋ฒ„์— host.docker.internal ๊ฒฝ๋กœ๋กœ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. ๋งŒ์•ฝ ํ…Œ์ŠคํŠธํ•  ์„œ๋ฒ„๋„ ํ•จ๊ป˜ ์ปจํ…Œ์ด๋„ˆ๋กœ ์‹คํ–‰์‹œํ‚ค๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด extra_hosts ์˜ต์…˜์€ ๋นผ์ฃผ์–ด๋„ ๋œ๋‹ค.

 

์‹คํ–‰ ๋ฐ ๊ฒฐ๊ณผ

๋‹จ์ˆœํžˆ docker-compose up ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค.

์ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด k6๊ฐ€ ๊ฐœ๋ณ„ ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋ถ€ํ•˜ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ , ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ๋“ค์ด Prometheus - Grafana ํ๋ฆ„์œผ๋กœ ์ „๋‹ฌ๋œ๋‹ค. ๊ทธ ํ›„ localhost:3000์— ์ ‘์†, ๋กœ๊ทธ์ธ์„ ํ•ด์ฃผ๊ณ  (id: admin, pw: admin, ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ์ฐฝ์€ skip) ๋Œ€์‹œ๋ณด๋“œ ์ชฝ์— ๋“ค์–ด๊ฐ€๋ณด๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค!

 

Code Repository

 

GitHub - One-armed-boy/k6-prom-grafana-script

Contribute to One-armed-boy/k6-prom-grafana-script development by creating an account on GitHub.

github.com