三流エンジニアの落書き帳

さあ、今日も未知という扉を開けてみよう

CentOS8上でPrometheusによる監視環境を構築する

最近こちらの書籍を読んでいるのですが、英語なので読むのに非常に時間がかかっています...

内容は良さそうなのでしっかり理解していきたいですね。

引き続き環境構築中です。

前回はCentOS8上でMySQL 8.0.19をビルドインストールしました。

今回は監視のためにPrometheusの構築を行います。

とりあえず今回はGrafanaで可視化は行わず、メトリクスを取得できるところまで構築していこうと思います。

Prometheusの準備

このインスタンスは前回MySQLを構築したインスタンスとは別のものです。

バイナリの取得

[root@centos8-prometheus:/usr/local/src/prometheus]# wget https://github.com/prometheus/prometheus/releases/download/v2.17.1/prometheus-2.17.1.linux-amd64.tar.gz
--2020-04-18 16:14:46--  https://github.com/prometheus/prometheus/releases/download/v2.17.1/prometheus-2.17.1.linux-amd64.tar.gz
github.com (github.com) をDNSに問いあわせています... 13.114.40.48
github.com (github.com)|13.114.40.48|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 302 Found
場所: https://github-production-release-asset-2e65be.s3.amazonaws.com/6838921/2ab24c80-6fa0-11ea-885e-96dd0c3494de?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200418%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200418T071447Z&X-Amz-Expires=300&X-Amz-Signature=1a269b4737f909fb25857cb14053034469faae19f202b0d0aac48abe56777bcf&X-Amz-SignedHeaders=host&actor_id=0&repo_id=6838921&response-content-disposition=attachment%3B%20filename%3Dprometheus-2.17.1.linux-amd64.tar.gz&response-content-type=application%2Foctet-stream [続く]
--2020-04-18 16:14:47--  https://github-production-release-asset-2e65be.s3.amazonaws.com/6838921/2ab24c80-6fa0-11ea-885e-96dd0c3494de?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200418%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200418T071447Z&X-Amz-Expires=300&X-Amz-Signature=1a269b4737f909fb25857cb14053034469faae19f202b0d0aac48abe56777bcf&X-Amz-SignedHeaders=host&actor_id=0&repo_id=6838921&response-content-disposition=attachment%3B%20filename%3Dprometheus-2.17.1.linux-amd64.tar.gz&response-content-type=application%2Foctet-stream
github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com) をDNSに問いあわせています... 52.216.171.147
github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.171.147|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 60140415 (57M) [application/octet-stream]
`prometheus-2.17.1.linux-amd64.tar.gz' に保存中

prometheus-2.17.1.linux-amd64.tar.gz  100%[=========================================================================>]  57.35M  35.7KB/s 時間 18m 41s  

2020-04-18 16:33:29 (52.4 KB/s) - `prometheus-2.17.1.linux-amd64.tar.gz' へ保存完了 [60140415/60140415]

解凍して適当にディレクトリとかを整形しつつ

[root@centos8-prometheus:/usr/local/src/prometheus]# ll
合計 201636
-rw-r--r--. 1 3434 3434    11357  327 03:22 LICENSE
-rw-r--r--. 1 3434 3434     3184  327 03:22 NOTICE
drwxr-xr-x. 2 3434 3434       38  327 03:22 console_libraries
drwxr-xr-x. 2 3434 3434      173  327 03:22 consoles
drwxr-xr-x. 3 root root       51  418 16:35 data
-rwxr-xr-x. 1 3434 3434 84338005  327 01:20 prometheus
-rw-r--r--. 1 root root 60140415  327 03:27 prometheus-2.17.1.linux-amd64.tar.gz
-rw-r--r--. 1 3434 3434      926  327 03:22 prometheus.yml
-rwxr-xr-x. 1 3434 3434 48235996  327 01:22 promtool
-rwxr-xr-x. 1 3434 3434 13732141  327 01:22 tsdb
[root@centos8-prometheus:/usr/local/src/prometheus]# 

とりあえずUnitfファイルを作成します。

[root@centos8-prometheus:/usr/local/src/prometheus]# cat /etc/systemd/system/prometheus.service 
[Unit]
Description=Prometheus
After=network-online.target

[Service]
Type=simple
ExecStart=/usr/local/src/prometheus/prometheus --config.file=/usr/local/src/prometheus/prometheus.yml
Restart=on failuer

[Install]
WantedBy=multi-user.target

9090ポートを開けておきます。

[root@centos8-prometheus:/usr/local/src/prometheus]# firewall-cmd --add-port=9090/tcp --zone=public --permanent
success
[root@centos8-prometheus:/usr/local/src/prometheus]# firewall-cmd --reload
success

Prometheusを起動します。

[root@centos8-prometheus:/usr/local/src/prometheus]# systemctl start prometheus
[root@centos8-prometheus:/usr/local/src/prometheus]# systemctl status prometheus
● prometheus.service - Prometheus
   Loaded: loaded (/etc/systemd/system/prometheus.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-04-18 16:49:38 JST; 3s ago
 Main PID: 2400 (prometheus)
    Tasks: 7 (limit: 5030)
   Memory: 23.8M
   CGroup: /system.slice/prometheus.service
           └─2400 /usr/local/src/prometheus/prometheus --config.file=/usr/local/src/prometheus/prometheus.yml

ホストからPormetheusに接続してみます。

f:id:koreshiki-nanno:20200418165438p:plain

問題なさそうです。

Prometheusはとりあえずこれで完了です。

次は各exporterの設定を行っていきます。

node-exporterの準備

バイナリの取得

[root@centos8:/usr/local/src/prometheus]# wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
--2020-04-18 15:18:00--  https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
github.com (github.com) をDNSに問いあわせています... 52.192.72.89
github.com (github.com)|52.192.72.89|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 302 Found
場所: https://github-production-release-asset-2e65be.s3.amazonaws.com/9524057/5dc5df80-86f1-11e9-924c-ef392e7300e3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200418%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200418T061803Z&X-Amz-Expires=300&X-Amz-Signature=c476f82a439120199bf5688f722b4f3e2b71554c8cec08e6d4232e671f488341&X-Amz-SignedHeaders=host&actor_id=0&repo_id=9524057&response-content-disposition=attachment%3B%20filename%3Dnode_exporter-0.18.1.linux-amd64.tar.gz&response-content-type=application%2Foctet-stream [続く]
--2020-04-18 15:18:01--  https://github-production-release-asset-2e65be.s3.amazonaws.com/9524057/5dc5df80-86f1-11e9-924c-ef392e7300e3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200418%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200418T061803Z&X-Amz-Expires=300&X-Amz-Signature=c476f82a439120199bf5688f722b4f3e2b71554c8cec08e6d4232e671f488341&X-Amz-SignedHeaders=host&actor_id=0&repo_id=9524057&response-content-disposition=attachment%3B%20filename%3Dnode_exporter-0.18.1.linux-amd64.tar.gz&response-content-type=application%2Foctet-stream
github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com) をDNSに問いあわせています... 52.216.171.123
github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.171.123|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 8083296 (7.7M) [application/octet-stream]
`node_exporter-0.18.1.linux-amd64.tar.gz' に保存中

node_exporter-0.18.1.linux-amd64.tar. 100%[=========================================================================>]   7.71M  17.7KB/s 時間 3m 33s   

2020-04-18 15:21:40 (37.1 KB/s) - `node_exporter-0.18.1.linux-amd64.tar.gz' へ保存完了 [8083296/8083296]

[root@centos8:/usr/local/src/prometheus]# tar xf node_exporter-0.18.1.linux-amd64.tar.gz 
[root@centos8:/usr/local/src/prometheus]# mv node_exporter-0.18.1.linux-amd64 node_exporter

今回はnode-exporterとは別のホストにPrometheusを立てるため、9100ポートを開けておきます。

[root@centos8:/usr/local/src/prometheus]# firewall-cmd --add-port=9100/tcp --zone=public --permanent
success
[root@centos8:/usr/local/src/prometheus]# firewall-cmd --reload
success

Unitファイルを作成します。

[root@centos8:/usr/local/src/prometheus]# cat /etc/systemd/system/node-exporter.service 
[Unit]
Description=Node Exporter
Documentation=https://github.com/prometheus/node_exporter

[Service]
Type=simple
ExecStart=/usr/local/src/prometheus/node_exporter/node_exporter
Restart=always

[Install]
WantedBy=multi-user.target

systemctlでnode-exporterを起動します。 ついでに自動起動も有効にしておきます。

[root@centos8:/usr/local/src/prometheus]# systemctl enable node-exporter.service
Created symlink /etc/systemd/system/multi-user.target.wants/node-exporter.service → /etc/systemd/system/node-exporter.service.

[root@centos8:/usr/local/src/prometheus]# systemctl start node-exporter.service

[root@centos8:/usr/local/src/prometheus]# systemctl status node-exporter.service
● node-exporter.service - Node Exporter
   Loaded: loaded (/etc/systemd/system/node-exporter.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-04-18 15:39:53 JST; 3s ago
     Docs: https://github.com/prometheus/node_exporter
 Main PID: 14102 (node_exporter)
    Tasks: 6 (limit: 23978)
   Memory: 6.6M
   CGroup: /system.slice/node-exporter.service
           └─14102 /usr/local/src/prometheus/node_exporter/node_exporter

mysqld-exporterの準備

mysqld-exporterも同様に準備します。

バイナリの取得

[root@centos8:/usr/local/src/prometheus]# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
--2020-04-18 15:24:23--  https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
github.com (github.com) をDNSに問いあわせています... 52.69.186.44
github.com (github.com)|52.69.186.44|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 302 Found
場所: https://github-production-release-asset-2e65be.s3.amazonaws.com/32075541/113de280-b210-11e9-8359-0f8a58a8c694?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200418%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200418T062242Z&X-Amz-Expires=300&X-Amz-Signature=1bc32d851e911cdc2bd5679c9dcd69d2199e316602448cef4ed9b0f6cc85c8a7&X-Amz-SignedHeaders=host&actor_id=0&repo_id=32075541&response-content-disposition=attachment%3B%20filename%3Dmysqld_exporter-0.12.1.linux-amd64.tar.gz&response-content-type=application%2Foctet-stream [続く]
--2020-04-18 15:24:24--  https://github-production-release-asset-2e65be.s3.amazonaws.com/32075541/113de280-b210-11e9-8359-0f8a58a8c694?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200418%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200418T062242Z&X-Amz-Expires=300&X-Amz-Signature=1bc32d851e911cdc2bd5679c9dcd69d2199e316602448cef4ed9b0f6cc85c8a7&X-Amz-SignedHeaders=host&actor_id=0&repo_id=32075541&response-content-disposition=attachment%3B%20filename%3Dmysqld_exporter-0.12.1.linux-amd64.tar.gz&response-content-type=application%2Foctet-stream
github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com) をDNSに問いあわせています... 52.216.234.59
github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.234.59|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 7121565 (6.8M) [application/octet-stream]
`mysqld_exporter-0.12.1.linux-amd64.tar.gz' に保存中

mysqld_exporter-0.12.1.linux-amd64.ta 100%[=========================================================================>]   6.79M  21.7KB/s 時間 3m 30s   

2020-04-18 15:27:55 (33.1 KB/s) - `mysqld_exporter-0.12.1.linux-amd64.tar.gz' へ保存完了 [7121565/7121565]

[root@centos8:/usr/local/src/prometheus]# tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz 
[root@centos8:/usr/local/src/prometheus]# mv mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter

9104ポートを開ける

[root@centos8:/usr/local/src/prometheus]# firewall-cmd --add-port=9104/tcp --zone=public --permanent
success
[root@centos8:/usr/local/src/prometheus]# firewall-cmd --reload
success

Unitファイルを作成する

[root@centos8:/usr/local/src/prometheus/mysqld_exporter]# cat /etc/systemd/system/mysqld-exporter.service 
[Unit]
Description=Mysqld Exporter
Documentation=https://github.com/prometheus/mysqld_exporter

[Service]
Type=simple
ExecStart=/usr/local/src/prometheus/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/src/prometheus/mysqld_exporter/.my.cnf
Restart=always

[Install]
WantedBy=multi-user.target

監視用のユーザーを作成

mysql80-root@localhost:[(none)] >create user prometheus@127.0.0.1 identified by random password;
+------------+-----------+----------------------+
| user       | host      | generated password   |
+------------+-----------+----------------------+
| prometheus | 127.0.0.1 | po[WMk!2+7O:nu<Ibv}> |
+------------+-----------+----------------------+
1 row in set (0.02 sec)

mysql80-root@localhost:[(none)] >grant select on *.* to prometheus@127.0.0.1;
Query OK, 0 rows affected (0.01 sec)

接続情報をコンフィグファイルにまとめておきます。 このコンフィグはmysqld-exporterを起動するときに利用します。

[root@centos8:/usr/local/src/prometheus/mysqld_exporter]# cat /usr/local/src/prometheus/mysqld_exporter/.my.cnf
[client]
user=prometheus
password=po[WMk!2+7O:nu<Ibv}>
host=127.0.0.1
port=3306

systemctlでmysqld-exporterを起動する

[root@centos8:/usr/local/src/prometheus/mysqld_exporter]# systemctl enable mysqld-exporter
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld-exporter.service → /etc/systemd/system/mysqld-exporter.service.
[root@centos8:/usr/local/src/prometheus/mysqld_exporter]# systemctl start mysqld-exporter
[root@centos8:/usr/local/src/prometheus/mysqld_exporter]# systemctl status mysqld-exporter
● mysqld-exporter.service - Mysqld Exporter
   Loaded: loaded (/etc/systemd/system/mysqld-exporter.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-04-18 16:07:07 JST; 2s ago
     Docs: https://github.com/prometheus/mysqld_exporter
 Main PID: 15512 (mysqld_exporter)
    Tasks: 5 (limit: 23978)
   Memory: 4.4M
   CGroup: /system.slice/mysqld-exporter.service
           └─15512 /usr/local/src/prometheus/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/src/prometheus/mysqld_exporter/.my.cnf

これでexporterの設定は完了しました。

Prometheusに監視対象を追加する

次は実際にPrometheusに監視対象を追加させてみます。

prometheus.ymlをいろいろと編集しておくのですが、万一Prometheusが起動しなくなってしまうと怖いのでバックアップを取っておきます。

[root@centos8-prometheus:/usr/local/src/prometheus]# cp prometheus.yml{,.org}

とりあえず初期のymlをまねながらnode-exporter, mysqld-exporterの設定を書いていきます。

[root@centos8-prometheus:/usr/local/src/prometheus]# diff prometheus.yml prometheus.yml.org 
30,47d29
< 
<   - job_name: 'node-exporter'
< 
<     # metrics_path defaults to '/metrics'
<     # scheme defaults to 'http'.
< 
<     static_configs:
<     - targets: ['192.168.0.10:9100']
< 
<   - job_name: 'mysqld-exporter'
< 
<     # metrics_path defaults to '/metrics'
<     # scheme defaults to 'http'.
< 
<     static_configs:
<     - targets: ['192.168.0.10:9104']

設定を再読み込みをさせます。

Configuration | Prometheus

これを見るとHUPシグナルを送れば再読み込みをしてくれるようです。

[root@centos8-prometheus:/usr/local/src/prometheus]# kill -HUP $(pidof prometheus)

この状態でPrometheusを確認してみます。

StatusタグのTargetsから確認できます。

f:id:koreshiki-nanno:20200418171130p:plain

各exporterが監視対象として追加されたことがわかりますね。

Graphタグからいくつかメトリクスも確認しておきましょう。

Load Average(1min) node-exporter

f:id:koreshiki-nanno:20200418171748p:plain

mysql-uptime mysqld-exporter

f:id:koreshiki-nanno:20200418172103p:plain

メトリクスの確認にGraphかConsoleを利用できます。

なお、グラフの横軸に表示される時刻はUTCなので注意してください。

Prometheusのグラフはお世辞にも見やすいとはいえないので、Grafanaを使って可視化する必要があります。

Prometheusの設定は比較的簡単に行えましたが、Grafanaの設定は結構時間がかかりそうですねー

というわけで今回はPrometheusおよびexporterを使って監視環境を構築しました。