Neinvalli

研究テーマは ”IT 環境でのリソース最適化” です

Prometheus, Grafana を利用して MySQL の各種パフォーマンスをモニタリングする

前回(http://neinvalli.hatenablog.com/entry/2017/10/31/020644)は Munin についての話でしたが、今回は Prometheus についての話です。

  • Prometheus + Grafana では 15秒ごとのグラフが簡単に作成出来ますので Munin と比較してこの点が強みです。
  • 前回述べたように、5分ごとのグラフだとどうしても瞬発的な現象を捕捉しきれないからです。

Prometheus のインストール関連の情報は下記サイトを参考にしました。

https://knowledge.sakura.ad.jp/11633/

http://mmorita44.hateblo.jp/entry/2017/08/24/123438

http://d.hatena.ne.jp/wyukawa/20160214/1455439298

http://ngyuki.hatenablog.com/entry/2017/11/14/203554

https://libetrada.com/prometheus_healthcheck/

下記インストール関連はすべて Ubuntu 16.04 向けの情報です 下記サンプルのホスト名と役割はそれぞれ下記のとおりです

  • fronttest011 監視される側のサーバー。MySQL やウェブサーバーが動作している全部入りウェブサーバー。このサーバーで Prometheus のエージェントが動作。
  • admintest011 監視する側のサーバー。このサーバーで Prometheus 本体部分が動作。

まずは Node Exporter のインストールと起動

  • Munin でいうところの munin-node です
  • ただし Node Exporter はシステム系のメトリクスしか扱っていません。

https://prometheus.io/download/

公式サイトからダウンロードしてバイナリを起動するだけです

[root@fronttest011 prom]# wget "https://github.com/prometheus/node_exporter/releases/download/v0.15.2/node_exporter-0.15.2.linux-amd64.tar.gz"
[root@fronttest011 prom]# (cd node_exporter-0.15.2.linux-amd64/;  ./node_exporter &)

Prometheus 本体のインストールと起動

Node Exporter とほぼ同じで公式サイトからダウンロードしてバイナリを起動するだけですが、設定ファイルの修正が必要です(下記サンプルは以降の設定に合うようにすでに修正済みのものです)

[root@admintest011 prom]# wget "https://github.com/prometheus/prometheus/releases/download/v2.2.1/prometheus-2.2.1.linux-amd64.tar.gz"
[root@admintest011 prom]# tar xzf prometheus-2.2.1.linux-amd64.tar.gz

[root@admintest011 prom]# cat prometheus-2.2.1.linux-amd64/prometheus.yml
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
#      - localhost:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
#  - "alert_rules.yml"
  # - "first_rules.yml"
  # - "second_rules.yml"

# 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'

    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'node'
    static_configs:
      - targets: ['localhost:9100']
      - targets: ['fronttest011:9100']
  - job_name: 'mysql'
    static_configs:
      - targets: ['fronttest011:9104']



[root@admintest011 prom]# (cd prometheus-2.2.1.linux-amd64/;  ./prometheus --config.file=prometheus.yml &)

その後下記 URL にてアクセス

http://admintest011:9090

Grafana のインストールと起動

公式サイトからダウンロードしてバイナリを起動するだけです

https://grafana.com/grafana/download

[root@admintest011 prom]# wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.0.3.linux-x64.tar.gz
[root@admintest011 prom]# tar xzf grafana-5.0.3.linux-x64.tar.gz
[root@admintest011 prom]# (cd grafana-5.0.3/;  ./bin/grafana-server web &)

http://admintest011:3000/dashboard/

Data Source で Prometheus を設定。 ここで Name に Prometheus と設定している箇所はのちのダッシュボードが参照している名前と関係ありますのでここではこの通りにする必要があります。

f:id:neinvalli:20180319005520p:plain

すでに作成されたダッシュボードのインポートをします。

システム系のグラフでは Node Exporter Server Metrics というダッシュボードが複数台対応もしており使いやすいかと思います https://grafana.com/dashboards/405

インポート方法はインポートのページにて上記ダッシュボードの ID を入力するだけです

http://admintest011:3000/dashboard/import

f:id:neinvalli:20180319005854p:plain

Data Source で Prometheus を選択することを忘れずに。

Node の箇所で複数サーバーを選択すればグラフが横に並んで表示されます

f:id:neinvalli:20180319011932p:plain

他に入れる場合はこちらのページから検索できます

https://grafana.com/dashboards?dataSource=prometheus&search=node

MySQL Exporter のインストールと起動

Prometheus 公式サイトからダウンロードできます

https://prometheus.io/download/#mysqld_exporter

[root@fronttest011 prom]# wget "https://github.com/prometheus/mysqld_exporter/releases/download/v0.10.0/mysqld_exporter-0.10.0.linux-amd64.tar.gz"

MySQL にて下記を実行
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'pass' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';

起動
[root@fronttest011 prom]# (cd mysqld_exporter-0.10.0.linux-amd64;  export DATA_SOURCE_NAME='exporter:pass@(localhost:3306)/';  ./mysqld_exporter &)

MySQL 向けのダッシュボードのインポート

Percona のものを利用します

https://www.percona.com/blog/2016/02/29/graphing-mysql-performance-with-prometheus-and-grafana/

こちらの情報少し古いのか、うまく行きませんでしたので下記の方法で対応しました

https://github.com/percona/grafana-dashboards/tree/master/dashboards

こちらからインポートしたいダッシュボードの JSON ファイルの内容をコピー&ペーストで Prometheus のインポートのページに貼り付けてインポートします

f:id:neinvalli:20180319010202p:plain

表示されるようになりました

f:id:neinvalli:20180319012009p:plain

まとめ

ざっとインストール手順をメインで書きました 私自身他のウェブサイトを参考にインストールしようとしましたが、情報が古かったのかうまく行かない箇所もありましたので、今回私が試した手順をまとめました ここの情報は 2018年3月19日 時点では動作確認とれている手順ですので、何かの参考になれば嬉しいです。

Prometheus の導入理由が「Munin では捕捉しづらい負荷情報を捕捉する」でしたので、次回はその点を言及できればと思います。