EC2にprometheus導入+多数のexporterと連携してみる
対象
kubernetesもprometheusも初めてなのに、いきなりkubernetes環境のpodを監視するprometheusを立てることになった人が、その前段階としてせめて、prometheusの知識を実践で見に付けようという趣旨で書きました。
環境
EC2
- os:amazon linux2
- type:t2.micro
rootユーザにて作業
ファイルパスのルール(パスがめちゃくちゃだと煩雑になるので)
ソースをDL(wget)するpathは以下とする
/usr/local/src
prometheus系アプリを起動させるpathは以下とする
/usr/bin
prometheus系アプリ起動時に読ませる設定ファイルpathは以下とする
/etc/prometheus
prometheus導入
https://www.server-world.info/query?os=CentOS_8&p=prometheus&f=1
https://recruit.gmo.jp/engineer/jisedai/blog/prometheus-grok-log-monitoring/
公式はこちら。ダウンロードすればすぐに動作させることができます。
https://prometheus.io/
#DLパスへ移動
/usr/local/src
#prometheusダウンロード
wget https://github.com/prometheus/prometheus/releases/download/v2.0.0/prometheus-2.0.0.linux-amd64.tar.gz
#解凍
tar -xzvf prometheus-*.gz
#解凍したディレクトリ内に入る
cd prometheus-2.0.0.linux-amd64
#prometheus本体を自動起動するソース配下へコピー
cp -r ./prometheus /usr/bin
#prometheus設定ファイルの配置
mkdir /etc/prometheus
cd /etc/prometheus
## wget https://raw.githubusercontent.com/prometheus/prometheus/master/documentation/examples/prometheus.yml
#自動起動のためのサービスファイルviにて作成
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/bin/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
[Install]
WantedBy=multi-user.target
補足
サービス化の凡ミスとして、「Failed to execute operation: File exists」
というエラーが出たら、serviceファイルが既に出来ている可能性があるので、
以下コマンドでサービスファイルパスを確認して削除しよう。
systemctl show prometheus|grep ^FragmentPath|sed -e "s/^.*=//"
起動・動作確認
# systemctl daemon-reload
自動起動設定
# systemctl enable prometheus.service
起動
# systemctl start prometheus.service
ブラウザソフトのアドレスに「パブリックIP:9090」と入れることで、prometheusをGUIにて動作確認できる。
prometheusUI画面が見れる=設定ファイル&ルールファイルが正常な証拠(逆に、一つでもファイルに異常があると、起動しないし、設定ファイルの中身が見れない。)
今はまだ、prometheus自身しか監視できてないので、prometheusのメトリクスしか取得できない。
なのでnode-exporterを入れてみる。
Node Exporterのインストール
# mkdir /usr/local/src/prometheus
# cd /usr/local/src/prometheus
# wget https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz
# tar zxvf node_exporter-0.16.0.linux-amd64.tar.gz
# mv node_exporter-0.16.0.linux-amd64 node_exporter
起動スクリプト作成
[Unit]
Description=Node Exporter
Documentation=https://github.com/prometheus/node_exporter
[Service]
Type=simple
ExecStart=/usr/local/src/prometheus/node_exporter/node_exporter $OPTIONS
Restart=always
[Install]
WantedBy=multi-user.target
起動
# systemctl daemon-reload
-- 自動起動設定 --
# systemctl enable prometheus-node-exporter.service
-- 起動 --
# systemctl start prometheus-node-exporter.service
prometheus側の設定と動作確認
prometheus側でnode-exporterのメトリクスを確認するために、設定ファイルを書き換えます。以下、初期の設定ファイルに一行手を加えたもの
# 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: ['localhost:9090'] #prometheusの監視
- targets: ['localhost:9100'] #node-exporterの監視
AlertManagerの導入
prometheusと連携することが前提に作られているalertmanagerだが、
連携させるためには、prometheus設定ファイルとalertmanager設定ファイルでそれぞれ定義してやらないといけない。
※今回はgmailを使ったアラート通知を行いたいのでこちらの記事を参考に進めていく。
https://uzimihsr.github.io/post/2020-01-27-alertmanager-gmail/
# AlertManagerインストール
## cd /usr/local/src
## wget https://github.com/prometheus/alertmanager/releases/download/v0.5.1/alertmanager-0.5.1.linux-amd64.tar.gz
## tar xfvz alertmanager-0.5.1.linux-amd64.tar.gz
## cd alertmanager-0.5.1.linux-amd64/
## cp -p alertmanager /usr/bin/.
3.alertmanger設定ファイルの確認・配置
systemctl show alertmanager|grep ^FragmentPath|sed -e "s/^.*=//"
## cd /etc/prometheus
## wget https://raw.githubusercontent.com/alerta/prometheus-config/master/alertmanager.yml
(Default状態)
## cat /etc/prometheus/alertmanager.yml
global:
# The smarthost and SMTP sender used for mail notifications.
smtp_smarthost: 'localhost:25'
smtp_from: '[email protected]'
route:
receiver: "alerta"
group_by: ['alertname']
group_wait: 30s
group_interval: 5m
repeat_interval: 2h
receivers:
- name: "alerta"
webhook_configs:
- url: 'http://localhost:8080/webhooks/prometheus'
send_resolved: true
4.alertmangerのservice起動設定
<Promethusサーバ>
## vi /etc/default/alertmanager
OPTIONS="-config.file /etc/prometheus/alertmanager.yml"
## vi /usr/lib/systemd/system/alertmanager.service
[Unit]
Description=Prometheus alertmanager Service
After=syslog.target.prometheus.alertmanager.service
[Service]
Type=simple
EnvironmentFile=-/etc/default/alertmanager
ExecStart=/usr/bin/alertmanager $OPTIONS
PrivateTmp=true
[Install]
WantedBy=multi-user.target
## systemctl enable alertmanager.service
Created symlink from /etc/systemd/system/multi-user.target.wants/alertmanager.service to /usr/lib/systemd/system/alertmanager.service.
## systemctl start alertmanager
5.アラート設定前準備(メール設定)
SMTPサーバーの設定
ここを参考に, Googleアカウントでアプリ用パスワードを設定する.
https://myaccount.google.com/u/1/security を開き, セキュリティ->2段階認証プロセスと進む.
6.アラート設定
alertmanagerの機能は、prometheusから送られた発砲処理を
メールや、slackなどのwebhookを使って通知させるだけの機能ですので、通知の設定は間違わないように書く必要があります。
ここではalertmanagerが発砲を受け取ったらgmail/slackでメール送信する設定を記していきます。
vi /etc/prometheus/
global:
resolve_timeout: 1m
route:
receiver: 'alerta'
receivers:
- name: 'alerta'
email_configs:
# 通知メールの受信者アドレス
- to: [email protected]
# 通知メールの送信者アドレス
from: [email protected]
# 使用する SMTP サーバー
smarthost: smtp.gmail.com:587
# SMTP Auth を設定している場合はアカウント名とパスワード
auth_username: [email protected]
auth_identity: [email protected]
auth_password: password
send_resolved: true
7.ルール設定
rulesファイルで発砲条件を記して、条件が満たされたら発砲させるのはprometheusの役目です。
また、rulesファイルはjsonでも書けますがここではyamlにて例文を記します。
## cat /etc/prometheus/alert.rules
# node-exporterがダウンしたら発砲
groups:
- name: Instances
rules:
- alert: InstanceDown
expr: up == 0
for: 1m
labels:
severity: critical
annotations:
description: '{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes.'
summary: 'Instance {{ $labels.instance }} down'
8.prometheus に組み込み
PrometheusにAlertmanagerを組み込みましょう。
/etc/prometheus/prometheus.yml の末尾に追加します。
alerting:
alertmanagers:
- scheme: http
static_configs:
- targets: ['localhost:9093']
9.最後に
設定ファイルの記載が正しいかちゃんと確認しましょう。
<Promethusサーバ>
## promtool check-config /etc/prometheus/prometheus.yml
## promtool check-config /etc/prometheus/alertmanager.yml
alertmanager、Prometheusを再起動して完了です。
<Promethusサーバ>
## systemctl restart alertmanager
## systemctl restart prometheus
10.動作確認
わざとnode-exporterを止めて、prometheusとalertmanagerを発砲させてみましょう。
service設定を解除する
systemctl disable prometheus-node-exporter.service
node-exporterを止める
systemctl stop prometheus-node-exporter.service
node-exporterが停止したことをprometheusが知らせている。
定めたルールが、条件を満たして、発砲していることが確認できる。
メールを確認するとalertmanagerからメールが届いていることが確認できる。
次回はログ監視ツールの動作を確認してみようと思う。
Author And Source
この問題について(EC2にprometheus導入+多数のexporterと連携してみる), 我々は、より多くの情報をここで見つけました https://qiita.com/gamelike319/items/4505aed5761607b9a91c著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .