かんがるーさんの日記

最近自分が興味をもったものを調べた時の手順等を書いています。今は Spring Boot をいじっています。

Spring Boot 1.5.x の Web アプリを 2.0.x へバージョンアップする ( その14 )( Docker で Prometheus+Grafana の環境を構築して Spring Actuator で収集したメトリックスを表示する )

概要

記事一覧はこちらです。

Spring Boot 1.5.x の Web アプリを 2.0.x へバージョンアップする ( その13 )( Remember Me 認証が使えなくなっていたので調査・修正する ) の続きです。

  • 今回の手順で確認できるのは以下の内容です。
    • Docker で Prometheus+Grafana の環境を構築して Spring Actuator で収集したメトリックスを表示させてみます。

参照したサイト・書籍

目次

  1. Prometheus の環境を構築する
    1. prometheus.yml を作成する
    2. docker-compose.yml を作成する
    3. prometheus を起動する
    4. 動作確認
  2. Grafana の環境を構築する
    1. docker-compose.yml に Grafana の設定を追加する
    2. Grafana を起動する
    3. 設定&動作確認
  3. 次回は。。。

手順

Prometheus の環境を構築する

prometheus.yml を作成する

プロジェクトのルートディレクトリの下に docker/prometheus のディレクトリを作成し、その下に prometheus.yml というファイルを新規作成して以下の内容を記述します。

global:
  scrape_interval: 15s # By default, scrape targets every 15 seconds.
  evaluation_interval: 15s # Evaluate rules every 15 seconds.

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
  scrape_interval: 15s
  static_configs:
  - targets: ['localhost:9090']

- job_name: 'spring-actuator'
  metrics_path: '/actuator/prometheus'
  scrape_interval: 15s
  basic_auth:
    username: actuator
    password: xxxxxxxx
  static_configs:
  # Docker で起動した Prometheus からローカルPCで起動している Spring Boot のアプリケーション
  # にアクセスするので、localhost ではなくローカルPCに割り当てられているIPアドレスを設定する
  - targets: ['172.23.136.33:8080']

docker-compose.yml を作成する

プロジェクトのルートディレクトリの下に docker-compose.yml を新規作成して以下の内容を記述します。

version: '3'

services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    ports:
    - "9090:9090"
    volumes:
    - ./docker/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml

prometheus を起動する

docker-compose up -d コマンドを実行します。

f:id:ksby:20181104001731p:plain

動作確認

http://localhost:9090/ にアクセスすると http://localhost:9090/graph にリダイレクトされて以下の画面が表示されます。

f:id:ksby:20181104163419p:plain

jvm と入力すると Spring Actuator から収集した jvm が含まれるメトリックスが表示されて、

f:id:ksby:20181104163658p:plain

jvm_memory_used_bytes を選択して「Execute」ボタンをクリックすると以下のように表示されます。

f:id:ksby:20181104164000p:plain

「Graph」タブをクリックするとグラフが表示されます(表示間隔をデフォルトの 1h から 15m に変更しています)。

f:id:ksby:20181104164227p:plain

Grafana の環境を構築する

docker-compose.yml に Grafana の設定を追加する

docker-compose.yml に Grafana の設定を追加します。

version: '3'

services:
  ..........

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
    - "3000:3000"

Grafana を起動する

docker-compose down コマンドを実行した後、docker-compose up -d コマンドを実行します。

f:id:ksby:20181104173129p:plain

設定&動作確認

http://localhost:3000/ にアクセスすると http://localhost:3000/login にリダイレクトされてログイン画面が表示されます。

f:id:ksby:20181104173408p:plain

admin / admin を入力した後「Log In」ボタンをクリックすると、今度はパスワードの変更を求められるのでパスワードを入力して「Save」ボタンをクリックします。

f:id:ksby:20181104173727p:plain

「Home Dashboard」画面が表示されるので「Add data source」をクリックします。

f:id:ksby:20181104173906p:plain

以下の画面の値を入力して「Save & Test」ボタンをクリックします。Prometheus は Docker の別コンテナで起動しているので、「HTTP」-「URL」には localhost 以外の IP アドレスを記述します。

f:id:ksby:20181104174556p:plain

画面左側から「Dashboards」-「Home」を選択します。

f:id:ksby:20181104195840p:plain

「New dashboard」をクリックします。

f:id:ksby:20181104200109p:plain

Panel を追加します。「Graph」をクリックします。

f:id:ksby:20181104200252p:plain

「Panel Title」-「Edit」を選択します。

f:id:ksby:20181104200444p:plain

jvm_memory_used_bytes を入力した後、画面右上の「Back to dashboard」ボタンをクリックします。

f:id:ksby:20181104200701p:plain

Panel 上に jvm_memory_used_bytes のメトリックスのグラフが表示されます。

f:id:ksby:20181104200924p:plain

グラフの表示期間がデフォルトでは Last 6 hours になっているので Last 1 hour に変更し、かつ自動リフレッシュされるようにします。画面右上の「Last 6 hours」と表示されているところをクリックした後、「Custom range」-「Refreshing every」で「5s」を選択して「Apply」ボタンをクリックし、「Quick ranges」で「Last 1 hour」をクリックします。

f:id:ksby:20181104201755p:plain

f:id:ksby:20181104201443p:plain

次回は。。。

Prometheus+Grafana が面白い!! もう少しいろいろ試してみたいので、Prometheus+Grafana で jvm 以外のデータを表示させてみます。

履歴

2018/11/04
初版発行。