1.はじめに
Prometheusは、クラウドインフラストラクチャーやアプリケーションの監視に広く使用されているオープンソースの監視ツールです。この記事では、AWS EC2上にPrometheus、Grafanaをセットアップし、監視元サーバー、監視対象サーバーを準備して、実際にサーバーの監視を行ってみます。
2.EC2の準備
監視用サーバーと、監視対象サーバー用の2つのインスタンスを準備してください
・監視用サーバー
セキュリティグループのインバウンドルールにて、9090,3000のポートからアクセスできるように設定してください(Prometheusへのアクセスに9090,Grafanaへのアクセスに3000を利用)
・監視対象サーバー
SGにて、9100のポートからアクセスできるように設定してください(NodeExporterへのアクセス)
3.Node Exporterの構築 (監視対象サーバー)
3.1 作業用ディレクトリの作成
$ sudo mkdir /usr/local/node-exporter
$ cd /usr/local/node-exporter
$ pwd
/usr/local/node-exporter
3.2 Node Exporterのインストール
# v1.7.0のNode Exporterをインストールする
$ sudo wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
# 圧縮ファイルがインストールされていることを確認する
$ ls
node_exporter-1.7.0.linux-amd64.tar.gz
# 展開する
$ sudo tar xvfz node_exporter-1.7.0.linux-amd64.tar.gz
node_exporter-1.7.0.linux-amd64/
node_exporter-1.7.0.linux-amd64/LICENSE
node_exporter-1.7.0.linux-amd64/node_exporter
node_exporter-1.7.0.linux-amd64/NOTICE
# 展開後、圧縮ファイルは削除する
$ sudo rm node_exporter-1.7.0.linux-amd64.tar.gz
$ ls
node_exporter-1.7.0.linux-amd64
下記ダウンロードページより、インストールするNode Exporterのバージョンを確認する。
今回は、1.7.0のバージョンをインストールする。
https://prometheus.io/download/
3.3 systemdのサービスユニットファイルを作成
$ sudo vim /etc/systemd/system/node_exporter.service
下記の通り入力する
[Unit]
Description=Node Exporter
[Service]
User=root
Restart=always
ExecStart=/usr/local/node-exporter/node_exporter-1.7.0.linux-amd64/node_exporter
[Install]
WantedBy=multi-user.target
3.4 Node Exporterの起動
Activeがrunningになっていれば起動できています。
$ sudo systemctl daemon-reload
$ sudo systemctl enable node_exporter
$ sudo systemctl start node_exporter
$ sudo systemctl status node_exporter
● node_exporter.service - Node Exporter
Loaded: loaded (/etc/systemd/system/node_exporter.service; enabled; preset: disabled)
Active: active (running) since Wed 2024-04-24 06:43:06 UTC; 2s ago
Main PID: 6597 (node_exporter)
Tasks: 3 (limit: 1114)
Memory: 1.9M
CPU: 10ms
CGroup: /system.slice/node_exporter.service
└─6597 /usr/local/node-exporter/node_exporter-1.7.0.linux-amd64/node_exporter
Apr 24 06:43:06 ip-172-31-45-76.ap-northeast-1.compute.internal node_exporter[6597]: ts=2024-04-24T06:43:06.436Z caller=node_exporter.go:117 level=info collector=thermal_zone
Apr 24 06:43:06 ip-172-31-45-76.ap-northeast-1.compute.internal node_exporter[6597]: ts=2024-04-24T06:43:06.436Z caller=node_exporter.go:117 level=info collector=time
Apr 24 06:43:06 ip-172-31-45-76.ap-northeast-1.compute.internal node_exporter[6597]: ts=2024-04-24T06:43:06.436Z caller=node_exporter.go:117 level=info collector=timex
Apr 24 06:43:06 ip-172-31-45-76.ap-northeast-1.compute.internal node_exporter[6597]: ts=2024-04-24T06:43:06.436Z caller=node_exporter.go:117 level=info collector=udp_queues
Apr 24 06:43:06 ip-172-31-45-76.ap-northeast-1.compute.internal node_exporter[6597]: ts=2024-04-24T06:43:06.436Z caller=node_exporter.go:117 level=info collector=uname
Apr 24 06:43:06 ip-172-31-45-76.ap-northeast-1.compute.internal node_exporter[6597]: ts=2024-04-24T06:43:06.437Z caller=node_exporter.go:117 level=info collector=vmstat
Apr 24 06:43:06 ip-172-31-45-76.ap-northeast-1.compute.internal node_exporter[6597]: ts=2024-04-24T06:43:06.437Z caller=node_exporter.go:117 level=info collector=xfs
Apr 24 06:43:06 ip-172-31-45-76.ap-northeast-1.compute.internal node_exporter[6597]: ts=2024-04-24T06:43:06.437Z caller=node_exporter.go:117 level=info collector=zfs
Apr 24 06:43:06 ip-172-31-45-76.ap-northeast-1.compute.internal node_exporter[6597]: ts=2024-04-24T06:43:06.438Z caller=tls_config.go:274 level=info msg="Listening on" address=[::]:9100
Apr 24 06:43:06 ip-172-31-45-76.ap-northeast-1.compute.internal node_exporter[6597]: ts=2024-04-24T06:43:06.438Z caller=tls_config.go:277 level=info msg="TLS is disabled." http2=false address=[::]:9100
3.5 Node ExporterUiにアクセスする
作成したEC2インスタンス(監視対象サーバー)のパブリックIPアドレスを確認し、アクセスしてください
下記のようなページが開かれれば成功です。
http://{監視対象サーバーのパブリックIPアドレス}:9100
4.prometheusの構築 (監視用サーバー)
監視用サーバーに以下の通りprometheusを構築していく。
EC2にSSHして、監視用サーバー上でコマンドを実施していく。
4.1 作業用ディレクトリの作成
$ sudo mkdir /usr/local/prometheus
$ cd /usr/local/prometheus/
$ pwd
/usr/local/prometheus
4.2 prometheusのインストール
# v2.51.2のPrometheusをインストールする
$ sudo wget https://github.com/prometheus/prometheus/releases/download/v2.51.2/prometheus-2.51.2.linux-amd64.tar.gz
# 圧縮ファイルがインストールされていることを確認する
$ ls
prometheus-2.51.2.linux-amd64.tar.gz
# 展開する
$ sudo tar xvfz prometheus-2.51.2.linux-amd64.tar.gz
prometheus-2.51.2.linux-amd64/
prometheus-2.51.2.linux-amd64/console_libraries/
prometheus-2.51.2.linux-amd64/console_libraries/menu.lib
prometheus-2.51.2.linux-amd64/console_libraries/prom.lib
prometheus-2.51.2.linux-amd64/promtool
prometheus-2.51.2.linux-amd64/NOTICE
prometheus-2.51.2.linux-amd64/LICENSE
prometheus-2.51.2.linux-amd64/consoles/
prometheus-2.51.2.linux-amd64/consoles/node-disk.html
prometheus-2.51.2.linux-amd64/consoles/index.html.example
prometheus-2.51.2.linux-amd64/consoles/node-overview.html
prometheus-2.51.2.linux-amd64/consoles/node.html
prometheus-2.51.2.linux-amd64/consoles/node-cpu.html
prometheus-2.51.2.linux-amd64/consoles/prometheus-overview.html
prometheus-2.51.2.linux-amd64/consoles/prometheus.html
prometheus-2.51.2.linux-amd64/prometheus.yml
prometheus-2.51.2.linux-amd64/prometheus
# 展開後、圧縮ファイルは削除する
$ sudo rm prometheus-2.51.2.linux-amd64.tar.gz
$ ls
prometheus-2.51.2.linux-amd64
下記ダウンロードページより、インストールするPrometheusのバージョンを確認する。
今回は、2.51.2のバージョンをインストールする。
4.3 systemdのサービスユニットファイルを作成
$ sudo vim /etc/systemd/system/prometheus.service
下記の内容をprometheus.service入力する
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target
[Service]
User=root
Restart=always
ExecStart=/usr/local/prometheus/prometheus-2.51.2.linux-amd64/prometheus --config.file=/usr/local/prometheus/prometheus-2.51.2.linux-amd64/prometheus.yml
[Install]
WantedBy=multi-user.target
4.4 prometheusの起動
Activeがrunningになっていれば起動できています。
$ sudo systemctl daemon-reload
$ sudo systemctl start prometheus
$ sydo systemctl status prometheus
$ sudo systemctl status prometheus
● prometheus.service - Prometheus Server
Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; preset: disabled)
Active: active (running) since Wed 2024-04-24 05:56:22 UTC; 2min 12s ago
Docs: https://prometheus.io/docs/introduction/overview/
Main PID: 6032 (prometheus)
Tasks: 6 (limit: 1114)
Memory: 19.7M
CPU: 112ms
CGroup: /system.slice/prometheus.service
└─6032 /usr/local/prometheus/prometheus-2.51.2.linux-amd64/prometheus --config.file=/usr/local/prometheus/prometheus-2.51.2.linux-amd64/prometheus.yml
Apr 24 05:56:22 ip-172-31-35-92.ap-northeast-1.compute.internal prometheus[6032]: ts=2024-04-24T05:56:22.919Z caller=tls_config.go:313 level=info component=web msg="Listening on" address=[::]:9090
Apr 24 05:56:22 ip-172-31-35-92.ap-northeast-1.compute.internal prometheus[6032]: ts=2024-04-24T05:56:22.919Z caller=tls_config.go:316 level=info component=web msg="TLS is disabled." http2=false address=[::]:9090
Apr 24 05:56:22 ip-172-31-35-92.ap-northeast-1.compute.internal prometheus[6032]: ts=2024-04-24T05:56:22.919Z caller=head.go:778 level=info component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
Apr 24 05:56:22 ip-172-31-35-92.ap-northeast-1.compute.internal prometheus[6032]: ts=2024-04-24T05:56:22.919Z caller=head.go:815 level=info component=tsdb msg="WAL replay completed" checkpoint_replay_duration=44.443µs wal_replay_duration=2.298018ms wbl_replay_duration=7>
Apr 24 05:56:22 ip-172-31-35-92.ap-northeast-1.compute.internal prometheus[6032]: ts=2024-04-24T05:56:22.922Z caller=main.go:1150 level=info fs_type=XFS_SUPER_MAGIC
Apr 24 05:56:22 ip-172-31-35-92.ap-northeast-1.compute.internal prometheus[6032]: ts=2024-04-24T05:56:22.922Z caller=main.go:1153 level=info msg="TSDB started"
Apr 24 05:56:22 ip-172-31-35-92.ap-northeast-1.compute.internal prometheus[6032]: ts=2024-04-24T05:56:22.922Z caller=main.go:1335 level=info msg="Loading configuration file" filename=/usr/local/prometheus/prometheus-2.51.2.linux-amd64/prometheus.yml
Apr 24 05:56:22 ip-172-31-35-92.ap-northeast-1.compute.internal prometheus[6032]: ts=2024-04-24T05:56:22.927Z caller=main.go:1372 level=info msg="Completed loading of configuration file" filename=/usr/local/prometheus/prometheus-2.51.2.linux-amd64/prometheus.yml totalDu>
Apr 24 05:56:22 ip-172-31-35-92.ap-northeast-1.compute.internal prometheus[6032]: ts=2024-04-24T05:56:22.927Z caller=main.go:1114 level=info msg="Server is ready to receive web requests."
Apr 24 05:56:22 ip-172-31-35-92.ap-northeast-1.compute.internal prometheus[6032]: ts=2024-04-24T05:56:22.927Z caller=manager.go:163 level=info component="rule manager" msg="Starting rule manager..."
4.5 prometheusUiにアクセスする
作成したEC2インスタンス(監視用サーバー)のパブリックIPアドレスを確認し、アクセスしてください
下記のようなページが開かれれば成功です。
http://{監視用サーバーのパブリックIPアドレス}:9090
4.6 デフォルトの監視先を確認する
デフォルトでは、Endpointがlocalhostになっていることが確認できる。
現在は、監視用サーバーが自身を監視しているので、このターゲットを監視対象サーバーに変更することで監視するサーバーを決めることができる。
※ターゲットは複数設定することもできる
4.7 監視先サーバーを監視対象サーバーに変更する
prometheus.yml内のターゲットを監視対象サーバーのNodeExporterに変更する
$ sudo vim /usr/local/prometheus/prometheus-2.51.2.linux-amd64/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).
# 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: ["{監視対象サーバーのIPアドレス}:9100"]
ファイル変更後、再起動する
$ sudo systemctl restart prometheus
PrometheusのTargetページに移動し,Endpointが監視対象サーバーに変更できており、StateがUPであることを確認する
5.Grafanaの構築(監視用サーバー)
監視用サーバーに以下の通りGrafanaを構築していく。
5.1 作業用ディレクトリの作成
$ sudo mkdir /usr/local/grafana
$ cd /usr/local/grafana
$ pwd
/usr/local/grafana
5.2 Grafanaのインストール
# v10.4.2のGrafanaをインストールする
$ sudo yum install -y https://dl.grafana.com/oss/release/grafana-10.4.2-1.x86_64.rpm
下記ダウンロードページより、インストールするGrafanaのバージョンを確認する。
今回は、10.4.2のバージョンをインストールする。
5.3 Grafanaの起動
Activeがrunningになっていれば起動できています。
$ sudo systemctl start grafana-server
$ sudo systemctl enable grafana-server
$ systemctl status grafana-server
● grafana-server.service - Grafana instance
Loaded: loaded (/usr/lib/systemd/system/grafana-server.service; enabled; preset: disabled)
Active: active (running) since Wed 2024-04-24 16:17:07 UTC; 56s ago
Docs: http://docs.grafana.org
Main PID: 44755 (grafana)
Tasks: 6 (limit: 1114)
Memory: 169.2M
CPU: 2.989s
CGroup: /system.slice/grafana-server.service
└─44755 /usr/share/grafana/bin/grafana server --config=/etc/grafana/grafana.ini --pidfile=/var/run/grafana/grafana-server.pid --packaging=rpm cfg:default.paths.logs=/var/log/grafana cfg:default.paths.data=/var/lib/grafana cfg:default.paths.plugins=/var/lib/>
Apr 24 16:17:07 ip-172-31-35-92.ap-northeast-1.compute.internal grafana[44755]: logger=provisioning.dashboard t=2024-04-24T16:17:07.143021677Z level=info msg="starting to provision dashboards"
Apr 24 16:17:07 ip-172-31-35-92.ap-northeast-1.compute.internal grafana[44755]: logger=provisioning.dashboard t=2024-04-24T16:17:07.143188995Z level=info msg="finished to provision dashboards"
Apr 24 16:17:07 ip-172-31-35-92.ap-northeast-1.compute.internal grafana[44755]: logger=grafanaStorageLogger t=2024-04-24T16:17:07.154575987Z level=info msg="Storage starting"
Apr 24 16:17:07 ip-172-31-35-92.ap-northeast-1.compute.internal grafana[44755]: logger=ngalert.multiorg.alertmanager t=2024-04-24T16:17:07.182408554Z level=info msg="Starting MultiOrg Alertmanager"
Apr 24 16:17:07 ip-172-31-35-92.ap-northeast-1.compute.internal grafana[44755]: logger=ngalert.scheduler t=2024-04-24T16:17:07.182588159Z level=info msg="Starting scheduler" tickInterval=10s maxAttempts=1
Apr 24 16:17:07 ip-172-31-35-92.ap-northeast-1.compute.internal grafana[44755]: logger=ticker t=2024-04-24T16:17:07.184044777Z level=info msg=starting first_tick=2024-04-24T16:17:10Z
Apr 24 16:17:07 ip-172-31-35-92.ap-northeast-1.compute.internal grafana[44755]: logger=plugins.update.checker t=2024-04-24T16:17:07.666749766Z level=info msg="Update check succeeded" duration=514.201099ms
Apr 24 16:17:07 ip-172-31-35-92.ap-northeast-1.compute.internal grafana[44755]: logger=grafana.update.checker t=2024-04-24T16:17:07.667590967Z level=info msg="Update check succeeded" duration=523.109774ms
Apr 24 16:17:08 ip-172-31-35-92.ap-northeast-1.compute.internal grafana[44755]: logger=grafana-apiserver t=2024-04-24T16:17:08.273156278Z level=info msg="Adding GroupVersion playlist.grafana.app v0alpha1 to ResourceManager"
Apr 24 16:17:08 ip-172-31-35-92.ap-northeast-1.compute.internal grafana[44755]: logger=grafana-apiserver t=2024-04-24T16:17:08.275531531Z level=info msg="Adding GroupVersion featuretoggle.grafana.app v0alpha1 to ResourceManager"
5.4 Grafana Uiにアクセス
作成したEC2インスタンス(監視用サーバー)のパブリックIPアドレスを確認し、アクセスしてください
下記のようなページが開かれれば成功です。
http://{監視用サーバーのパブリックIPアドレス}:3000
5.5 GrafanaでPrometheusをデータソースとして設定する
1.初期のUserNameとPasswordはadmin
です
2.update yourpasswordとでますが、今回はskipします
4.Datasourcesより、Add data sourceを押下します
6.prometheus server URLに下記の通り入力します。
http://localhost:9090
その他は特に入力しなくて大丈夫です。
7.Save&testを押下し、Successfully queried the Prometheus API.と表示されればOKです。
5.6 Grafana Dashboardを作成する
Grafanaのダッシュボードを作成します。
今回は、テンプレートのダッシュボードをインポートして利用してみます。
1.Dashboardsより、create Dashboardを押下する
3.ID 1860
を入力し、Loadを押下します。
今回利用するDashBoardは下記のものとなります。
下記のサイトより、使いたいダッシュボードを探すことができます。
4.先ほど、Datasourcesで設定したPrometheusを選択し、Importを押下します。
5.ダッシュボードが確認できる
ダッシュボードから、監視対象サーバーの状態を監視することができます。
6.まとめ
今回は、監視用サーバーと、監視対象サーバーを用意し、PrometheusとGrafanaを用いた監視環境の構築の仕方をまとめてみました。
Prometheusでは、PromQLを用いて自分が欲しいメトリクスが取れたり、それを利用してGrafanaでは自身の環境で使いやすいようにカスタマイズしたりと、もっともっと深い部分があるのでそれらも今後もっと詳しくなっていきたいです。