Prometheusのインストール

環境

Node exporterのインストール

$ mkdir /usr/local/src/prometheus
$ cd /usr/local/src/prometheus
$ wget https://github.com/prometheus/node_exporter/releases/download/v0.18.0/node_exporter-0.18.0.linux-amd64.tar.gz
$ tar zxvf node_exporter-0.18.0.linux-amd64.tar.gz
$ sudo cp node_exporter-0.18.0.linux-amd64/node_exporter /sbin/
$ sudo chown root:root /sbin/node_exporter
$ sudo vi /etc/systemd/system/node-exporter.service

[Unit]
Description=Node Exporter for Prometheus
After=network.target

[Service]
Type=simple
User=prometheus
ExecStart=/sbin/node_exporter
PrivateTmp=true

[Install]
WantedBy=multi-user.target
$ sudo systemctl daemon-reload
$ sudo systemctl start node-exporter.service

http://ipアドレス:9100/metricsにアクセスできる
アクセスできない場合は、firewallをチェックする

  • ポート開放
$ firewall-cmd --add-port=9100/tcp --zone=public --permanent
$ firewall-cmd --reload
$ firewall-cmd --list-ports --zone=public

Prometheusのインストール

  • Prometheusユーザの作成
$ sudo groupadd prometheus
$ sudo useradd -d /var/lib/prometheus -g prometheus -s /bin/false -m prometheus
$ cd /usr/local/src/prometheus
$ wget https://github.com/prometheus/prometheus/releases/download/v2.9.2/prometheus-2.9.2.linux-amd64.tar.gz
$ tar xvzf prometheus-2.9.2.linux-amd64.tar.gz
$ sudo cp -a prometheus-2.9.2.linux-amd64 /usr/local/src/prometheus/prometheus
$ cd prometheus
$ sudo cp prometheus promtool /sbin/
$ sudo chown root:root /sbin/prometheus /sbin/promtool
$ sudo mkdir /etc/prometheus
$ sudo mkdir /var/lib/prometheus/data
$ sudo chown -R prometheus:prometheus /var/lib/prometheus/data
$ sudo cp -r prometheus.yml consoles console_libraries /etc/prometheus/
$ sudo chown -R root:prometheus /etc/prometheus
$ sudo vi /etc/systemd/system/prometheus.service

[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target

[Service]
User=prometheus
ExecStart=/sbin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus/data --web.console.templates=/etc/prometheus/consoles  --web.console.libraries=/etc/prometheus/console_libraries
ExecStop=/bin/kill -TERM ${MAINPID}
ExecReload=/bin/kill -HUP ${MAINPID}

[Install]
WantedBy=multi-user.target
$ sudo vi /etc/prometheus/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: ['localhost:9090']

  - job_name: 'node'

    file_sd_configs:
    - files:
       - /etc/prometheus/nodes.yml
$ sudo vi /etc/prometheus/nodes.yml

- targets:
   - localhost:9100
  labels:
    role: prometheus
  • ymlファイルのチェック
$ promtool check config /etc/prometheus/prometheus.yml 
$ systemctl daemon-reload
$ systemctl start prometheus.service
$ systemctl status prometheus.service

Grafanaインストール

grafana.com

$ sudo yum install -y https://dl.grafana.com/oss/release/grafana-5.4.2-1.x86_64.rpm
$ sudo systemctl start grafana-server.service 

http://ipアドレス:3000にアクセス 初期ID・Passwordはadmin

prometheusとgrafanaを入れたみたが、どうグラフとか出すといいのか? - Qiita

grafana.com

  • ポートを開放した場合、誰でもアクセスできるので認証をかけるか、閉じる
$ firewall-cmd --remove-port=9090/tcp --zone=public --permanent
$ firewall-cmd --remove-port=9100/tcp --zone=public --permanent
$ firewall-cmd --reload

prometheus.io