h9803660ブログ

ネットワーク関連の技術検証のメモ、ネットワーク製品動向、投資のこと等を気が向いた時に書くかもしれません。

DockerでPrometheusの実行メモ

自分向けのPrometheusの構築メモ

1.イメージの取得

Dcoker Hubにアクセスするとprometheus用のDocker Pull Commandが記載されている。

docker pull prom/prometheus

https://hub.docker.com/r/prom/prometheus/

実際に実行する

docker pull prom/prometheus
Using default tag: latest
latest: Pulling from prom/prometheus
76df9210b28c: Pull complete
559be8e06c14: Pull complete
f2a3d2457edd: Pull complete
c10a6fd58778: Pull complete
2ad73ff104d9: Pull complete
aec3e0b4446f: Pull complete
4cb49d0057e3: Pull complete
04db6a5b5334: Pull complete
ca0f04bfda22: Pull complete
ffd457fc1981: Pull complete
9fcdfe67a33b: Pull complete
d4f360942982: Pull complete
Digest: sha256:f7ffebdd428ba005e2c65ec1c7debdffdd39c43eb6bd0ddfa64ea7aba2a3fd14
Status: Downloaded newer image for prom/prometheus:latest
docker.io/prom/prometheus:latest

2.イメージの確認

docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
prom/prometheus     latest              6fa696e177e3        6 days ago  

3.Prometheusを起動

docker run -p 9090:9090 prom/prometheus
level=info ts=2020-11-22T14:35:11.088Z caller=main.go:315 msg="No time or size retention was set so using the default time retention" duration=15d
level=info ts=2020-11-22T14:35:11.088Z caller=main.go:353 msg="Starting Prometheus" version="(version=2.22.2, branch=HEAD, revision=de1c1243f4dd66fbac3e8213e9a7bd8dbc9f38b2)"
level=info ts=2020-11-22T14:35:11.088Z caller=main.go:358 build_context="(go=go1.15.5, user=root@f7d7e91063f0, date=20201116-12:43:43)"
level=info ts=2020-11-22T14:35:11.088Z caller=main.go:359 host_details="(Linux 4.18.0-193.28.1.el8_2.x86_64 #1 SMP Thu Oct 22 00:20:22 UTC 2020 x86_64 32b6cb369259 (none))"
level=info ts=2020-11-22T14:35:11.088Z caller=main.go:360 fd_limits="(soft=1048576, hard=1048576)"
level=info ts=2020-11-22T14:35:11.088Z caller=main.go:361 vm_limits="(soft=unlimited, hard=unlimited)"
level=info ts=2020-11-22T14:35:11.089Z caller=main.go:712 msg="Starting TSDB ..."
level=info ts=2020-11-22T14:35:11.098Z caller=web.go:516 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2020-11-22T14:35:11.100Z caller=head.go:642 component=tsdb msg="Replaying on-disk memory mappable chunks if any"
level=info ts=2020-11-22T14:35:11.100Z caller=head.go:656 component=tsdb msg="On-disk memory mappable chunks replay completed" duration=1.901μs
level=info ts=2020-11-22T14:35:11.100Z caller=head.go:662 component=tsdb msg="Replaying WAL, this may take a while"
level=info ts=2020-11-22T14:35:11.100Z caller=head.go:714 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
level=info ts=2020-11-22T14:35:11.100Z caller=head.go:719 component=tsdb msg="WAL replay completed" checkpoint_replay_duration=15.602μs wal_replay_duration=163.428μs total_replay_duration=195.54μs
level=info ts=2020-11-22T14:35:11.101Z caller=main.go:732 fs_type=XFS_SUPER_MAGIC
level=info ts=2020-11-22T14:35:11.101Z caller=main.go:735 msg="TSDB started"
level=info ts=2020-11-22T14:35:11.101Z caller=main.go:861 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
level=info ts=2020-11-22T14:35:11.102Z caller=main.go:892 msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=539.829μs remote_storage=1.65μs web_handler=266ns query_engine=554ns scrape=253.613μs scrape_sd=24.038μs notify=19.707μs notify_sd=28.324μs rules=889ns
level=info ts=2020-11-22T14:35:11.102Z caller=main.go:684 msg="Server is ready to receive web requests."

^Clevel=warn ts=2020-11-22T14:36:34.658Z caller=main.go:562 msg="Received SIGTERM, exiting gracefully..."
level=info ts=2020-11-22T14:36:34.658Z caller=main.go:585 msg="Stopping scrape discovery manager..."
level=info ts=2020-11-22T14:36:34.658Z caller=main.go:599 msg="Stopping notify discovery manager..."
level=info ts=2020-11-22T14:36:34.658Z caller=main.go:621 msg="Stopping scrape manager..."
level=info ts=2020-11-22T14:36:34.658Z caller=main.go:581 msg="Scrape discovery manager stopped"
level=info ts=2020-11-22T14:36:34.658Z caller=main.go:595 msg="Notify discovery manager stopped"
level=info ts=2020-11-22T14:36:34.658Z caller=manager.go:924 component="rule manager" msg="Stopping rule manager..."
level=info ts=2020-11-22T14:36:34.658Z caller=manager.go:934 component="rule manager" msg="Rule manager stopped"
level=info ts=2020-11-22T14:36:34.659Z caller=main.go:615 msg="Scrape manager stopped"
level=info ts=2020-11-22T14:36:34.659Z caller=notifier.go:601 component=notifier msg="Stopping notification manager..."
level=info ts=2020-11-22T14:36:34.659Z caller=main.go:789 msg="Notifier manager stopped"
level=info ts=2020-11-22T14:36:34.660Z caller=main.go:801 msg="See you next time!"
[root@localhost keima-s]# docker run -p 9090:9090 prom/prometheus
level=info ts=2020-11-22T14:36:41.012Z caller=main.go:315 msg="No time or size retention was set so using the default time retention" duration=15d
level=info ts=2020-11-22T14:36:41.013Z caller=main.go:353 msg="Starting Prometheus" version="(version=2.22.2, branch=HEAD, revision=de1c1243f4dd66fbac3e8213e9a7bd8dbc9f38b2)"
level=info ts=2020-11-22T14:36:41.013Z caller=main.go:358 build_context="(go=go1.15.5, user=root@f7d7e91063f0, date=20201116-12:43:43)"
level=info ts=2020-11-22T14:36:41.013Z caller=main.go:359 host_details="(Linux 4.18.0-193.28.1.el8_2.x86_64 #1 SMP Thu Oct 22 00:20:22 UTC 2020 x86_64 e645299537df (none))"
level=info ts=2020-11-22T14:36:41.013Z caller=main.go:360 fd_limits="(soft=1048576, hard=1048576)"
level=info ts=2020-11-22T14:36:41.013Z caller=main.go:361 vm_limits="(soft=unlimited, hard=unlimited)"
level=info ts=2020-11-22T14:36:41.014Z caller=main.go:712 msg="Starting TSDB ..."
level=info ts=2020-11-22T14:36:41.022Z caller=web.go:516 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2020-11-22T14:36:41.023Z caller=head.go:642 component=tsdb msg="Replaying on-disk memory mappable chunks if any"
level=info ts=2020-11-22T14:36:41.023Z caller=head.go:656 component=tsdb msg="On-disk memory mappable chunks replay completed" duration=1.999μs
level=info ts=2020-11-22T14:36:41.023Z caller=head.go:662 component=tsdb msg="Replaying WAL, this may take a while"
level=info ts=2020-11-22T14:36:41.023Z caller=head.go:714 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
level=info ts=2020-11-22T14:36:41.023Z caller=head.go:719 component=tsdb msg="WAL replay completed" checkpoint_replay_duration=16.859μs wal_replay_duration=202.096μs total_replay_duration=236.534μs
level=info ts=2020-11-22T14:36:41.024Z caller=main.go:732 fs_type=XFS_SUPER_MAGIC
level=info ts=2020-11-22T14:36:41.024Z caller=main.go:735 msg="TSDB started"
level=info ts=2020-11-22T14:36:41.024Z caller=main.go:861 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
level=info ts=2020-11-22T14:36:41.025Z caller=main.go:892 msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=439.448μs remote_storage=1.627μs web_handler=174ns query_engine=660ns scrape=179.743μs scrape_sd=20.808μs notify=17.355μs notify_sd=22.231μs rules=1.038μs
level=info ts=2020-11-22T14:36:41.025Z caller=main.go:684 msg="Server is ready to receive web requests."

4.ブラウザで確認

Webブラウザを使ってlocalhost:9090でアクセス。起動を確認。

公式の手順をそのまま使ってみただけなので特にひねりもないが簡単にPrometheusの起動確認ができた。

prometheus.io

追記

5.バインドマウント

prometheus.ymlを作成し、実行。バインドマウントをする。/tmp/prometheusというディレクトリを作りその中にprometheus.ymlを入れた。

prometheus.ymlの設定は以下に記載のものをそのまま使う。

https://prometheus.io/docs/prometheus/latest/getting_started/

prometheus.yml(公式の設定そのまま)

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'

# 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'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']

バインドマウント実行

docker run -d --name prometheus -p 9090:9090 -v /tmp/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
c466c90d88b5c1a144f13c28a4d258edcef77840cb5c72196d763f7ad312d993

※バインドマウントはdockerの公式ドキュメントだと-vより --mount使った方が良いとあるがprometheusの公式で使っているコマンドが-vだったのでそのまま利用

確認

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
c466c90d88b5        prom/prometheus     "/bin/prometheus --c…"   5 minutes ago       Up 5 minutes        0.0.0.0:9090->9090/tcp   prometheus

--mount使った場合はこんな構文になる。

docker run -d --name prometheus -p 9090:9090 --mount type=bind,src=/tmp/prometheus/prometheus.yml,dst=/etc/prometheus/prometheus.yml prom/prometheus
9e53cda6fa1193fc101dfaa7b6a0a9f5c32e9b34e9d4fc9cdf1dc71ddd20f9aa