Prometheus と Grafana で Linux をサクッとモニタリングする


はじめに

Linux などサーバやシステムを長期的に安定運用、管理していく上でリソースモニタリングが必須になってきます。
運用をしているとアプリやミドルウェアのバージョンアップや設定変更、さらには利用状況の変化などによりシステムのレスポンスが低下したり、最悪ダウンするようなことも考えられます。
こういったことが起きた際、重要になってくるのがリソースモニタリングで、これによりいつ、なにが、どのように変化したかを時系列で確認することができ、問題解決の大きなヒントになるでしょう。

Prometheus と Grafana について

よく利用される組み合わせなので詳細はここでは割愛いたしますが、Prometheus は exporter というエージェントが動いているサーバからモニタリングメトリックを取得してきます。取得されたメトリックは Prometheus でも可視化はできますが、さらによい可視化ツールである Grafana を利用します。

これらにより以下のように見栄えもよく使い勝手の良いモニタリングシステムを準備できます。

実現する構成

今回は Oracle Cloud 上に構成をしたいと思います。
想定している構成は以下の通りで、Prometheus と Grafana を同一インスタンスで動かします。監視対象として Linux インスタンスを一つ準備します。

仮想ネットワーク(VCN)やインスタンスの作成については OCI のチュートリアルがあるのでそちらを参考に作成ください。
今回の手順はインスタンス作成後のインストールの部分を中心に記載いたします。

クラウド側の設定ポイントとしては以下でしょうか。
- Security List で Prometheus → node_exporter のポート空け
- インターネットから Grafana へのポート空け

インストール手順

以下の手順はほぼほぼコピペで実行できると思います。

Prometheus サーバのインストール

Prometheus-Grafana サーバに Prometheus サーバをインストールします。

ファイルをダウンロードして展開したものを /opt/prometheus に配置します。

wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz
tar xvf prometheus-2.27.1.linux-amd64.tar.gz
sudo mv prometheus-2.27.1.linux-amd64 /opt/prometheus
sudo chmod 755 /opt/prometheus
sudo chown -R root:root /opt/prometheus

設定ファイルのディレクトリやファイルを配置します。

sudo mkdir /etc/prometheus
sudo ln -s /opt/prometheus/console_libraries /etc/prometheus/console_libraries
sudo ln -s /opt/prometheus/consoles /etc/prometheus/consoles
sudo ln -s /opt/prometheus/prometheus.yml /etc/prometheus/prometheus.yml

systemd のサービスとして登録します。

sudo vi /etc/systemd/system/prometheus.service

以下を記述します。

[Unit]
Description=Prometheus Service
After=network.target

[Service]
Type=simple
EnvironmentFile=-/etc/default/prometheus
ExecStart=/opt/prometheus/prometheus $OPTIONS
PrivateTmp=true
Restart=always

[Install]
WantedBy=multi-user.target

EnviromentFile を作成します。

sudo vi /etc/default/prometheus

以下を記述する。

OPTIONS="--config.file=/etc/prometheus/prometheus.yml  --web.console.libraries=/etc/prometheus/console_libraries --web.console.templates=/etc/prometheus/consoles"

設定ファイルを変更します。

 sudo vi /etc/prometheus/prometheus.yml

以下を記述し、一番下の target をモニタリング対象の IP と node_exporter のポートに変更します。

# 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).

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

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "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'

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

    static_configs:
    - targets: ['Target IP Address:9100']

prometheus を起動させます。

sudo systemctl daemon-reload
sudo systemctl enable prometheus.service
sudo systemctl start prometheus.service

node_exporter のインストール

モニタリング対象の Linux に node_exporter をインストールします。
これによりこの Linux に関する様々なメトリックが取得されます。

ファイルをダウンロードし、展開して /opt/ にコピーします。

wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz

tar xvf node_exporter-1.1.2.linux-amd64.tar.gz

sudo mkdir /opt/node_exporter
sudo cp node_exporter-1.1.2.linux-amd64/node_exporter /opt/node_exporter

node_exporter を管理する systemd サービスを作成します。

sudo vim /etc/systemd/system/node_exporter.service

以下を記述。

[Unit]
Description=Node exporter for prometheus
After=network.target

[Service]
Type=simple
EnvironmentFile=-/etc/default/node_exporter
ExecStart=/opt/node_exporter/node_exporter $OPTIONS
PrivateTmp=true

[Install]
WantedBy=multi-user.target

起動させ、Boot 時に自動起動するようにします。

sudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporter

Prometheus が node_exporter にアクセスするためのポートを開けます。

sudo firewall-cmd --add-port=9100/tcp --permanent
sudo firewall-cmd --reload

Grafana のインストール

パッケージを取得し、インストールします。

wget https://dl.grafana.com/oss/release/grafana-7.5.7-1.x86_64.rpm
sudo yum install -y grafana-7.5.7-1.x86_64.rpm

サービスを起動し、Boot 時に自動起動するようにします。

sudo systemctl start grafana-server.service
sudo systemctl enable grafana-server.service

ブラウザーで Grafana にアクセスするためのポートを空けます。

sudo firewall-cmd --add-port=3000/tcp --permanent
sudo firewall-cmd --reload

Grafana にアクセスしてみます。

管理ユーザである admin でログインします。デフォルトのパスワードは admin です。
その後パスワードの変更を求められるので必ず変更しましょう。

データソースの追加

Grafana が Prometheus にアクセスできるように設定を追加します。

Configuration から Data Source を選択します。

Add data source をクリックします。

Prometheus を選択します。

同一サーバ上で動いているので、URL に http://localhost:9090 を指定します。

画面下の Save & Test をクリックし、working となることを確認します。

ダッシュボードの作成

ここまででの手順により Prometheus が node_exporter からメトリックを取得でき、それらを Grafana から表示できる状態となりました。

ここではメトリックを見るためのダッシュボードを作成していきます。
Grafana ではダッシュボードのテンプレートがたくさん公開されています。

今回は上記で配布されている以下のダッシュボードをインポートして、使おうと思います。
https://grafana.com/grafana/dashboards/11074
https://grafana.com/grafana/dashboards/2747

上記ページにアクセスして、ID をコピーします。

Create から Import を選択します。

先程コピーした ID を以下にコピーして Load をクリックします。

Prometheus を選択して、Import をクリックします。

以下のようにモニタリング対象の IP が表示されれば OK です。

以上、