ウェブサイト検索

Debian 11にPrometheusシステム監視ツールをインストールする方法


Debian 11にPrometheusシステム監視ツールをインストールする方法

このチュートリアルはこれらのOSバージョンのために存在します

  • Debian 12 (ブックワーム)
  • Debian 11 (Bullseye)

このページで

  1. 前提条件
  2. Prometheusをインストールする
  3. PrometheusのためのSystemdサービスファイルを作成する
  4. PrometheusのリバースプロキシとしてNginxを構成する
  5. Prometheusダッシュボードにアクセスする
  6. node_exporterをインストールして設定する
  7. Prometheusサーバーにnode_exporterを追加する
  8. Prometheusとnode_exporterを確認する
  9. 結論

Prometheusは、サービスからメトリクスを収集し、時系列データベースに保存する無料のオープンソースのウェブベースの監視アプリケーションです。Prometheusのデフォルト設定では、自身に関するメトリクスのみがエクスポートされます。しかし、エクスポーターや他のプログラムをインストールすることで、機能を拡張できます。複数次元のデータモデル、さまざまなグラフ作成とダッシュボード作成のモードをサポートしています。

この投稿では、Debian 11にPrometheusモニタリングをインストールする方法を説明します。

前提条件

  • Debian 11を実行しているサーバー。
  • サーバーにはルートパスワードが設定されています。

Prometheusをインストールする

開始する前に、Prometheus専用のユーザーとグループを作成することをお勧めします。以下のコマンドを使用して作成できます。

groupadd --system prometheus
useradd -s /sbin/nologin --system -g prometheus prometheus

次に、以下のコマンドを実行してPrometheusの最新バージョンをダウンロードします:

curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest|grep browser_download_url|grep linux-amd64|cut -d '"' -f 4|wget -qi -

プロメテウスがダウンロードされると、次のコマンドを使用してダウンロードしたファイルを見ることができます。

ls -l

次の出力が表示されるはずです:

-rw-r--r-- 1 root root 72638078 Oct  5 16:46 prometheus-2.30.3.linux-amd64.tar.gz

次に、以下のコマンドを使用してダウンロードしたファイルを抽出します:

tar -xvf prometheus*.tar.gz

次に、以下のコマンドを使って抽出したディレクトリに移動します:

cd prometheus-2.30.3.linux-amd64

次に、以下のコマンドを使用して必要なディレクトリを作成します:

mkdir /etc/prometheus
mkdir /var/lib/prometheus

次に、以下のコマンドを使用して必要な構成ファイルとツールをコピーします。

mv prometheus.yml /etc/prometheus/prometheus.yml
mv consoles/ console_libraries/ /etc/prometheus/
mv prometheus promtool /usr/local/bin/

完了したら、次のステップに進むことができます。

PrometheusのためのSystemdサービスファイルを作成する

次に、Prometheusサービスを管理するためのsystemdサービスファイルを作成する必要があります。以下のコマンドを実行することで作成できます:

nano /etc/systemd/system/prometheus.service

次の行を追加してください:

[Unit]
Description=Prometheus
Documentation=https://prometheus.io/docs/introduction/overview/
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/local/bin/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/var/lib/prometheus \
  --web.console.templates=/etc/prometheus/consoles \
  --web.console.libraries=/etc/prometheus/console_libraries \
  --web.listen-address=0.0.0.0:9090 \
  --web.external-url=

SyslogIdentifier=prometheus
Restart=always

[Install]
WantedBy=multi-user.target

ファイルを保存して閉じた後、Prometheus 設定ディレクトリに適切な所有権と権限を設定してください。

chown -R prometheus:prometheus /etc/prometheus/
chmod -R 775 /etc/prometheus/
chown -R prometheus:prometheus /var/lib/prometheus/

次に、変更を適用するためにsystemdデーモンを再読み込みします:

systemctl daemon-reload

次に、Prometheusサービスを開始し、次のコマンドを使ってシステム再起動時に自動起動するように設定します:

systemctl start prometheus
systemctl enable prometheus

以下のコマンドでPrometheusのステータスを確認できます:

systemctl status prometheus

次の出力が得られます:

? prometheus.service - Prometheus
     Loaded: loaded (/etc/systemd/system/prometheus.service; disabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-10-16 14:06:39 UTC; 4s ago
       Docs: https://prometheus.io/docs/introduction/overview/
   Main PID: 18415 (prometheus)
      Tasks: 5 (limit: 2341)
     Memory: 19.6M
        CPU: 79ms
     CGroup: /system.slice/prometheus.service
             ??18415 /usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --web.cons>

Oct 16 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.172Z caller=head.go:513 component=tsdb msg="On-disk memory mappa>
Oct 16 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.172Z caller=head.go:519 component=tsdb msg="Replaying WAL, this >
Oct 16 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.174Z caller=head.go:590 component=tsdb msg="WAL segment loaded" >
Oct 16 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.174Z caller=head.go:596 component=tsdb msg="WAL replay completed>
Oct 16 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.177Z caller=main.go:849 fs_type=EXT4_SUPER_MAGIC
Oct 16 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.177Z caller=main.go:852 msg="TSDB started"
Oct 16 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.178Z caller=main.go:979 msg="Loading configuration file" filenam>
Oct 16 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.178Z caller=main.go:1016 msg="Completed loading of configuration>
Oct 16 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.179Z caller=main.go:794 msg="Server is ready to receive web requ>
Oct 16 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.179Z caller=tls_config.go:191 component=web msg="TLS is disabled>

デフォルトでは、Prometheusはポート9090でリッスンしています。次のコマンドを使用して確認できます:

ss -antpl | grep 9090

次の出力が表示されるはずです:

LISTEN 0      4096               *:9090            *:*    users:(("prometheus",pid=18415,fd=7))

完了したら、次のステップに進むことができます。

PrometheusのリバースプロキシとしてNginxを構成する

次に、PrometheusのためのリバースプロキシとしてNginxをインストールし、設定することをお勧めします。まず、以下のコマンドを使用してNginxウェブサーバーパッケージをインストールしてください。

apt-get install nginx -y

Nginxがインストールされたら、次のコマンドを使用してNginxの仮想ホスト設定ファイルを作成します:

nano /etc/nginx/conf.d/prometheus.conf

次の行を追加してください:

server {
    listen 80;
    server_name prometheus.example.com;
    location / {
        proxy_pass http://localhost:9090;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

}

ファイルを保存して閉じたら、次のコマンドを使ってNginxに構文エラーがないか確認してください。

nginx -t

すべてが順調であれば、次の出力が得られます:

nginx: configuration file /etc/nginx/nginx.conf test is successful

最後に、変更を適用するためにNginxサービスを再起動します:

systemctl restart nginx

次のコマンドを使用して、Nginxサービスのステータスを確認することもできます:

systemctl status nginx

次の出力を得るべきです:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-10-16 14:08:15 UTC; 2s ago
       Docs: man:nginx(8)
    Process: 18792 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 18793 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 18794 (nginx)
      Tasks: 2 (limit: 2341)
     Memory: 2.5M
        CPU: 35ms
     CGroup: /system.slice/nginx.service
             ??18794 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ??18795 nginx: worker process

Oct 16 14:08:15 debian11 systemd[1]: Starting A high performance web server and a reverse proxy server...
Oct 16 14:08:15 debian11 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Oct 16 14:08:15 debian11 systemd[1]: Started A high performance web server and a reverse proxy server.

この時点で、Nginxはインストールされ、Prometheusを提供するように設定されています。次のステップに進むことができます。

Prometheusダッシュボードにアクセスする

さて、ウェブブラウザを開き、URL http://prometheus.example.com を使用してPrometheusダッシュボードにアクセスしてください。次のページが表示されるはずです:

node_exporterをインストールして設定する

node_exporterは、Prometheusサーバーを監視し、メトリックを取得するためのエクスポーターです。まず、次のコマンドを使用してnode_exporterの最新バージョンをダウンロードします:

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

ダウンロードが完了したら、以下のコマンドでダウンロードしたファイルを展開します:

tar -xvzf node_exporter-1.2.2.linux-amd64.tar.gz

次に、抽出したディレクトリを /etc/prometheus/ ディレクトリに移動します:

mv node_exporter-1.2.2.linux-amd64 /etc/prometheus/node_exporter

次に、次のコマンドを使用して適切な所有権を設定します:

chown -R prometheus:prometheus /etc/prometheus/node_exporter

次に、node_exporterサービスを管理するためのsystemdサービスファイルを作成します。

nano /etc/systemd/system/node_exporter.service

次の行を追加してください:

[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
ExecStart=/etc/prometheus/node_exporter/node_exporter

[Install]
WantedBy=default.target

ファイルを保存して閉じたら、変更を適用するためにsystemdデーモンを再読み込みします:

systemctl daemon-reload

次に、node_exporterサービスを開始し、システム再起動時に自動的に起動するように設定します。

systemctl start node_exporter
systemctl enable node_exporter

以下のコマンドでnode_exporterのステータスを確認できます:

systemctl status node_exporter

次の出力が得られます:

? node_exporter.service - Node Exporter
     Loaded: loaded (/etc/systemd/system/node_exporter.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-10-17 05:38:20 UTC; 4s ago
   Main PID: 513 (node_exporter)
      Tasks: 4 (limit: 2341)
     Memory: 4.7M
        CPU: 11ms
     CGroup: /system.slice/node_exporter.service
             ??513 /etc/prometheus/node_exporter/node_exporter

Oct 17 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=thermal_zone
Oct 17 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=time
Oct 17 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=timex
Oct 17 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=udp_queues
Oct 17 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=uname
Oct 17 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=vmstat
Oct 17 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=xfs
Oct 17 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=zfs
Oct 17 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.383Z caller=node_exporter.go:199 msg="Listening on" address=:91>
Oct 17 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.383Z caller=tls_config.go:191 msg="TLS is disabled." http2=false

デフォルトでは、node_exporterはポート9100でリッスンしています。以下のコマンドで確認できます:

ss -antpl | grep 9100

次の出力を得るべきです:

LISTEN 0      4096               *:9100            *:*    users:(("node_exporter",pid=513,fd=3))

Prometheusサーバーにnode_exporterを追加する

次に、node_exporterをPrometheusの設定ファイルに追加する必要があります。これは、Prometheusのデフォルト設定ファイルを編集することで行えます:

nano /etc/prometheus/prometheus.yml

「scrape_config」の行の下に、新しいjob_nameノードエクスポーターを追加するために、次の行を追加してください。

  - job_name: 'node_exporter'
    static_configs:
    - targets: ['localhost:9100']

ファイルを保存して閉じたら、変更を適用するためにPrometheusサービスを再起動してください。

systemctl restart prometheus

Prometheusとnode_exporterを確認する

次に、Prometheusダッシュボードに移動し、ステータス => ターゲットをクリックしてください。次の画面にnode_exporterが表示されるはずです:

今度は、Prometheusのホームページに戻り、クエリフィールドにnode_memory_MemAvailable_bytesと入力し、'実行'ボタンをクリックしてください。次の結果が得られます:

以下のように、http://your-server-ip:9100/metrics のURLを使用してnode_exporterメトリクスデータを取得することもできます:

結論

おめでとうございます!あなたはDebian 11にnode_exporterを使ってPrometheusを正常にインストールしました。質問があればお気軽にどうぞ。詳細については、公式のPrometheusドキュメントをご覧ください。

関連記事: