とあるインフラエンジニアの日常

インフラ、ネットワークに囲まれた日常をつらつらと書いていきます。時々旨い酒

Prometheusを試してみた。 その1

おひさしぶりです。

皆さんお久しぶりです。色々あって今はネットワーク機器を触りながら運用監視ツールを色々試しています。

そんな中でここ数年話題の監視ツールPrometheusを試したので紹介してみます。

Prometheusとは

Prometheusとはgoogleの監視システムに触発されて、soundcloudのエンジニアが開発した監視ツールで、下記のような特徴があります。

  • Prometheusからデータを取得に行くpull型の監視システム
  • 監視対象にはExporterと呼ばれるアプリをlistenさせておいて、Prometheusがそこにアクセスする
  • goで書かれているため1バイナリをダウンロードして設定を投入だけで使える
  • PromQLを利用して、様々なクエリーをかんたんに書くことができる。
  • Graphanaとの連携で、視覚化が容易にできる。
  • Alertの抑止機能が豊富

https://prometheus.io/

Prometheus関連の大まかなアーキテクチャ

Prometheus自体がしていることはメトリクスを取得→ストレージへの格納を行っているだけです。 GUIやアラート時の通知機能などはwebUIやAlertmanager, pushgatewayなどのモジュールと連携を行うことで実現しています。

f:id:kurokiyokiyo:20170903221606p:plain

https://prometheus.io/docs/introduction/overview/

インストール

今回はGCP上のCentOS7.3に構築を進めていきます。

#download
wget https://github.com/prometheus/prometheus/releases/download/v1.7.1/prometheus-1.7.1.linux-amd64.tar.gz

#解凍
tar zxvf prometheus-1.7.1.linux-amd64.tar.gz

# prometheusを実行ディレクトリに移動
mv prometheus-1.7.1.linux-amd64/prometheus /usr/local/bin/

動かしてみる

かんたんなconfigを作って試してみる。 下記はprometheusが導入されているサーバーのみ監視対象にする設定になる(ダウンロードしたフォルダ内のsimple.ymlを元にすると楽)

vi /etc/prometheus.yml

# my global config
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).

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
      monitor: 'codelab-monitor'

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first.rules"
  # - "second.rules"

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

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ['localhost:9090']

上記設定を作成後、下記コマンド実行。ブラウザでアクセスするとPrometheusの管理画面が参照可能。

prometheus --config.file=/etc/prometheus.yml

f:id:kurokiyokiyo:20170903224619p:plain

Status→targetsで監視対象がUpしているかどうかを確認できる。 f:id:kurokiyokiyo:20170903224718p:plain

GraphからPrometheusにクエリを投入して、データを確認することができる。 f:id:kurokiyokiyo:20170903224947p:plain

毎回起動するのは大変なので。。。

毎回手動でコマンドを入力するのは大変なので、systemctlから起動できるよう、serviceに登録してしまいましょう。

vi /lib/systemd/system/prometheus.service

[Unit]
Description=Prometheus service
After=syslog.service prometheus.service

[Service]
Type=simple
ExecStart=prometheus --config.file=/etc/prometheus.yml
PrivateTmp=true

[Install]
WantedBy=multi-user.target

上記を保存したあと、下記のコマンドで起動できるはず。

# 自動起動を有効
systemctl enable prometheus
# prometheus実行
systemctl start prometheus

次回はnode_exporterを導入して、実際にクエリを書いて見ようと思います。