Categories: オープンソース

Prometheus, Node_Exporter, Grafana

サーバのCPU使用率等をブラウザから確認するためのツールを導入する。

Prometheusはモニタリングツール

Node_Exporterはハードウェア関連のメトリックを収集

Grafanaはグラフを表示するツール

という構成である。

まずはPrometheusのインストールから。今回はバイナリを直接取得した。最新バージョンは2.14.0(現時点)。

$ sudo groupadd prometheus
$ sudo useradd -d /var/lib/prometheus -g prometheus -s /bin/false -m prometheus 
$ wget https://github.com/prometheus/prometheus/releases/download/v2.14.0/prometheus-2.14.0.linux-amd64.tar.gz
$ tar xf prometheus-2.14.0.linux-amd64.tar.gz
$ sudo cp prometheus promtool /sbin/
$ sudo chown root:root /sbin/prometheus /sbin/promtool
$ sudo mkdir /etc/prometheus
$ sudo mkdir /var/lib/prometheus/data
$ sudo chown -R prometheus:prometheus /var/lib/prometheus/data
$ sudo cp -r prometheus.yml consoles console_libraries /etc/prometheus/
$ sudo chown -R root:prometheus /etc/prometheus

最後に/etc/systemd/system/prometheus.serviceへsystemdのservice unit fileを設置 。

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

[Service]
User=prometheus
ExecStart=/sbin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus/data --web.console.templates=/etc/prometheus/consoles  --web.console.libraries=/etc/prometheus/console_libraries
ExecStop=/bin/kill -TERM ${MAINPID}
ExecReload=/bin/kill -HUP ${MAINPID}

[Install]
WantedBy=multi-user.target

で、リロード。

$ sudo systemctl daemon-reload
$ sudo systemctl start prometheus.service
$ sudo systemctl status prometheus.service

これで9090番ポートにアクセスするとダッシュボードにアクセスできる。

また、consoles/prometheus.html にアクセスすると PromehtuesのStatus が確認できる。

次に Node_Exporter のインストール。最新が0.18.1(現時点)。

$ wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz 
$ tar xf node_exporter-0.18.1.linux-amd64.tar.gz
$ sudo cp node_exporter-0.18.1.linux-amd64/node_exporter /sbin/
$ sudo chown root:root /sbin/node_exporter

/etc/systemd/system/node_exporter.service ファイル設置。

[Unit]
Description=Node Exporter for Prometheus
After=network.target

[Service]
Type=simple
User=prometheus
ExecStart=/sbin/node_exporter
PrivateTmp=true

[Install]
WantedBy=multi-user.target

で、リロード。

$ sudo systemctl daemon-reload
$ sudo systemctl start node_exporter.service

これで9100番ポートにアクセスすると、メトリックがテキストで表示される。

次に、 Prometheusの設定ファイルに先程起動したNode_Exporterの設定を追加。

/etc/prometheus/prometheus.ymlのscrape_configsの末尾に以下を追記。

scrape_configs:

(略)

  - job_name: 'node'

    file_sd_configs:
    - files:
       - /etc/prometheus/nodes.yml

/etc/prometheus/nodes.ymlへNode_Exporterを登録。

- targets:
   - localhost:9100
  labels:
    role: prometheus

Prometheusをリロード。

$ sudo systemctl daemon-reload
$ sudo systemctl reload prometheus.service
$ sudo systemctl status prometheus.service

ブラウザでPrometheusのダッシュボードの/targetsにアクセスし、 画面に’node’という項目とlocalhost:9100が存在しているようであればNode_Exporterの登録に成功 。

Prometheusの可視化ツールの Grafanaをインストール。

$ sudo /bin/bash -c "echo deb https://packages.grafana.com/oss/deb stable main > /etc/apt/sources.list.d/grafana.list"
$ curl https://packages.grafana.com/gpg.key | sudo apt-key add -
$ sudo apt update
$ sudo apt install grafana
$ sudo systemctl start grafana-server.service 

3000ポートにアクセスするとログインパスワードが聞かれる。

初期パスワードを変更後(自動的に催促される)、Add data sourceでPrometheusを追加。

URLを以下としてSave & Test をクリック。

http://localhost:3000

以下のサイトを参考にした。

https://gihyo.jp/admin/serial/01/ubuntu-recipe/0562?page=1

国産部部長

Share
Published by
国産部部長

Recent Posts

Web会議システム Openmeetings

Apacheが開発しているWe…

4年 ago

OpenStack

いわゆる仮想環境を構築するため…

4年 ago

Limesurvey

オープンソースのアンケートサイ…

4年 ago

Grafana Alert with image renderer

Grafanaで以前、ハードウ…

4年 ago

Lychee

写真サーバのLycheeをイン…

4年 ago

systemctl自動起動

以下のサイトで詳細が記載されて…

4年 ago