二進制方式
-
下載安裝包 最新安裝包
$ mkdir -p /opt/k8s/prometheus $ cd /opt/k8s/prometheus $ wget https://github.com/prometheus/prometheus/releases/download/v2.16.0/prometheus-2.16.0.linux-amd64.tar.gz $ tar -xzvf prometheus-2.16.0.linux-amd64.tar.gz -
查看解壓后的檔案目錄
$ cd /opt/k8s/prometheus/prometheus-2.16.0.linux-amd64 $ ls -al 總用量 141000 drwxr-xr-x 4 3434 3434 4096 2月 14 09:54 . drwxr-xr-x 3 root root 4096 3月 18 12:05 .. drwxr-xr-x 2 3434 3434 4096 2月 14 09:52 console_libraries drwxr-xr-x 2 3434 3434 4096 2月 14 09:52 consoles -rw-r--r-- 1 3434 3434 11357 2月 14 09:52 LICENSE -rw-r--r-- 1 3434 3434 3184 2月 14 09:52 NOTICE -rwxr-xr-x 1 3434 3434 82329106 2月 14 07:52 prometheus -rw-r--r-- 1 3434 3434 926 2月 14 09:52 prometheus.yml -rwxr-xr-x 1 3434 3434 48417809 2月 14 07:54 promtool -rwxr-xr-x 1 3434 3434 13595766 2月 14 07:54 tsdb- consoles: prometheus自帶的一些view對應的html檔案
- prometheus: 一個可執行檔案,是Prometheus server的啟動檔案
- prometheus.yml: 默認的組態檔
- data: Prometheus 抓取的指標資料存放目錄
- promtool: 一個工具集,可以用來對組態檔、規則檔案進行檢查,也可以利用PromQl進行查詢操作
- tsdb: prometheus 自身的時間序列資料庫TSDB的客戶端工具
運行prometheus
-
使用默認的組態檔啟動prometheus
$ cd /opt/k8s/prometheus/prometheus-2.16.0.linux-amd64 $ ./prometheus --config.file=prometheus.yml level=info ts=2020-03-18T05:41:13.719Z caller=main.go:295 msg="no time or size retention was set so using the default time retention" duration=15d level=info ts=2020-03-18T05:41:13.719Z caller=main.go:331 msg="Starting Prometheus" version="(version=2.16.0, branch=HEAD, revision=b90be6f32a33c03163d700e1452b54454ddce0ec)" level=info ts=2020-03-18T05:41:13.719Z caller=main.go:332 build_context="(go=go1.13.8, user=root@7ea0ae865f12, date=20200213-23:50:02)" level=info ts=2020-03-18T05:41:13.719Z caller=main.go:333 host_details="(Linux 5.3.0-40-generic #32~18.04.1-Ubuntu SMP Mon Feb 3 14:05:59 UTC 2020 x86_64 master (none))" level=info ts=2020-03-18T05:41:13.719Z caller=main.go:334 fd_limits="(soft=1024, hard=1048576)" level=info ts=2020-03-18T05:41:13.719Z caller=main.go:335 vm_limits="(soft=unlimited, hard=unlimited)" level=info ts=2020-03-18T05:41:13.720Z caller=main.go:661 msg="Starting TSDB ..." level=info ts=2020-03-18T05:41:13.720Z caller=web.go:508 component=web msg="Start listening for connections" address=0.0.0.0:9090 level=info ts=2020-03-18T05:41:13.723Z caller=head.go:577 component=tsdb msg="replaying WAL, this may take awhile" level=info ts=2020-03-18T05:41:13.723Z caller=head.go:625 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0 level=info ts=2020-03-18T05:41:13.724Z caller=main.go:676 fs_type=EXT4_SUPER_MAGIC level=info ts=2020-03-18T05:41:13.724Z caller=main.go:677 msg="TSDB started" level=info ts=2020-03-18T05:41:13.724Z caller=main.go:747 msg="Loading configuration file" filename=prometheus.yml level=info ts=2020-03-18T05:41:13.725Z caller=main.go:775 msg="Completed loading of configuration file" filename=prometheus.yml level=info ts=2020-03-18T05:41:13.725Z caller=main.go:630 msg="Server is ready to receive web requests." -
默認組態檔
$ cat 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']-
global 配置prometheus拉取指標資料的頻率和進行計算的頻率,都是15s
-
scrape_configs 配置段中,通過靜態配置方式指定從localhost:9090/metrics拉取指標資訊,其中9090是prometheus server啟動后監聽的埠
-
通過訪問 http://localhost:9090/metrics,可以觀察prometheus自身提供的各種監控指標
-
-
通過界面訪問

-
將Promethues配置成系統服務,隨系統啟動
-
編輯啟動檔案
$ cat > /etc/systemd/system/prometheus.service<< EOF [Unit] Description=Prometheus Server Documentation=https://prometheus.io/docs/introduction/overview/ After=network.target [Service] Restart=on-failure WorkingDirectory=/opt/k8s/prometheus/prometheus-2.16.0.linux-amd64 ExecStart=/opt/k8s/prometheus/prometheus-2.16.0.linux-amd64/prometheus \ --config.file=/opt/k8s/prometheus/prometheus-2.16.0.linux-amd64/prometheus.yml \ --storage.tsdb.path=/opt/k8s/prometheus/prometheus-2.16.0.linux-amd64/data \ --storage.tsdb.retention.time=30d ExecReload=/bin/kill -s HUP $MAINPID [Install] WantedBy=multi-user.target EOF- config.file指定組態檔地址
- storage.tsdb.path指定資料檔案目錄
- storage.tsdb.retention.time指定資料檔案保留時間
Promethues提供了很多的命令列配置選項,可通過./prometheus -h進行查看
-
通過systemctl啟動prometheus
$ systemctl daemon-reload $ systemctl start prometheus $ systemctl enable prometheus
-
docker 容器方式
前提是服務器上已經裝上了docker環境
-
下載鏡像
$ docker pull prom/prometheus:v2.16.0 -
啟動鏡像
$ docker run -p 9090:9090 prom/prometheus:v2.16.0- 以上命令采用默認配置啟動prometheus
-
通過掛載方式將組態檔掛載到容器內來自定義配置項
docker run \ -p 9090:9090 \ -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml \ prom/prometheus:v2.16.0或者將配置目錄直接掛載進去
docker run \ -p 9090:9090 \ -v /path/to/config:/etc/prometheus \ prom/prometheus:v2.16.0如果監控目標以及rule等資訊都穩定下來,可以基于prometheus的鏡像,將組態檔等資訊追加到容器中,Dockerfile如下
FROM prom/prometheus:v2.16.0 ADD prometheus.yml /etc/prometheus/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/36619.html
標籤:其他
上一篇:Prometheus(一)原理
下一篇:Prometheus(三)配置
