Prometheus從入門到精通:一、部署

冷冰若水發表於2020-12-25

一、Prometheus是什麼?

prometheus是一個開源指標監控解決方案,指標就是指的CPU的使用率、記憶體使用率等資料。

二、Prometheus的架構

這裡直接貼上官網的架構圖:

三、安裝

這裡採用docker的方式來安裝,如果需要使用其他方式的,可以參考官網。

3.1、設定配置檔案

個人使用的配置檔案:

# 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: '/webUI/prometheus/metrics'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

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

  - job_name: 'example'
    metrics_path: '/example/metrics'
    static_configs:
    - targets: ['example.com']

Prometheus的配置檔案使用的yaml格式,如果對yaml不熟悉的可以搜一下相關的資料瞭解一下;這裡先簡單介紹一下其中的scrape_configs下的job,job就是Prometheus會定時從它配置的target抓取資料;這個示例中配置了兩個job,第一個是Prometheus自己的metrics,第二個是一個示例;這裡需要著重介紹一下metrics_path,它的作用是指定抓取metrics資料的路徑,預設是targets+'/metrics',如果我們的metrics路徑不是這個,那麼就需要通過該引數額外指定。

3.2、啟動容器

參考:https://prometheus.io/docs/prometheus/latest/installation/#using-docker

1、官方示例方式

docker run \
    -p 9090:9090 \
    -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
    prom/prometheus

這個命令是前端執行的方式啟動的,可以用來一開始做測試使用,一旦當前視窗關閉,則該docker容器也會被關閉掉。

2、個人推薦方式

docker run \
    -d \
    --name prometheus \
    --network host \
    -v /data1/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
    prom/prometheus --web.listen-address="127.0.0.1:9090" --web.external-url=http://localhost:9090/webUI/prometheus/ --config.file=/etc/prometheus/prometheus.yml

個人推薦使用這個命令,這裡簡單介紹一個和官網示例不同的地方:

  1. -d:該選項的作用是讓docker以daemon的方式執行,也就是後臺執行,關閉當前視窗不影響容器。
  2. --name:指定容器的名字,預設是以一個隨機字串來作為容器的名字的,不方便後續觀察容器的狀態,因為後續對容器的操作都需要以容器名作為引數。
  3. --network:指定容器使用的網路方式,表示容器不需要網路隔離,和宿主機在一個網路中執行;如果想要使用其他方式的話,請確保宿主機已經安裝了相應的driver;我個人在剛開始使用時就未指定該引數,使用了預設的bridge方式,但是發現-p設定埠對映不生效,排查了許久發現是因為沒有安裝bridge的driver。
  4. -v:對映檔案到容器中,冒號前面是宿主機路徑,冒號後面是容器中的路徑;前面根據你的實際情況而定,後臺的值和Prometheus啟動引數--config.file有關,預設是/etc/prometheus/prometheus.yml。
  5. prom/prometheus:映象名字
  6. --web.listen-address:指定Prometheus的web頁面監聽的地址,這裡建議監聽本地,然後通過nginx做轉發。
  7. --web.external-url:指定Prometheus暴露出來的基礎地址,這個在需要用nginx做轉發時會用到;如果是直接訪問Prometheus的web頁面,則不需要。
  8. --config.file:指定Prometheus的配置檔案路徑。

執行完上面的命令後使用docker logs prometheus檢視一下日誌,如果是正確執行了,日誌會像下面這樣:

level=info ts=2020-12-25T11:07:34.247Z caller=main.go:322 msg="No time or size retention was set so using the default time retention" duration=15d
level=info ts=2020-12-25T11:07:34.247Z caller=main.go:360 msg="Starting Prometheus" version="(version=2.23.0, branch=HEAD, revision=26d89b4b0776fe4cd5a3656dfa520f119a375273)"
level=info ts=2020-12-25T11:07:34.247Z caller=main.go:365 build_context="(go=go1.15.5, user=root@37609b3a0a21, date=20201126-10:56:17)"
level=info ts=2020-12-25T11:07:34.247Z caller=main.go:366 host_details="(Linux 3.10.107-1-tlinux2-0048 #1 SMP Wed Feb 27 14:30:34 CST 2019 x86_64 CMS-154864507 (none))"
level=info ts=2020-12-25T11:07:34.247Z caller=main.go:367 fd_limits="(soft=1048576, hard=1048576)"
level=info ts=2020-12-25T11:07:34.247Z caller=main.go:368 vm_limits="(soft=unlimited, hard=unlimited)"
level=info ts=2020-12-25T11:07:34.250Z caller=main.go:722 msg="Starting TSDB ..."
level=info ts=2020-12-25T11:07:34.250Z caller=web.go:528 component=web msg="Start listening for connections" address=127.0.0.1:9090
level=info ts=2020-12-25T11:07:34.250Z caller=web.go:550 component=web msg="Router prefix" prefix=/webUI/prometheus
level=info ts=2020-12-25T11:07:34.255Z caller=head.go:645 component=tsdb msg="Replaying on-disk memory mappable chunks if any"
level=info ts=2020-12-25T11:07:34.255Z caller=head.go:659 component=tsdb msg="On-disk memory mappable chunks replay completed" duration=8.196µs
level=info ts=2020-12-25T11:07:34.255Z caller=head.go:665 component=tsdb msg="Replaying WAL, this may take a while"
level=info ts=2020-12-25T11:07:34.256Z caller=head.go:717 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
level=info ts=2020-12-25T11:07:34.256Z caller=head.go:722 component=tsdb msg="WAL replay completed" checkpoint_replay_duration=28.503µs wal_replay_duration=1.160856ms total_replay_duration=1.217683ms
level=info ts=2020-12-25T11:07:34.258Z caller=main.go:742 fs_type=EXT4_SUPER_MAGIC
level=info ts=2020-12-25T11:07:34.258Z caller=main.go:745 msg="TSDB started"
level=info ts=2020-12-25T11:07:34.258Z caller=main.go:871 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
level=info ts=2020-12-25T11:07:34.258Z caller=main.go:902 msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=873.002µs remote_storage=6.228µs web_handler=850ns query_engine=2.311µs scrape=351.488µs scrape_sd=50.71µs notify=22.741µs notify_sd=10.615µs rules=2.97µs
level=info ts=2020-12-25T11:07:34.258Z caller=main.go:694 msg="Server is ready to receive web requests."

然後執行curl curl 127.0.0.1:9090/webUI/prometheus/metrics/,正常情況下會展示Prometheus預設的metrics資料,這裡僅展示前幾行:

# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 9.755e-05
go_gc_duration_seconds{quantile="0.25"} 0.000129779
go_gc_duration_seconds{quantile="0.5"} 0.000154338
go_gc_duration_seconds{quantile="0.75"} 0.000199801
go_gc_duration_seconds{quantile="1"} 0.000343164
go_gc_duration_seconds_sum 0.008667863
go_gc_duration_seconds_count 50
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 34
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.15.5"} 1
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 3.2321096e+07
# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
# TYPE go_memstats_alloc_bytes_total counter
go_memstats_alloc_bytes_total 2.79534016e+08

3.3、配置nginx轉發

個人建議通過nginx做轉發來實現Prometheus的web頁面訪問,具體配置如下:

location ^~ /webUI/prometheus/ {
      proxy_pass http://127.0.0.1:9090/webUI/prometheus/;
}

然後執行sbin/nginx -c config/nginx.conf -s reload使新增的路由生效,然後就可以通過www.example.com/webUI/prometheus/來檢視Prometheus的web頁面了,如下圖所示:

四、Prometheus視覺化頁面

Prometheus的介面比較簡潔,這裡簡單介紹一下如何根據Prometheus提供的goroutine的個數變化配置一個圖表:

五、配置grafana

Prometheus提供的視覺化圖表比較簡單,因此這裡引入grafana作為metrics的視覺化展示介面,下面仍然使用goroutine的個數變化展示如何在grafana配置一個圖表:

5.1、Configration->Data Sources,然後點選Add Data Source

5.2、配置Prometheus

5.3、配置goroutine圖表

5.4、效果

六、總結

到這裡就把Prometheus的部署介紹完了,Prometheus的更高階用法以及具體實踐,例如生產環境如何在程式碼中使用這些會留待後續介紹。

相關文章