CLOVER🍀

That was when it all began.

Ubuntu Linux 18.04 LTSに、Prometheusをインストールする

Ubuntu Linux 18.04 LTSに、Prometheusをインストールしてみます。

環境。

$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="18.04.1 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.1 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

オフィシャルのドキュメントを見ると、tar.gzをダウンロードして展開するか、Dockerイメージを使うか、みたいですね。

Getting started | Prometheus

Installation | Prometheus

今回は、tar.gzをダウンロードしてインストールしてみます。

$ wget https://github.com/prometheus/prometheus/releases/download/v2.5.0/prometheus-2.5.0.linux-amd64.tar.gz
$ tar xvf prometheus-2.5.0.linux-amd64.tar.gz
$ cd prometheus-2.5.0.linux-amd64

tar.gzを展開すると、こんなファイルが現れます。

$ ls -l
合計 94944
-rw-r--r-- 1 xxxxx xxxxx    11357 11月  6 21:39 LICENSE
-rw-r--r-- 1 xxxxx xxxxx     2769 11月  6 21:39 NOTICE
drwxr-xr-x 2 xxxxx xxxxx     4096 11月  6 21:39 console_libraries
drwxr-xr-x 2 xxxxx xxxxx     4096 11月  6 21:39 consoles
-rwxr-xr-x 1 xxxxx xxxxx 58074162 11月  6 20:41 prometheus
-rw-r--r-- 1 xxxxx xxxxx      926 11月  6 21:39 prometheus.yml
-rwxr-xr-x 1 xxxxx xxxxx 39113694 11月  6 20:43 promtool

「prometheus」というファイルを実行すれば、起動しそうですね。

$ ./prometheus 
level=info ts=2018-12-01T15:44:12.434082075Z caller=main.go:244 msg="Starting Prometheus" version="(version=2.5.0, branch=HEAD, revision=67dc912ac8b24f94a1fc478f352d25179c94ab9b)"
level=info ts=2018-12-01T15:44:12.434131319Z caller=main.go:245 build_context="(go=go1.11.1, user=root@578ab108d0b9, date=20181106-11:40:44)"
level=info ts=2018-12-01T15:44:12.434150488Z caller=main.go:246 host_details="(Linux 4.15.0-39-generic #42-Ubuntu SMP Tue Oct 23 15:48:01 UTC 2018 x86_64 xxxxx (none))"
level=info ts=2018-12-01T15:44:12.434167836Z caller=main.go:247 fd_limits="(soft=1024, hard=4096)"
level=info ts=2018-12-01T15:44:12.43418231Z caller=main.go:248 vm_limits="(soft=unlimited, hard=unlimited)"
level=info ts=2018-12-01T15:44:12.434670645Z caller=web.go:399 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2018-12-01T15:44:12.434642133Z caller=main.go:562 msg="Starting TSDB ..."
level=info ts=2018-12-01T15:44:12.438996469Z caller=main.go:572 msg="TSDB started"
level=info ts=2018-12-01T15:44:12.439049116Z caller=main.go:632 msg="Loading configuration file" filename=prometheus.yml
level=info ts=2018-12-01T15:44:12.440112435Z caller=main.go:658 msg="Completed loading of configuration file" filename=prometheus.yml
level=info ts=2018-12-01T15:44:12.440131604Z caller=main.go:531 msg="Server is ready to receive web requests."

起動がシンプルで良いですね。

これで、「http://localhost:9090」にアクセスすると、PrometheusのUIを見ることができます。 f:id:Kazuhira:20181202004516p:plain

なお、この時に読んでいる設定ファイルは

level=info ts=2018-12-01T15:44:12.440112435Z caller=main.go:658 msg="Completed loading of configuration file" filename=prometheus.yml

「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']

収集先が「prometheus」、「localhost:9090」なので、Prometheus自身の情報を収集するようになっていますね。

カレントディレクトリにある「prometheus.yml」を読むようにデフォルトで構成されているので、これが見つからないと
起動に失敗します。

level=info ts=2018-12-01T15:48:16.478390171Z caller=main.go:632 msg="Loading configuration file" filename=prometheus.yml
level=info ts=2018-12-01T15:48:16.478427004Z caller=main.go:431 msg="Stopping scrape discovery manager..."
level=info ts=2018-12-01T15:48:16.478438562Z caller=main.go:445 msg="Stopping notify discovery manager..."
level=info ts=2018-12-01T15:48:16.478447031Z caller=main.go:467 msg="Stopping scrape manager..."
level=info ts=2018-12-01T15:48:16.478456071Z caller=main.go:441 msg="Notify discovery manager stopped"
level=info ts=2018-12-01T15:48:16.478475974Z caller=main.go:427 msg="Scrape discovery manager stopped"
level=info ts=2018-12-01T15:48:16.478492641Z caller=main.go:461 msg="Scrape manager stopped"
level=info ts=2018-12-01T15:48:16.478496289Z caller=manager.go:657 component="rule manager" msg="Stopping rule manager..."
level=info ts=2018-12-01T15:48:16.478523964Z caller=manager.go:663 component="rule manager" msg="Rule manager stopped"
level=info ts=2018-12-01T15:48:16.488035236Z caller=notifier.go:512 component=notifier msg="Stopping notification manager..."
level=info ts=2018-12-01T15:48:16.488072431Z caller=main.go:616 msg="Notifier manager stopped"
level=error ts=2018-12-01T15:48:16.488150762Z caller=main.go:625 err="error loading config from \"prometheus.yml\": couldn't load configuration (--config.file=\"prometheus.yml\"): open prometheus.yml: no such file or directory"

設定ファイルのパスを変更する場合は、「--config.file」オプションを使用するようです。

$ ./prometheus --config.file=/path/to/prometheus.yml

ヘルプでの情報。

      --config.file="prometheus.yml"  
                                 Prometheus configuration file path.

けっこう簡単に起動できるものですね。

なお、Prometheusを起動すると、カレントに「data」というディレクトリができ、この中にデータが入るようです。

drwxr-xr-x 3 xxxxx xxxxx     4096 12月  2 00:43 data/

変更する場合は、「--storage.tsdb.path」オプションで。

      --storage.tsdb.path="data/"  
                                 Base path for metrics storage.

ヘルプを見ると、これらのオプションを確認することできます。

$ ./prometheus -h

オマケ/aptでインストールしたい

とはいえ、できればaptでインストールしたいなぁとも思うもの。ちょっと調べてみました。

How To Install Prometheus on Ubuntu 18.04 LTS

こちらを参考にすればよさそうです。

ただし、実際に起動してみると、ちょっと古かったです…。

f:id:Kazuhira:20181202012341p:plain

一応、手順は載せておきます。

キーの追加。

$ curl -fsSL https://s3-eu-west-1.amazonaws.com/deb.robustperception.io/41EFC99D.gpg | sudo apt-key add -
OK

リポジトリのアップデートと、Prometheusのインストール。
※一緒にいろいろインストールしているものの、「prometheus」以外は今回は使いません

$ sudo apt update
$ sudo apt install prometheus prometheus-node-exporter prometheus-pushgateway prometheus-alertmanager

systemdに登録、起動。

$ sudo systemctl enable prometheus
$ sudo systemctl start prometheus

これで、systemdの制御下でPrometheusを使うことができます。

ログや設定ファイルは、こちらにあるようです。

/var/log/prometheus/prometheus.log
/etc/prometheus/prometheus.yml