使用 Prometheus Agent 模式写入远端 Prometheus

相关介绍

Prometheus Agent 是 Prometheus 在 2.32.0 版本推出的实验性功能,当启用此功能后,将不会在本地文件系统上生成块,并且无法在本地查询。如果网络出现异常状态无法连接至远程端点,数据将暂存在本地磁盘,但是仅限于两个小时的缓冲。
remote-write-receiver 是 Prometheus 在 2.25 版本推出的实验性功能,当启用此功能后,prometheus 可以作为另一个 prometheus 的远程存储。

本文使用 docker-compose 部署相关服务

版本信息:

docker: 20.10.2
docker-compose: 1.29.2
OS: centos 7.9.2009
kernel: 4.18.0-305.10.2.el7.aarch64

部署流程

创建 docker-compose 启动文件

$ cat prometheus-agent-test.yaml
version: "3.9"
services:
  prometheus:
    image: prom/prometheus:v2.32.1
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'    # 指定配置文件存储路径
      - '--storage.tsdb.path=/prometheus'     # 指定 tsdb 存储路径
      - '--web.enable-lifecycle'    # 允许通过发送 HTTP POST 请求的方式热加载配置文件 `curl -X POST [http://IP/-/reload](http://ip/-/reload)`
      - '--enable-feature=remote-write-receiver'    # 启用试验性功能 remote-write-receiver
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'    # 指定页面组件存储路径
      - '--web.console.templates=/usr/share/prometheus/consoles'    # 指定页面存储路径
    ports:
      - 23335:9090
    volumes:
      - ./prometheus/data:/prometheus
      - ./prometheus/conf:/etc/prometheus
    networks:
      - test

  prometheus-agent:
    image: prom/prometheus:v2.32.1
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--web.enable-lifecycle'
      - '--enable-feature=agent'    # 启用试验性功能 agent
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'
    volumes:
      - ./prometheus-agent/data:/prometheus
      - ./prometheus-agent/conf:/etc/prometheus
    networks:
      - test

networks:
  test:
    external: true

Prometheus 配置文件

$ mkdir -p ./prometheus/{conf,data}
$ cd ./prometheus/conf
$ cat prometheus.yml 
global:                           
  scrape_interval: 15s            
  evaluation_interval: 15s        
  scrape_timeout: 10s             
  external_labels:
    environment: test

Prometheus Agent 配置文件

$ cd ../../
$ mkdir -p ./prometheus-agent/{conf,data}
$ cd ./prometheus-agent/conf
$ cat prometheus.yml 
global:
  scrape_interval: 15s
  scrape_timeout: 10s
  evaluation_interval: 15s
  external_labels:
    environment: test

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

remote_write:
- url: 'http://prometheus:9090/api/v1/write'

启动

$ cd ../../
$ chmod 777 -R prometheus/
$ chmod 777 -R prometheus-agent/
$ docker-compose -f prometheus-agent-test.yaml up -d

查看

$ docker ps
CONTAINER ID   IMAGE                     COMMAND                  CREATED         STATUS         PORTS                                      NAMES
a645439377d0   prom/prometheus:v2.32.1   "/bin/prometheus --c…"   4 seconds ago   Up 2 seconds   9090/tcp                                   prometheus_prometheus-agent_1
250ef708325c   prom/prometheus:v2.32.1   "/bin/prometheus --c…"   4 seconds ago   Up 2 seconds   0.0.0.0:23335->9090/tcp                    prometheus_prometheus_1

# Prometheus Agent 日志
$ docker logs -f a645439377d0
ts=2021-12-27T09:25:39.060Z caller=main.go:195 level=info msg="Experimental agent mode enabled."
ts=2021-12-27T09:25:39.060Z caller=main.go:515 level=info msg="Starting Prometheus" version="(version=2.32.1, branch=HEAD, revision=41f1a8125e664985dd30674e5bdf6b683eff5d32)"
ts=2021-12-27T09:25:39.060Z caller=main.go:520 level=info build_context="(go=go1.17.5, user=root@54b6dbd48b97, date=20211217-22:08:06)"
ts=2021-12-27T09:25:39.060Z caller=main.go:521 level=info host_details="(Linux 4.18.0-305.10.2.el7.aarch64 #1 SMP Fri Jul 23 21:19:40 UTC 2021 aarch64 920dcdb94365 (none))"
ts=2021-12-27T09:25:39.061Z caller=main.go:522 level=info fd_limits="(soft=1048576, hard=1048576)"
ts=2021-12-27T09:25:39.061Z caller=main.go:523 level=info vm_limits="(soft=unlimited, hard=unlimited)"
ts=2021-12-27T09:25:39.075Z caller=web.go:570 level=info component=web msg="Start listening for connections" address=0.0.0.0:9090
ts=2021-12-27T09:25:39.076Z caller=main.go:980 level=info msg="Starting WAL storage ..."
ts=2021-12-27T09:25:39.120Z caller=db.go:332 level=info msg="replaying WAL, this may take a while" dir=data-agent/wal
ts=2021-12-27T09:25:39.120Z caller=db.go:383 level=info msg="WAL segment loaded" segment=0 maxSegment=0
ts=2021-12-27T09:25:39.120Z caller=main.go:1001 level=info fs_type=EXT4_SUPER_MAGIC
ts=2021-12-27T09:25:39.121Z caller=main.go:1004 level=info msg="Agent WAL storage started"
ts=2021-12-27T09:25:39.121Z caller=main.go:1129 level=info msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
ts=2021-12-27T09:25:39.122Z caller=dedupe.go:112 component=remote level=info remote_name=dc2753 url=http://prometheus:9090/api/v1/write msg="Starting WAL watcher" queue=dc2753
ts=2021-12-27T09:25:39.122Z caller=dedupe.go:112 component=remote level=info remote_name=dc2753 url=http://prometheus:9090/api/v1/write msg="Starting scraped metadata watcher"
ts=2021-12-27T09:25:39.122Z caller=main.go:1166 level=info msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=1.680778ms db_storage=1.081µs remote_storage=828.171µs web_handler=1.163µs query_engine=650ns scrape=323.645µs scrape_sd=42.589µs notify=1.7µs notify_sd=2.639µs rules=400ns
ts=2021-12-27T09:25:39.122Z caller=main.go:897 level=info msg="Server is ready to receive web requests."
ts=2021-12-27T09:25:39.124Z caller=tls_config.go:195 level=info component=web msg="TLS is disabled." http2=false
ts=2021-12-27T09:25:39.124Z caller=dedupe.go:112 component=remote level=info remote_name=dc2753 url=http://prometheus:9090/api/v1/write msg="Replaying WAL" queue=dc2753
ts=2021-12-27T09:25:57.516Z caller=dedupe.go:112 component=remote level=info remote_name=dc2753 url=http://prometheus:9090/api/v1/write msg="Done replaying WAL" duration=18.391770521s

# Prometheus 日志
$ docker logs -f 250ef708325c
ts=2021-12-27T09:25:39.149Z caller=main.go:177 level=info msg="Experimental remote-write-receiver enabled"
ts=2021-12-27T09:25:39.149Z caller=main.go:478 level=info msg="No time or size retention was set so using the default time retention" duration=15d
ts=2021-12-27T09:25:39.149Z caller=main.go:515 level=info msg="Starting Prometheus" version="(version=2.32.1, branch=HEAD, revision=41f1a8125e664985dd30674e5bdf6b683eff5d32)"
ts=2021-12-27T09:25:39.150Z caller=main.go:520 level=info build_context="(go=go1.17.5, user=root@54b6dbd48b97, date=20211217-22:08:06)"
ts=2021-12-27T09:25:39.150Z caller=main.go:521 level=info host_details="(Linux 4.18.0-305.10.2.el7.aarch64 #1 SMP Fri Jul 23 21:19:40 UTC 2021 aarch64 920dcdb94365 (none))"
ts=2021-12-27T09:25:39.150Z caller=main.go:522 level=info fd_limits="(soft=1048576, hard=1048576)"
ts=2021-12-27T09:25:39.150Z caller=main.go:523 level=info vm_limits="(soft=unlimited, hard=unlimited)"
ts=2021-12-27T09:25:39.152Z caller=web.go:570 level=info component=web msg="Start listening for connections" address=0.0.0.0:9090
ts=2021-12-27T09:25:39.153Z caller=main.go:924 level=info msg="Starting TSDB ..."
ts=2021-12-27T09:25:39.168Z caller=head.go:488 level=info component=tsdb msg="Replaying on-disk memory mappable chunks if any"
ts=2021-12-27T09:25:39.168Z caller=head.go:522 level=info component=tsdb msg="On-disk memory mappable chunks replay completed" duration=8.439µs
ts=2021-12-27T09:25:39.168Z caller=head.go:528 level=info component=tsdb msg="Replaying WAL, this may take a while"
ts=2021-12-27T09:25:39.169Z caller=tls_config.go:195 level=info component=web msg="TLS is disabled." http2=false
ts=2021-12-27T09:25:39.169Z caller=head.go:599 level=info component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
ts=2021-12-27T09:25:39.169Z caller=head.go:605 level=info component=tsdb msg="WAL replay completed" checkpoint_replay_duration=74.352µs wal_replay_duration=1.21529ms total_replay_duration=1.314955ms
ts=2021-12-27T09:25:39.171Z caller=main.go:945 level=info fs_type=EXT4_SUPER_MAGIC
ts=2021-12-27T09:25:39.171Z caller=main.go:948 level=info msg="TSDB started"
ts=2021-12-27T09:25:39.171Z caller=main.go:1129 level=info msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
ts=2021-12-27T09:25:39.172Z caller=main.go:1166 level=info msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=691.44µs db_storage=1.512µs remote_storage=1.844µs web_handler=463ns query_engine=1.376µs scrape=414.021µs scrape_sd=10.649µs notify=1.256µs notify_sd=2.381µs rules=3.884µs
ts=2021-12-27T09:25:39.172Z caller=main.go:897 level=info msg="Server is ready to receive web requests."

打开 web 查看

image.png

其它

在实际生产中,需要按照具体情况调节参数
例如

remote_write:
- url: 'http://prometheus:9090/api/v1/write'
  remote_timeout: 30s
  follow_redirects: true
  queue_config:
    capacity: 2500
    max_shards: 200
    min_shards: 1
    max_samples_per_send: 500
    batch_send_deadline: 5s
    min_backoff: 30ms
    max_backoff: 5s
  metadata_config:
    send: true
    send_interval: 1m
    max_samples_per_send: 500

转载请注明出处

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容