archive2020

From InfluxDB 1.x to 2.0: orgs, buckets, tokens and Flux

InfluxDB 2.0 reached general availability. The new concepts (orgs, buckets, tokens, Flux), how Telegraf and Grafana connect, how to upgrade, and why my own Pi stays on 1.8 for now.

4 min read#ops#influxdb#telegraf#grafana

InfluxData announced general availability of InfluxDB 2.0 open source on 10 November 2020 (announcement). My sensor data has lived in InfluxDB 1.x with Telegraf and Grafana since late 2016, so about four years, so I spent a few evenings with 2.0 in a test container. It is a different product in many ways. I would not upgrade a working 1.x setup in place without testing first.

New concepts

1.x 2.0 Notes
database + retention policy bucket A bucket has one retention period
users with passwords users + API tokens Clients use tokens, not passwords
(none) organization A workspace that owns buckets, tokens, dashboards
InfluxQL Flux (InfluxQL still possible through a 1.x compatibility API) Flux is a new functional language
Continuous queries Tasks Tasks are Flux scripts on a schedule
Chronograf, Kapacitor Built into the influxd UI UI, dashboards, alerts in one binary

The HTTP port is still 8086.

First start

On a fresh install, run setup once. It creates the first user, org, bucket and an admin token:

influx setup \
  --username admin \
  --password 'change-me-long-password' \
  --org home \
  --bucket sensors \
  --retention 30d \
  --force

Then make a token that can only write to one bucket, for Telegraf, in the UI under Data > Tokens or with influx auth create. The admin token should not go to any client.

Telegraf and Grafana

Telegraf has a separate output plugin for 2.0:

[[outputs.influxdb_v2]]
  urls = ["http://127.0.0.1:8086"]
  token = "$INFLUX_TOKEN"
  organization = "home"
  bucket = "sensors"

I put the token in an environment variable in /etc/default/telegraf, not in the config file.

Grafana 7.1 and later can query InfluxDB with Flux. In the data source settings choose InfluxDB, set the query language to Flux, and enter the URL, the org, a read token and a default bucket.

For many existing dashboards with InfluxQL there is another way. InfluxDB 2.0 has a 1.x-compatible /query endpoint, with a mapping from database and retention policy names to buckets (DBRP mapping). Old dashboards keep working while you move to Flux step by step. I did not get this fully working in my test yet, so check the documentation for your exact version.

Flux

The same query in both languages: average temperature per room, 5-minute windows, last 6 hours.

SELECT mean("temperature") FROM "climate"
WHERE time > now() - 6h GROUP BY time(5m), "room"
from(bucket: "sensors")
  |> range(start: -6h)
  |> filter(fn: (r) => r._measurement == "climate" and r._field == "temperature")
  |> aggregateWindow(every: 5m, fn: mean, createEmpty: false)

Flux is longer for simple queries. It is better at joins, math between measurements and pivots, which InfluxQL could not do well.

Two things confused me at the start. Flux results are tables, and each series (each set of tags) is its own table; group() changes how rows are split. And every query needs range(), or it fails.

Upgrading

2.0 includes an influxd upgrade command. It reads the 1.x config and data, creates buckets from database and retention policy pairs, and copies the data. The way I plan to do it:

  1. Back up the 1.x data directory (influxd backup -portable).
  2. Run the upgrade on a copy, on another machine or in a container.
  3. Point a second Telegraf and the dashboards at the new instance and compare.
  4. Only then switch.

Continuous queries are not converted. They have to be rewritten as tasks.

What I will do

InfluxDB 1.8 is still maintained, and my setup works. The sensor database runs on a 32-bit Pi, and I have not found a 2.0 build for that architecture, so it stays on 1.8 for now. For a new project on a normal server I would start with 2.0. Tokens are a better model than shared passwords, and the UI is included.