Containers / Data / Sponsored / Contributed

How to Setup InfluxDB, Telegraf and Grafana on Docker: Part 2

29 Nov 2019 1:09pm, by

InfluxData sponsored this post.

In Part 1 of this tutorial series, we covered the steps to install InfluxDB 1.7 on Docker for Linux instances. We describe in Part 2 how to install the Telegraf plugin as a data-collection interface with InfluxDB 1.7 and Docker.

Installing Telegraf on Docker

For those who are not familiar with Telegraf, it is a plugin-driven agent that periodically collects metrics from a variety of different systems. The metrics are pushed to InfluxDB and they can be later analyzed in Chronograf or Grafana. Luckily, Telegraf also belongs to the official Docker images.

In this section, we are going to configure Telegraf to gather system metrics on our Linux host (in this case, a Debian 10, but it works in the same way for Ubuntu-based hosts).

Prepare Telegraf for InfluxDB and Docker

Similarly to our InfluxDB setup, we are going to create a Telegraf user for our host. It ensures that correct permissions are set for our future configuration files.

In your etc directory, create a new folder for your Telegraf configuration files.

Creating a Configuration File for Telegraf and Docker

Antoine Solnichkin
Antoine is an experienced software engineer and aspiring enterprise architect in Luxembourg. On a daily basis, he is involved in architecting, developing and maintaining large industrial projects with complex needs. Antoine writes technical articles focused on system administration and modern open-source monitoring solutions.

Again, we don’t have to create a Telegraf configuration file by ourselves.

The Telegraf Docker image is built very closely to the InfluxDB one.

As a consequence, it is able to run a simple telegraf config command to generate a configuration on the fly.

The Telegraf configuration file has the following defaults:

  • Interval: 10 seconds. Telegraf is going to gather and send metrics to InfluxDB every 10 seconds.
  • Round_interval : true. The agent is going to collect metrics on :00, :10, or :(00 + n*interval)
  • The InfluxDB output plugin is enabled by default.
  • The CPU, disk, diskio, kernel, memory, processes, swap and system inputs plugins are enabled. As those inputs use the /proc mountpoint to gather metrics, we will have to remap volumes on the container.

To create a Telegraf configuration file using Docker, run the following command.

Next, reassign the correct permissions to your Telegraf configuration folder. This will ensure that only Telegraf itself and the root account are able to write to the configuration file.

Modify Your Telegraf Configuration File

With Telegraf, most of the time, you will want to send metrics directly to InfluxDB itself. This is why the InfluxDB output is enabled by default in your Telegraf configuration file. By default, Telegraf will send metrics to a database named “telegraf” on InfluxDB. This is a customizable parameter; however, in this case, we are only going to specify the InfluxDB authentication parameters.

Edit your Telegraf configuration file, and locate the [[outputs.influxdb]] section.

In this configuration file, locate the “HTTP Basic Auth” section and modify the credentials accordingly.

Of course, you are free to create a dedicated administrator account for Telegraf by using the method we described above (using docker exec).
Save and exit your file. Now it is time to run the container.

Running the Telegraf Container on Docker

As stated above, Telegraf enables system inputs by default. As a consequence, we will have to remap the /proc host folder to the /host folder on our Docker image.

This is to ensure that Telegraf is not gathering metrics from the Docker container itself, and that the container filesystem is not altered in any way.

To achieve this, unless you gave a name to your InfluxDB container, run this command to get your InfluxDB container ID. It will be used to connect Telegraf and InfluxDB to the same virtual network.

Isolate your Telegraf user ID by running the following command:

Next, to run the Telegraf Docker image, run the following command:

Note: the net option can be replaced by –net=influxdb if you chose to create your InfluxDB container with a name.

Great! To make sure your Telegraf instance is running correctly, run the following command:

Telegraf seems not to raise any error messages, but let’s double-check the correctness of our setup by inspecting the InfluxDB database:

If you are seeing data points, congratulations! Your Telegraf instance is correctly sending metrics to your InfluxDB server:

Now that all metrics are stored in Telegraf, for one week, we can install a modern dashboarding tool in order to visualize them: Grafana.

Visualizing Telegraf Metrics in Grafana

For those who are unfamiliar with Grafana, it is a dashboarding tool that pulls from a wide variety of different datasources in order to create beautiful graphs and dashboards. It can pull from traditional SQL databases, but it can also pull from a variety of time series databases, which is what we are going to do in this section.

Installing Grafana on Docker

The Grafana docker image is stored under the Grafana repository:

To create a Grafana container, run the following command on your host:

Configuring Grafana for InfluxDB

With your web browser, head over to http://localhost:3000

You should be redirected to Grafana homepage. The default credentials for Grafana are admin/admin. Immediately, you are asked to change your password. Choose a strong password and click on “Save.”

You should now be redirected to the Grafana default Web UI:

Click on “Add data source” to add an InfluxDB datasource:

Next, select the InfluxDB option and click on “Select.”

For this tutorial, we are not using InfluxDB images as Docker services. As a consequence, we have to isolate InfluxDB public IP on our bridge network.

To do that, run the following command:

Copy the IPv4 address and paste it in the InfluxDB configuration for Grafana. Select the Basic Auth option, specify your administrator credentials, and fill the details about your InfluxDB database. Here is the final configuration:

Click on “Save and Test” to make sure that your configuration is working properly.

Awesome! Now that everything is set up in Grafana, let’s import a dashboard designed for Telegraf host metrics.

Importing a Grafana Dashboard

To import a Grafana dashboard, select the “Plus” icon in the left menu, and click on “Import“:

In the import text box, put 1443 as a dashboard ID:

In the next window, make sure that the information is correct and bind it to your newly created datasource:

Conclusion

Congratulations, you have learned how to install InfluxDB 1.7.x, Telegraf and Grafana using Docker. As you probably realized, this tutorial focuses on a fully customizable installation of your images. If you want to automate your container setups, it might be a good idea to use docker-compose. Also, make sure to read through the entire list of Telegraf input plugins to start getting ideas about what to monitor.

More is yet to come: InfluxDB will soon become InfluxDB 2.0, which will serve as a single platform to manage all the components of the TICK Stack. Another tutorial about how to install and set up InfluxDB 2.0 will be coming soon.

Until then, have fun, as always.

A newsletter digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.