Prometheus使用Influxdb(2.X版本)作为远端存储配置流程

799 阅读4分钟

一、背景说明

Influxdb2.X版本以前,Prometheus可以直接对接Influxdb,因为Influxdb提供了针对Prometheus的API接口。 但是在2.X版本开始,influxdb取消这一系列的接口,因此需要一个中间件Telegraf,先将Prometheus的数据上传至Telegraf,再由Telegraf上传至Influxdb。

Prometheus整合influxdb.png

二、Telegraf配置

可以采用配置文件方式进行手动配置,本文后续采用influxdb提供的远程配置管理的方式队telegraf进行配置

本文采用的Influxdb版本为2.7

2.1 在Influxdb创建配置文件

在Influxdb的WebUI中菜单栏Load Data/Telegraf点击右侧创建配置文件

选择HTTP Listener V2模版,同时选择任意一个Bucket,编辑如下配置:

# Generic HTTP write listener
[[inputs.http_listener_v2]]
  ## Address and port to host HTTP listener on
  service_address = ":8087"

  ## Paths to listen to.
  paths = ["/receive"]

  ## Save path as http_listener_v2_path tag if set to true
  # path_tag = false

  ## HTTP methods to accept.
  # methods = ["POST", "PUT"]

  ## maximum duration before timing out read of the request
  # read_timeout = "10s"
  ## maximum duration before timing out write of the response
  # write_timeout = "10s"

  ## Maximum allowed http request body size in bytes.
  ## 0 means to use the default of 524,288,000 bytes (500 mebibytes)
  # max_body_size = "500MB"

  ## Part of the request to consume.  Available options are "body" and
  ## "query".
  # data_source = "body"

  ## Set one or more allowed client CA certificate file names to
  ## enable mutually authenticated TLS connections
  # tls_allowed_cacerts = ["/etc/telegraf/clientca.pem"]

  ## Add service certificate and key
  # tls_cert = "/etc/telegraf/cert.pem"
  # tls_key = "/etc/telegraf/key.pem"

  ## Optional username and password to accept for HTTP basic authentication.
  ## You probably want to make sure you have TLS configured above for this.
  # basic_username = "foobar"
  # basic_password = "barfoo"

  ## Optional setting to map http headers into tags
  ## If the http header is not present on the request, no corresponding tag will be added
  ## If multiple instances of the http header are present, only the first value will be used
  # http_header_tags = {"HTTP_HEADER" = "TAG_NAME"}

  ## Data format to consume.
  ## Each data format has its own unique set of configuration options, read
  ## more about them here:
  ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
  data_format = "prometheusremotewrite"

service_address为telegraf的监听端口

paths为telegraf的监听路径,可以任意写一个或多个

data_format为监听的数据类型,详细类型可以参考github的链接:github.com/influxdata/…

这边将数据类型设置为prometheusremotewrite

完整配置文件如下:

# Configuration for telegraf agent
[agent]
  ## Default data collection interval for all inputs
  interval = "10s"
  ## Rounds collection interval to 'interval'
  ## ie, if interval="10s" then always collect on :00, :10, :20, etc.
  round_interval = true

  ## Telegraf will send metrics to outputs in batches of at most
  ## metric_batch_size metrics.
  ## This controls the size of writes that Telegraf sends to output plugins.
  metric_batch_size = 1000

  ## Maximum number of unwritten metrics per output.  Increasing this value
  ## allows for longer periods of output downtime without dropping metrics at the
  ## cost of higher maximum memory usage.
  metric_buffer_limit = 10000

  ## Collection jitter is used to jitter the collection by a random amount.
  ## Each plugin will sleep for a random time within jitter before collecting.
  ## This can be used to avoid many plugins querying things like sysfs at the
  ## same time, which can have a measurable effect on the system.
  collection_jitter = "0s"

  ## Default flushing interval for all outputs. Maximum flush_interval will be
  ## flush_interval + flush_jitter
  flush_interval = "10s"
  ## Jitter the flush interval by a random amount. This is primarily to avoid
  ## large write spikes for users running a large number of telegraf instances.
  ## ie, a jitter of 5s and interval 10s means flushes will happen every 10-15s
  flush_jitter = "0s"

  ## By default or when set to "0s", precision will be set to the same
  ## timestamp order as the collection interval, with the maximum being 1s.
  ##   ie, when interval = "10s", precision will be "1s"
  ##       when interval = "250ms", precision will be "1ms"
  ## Precision will NOT be used for service inputs. It is up to each individual
  ## service input to set the timestamp at the appropriate precision.
  ## Valid time units are "ns", "us" (or "µs"), "ms", "s".
  precision = ""

  ## Override default hostname, if empty use os.Hostname()
  hostname = ""
  ## If set to true, do no set the "host" tag in the telegraf agent.
  omit_hostname = false
[[outputs.influxdb_v2]]
  ## The URLs of the InfluxDB cluster nodes.
  ##
  ## Multiple URLs can be specified for a single cluster, only ONE of the
  ## urls will be written to each interval.
  ##   ex: urls = ["https://us-west-2-1.aws.cloud2.influxdata.com"]
  urls = ["http://hadoop104:8086"]

  ## Token for authentication.
  token = "$INFLUX_TOKEN"

  ## Organization is the name of the organization you wish to write to; must exist.
  organization = "root"

  ## Destination bucket to write into.
  bucket = "prometheus"

# Generic HTTP write listener
[[inputs.http_listener_v2]]
  ## Address and port to host HTTP listener on
  service_address = ":8087"

  ## Paths to listen to.
  paths = ["/receive"]

  data_format = "prometheusremotewrite"

2.2启动telegraf

先设置INFLUX_TOKEN,这边填写上自己的token信息,添加到linux环境变量中,也可以手动添加在telegraf配置文件中

export INFLUX_TOKEN=<INFLUX_TOKEN>

启动telegraf,可以通过配置文件方式启动,也可以采用远程方式启动

# 配置文件方式启动
# telegraf --config <CONFIG_FILE>
# 远程配置启动
telegraf --config http://hadoop104:8086/api/v2/telegrafs/0c7012b3baeca000

三、Prometheus配置

Prometheus的配置方式很简单,只需要修改yml配置文件,添加如下配置:

remote_write:
  - url: "http://localhost:8087/receive"

这边的URL填写上述telegraf配置中的端口号+path即可,ip填写telegraf部署的ip。

启动Prometheus。

四、测试结果

Data Explorer中查看刚才设置的Bucket,可以看到已经有数据了,说明配置成功。

image-20240116150309643.png