InfluxDB
InfluxDB is a time series database written in Go. This page contains notes relating to InfluxDB 2.x. My older notes on InfluxDB 1.x has been moved.
Introduction
InfluxDB 2.x integrates the TICK stack (Telegraf, InfluxDB, Chronograf, Kapacitor) into one binary.
Migration considerations
Before you mgirate to InfluxDB 2.x, be aware of a few things:
- You can't run InfluxQL. All your existing dashboards based on InfluxQL will need to be rewritten
- InfluxQL is nice and might make more sense than the SQL-based language, but it's completely different and converting SQL to this functional-like syntax can be tricky. You should probably learn flux before you do the migration.
- Your telegraf configs will need some tweaking. The database defaults to collectd and requires a token
- Everything requires a token or authentication. If you use the line protocol with a shell script somewhere, you will need to update these scripts.
Installation and migration from InfluxDB 1.x
I had an existing InfluxDB 1.x dataset which I want to migrate to InfluxDB 2.x. The upgrade is automatically triggered by the official docker image if it detects 1.x data but no 2.x data. You must also mount /var/lib/influxdb2 as that will be where the migrated data as well as any new data will be written to.
I used the following docker-compose when I first did the migration.
services:
influxdb:
image: influxdb:2.0.7
hostname: influxdb
networks:
- influxnet
- traefik
expose:
- "8086"
restart: always
volumes:
- /var/volumes/influx/influxdb:/var/lib/influxdb
- /var/volumes/influx/influxdb2:/var/lib/influxdb2
- /var/volumes/influx/config/influxdb.conf:/etc/influxdb/influxdb.conf
- /var/volumes/influx/types.db:/usr/share/collectd/types.db
environment:
DOCKER_INFLUXDB_INIT_USERNAME: admin
DOCKER_INFLUXDB_INIT_PASSWORD: AdminPassword1
DOCKER_INFLUXDB_INIT_ORG: home
DOCKER_INFLUXDB_INIT_BUCKET: initial
DOCKER_INFLUXDB_INIT_MODE: upgrade
Once the migration is complete, you can remove the INIT environment variables and the old influxdb volume.
Flux
Flux is the new query language introduced in InfluxDB v2. Its syntax is basically a list of functions that you apply to the data.
See: https://www.sqlpac.com/en/documents/influxdb-flux-language-advanced-features.html
Below are some common Flux queries.
Get list of fields
Useful for Grafana variables, where you want to have the user be able to select specific sets of fields.
from(bucket: "cloud")
Tasks
Importing data from one bucket to another
I had Telegraf data written to a Telegraf database. However, InfluxDB 2.x uses the collectd bucket. To import old Telegraf data to the new bucket, you can use the following flux query.
from(bucket: "telegraf/autogen")
|> range(start: -30d)
|> to(bucket: "collectd/autogen")
However, I ran into this error: input field "free" on measurement "disk" is type integer, already exists as type float dropped. As a workaround, I made a filter which converted values to floats for this particular measurement:
from(bucket: "telegraf/autogen")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "disk")
|> map(fn:(r) => ({ r with x: float(v: r.x) }))
|> to(bucket: "collectd/autogen")
Create a new variable
Unlike in Grafana where variables are part of a dashboard, in InfluxDB, variables are its own entity which needs to be created separately. To do so, go to settings -> variables. A variable can be defined as a query, csv, or key-pair map. Some common variables from queries can be found at https://docs.influxdata.com/influxdb/v2.0/visualize-data/variables/common-variables/.
To create a new 'hosts' variable which contains all Telegraf hosts pushing metrics to InfluxDB, use this query:
import "influxdata/influxdb/schema"
schema.tagValues(bucket: "collectd/autogen", tag: "host")
When creating a dashboard, you can inject this hosts variable in the query editor or reference it from the 'v' key. That is, v.hosts.