安装

官方下载地址:https://prometheus.io/download/
Github下载地址:https://github.com/prometheus/prometheus/releases

二进制文件安装

根据宿主机的系统版本选择下载文件
image.png
如果是Linux系统

  1. $ wget https://github.com/prometheus/prometheus/releases/download/v2.21.0/prometheus-2.21.0.linux-amd64.tar.gz
  2. $ tar -zxvf prometheus-2.21.0.linux-amd64.tar.gz

如果是mac os 系统则选择darwin-amd64

  1. $ wget https://github.com/prometheus/prometheus/releases/download/v2.17.1/prometheus-2.17.1.darwin-amd64.tar.gz
  2. $ tar -zxvf prometheus-2.17.1.darwin-amd64.tar.gz -C ./
  3. $ cd prometheus-2.17.1.darwin-amd64
  4. $ tree
  5. .
  6. ├── LICENSE
  7. ├── NOTICE
  8. ├── console_libraries
  9. ├── menu.lib
  10. └── prom.lib
  11. ├── consoles
  12. ├── index.html.example
  13. ├── node-cpu.html
  14. ├── node-disk.html
  15. ├── node-overview.html
  16. ├── node.html
  17. ├── prometheus-overview.html
  18. └── prometheus.html
  19. ├── prometheus
  20. ├── prometheus.yml
  21. ├── promtool
  22. └── tsdb
  23. 2 directories, 15 files

Docker安装

dockerhub 地址:https://hub.docker.com/r/prom/prometheus/,执行下面的docker命令,然后通过 http://localhost:9090/ 来访问容器内的 Prometheus 服务

  1. $ docker run --name prometheus -d -p 127.0.0.1:9090:9090 prom/prometheus

官方Dockfile文件:https://github.com/prometheus/prometheus/blob/master/Dockerfile

增加配置文件本地路径

  1. docker run \
  2. -p 9090:9090 \
  3. -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml \
  4. prom/prometheus

源码安装

  1. $ mkdir -p $GOPATH/src/github.com/prometheus
  2. $ cd $GOPATH/src/github.com/prometheus
  3. $ git clone https://github.com/prometheus/prometheus.git
  4. $ cd prometheus
  5. $ make build
  6. $ ./prometheus -config.file=your_config.yml

启动参数

帮助文档

  1. $ ./prometheus -h
  2. usage: prometheus [<flags>]
  3. The Prometheus monitoring server
  4. Flags:
  5. -h, --help Show context-sensitive help (also try --help-long and --help-man).
  6. --version Show application version.
  7. --config.file="prometheus.yml"
  8. Prometheus configuration file path.
  9. --web.listen-address="0.0.0.0:9090"
  10. Address to listen on for UI, API, and telemetry.
  11. --web.read-timeout=5m Maximum duration before timing out read of the request, and closing idle connections.
  12. --web.max-connections=512 Maximum number of simultaneous connections.
  13. --web.external-url=<URL> The URL under which Prometheus is externally reachable (for example, if Prometheus is served via a
  14. reverse proxy). Used for generating relative and absolute links back to Prometheus itself. If the URL
  15. has a path portion, it will be used to prefix all HTTP endpoints served by Prometheus. If omitted,
  16. relevant URL components will be derived automatically.
  17. --web.route-prefix=<path> Prefix for the internal routes of web endpoints. Defaults to path of --web.external-url.
  18. --web.user-assets=<path> Path to static asset directory, available at /user.
  19. --web.enable-lifecycle Enable shutdown and reload via HTTP request.
  20. --web.enable-admin-api Enable API endpoints for admin control actions.
  21. --web.console.templates="consoles"
  22. Path to the console template directory, available at /consoles.
  23. --web.console.libraries="console_libraries"
  24. Path to the console library directory.
  25. --web.page-title="Prometheus Time Series Collection and Processing Server"
  26. Document title of Prometheus instance.
  27. --web.cors.origin=".*" Regex for CORS origin. It is fully anchored. Example: 'https?://(domain1|domain2)\.com'
  28. --storage.tsdb.path="data/"
  29. Base path for metrics storage.
  30. --storage.tsdb.retention=STORAGE.TSDB.RETENTION
  31. [DEPRECATED] How long to retain samples in storage. This flag has been deprecated, use
  32. "storage.tsdb.retention.time" instead.
  33. --storage.tsdb.retention.time=STORAGE.TSDB.RETENTION.TIME
  34. How long to retain samples in storage. When this flag is set it overrides "storage.tsdb.retention". If
  35. neither this flag nor "storage.tsdb.retention" nor "storage.tsdb.retention.size" is set, the retention
  36. time defaults to 15d. Units Supported: y, w, d, h, m, s, ms.
  37. --storage.tsdb.retention.size=STORAGE.TSDB.RETENTION.SIZE
  38. [EXPERIMENTAL] Maximum number of bytes that can be stored for blocks. Units supported: KB, MB, GB, TB,
  39. PB. This flag is experimental and can be changed in future releases.
  40. --storage.tsdb.no-lockfile
  41. Do not create lockfile in data directory.
  42. --storage.tsdb.allow-overlapping-blocks
  43. [EXPERIMENTAL] Allow overlapping blocks, which in turn enables vertical compaction and vertical query
  44. merge.
  45. --storage.tsdb.wal-compression
  46. Compress the tsdb WAL.
  47. --storage.remote.flush-deadline=<duration>
  48. How long to wait flushing sample on shutdown or config reload.
  49. --storage.remote.read-sample-limit=5e7
  50. Maximum overall number of samples to return via the remote read interface, in a single query. 0 means no
  51. limit. This limit is ignored for streamed response types.
  52. --storage.remote.read-concurrent-limit=10
  53. Maximum number of concurrent remote read calls. 0 means no limit.
  54. --storage.remote.read-max-bytes-in-frame=1048576
  55. Maximum number of bytes in a single frame for streaming remote read response types before marshalling.
  56. Note that client might have limit on frame size as well. 1MB as recommended by protobuf by default.
  57. --rules.alert.for-outage-tolerance=1h
  58. Max time to tolerate prometheus outage for restoring "for" state of alert.
  59. --rules.alert.for-grace-period=10m
  60. Minimum duration between alert and restored "for" state. This is maintained only for alerts with
  61. configured "for" time greater than grace period.
  62. --rules.alert.resend-delay=1m
  63. Minimum amount of time to wait before resending an alert to Alertmanager.
  64. --alertmanager.notification-queue-capacity=10000
  65. The capacity of the queue for pending Alertmanager notifications.
  66. --alertmanager.timeout=10s
  67. Timeout for sending alerts to Alertmanager.
  68. --query.lookback-delta=5m The maximum lookback duration for retrieving metrics during expression evaluations and federation.
  69. --query.timeout=2m Maximum time a query may take before being aborted.
  70. --query.max-concurrency=20
  71. Maximum number of queries executed concurrently.
  72. --query.max-samples=50000000
  73. Maximum number of samples a single query can load into memory. Note that queries will fail if they try
  74. to load more samples than this into memory, so this also limits the number of samples a query can
  75. return.
  76. --log.level=info Only log messages with the given severity or above. One of: [debug, info, warn, error]
  77. --log.format=logfmt Output format of log messages. One of: [logfmt, json]

数据存储

默认的存储路径为data/,通过参数--storage.tsdb.path="data/"修改本地数据存储的路径。

  1. mkdir -p data

配置文件

当前通过二进制压缩文件安装在 解压完成的目录会包含默认的Prometheus配置文件promethes.yml,

  1. $ ls -l
  2. total 161140
  3. drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 11 21:29 console_libraries
  4. drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 11 21:29 consoles
  5. -rw-r--r-- 1 ubuntu ubuntu 11357 Sep 11 21:29 LICENSE
  6. -rw-r--r-- 1 ubuntu ubuntu 3420 Sep 11 21:29 NOTICE
  7. -rwxr-xr-x 1 ubuntu ubuntu 88471209 Sep 11 19:37 prometheus
  8. -rw-r--r-- 1 ubuntu ubuntu 926 Sep 11 21:29 prometheus.yml
  9. -rwxr-xr-x 1 ubuntu ubuntu 76493104 Sep 11 19:39 promtool

加载自定义的配置文件路径

  1. ./prometheus --config.file=启动配置文件.yml

默认启动

启动prometheus服务,其会默认加载当前路径下的prometheus.yaml文件

  1. $ ./prometheus

启动信息

  1. level=info ts=2020-10-11T06:36:14.389Z caller=main.go:310 msg="No time or size retention was set so using the default time retention" duration=15d
  2. level=info ts=2020-10-11T06:36:14.389Z caller=main.go:346 msg="Starting Prometheus" version="(version=2.21.0, branch=HEAD, revision=e83ef207b6c2398919b69cd87d2693cfc2fb4127)"
  3. level=info ts=2020-10-11T06:36:14.389Z caller=main.go:347 build_context="(go=go1.15.2, user=root@a4d9bea8479e, date=20200911-11:35:02)"
  4. level=info ts=2020-10-11T06:36:14.389Z caller=main.go:348 host_details="(Linux 4.15.0-54-generic #58-Ubuntu SMP Mon Jun 24 10:55:24 UTC 2019 x86_64 VM-0-2-ubuntu (none))"
  5. level=info ts=2020-10-11T06:36:14.389Z caller=main.go:349 fd_limits="(soft=1024, hard=1048576)"
  6. level=info ts=2020-10-11T06:36:14.389Z caller=main.go:350 vm_limits="(soft=unlimited, hard=unlimited)"
  7. level=info ts=2020-10-11T06:36:14.396Z caller=main.go:701 msg="Starting TSDB ..."
  8. level=info ts=2020-10-11T06:36:14.397Z caller=web.go:523 component=web msg="Start listening for connections" address=0.0.0.0:9090
  9. level=info ts=2020-10-11T06:36:14.402Z caller=head.go:644 component=tsdb msg="Replaying on-disk memory mappable chunks if any"
  10. level=info ts=2020-10-11T06:36:14.402Z caller=head.go:658 component=tsdb msg="On-disk memory mappable chunks replay completed" duration=4.345µs
  11. level=info ts=2020-10-11T06:36:14.403Z caller=head.go:664 component=tsdb msg="Replaying WAL, this may take a while"
  12. level=info ts=2020-10-11T06:36:14.403Z caller=head.go:716 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
  13. level=info ts=2020-10-11T06:36:14.403Z caller=head.go:719 component=tsdb msg="WAL replay completed" checkpoint_replay_duration=32.293µs wal_replay_duration=194.417µs total_replay_duration=277.95µs
  14. level=info ts=2020-10-11T06:36:14.404Z caller=main.go:721 fs_type=EXT4_SUPER_MAGIC
  15. level=info ts=2020-10-11T06:36:14.404Z caller=main.go:724 msg="TSDB started"
  16. level=info ts=2020-10-11T06:36:14.404Z caller=main.go:850 msg="Loading configuration file" filename=prometheus.yml
  17. level=info ts=2020-10-11T06:36:14.409Z caller=main.go:881 msg="Completed loading of configuration file" filename=prometheus.yml totalDuration=4.560441ms remote_storage=2.148µs web_handler=676ns query_engine=1.153µs scrape=2.568956ms scrape_sd=36.388µs notify=1.581758ms notify_sd=47.157µs rules=1.829µs
  18. level=info ts=2020-10-11T06:36:14.409Z caller=main.go:673 msg="Server is ready to receive web requests."

Expression Browser

Prometheus的-web.listen-address=监听的就是PromQL浏览器的地址,例如:-web.listen-address=:9090,就以http://serverip:9090/graph访问PromQL浏览器。
启动完成后,如果是本地自检环境可以通过 http://localhost:9090/graph
image.png

http://localhost:9090/metrics
image.png
其输入表达式控制台:

  1. promhttp_metric_handler_requests_total

image.png


部署优化

远端存储

prometheus默认是将监控数据保存在本地磁盘中的,当然在分布式架构环境下,这样是不太可取的。不过它支持远端存储,可与远端存储系统集成。
Prometheus integrates with remote storage systems in two ways:

  • Prometheus can write samples that it ingests to a remote URL in a standardized format.
  • Prometheus can read (back) sample data from a remote URL in a standardized format.

Prometheus——安装 - 图5
目前支持的远端存储系统如下:
The remote write and remote read features of Prometheus allow transparently sending and receiving samples. This is primarily intended for long term storage. It is recommended that you perform careful evaluation of any solution in this space to confirm it can handle your data volumes.

https://juejin.im/post/5d04ab5b5188254a6c23e987#heading-8

https://blog.csdn.net/qq_32415063/article/details/105607008