如何在 Linux 上安裝和配置 Prometheus? (Ubuntu 和 CentOS)
已發表: 2019-08-26什麼是普羅米修斯?
Prometheus 是世界一流的監控系統,默認帶有時間序列數據庫。 它是最初於 2012 年構建的開源系統。
世界 500 強公司一直在使用 Prometheus 來收集其基礎設施和應用程序的指標。 Prometheus 支持多維數據模型,時間序列數據主要標識為 Metrics。
沒有特殊的數據存儲要求,因為 Prometheus 在其自己的安裝中使用了時序Time Series DB
(TSDB) 部分。
在本教程中,我們將通過超級簡單的步驟在 Linux Ubuntu 和 CentOS 上安裝 Prometheus。
讓我們開始吧:
Step-1 驗證 Linux 操作系統版本
命令: cat /etc/os-release
1 2 3 4 5 6 7 8 9 10 11 12 13 |
root @ localhost : ~ # cat /etc/os-release NAME = "Ubuntu" VERSION = "19.04 (Disco Dingo)" ID = ubuntu ID_LIKE = debian PRETTY_NAME = "Ubuntu 19.04" VERSION_ID = "19.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 = disco UBUNTU_CODENAME = disco |
以下步驟適用於Ubuntu Linux OS
。
Step-2 創建 Prometheus 數據和配置文件夾
命令:
1 2 3 |
root @ localhost : ~ # sudo mkdir /crunchify/prometheus/conf root @ localhost : ~ # sudo mkdir /crunchify/prometheus/data |
讓我們驗證兩個文件夾:
1 2 3 4 5 6 7 8 9 |
root @ localhost : / crunchify / prometheus # pwd / crunchify / prometheus root @ localhost : / crunchify / prometheus # ls -ltra total 16 drwxr - xr - x 2 root root 4096 Aug 25 23 : 37 conf drwxr - xr - x 3 root root 4096 Aug 25 23 : 37 . . drwxr - xr - x 2 root root 4096 Aug 25 23 : 37 data drwxr - xr - x 4 root root 4096 Aug 25 23 : 37 . |
Step-3 更改數據文件夾的所有權
這是避免後續步驟中的權限問題所必需的。
1 |
root @ localhost : / crunchify / prometheus # sudo chown 65534:65534 /crunchify/prometheus/data/ |
Step-4 創建 prometheus.yml 文件
轉到文件夾/crunchify/prometheus/conf
並創建文件prometheus.yml
。
1 2 3 |
root @ localhost : / # cd /crunchify/prometheus/conf root @ localhost : / crunchify / prometheus / conf # vi prometheus.yml |
prometheus.yml 文件內容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 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 time series scraped from this config. - job_name : 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs : - targets : [ '65.19.71.11:9090' ] |
Note:
在上述文件中 - 將 IP 替換為您的主機/虛擬機 IP。
這是本教程的Directory structure
:
步驟 5 確保您已在 VM 上安裝 Docker。
如何檢查 Docker 是否安裝?
1 2 3 |
root @ localhost : ~ # docker Command 'docker' not found |
這意味著 docker 未安裝在您的主機/VM 上。 請完全按照 Docker 安裝教程在您的主機/VM 上安裝 Docker。
Step-6 安裝 Docker Compose
Pre-requisite
是在執行以下步驟之前先安裝 Docker。 (第 5 步)
執行以下三個命令:
1 2 3 4 5 6 7 8 9 |
root @ localhost : ~ # sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 617 0 617 0 0 2448 0 -- : -- : -- -- : -- : -- -- : -- : -- 2448 100 15.4M 100 15.4M 0 0 11.2M 0 0 : 00 : 01 0 : 00 : 01 -- : -- : -- 15.7M root @ localhost : ~ # sudo chmod +x /usr/local/bin/docker-compose root @ localhost : ~ # sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose |
Step-7 驗證 Docker Compose 版本
1 2 3 |
root @ localhost : ~ # docker-compose --version docker - compose version 1.24.1 , build 4667896b |
恭喜 - docker compose 已成功安裝在您的 VM 上。
Step-8 創建 docker-compose.yml 文件
轉到文件夾 /crunchify/prometheus。
1 2 3 |
root @ localhost : / crunchify # cd /crunchify/prometheus/ root @ localhost : / crunchify / prometheus # vi docker-compose.yml |
docker-compose.yml 文件內容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
version : "3" services : prometheus : image : prom / prometheus : latest container_name : prometheus volumes : - / crunchify / prometheus / conf : / etc / prometheus - / crunchify / prometheus / data : / prometheus command : - '--config.file=/etc/prometheus/prometheus.yml' - '--storage.tsdb.path=/prometheus' ports : - "9090:9090" |
Step-9 安裝 Prometheus Start/Stop 作為系統控制的一部分。
創建文件vi /etc/systemd/system/prometheus.service
prometheus.service 文件內容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[ Unit ] Description = Prometheus monitoring docker container After = docker . service BindsTo = docker . service [ Service ] Restart = always WorkingDirectory =/ crunchify / prometheus / # Ubuntu ExecStart =/ usr / bin / docker - compose up ExecStop =/ usr / bin / docker - compose down # CentOS #ExecStart=/usr/bin/docker-compose up #ExecStop=/usr/bin/docker-compose down [ Install ] WantedBy = multi - user . target |

Step-10 啟動 Prometheus 服務
1 2 3 4 5 6 |
root @ localhost : / crunchify / prometheus / config # sudo systemctl daemon-reload root @ localhost : / crunchify / prometheus / config # sudo systemctl enable prometheus Created symlink / etc / systemd / system / multi - user . target . wants / prometheus . service → / etc / systemd / system / prometheus . service . root @ localhost : / crunchify / prometheus / config # sudo systemctl start prometheus |
Prometheus 服務器在 Linux 上啟動:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
root @ localhost : / crunchify / prometheus # sudo systemctl start prometheus Creating network "prometheus_default" with the default driver Pulling prometheus ( prom / prometheus : latest ) . . . latest : Pulling from prom / prometheus 8e674ad76dce : Pull complete e77d2419d1c2 : Pull complete d7c0a2a2ca7e : Pull complete bd46de67c79d : Pull complete 35f6078df7e9 : Pull complete 8b3f04e26151 : Pull complete 9d234d9c8a77 : Pull complete 1a8a2a4f314d : Pull complete 62e28fd716ec : Pull complete Digest : sha256 : cd93b8711bb92eb9c437d74217311519e0a93bc55779aa664325dc83cd13cb32 Status : Downloaded newer image for prom / prometheus : latest Creating prometheus . . . done Attaching to prometheus prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.796Z caller = main . go : 293 msg = "no time or size retention was set so using the default time retention" duration = 15d prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.797Z caller = main . go : 329 msg = "Starting Prometheus" version = "(version=2.12.0, branch=HEAD, revision=43acd0e2e93f9f70c49b2267efa0124f1e759e86)" prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.798Z caller = main . go : 330 build_context = "(go=go1.12.8, user=root@7a9dbdbe0cc7, date=20190818-13:53:16)" prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.798Z caller = main . go : 331 host_details = "(Linux 5.0.0-13-generic #14-Ubuntu SMP Mon Apr 15 14:59:14 UTC 2019 x86_64 d870070a8ec7 (none))" prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.799Z caller = main . go : 332 fd_limits = "(soft=1048576, hard=1048576)" prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.799Z caller = main . go : 333 vm_limits = "(soft=unlimited, hard=unlimited)" prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.803Z caller = main . go : 654 msg = "Starting TSDB ..." prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.813Z caller = head . go : 509 component = tsdb msg = "replaying WAL, this may take awhile" prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.815Z caller = head . go : 557 component = tsdb msg = "WAL segment loaded" segment = 0 maxSegment = 0 prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.818Z caller = main . go : 669 fs_type = EXT4_SUPER_MAGIC prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.819Z caller = main . go : 670 msg = "TSDB started" prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.819Z caller = main . go : 740 msg = "Loading configuration file" filename =/ etc / prometheus / prometheus . yml prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.823Z caller = main . go : 768 msg = "Completed loading of configuration file" filename =/ etc / prometheus / prometheus . yml prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.823Z caller = main . go : 623 msg = "Server is ready to receive web requests." prometheus | level = info ts = 2019 - 08 - 26T02 : 57 : 48.824Z caller = web . go : 448 component = web msg = "Start listening for connections" address = 0.0.0.0 : 9090 |
恭喜!! 您已在主機上成功啟動 Prometheus。
讓我們玩一下 Prometheus Dashboard 和 UI
如何驗證 Prometheus 是否正常運行?
1 |
URL : http : //65.19.71.11:9090/metrics |
如何使用 Prometheus 驗證主機或虛擬機的 UP 狀態?
1 |
URL : http : //65.19.71.11:9090/targets |
如何在瀏覽器上查看 Prometheus 配置?
Prometheus 運行時信息
1 |
URL : http : //65.19.71.11:9090/status |
正常運行時間 | 2019-08-26 03:07:24.244852166 +0000 UTC |
---|---|
工作目錄 | /普羅米修斯 |
配置重新加載 | 成功的 |
上次成功的配置重新加載 | 2019-08-26 03:07:24 +0000 UTC |
頭塊 | 533 |
頭部時間序列 | 533 |
WAL 腐敗 | 0 |
協程 | 36 |
GOMAXPROCS | 1 |
GOGC | |
神蟲 | |
存儲保留 | 15天 |
普羅米修斯構建信息
版本 | 2.12.0 |
---|---|
修訂 | 43acd0e2e93f9f70c49b2267efa0124f1e759e86 |
分支 | 頭 |
構建用戶 | 根@7a9dbdbe0cc7 |
建造日期 | 20190818-13:53:16 |
版本 | 去1.12.8 |
Prometheus 命令行標誌:
1 |
URL : http : //65.19.71.11:9090/flags |
alertmanager.notification-queue-容量 | 10000 |
---|---|
alertmanager.timeout | 10s |
配置文件 | /etc/prometheus/prometheus.yml |
日誌格式 | 日誌文件 |
日誌級別 | 信息 |
query.lookback-delta | 5m |
query.max-並發 | 20 |
query.max-samples | 50000000 |
查詢超時 | 2m |
rules.alert.for-grace-period | 10m |
rules.alert.for-outage-tolerance | 1小時 |
rules.alert.resend-delay | 1m |
storage.remote.flush-deadline | 1m |
storage.remote.read-concurrent-limit | 10 |
storage.remote.read-sample-limit | 50000000 |
storage.tsdb.allow-overlapping-blocks | 錯誤的 |
storage.tsdb.max-block-duration | 36小時 |
storage.tsdb.min-block-duration | 2小時 |
storage.tsdb.no-lockfile | 錯誤的 |
storage.tsdb.path | /普羅米修斯 |
storage.tsdb.retention | 0s |
storage.tsdb.retention.size | 0B |
storage.tsdb.retention.time | 0s |
storage.tsdb.wal-壓縮 | 錯誤的 |
storage.tsdb.wal 段大小 | 0B |
web.console.libraries | 控制台庫 |
web.console.templates | 控制台 |
web.cors.origin | .* |
web.enable-admin-api | 錯誤的 |
web.enable-生命週期 | 錯誤的 |
web.external-url | |
web.listen-address | 0.0.0.0:9090 |
web.max-連接 | 512 |
web.page-title | Prometheus 時序採集和處理服務器 |
web.read-timeout | 5m |
web.route-前綴 | / |
你都準備好了。 您已successfully installed Prometheus
並驗證了它的所有功能。