快速修复:更新 Telegraf 配置以将数据发送到 InfluxDB 3.0

导航至

我们最近推出了 新版本的 InfluxDB,它从头开始重写,以全面提高性能。与任何此类项目一样,开发人员需要对他们的应用程序进行一些调整,以便整合新的数据库。甚至我们在内部也面临着这一挑战。

我们有许多 Telegraf 实例向旧版本的 InfluxDB 发送数据。我们不想立即关闭这些数据管道,因此我们需要将这些数据输出到 InfluxDB 的 1.x/2.x 和 3.0 实例。我们需要的是一种简单的方法来实现这一点,而不会造成任何中断或问题。

作为我们内部迁移到 InfluxDB Cloud Dedicated 的一部分,我们需要更新我们的 Telegraf 写入器,使其从向旧版 InfluxDB 实例发送数据更改为向新的 Dedicated 实例发送数据。经过一些实验,我们了解到,只需向我们的 Telegraf 配置中添加一个新的 outputs.influxdb 条目,该条目引用我们的 InfluxDB Cloud Dedicated 集群,就可以轻松实现。让我们探索一下这个过程。

基本的 Telegraf 配置文件由全局代理设置、收集数据的输入插件和将数据发送到目的地的输出插件组成。通过添加一个新的 outputs.influxdb 插件条目(除了配置中已有的指向旧版本 InfluxDB 的条目之外),我们可以将我们已经收集的所有数据发送到我们的新 InfluxDB Cloud Dedicated 集群,而无需任何其他配置更改。

示例

我们的 Telegraf 配置已经设置为将数据发送到我们的 1.x 实例

[[outputs.influxdb]]
  urls = ["http://our_old_db:8086"]
  database = "telegraf"
  username = "telegraf_user"
  password = "MY_PRECIOUS"
  retention_policy = ""
  write_consistency = "any"
  timeout = "5s"

因此,配置所需的一切只是一个新的输出插件,其中包含正确的凭据以验证 Dedicated 集群的身份。但是,您从哪里获取凭据呢?

作为入职的一部分,客户会收到他们自己唯一的集群 URL 和帐户 ID。但是,创建他们自己的数据库和令牌取决于他们自己。您可以在此处找到文档,但我们将在此处提供一个带有示例的快速演练。

首先联系 InfluxData 支持部门,以获取适用于您操作系统的最新管理客户端的下载信息。下载并解压缩文件,然后执行命令 influxctl init。应显示类似于以下内容;填写值以匹配您的环境

[root@server1]# ./influxctl init
info    Welcome to the interactive prompt to set up profile!
info    Name: using a profile name other than 'default' requires
info      	specifying the `--profile` option for all commands.
> What is the name of the profile? [default]:
info    Account ID: This was provided as a UUID
> What is the account ID: account-UUID-goes-here
info    Cluster ID: This was provided as a UUID
> What is the cluster ID: clusterID-goes-here
info    profile default successfully created and ready for use

完成该步骤后,我们现在可以添加数据库并创建令牌。

  1. 使用 ./influxctl login 命令向系统进行身份验证。此命令应打开 Web 浏览器或显示一个链接,允许您使用您提供的 Auth0 凭据对集群进行身份验证。
  2. 使用 ./influxctl database create 命令创建您的新数据库
    [techops@server1]$ ./influxctl database create telegraf
    database "telegraf" successfully created
  3. 使用 ./influxctl token create 命令创建新的数据库令牌,并指定要授予令牌的数据库权限
    ./influxctl token create -–read-database telegraf -–write-database telegraf "R/W token for telegraf"
    这将为 telegraf 数据库创建一个具有读写功能的令牌,描述为 R/W token for telegraf。成功执行应如下所示
    warn    please copy the token and store in a safe place
    warn    this is the *only time* you will see the token
    apiv1_abcdefghijkl123456789

使用提供的令牌字符串填充 Telegraf 配置文件中新输出插件中的 token 配置选项

[[outputs.influxdb_v2]]
urls = ["https://my_url.a.influxdb.io"]
token = "apiv1_abcdefghijkl123456789"
bucket = "telegraf"

重新启动 Telegraf 代理,数据现在应该流向您现有的 1.x 数据库以及新的 InfluxDB Cloud Dedicated 数据库!检查日志以确保没有错误,然后使用这些查询作为示例来测试您的数据库

[root@server1]# curl --get https://my_test_url.a.influxdb.io/query --header "Authorization: Token apiv1_abcdefghijkl123456789" --data-urlencode "db=telegraf" --data-urlencode "q=show measurements"

{"results":[{"statement_id":0,"series":[{"name":"measurements","columns":["name"],"values":[["cpu"],["disk"],["diskio"],["influxdb"],["influxdb_ae"],["influxdb_cluster"],["influxdb_cq"],["influxdb_database"],["influxdb_entitlements"],["influxdb_hh"],["influxdb_hh_database"],["influxdb_hh_node"],["influxdb_hh_processor"],["influxdb_httpd"],["influxdb_localStore"],["influxdb_memstats"],["influxdb_qc_all_active"],["influxdb_qc_all_duration_seconds"],["influxdb_qc_compiling_active"],["influxdb_qc_compiling_duration_seconds"],["influxdb_qc_executing_active"],["influxdb_qc_executing_duration_seconds"],["influxdb_qc_memory_unused_bytes"],["influxdb_qc_queueing_active"],["influxdb_qc_queueing_duration_seconds"],["influxdb_qc_requests_total"],["influxdb_queryExecutor"],["influxdb_rpc"],["influxdb_runtime"],["influxdb_shard"],["influxdb_subscriber"],["influxdb_tsm1_cache"],["influxdb_tsm1_engine"],["influxdb_tsm1_filestore"],["influxdb_tsm1_wal"],["influxdb_write"],["kapacitor"],["kapacitor_edges"],["kapacitor_ingress"],["kapacitor_load"],["kapacitor_memstats"],["kapacitor_nodes"],["kapacitor_topics"],["mem"],["net"],["netstat"],["ping"],["processes"],["swap"],["syslog"],["system"]]}]}]}

[root@server1]$ curl --get https://my_test_url.a.influxdb.io/query --header "Authorization: Token apiv1_abcdefghijkl123456789" --data-urlencode "db=telegraf" --data-urlencode "q=SELECT time, host, usage_user from cpu group by host limit 1" --header "Accept: application/csv"

name,tags,time,host,usage_user
cpu,host=data3,1684005120000000000,data3,2.4729520867069734
cpu,host=data4,1684005120000000000,data4,2.4318349298448556
cpu,host=meta0,1684005120000000000,meta0,0.2734107997271841
cpu,host=meta1,1684005110000000000,meta1,0.20491803264512043
cpu,host=meta2,1684005120000000000,meta2,0.06738544474608282

恭喜!您现在正在将数据写入您的 InfluxDB Cloud Dedicated 集群!希望这个简短的教程对您有所帮助。有关使用 InfluxDB Cloud Dedicated 的更多信息,请查看我们的文档。虽然这篇文章专门引用了 Cloud Dedicated 产品,但相同的过程也适用于 InfluxDB Cloud Serverless