前書き
仕事でvCenterServerのデータをPrometheusで収集してGrafanaで表示する・・・というタスクをもらったので備忘がてらその方法を記録する。
今回はCentOSにPrometheus(プロメテウス)とGrafana(グラファナ)をインストールするところから。
環境
CentOS 7.9 ※最小インストール
Prometheus 2.27.1
Grafana 7.1.5 または 7.5.7 ※どっちもやったけどどっちも同じ手順でOK
本環境では、PrometheusとGrafanaは同じサーバーにインストールする。
また、サーバー再起動しても自動でサービスが立ち上がるように自動起動用のスクリプトをあわせて作成する。
なお、本環境はサーバーがインターネットにアクセス可能であることを前提とする。
環境によってはプロキシ経由にしたりローカルに落とすなどの対応が必要なので留意されたし。
CentOSの入手
以下サイトよりISOをダウンロードする。
左側の「CentOS Linux」から自身の環境にあったバージョンとものを選択する。
私の環境では、ESXi上の仮想マシンにCentOS7.9をインストールするので、「7(2009)」を選択してからISOの「x86_64」を選択する。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image.png?resize=1024%2C778&ssl=1)
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-3.png?resize=1024%2C749&ssl=1)
ダウンロード元一覧が表示されるので、適当に選択する。(どれも中身は一緒)
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-4.png?resize=1024%2C548&ssl=1)
クリックするとIndexが表示されるので「CentOS-7-x86_64-Minimal-2009.iso」を選択して適切な場所に保存する。
※Minimal(最小インストール)じゃなくても勿論良い
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-5.png?resize=805%2C488&ssl=1)
CentOSのISO入手方法は以上である。
各々の環境に合わせてデプロイを行ってほしい。
Prometheusのインストール
サーバー(CentOS)の準備が出来たら、Prometheusのインストールを行う。
Prometheusのtarを入手して解凍していくことになるが、Prometheusのリリース状況は以下で確認することが可能。
今回はv2.27.1なので、入手先のURLは以下になる。(後ほどwgetで指定する)
https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz
Prometheus用のポート開放
Prometheusはポート9090へListenするので予め開放しておく。
firewall-cmd --add-port=9090/tcp --zone=public --permanent
firewall-cmd --reload
firewall-cmd --list-ports --zone=public
Prometheus Serverのインストールと起動スクリプトの作成
続いて、先程確認したPrometheusのインストールを行う。
Prometheusのインストールにはwgetコマンドを利用する。
但しCentOSの最小インストールではwgetコマンドがないので、予めwgetコマンドをyumでインストールする。
Prometheusのインストール後、サーバー再起動後もPrometheusを自動起動させるためのスクリプトを作成/設定する。
wgetコマンドのインストール
yum install wget
Prometheus Serverのインストール
cd /usr/local/src
wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz
tar zxvf prometheus-2.27.1.linux-amd64.tar.gz
cd prometheus-2.27.1.linux-amd64
Prometheus自動起動用のスクリプト作成(prometheus.serviceとする)
vi /usr/lib/systemd/system/prometheus.service
prometheus.serviceの中身を以下にする。
[Unit]
Description=Prometheus - Monitoring system and time series database
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target
[Service]
Type=simple
ExecStart=/usr/local/src/prometheus-2.27.1.linux-amd64/prometheus \
--config.file=/usr/local/src/prometheus-2.27.1.linux-amd64/prometheus.yml \
[Install]
WantedBy=multi-user.target
作成した自動起動用スクリプトを読み込ませ、自動起動を有効にし、最後にサービスを起動する。
systemctl daemon-reload
systemctl enable prometheus.service
systemctl start prometheus.service
以下URLを入力し、PrometheusのGUI画面が表示されていればOK
http://xxx.xxx.xxx.xxx:9090/
※xxx.xxx.xxx.xxxはPrometheusをインストールしたサーバーのIPアドレスを入力する。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-8.png?resize=748%2C472&ssl=1)
Prometheusのインストール方法及び設定は以上である。
Grafanaのインストール
Prometheusのインストールが終わったら、今度はGrafanaのインストールを行う。こちらはかなり簡単。
Grafanaのリリース状況は以下から確認する。
また、「Download page」のリンク先から各バージョンのダウンロード方法を確認できる。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-10.png?resize=1024%2C574&ssl=1)
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-11.png?resize=1024%2C558&ssl=1)
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-15.png?resize=1024%2C589&ssl=1)
今回はGrafana7.1.5とする(すでに割と古いバージョン。これは勤め先のバージョンがこれだからというだけ)
Grafana用のポート開放
Grafanaはポート3000へListenするので予め開放しておく。
firewall-cmd --add-port=3000/tcp --zone=public --permanent
firewall-cmd --reload
firewall-cmd --list-ports --zone=public
Grafanaのインストールと自動起動設定
続いて、先程確認したGrafanaのインストールを行う。
合わせて自動起動設定を行う。こちらは自動起動用のスクリプトの作成は不要。
cd /usr/local/src
wget https://dl.grafana.com/oss/release/grafana-7.1.5-1.x86_64.rpm
yum install grafana-7.1.5-1.x86_64.rpm
systemctl enable grafana-server
systemctl start grafana-server
以下URLを入力し、GrafanaのGUI画面が表示されていればOK
http://xxx.xxx.xxx.xxx:3000/
※xxx.xxx.xxx.xxxはGrafanaをインストールしたサーバーのIPアドレスを入力する。
初期ユーザーIDと初期パスワードは admin / admin である。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-16.png?resize=746%2C742&ssl=1)
Grafanaのインストールは以上である。
試しにGrafanaでPrometheusの情報を取得してみる。
ここまで設定が正しく出来ていれば、PrometheusのTargetページでPrometheus自身の監視を行っているはず。Status→Targetsの中の「prometheus」がupしていればOK。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-17.png?resize=699%2C339&ssl=1)
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-20.png?resize=637%2C322&ssl=1)
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-21.png?resize=1024%2C106&ssl=1)
これを踏まえて、Grafanaでprometheusが取得している情報を表示してみる。
GrafanaのDatasourceにPrometheusを追加する
PrometheusのデータをGrafanaで表示させるには、まずGrafanaのDatasource(データソース)としてPrometheusを追加してやる必要がある。データソースの追加はGrafanaログイン後「Configuration」の「Data Sources」から行う。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-24.png?resize=950%2C608&ssl=1)
Add data sourceをクリックする
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-26.png?resize=986%2C244&ssl=1)
Prometheusを選んで「Select」をクリックする。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-28.png?resize=1019%2C353&ssl=1)
対象Prometheusの各種情報を求められるので、入力していく。最低限入力必須なのはURLぐらいである。もし複数のPrometheusから情報を収集する予定があるならば、どのPrometheusかわかる名前にNameを変えておいても良い。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-30.png?resize=965%2C875&ssl=1)
入力が完了したら「Save & Test」をクリックする。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-34.png?resize=723%2C510&ssl=1)
このようにData SourcesにPrometheusが追加されていればOK。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-36.png?resize=968%2C328&ssl=1)
ダッシュボードを作成する
Datasourceの追加が出来たら、ダッシュボードを作成する。「Create」→Dashboardから新規ダッシュボードの作成を行う。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-38.png?resize=977%2C509&ssl=1)
Add empty panel をクリックする。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-42.png?resize=993%2C431&ssl=1)
「Query」タブでデータソースとして先程追加したPrometheusを指定してやる。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-43.png?resize=1024%2C571&ssl=1)
次に、Aというアイテムの下にある「Metrics」というプルダウンメニューを開いてみる。
色々候補が出ると思うが「prometheus」を選択するとPrometheus自身の監視情報(メトリックという)を選ぶことができる。
試しに「prometheus_build_info」を選択してみる。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-44.png?resize=714%2C419&ssl=1)
すると、こんな画面になるはず。
prometheus_build_infoはデータソースとなっているPrometheusのバージョン情報を取得しているアイテムなので、先程インストールしたprometheusのバージョン情報がラベル(label)値として表示されているはず。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-45.png?resize=1024%2C354&ssl=1)
「グラフ」という形で表示するような情報ではないが、Grafanaの表示形式を変えてやるとテキスト形式や表形式で表示させることが可能。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-47.png?resize=1024%2C387&ssl=1)
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-48.png?resize=1024%2C513&ssl=1)
Panel(パネル)の作成が出来たら、右上のApplyをクリックして保存する。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-50.png?resize=1024%2C133&ssl=1)
最後にダッシュボードそのもののセーブも忘れないようにする。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-52.png?resize=1024%2C239&ssl=1)
好きなダッシュボード名をつけてSaveをクリックする。Folderはダッシュボードの管理フォルダの指定。こだわりがなければGeneralのままでOK。
![](https://i0.wp.com/hamutetublog.com/wp-content/uploads/2021/07/image-53.png?resize=515%2C329&ssl=1)
以上でダッシュボードの作成は終了である。
後書き
次回は vCenterの情報をPrometheusで取得し、Grafanaで表示する方法です。ではでは。
改版履歴
2021/7/16 初版作成
コメント