LoginSignup
2
1

More than 3 years have passed since last update.

EC2にprometheus導入+多数のexporterと連携してみる

Last updated at Posted at 2021-01-07

対象

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にて動作確認できる。
prom1.JPG
prometheusUI画面が見れる=設定ファイル&ルールファイルが正常な証拠(逆に、一つでもファイルに異常があると、起動しないし、設定ファイルの中身が見れない。)


今はまだ、prometheus自身しか監視できてないので、prometheusのメトリクスしか取得できない。
prom2.JPG

なので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

起動

/usr/lib/systemd/system/prometheus-node-exporter.service
# 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: 'alertmanager@example.org'           

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/
alertmanager.yml
global:
  resolve_timeout: 1m
route:
  receiver: 'alerta'

receivers:
- name: 'alerta'
  email_configs:
  # 通知メールの受信者アドレス
  - to: xxxxx@gmail.com
  # 通知メールの送信者アドレス
    from: xxxxx@gmail.com
  # 使用する SMTP サーバー
    smarthost: smtp.gmail.com:587
  # SMTP Auth を設定している場合はアカウント名とパスワード
    auth_username: xxxxx@gmail.com
    auth_identity: xxxxx@gmail.com
    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

prom10.JPG

node-exporterが停止したことをprometheusが知らせている。


prom11.JPG

定めたルールが、条件を満たして、発砲していることが確認できる。


prom12.JPG

メールを確認するとalertmanagerからメールが届いていることが確認できる。


次回はログ監視ツールの動作を確認してみようと思う。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1