Promethus + Grafanaでモニタリング+ 可視化する
0. Grafanaとは
あまり説明しないので以下の記事を参照してください。
1. Promethusとは
公式サイト
Promethusとは
オープンソースのモニタリングシステムと時系列データベース
のことです。Golang
で書かれていて、2012年から開発が盛んに行われているそうです。
また、Kubernetesに続いて二番目にCloud Native Computing Foundation
に採択されました。
Home Page - Cloud Native Computing Foundation
1.1 特徴
<metric name>{<label name>=<label value>, ...}
で一意に決まる高次元データモデル。- クエリ言語の柔軟性
- 分散ストレージには依存しない自律シングルノード
- HTTPによるPULL型の時系列データの収集
- 時系列データのPUSHはゲートウェイを通して即座に反映される
- ターゲットは静的な設定か、サービス探索によって発見される
- いくつものグラフ、ダッシュボードがサポートされている
1.2 構成要素
主に以下の構成要素で構築されます。
- Promethus Server: 時系列データを消去したり、保存したりする
- Client Library: アプリケーションコードと一緒に合わせるために使用
- Push Gateway: 局所的なジョブを担う
- Exporters: Graphite, HAProxyなどのメトリクスを取得してくれるサービス。自作もできるしサードパーティのものが多くある。
- Alert Manager: アラートのハンドリングをするもの
1.3 アーキテクチャ
以下のようなアーキテクチャになっています。
この例ではPromethus ServerはKubernetesクラスタの外にありますが、必ずしもそうである必要はありません。
1.4 ユースケース
時系列データを扱うものならなんでもフィットする。
また、スタンドアローンであり、インフラ構築も容易である。
しかし、Promethusが重んじるのは信頼性で、常に可視化することが大事です。しかし、精度は100%ではないので注意が必要です。
1.5 他の時系列データベースとの比較
以下のページで紹介されています。
2. ローカルで試す
2.1 docker-composeで起動する
以下のリポジトリをクローンしてきて、バージョン2にセットアップします。
git clone https://github.com/vegasbrianc/prometheus.git cd prometheus git checkout version-2
そして以下のコマンドでコンテナ群を走らせます。
docker-compose up
2.2 PromethusのWebUIを触る
実際のプロダクションでは、設定を自分で書かないといけないのでdocker-composer.yml
をベースに解説しながら進めます。
以下のservices
以下のpromethus
が参考になります。
prometheus: image: prom/prometheus:v2.1.0 volumes: - ./prometheus/:/etc/prometheus/ - prometheus_data:/prometheus command: - '--config.file=/etc/prometheus/prometheus.yml' - '--storage.tsdb.path=/prometheus' - '--web.console.libraries=/usr/share/prometheus/console_libraries' - '--web.console.templates=/usr/share/prometheus/consoles' ports: - 9090:9090 links: - cadvisor:cadvisor - alertmanager:alertmanager depends_on: - cadvisor networks: - back-tier restart: always
image
はprom/prometheus:v2.1.0
です。
また、volumes
は以下のように定義されています。
"./prometheus/:/etc/prometheus/", "prometheus_data:/prometheus"
Docker-composeでは
ホストのパス:コンテナのパス
でマウントすることができます。
つまり、/prometheus
以下のファイルをコンテナの/etc/prometheus
にマウントしています。
また、Portは以下の9090:9090
で設定されているので
localhost:9090
で見ることができます。
あとはdocker-composer
ファイル自体は説明がいらないと思います。
2.3 Grafanaも見えることを確認する
もちろん設定をしていないので何もグラフは見れませんがlocalhost:3000
で見ることができます。
- ユーザー名:
admin
- パスワード:
foobar
3. Promethusの設定
3.1 設定ファイル
以下のパスに設定ファイルがあります。
.promethus/promethus.yml
3.2 Config
それでは設定していきます。
3.2.1 グローバルな設定
global
で設定します。
global: "scrape_interval": "15s", "evaluation_interval": "15s", "external_labels": "monitor": "my-project"
3.2.2 アラートの設定
アラートはどのような時にアラートを出すかというrule_filesと、そのアラートの設定が入ったalertを設定します。
rule_files: - 'alert.rules' # - "first.rules" # - "second.rules" alerting: alertmanagers: - scheme: http static_configs: - targets: - "alertmanager:9093"
3.2.3 PULLの設定
どこに対してデータを取得するかを設定するのが以下のscrape_configs
です。
scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s static_configs: - targets: ['localhost:9090'] - job_name: 'cadvisor' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s dns_sd_configs: - names: - 'tasks.cadvisor' type: 'A' port: 8080 # static_configs: # - targets: ['cadvisor:8080'] - job_name: 'node-exporter' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s dns_sd_configs: - names: - 'tasks.node-exporter' type: 'A' port: 9100 # static_configs: # - targets: ['node-exporter:9100']
4. Grafanaの設定
4.1 Datasourceの追加
Add datasource
で追加する。
4.2 ダッシュボードを作成する
4.1で作成したPromethusのDatasourceを設定すると追加することができる。
5. クリーンアップ
コンテナ群を終了させます。