Deploying Telegraf in Docker

For this guide series we will be deploying Telegraf in docker so we can monitor SNMP, Docker, and IPMI.

First we need to create the config directory where we will be housing the telegraf.conf file and telegraf.d directory.

# BASH [Linux VM]
mkdir /opt/containers/telegraf

Create the Dockerfile:

# BASH [Linux VM]
curl https://bin.alexsguardian.net/raw/telegraf-Dockerfile -o /opt/containers/telegraf/Dockerfile

Change directory to the Telegraf folder:

# BASH [Linux VM]
cd /opt/containers/telegraf

Build the image:

# BASH [Linux VM]
docker build -t local/telegraf:latest .

Copy down the compose file:

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

Edit the compose file ( nano /opt/containers/telegraf/Telegraf-compose.yml )and change the image: telegraf line to match the name you gave your custom image. e.g. image: custom/telegarf:latest

Create the telegraf.conf file and telegraf.d directory:

# BASH [Linux VM]
touch /opt/containers/telegraf/telegraf.conf && mkdir /opt/containers/telegraf/telegraf.d

Edit the telegraf.conf file and paste the following into it

# BASH [Linux VM]
nano /opt/containers/telegraf/telegraf.conf
# Telegraf Configuration
#
[global_tags]
[agent]
  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  debug = false
  quiet = false
  logfile = ""
  hostname = ""
  omit_hostname = false
###############################################################################
#                                  OUTPUTS                                    #
###############################################################################
# Configuration for sending metrics to InfluxDB
[[outputs.influxdb]]
  ## The full HTTP or UDP URL for your InfluxDB instance.
   urls = ["http://CHANGEME:8086"]
   database = "telegraf"
   skip_database_creation = true
  # retention_policy = ""
  # write_consistency = "any"
   timeout = "5s"
   username = "CHANGEME"
   password = "CHANGEME"
   user_agent = "telegraf"
  # udp_payload = "512B"
  # tls_ca = "/etc/telegraf/ca.pem"
  # tls_cert = "/etc/telegraf/cert.pem"
  # tls_key = "/etc/telegraf/key.pem"
  # insecure_skip_verify = false
  # http_proxy = "http://corporate.proxy:3128"
  # http_headers = {"X-Special-Header" = "Special-Value"}
  # content_encoding = "identity"
  # influx_uint_support = false

Edit the CHANGEME values to match your environment setup.

Save and close the telegraf.conf file. CTRL+X then Y.

Create the docker-input.conf file in telegraf.d:

nano /opt/containers/telegraf/telegraf.d/docker-input.conf

Paste the following config into the new file:

[[inputs.docker]]
  endpoint = "unix:///var/run/docker.sock"
  gather_services = false
  container_name_include = []
  container_name_exclude = []
  timeout = "5s"
  docker_label_include = []
  docker_label_exclude = []
  perdevice = true
  total = false

CTRL+X then Y to save and close the file.

Deploying Telegraf

Now that we have setup our Telegraf directory, built our custom image, and configured Telegraf we can go ahead and deploy it.

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

After a few moments your Telegraf container should be running. We can verify this via:

# BASH [Linux VM]
docker logs -f telegraf_telegraf_1

Importing the Docker dashboard in Grafana.

This section is to be followed after you deploy Grafana.

Open Grafana and navigate to your dashboard import screen. In the Grafana.com Dashboard box type: 10585. Click Import then make sure to select your Telegraf database and click Import again.


<< Deploying Telegraf on Linux