Deploying InfluxDB in Docker

<< Setting up Docker

InfluxDB is a open source time series database engine for ingesting real-time data. We will be deploying InfluxDB in Docker as its much easier than working with system services.

Deployment

First let’s build out the folder structure for InfluxDB on your host. This will allow us to persist data for InfluxDB between container updates.

# BASH [Linux VM]
sudo mkdir /opt/containers/ && sudo mkdir /opt/containers/influxdb
# BASH [Linux VM]
sudo chown -R yourusername:yourusername /opt/containers

If you do not use the chown step above, you MUST append sudo to the beginning of every command dealing with files / directories in /opt!

Download the influxdb.conf file from github.

# BASH [Linux VM]
curl https://raw.githubusercontent.com/influxdata/influxdb/1.7/etc/config.sample.toml -o /opt/containers/influxdb/influxdb.conf

Create an internal docker network for service communication.

# BASH [Linux VM]
docker network create --attachable InfluxNet

Copy the InfluxDB compose file from Github:

# BASH [Linux VM]
curl https://bin.alexsguardian.net/raw/influxdb-compose -o /opt/containers/influxdb/influxdb-compose.yml

Deploy Influx via docker-compose:

# BASH [Linux VM]
docker-compose -f /opt/containers/influxdb/influxdb-compose.yml up -d

To see if its running you can run docker logs -f influxdb_influx_1 or run curl http://localhost:8086/ping (CTRL+C exits the log view):

Shows the tailing of the influxdb container logs to show that the container is running in Docker.
Shows what the /ping endpoint for InfluxDB returns when its working correctly in Docker.

Creating an InfluxDB Admin User

Now that we have InfluxDB working correctly we need to secure it. First, create the new admin user:

# BASH [Linux VM]
curl -XPOST http://localhost:8086/query --data-urlencode "q=CREATE USER root WITH PASSWORD 'password' WITH ALL PRIVILEGES"

We then need to edit the influxdb.conf file to enforce users to be authenticated in order to use our database. So stop influx:

# BASH [Linux VM]
docker stop influxdb_influx_1

Then edit the influxdb.conf file:

# BASH [Linux VM]
nano /opt/containers/influxdb/influxdb.conf

Find the [HTTP] section, un-comment and set auth-enabled to true. CTRL+X then Y to save the file and then start InfluxDB:

# BASH [Linux VM]
docker start influxdb_influx_1

Quickly check and see if authentication is working:

# BASH [Linux VM]
curl -XPOST http://localhost:8086/query -u test:test --data-urlencode "q=SHOW DATABASES"

First Database

InfluxDB can be controlled via its HTTP API. This API allows us to create/edit/remove databases, users, retention polices, permissions, and even check the health of InfluxDB.

To create a database we need to first make sure we can hit the query API while authentication is enabled. Change the “user:password” section to the user you created above.

# BASH [Linux VM]
curl -XPOST http://localhost:8086/query -u user:password --data-urlencode "q=SHOW DATABASES"
If successful you should see the [[“_internal”]] database

Since our next guide page will be Telegraf, we can go ahead and make the influx database for it. These steps can be used for other service’s databases as well. Create the database for Telegraf:

# BASH [Linux VM]
curl -XPOST http://localhost:8086/query -u user:password --data-urlencode "q=CREATE DATABASE telegraf WITH DURATION 1d"
The “DURATION 24h” tells influxdb to drop data older than 24 hours. This helps limit the bloat from telegraf since we are going to be using it for real-time data.

Now we need to create a user for Telegraf and grant it write privileges to the telegraf database.

# BASH [Linux VM]
curl -XPOST http://localhost:8086/query -u user:password --data-urlencode "q=CREATE USER telegraf WITH PASSWORD 'passwordhere'"
# BASH [Linux VM]
curl -XPOST http://localhost:8086/query -u user:password --data-urlencode "q=GRANT ALL ON "telegraf" TO "telegraf""

Useful Links: