Monitoring and analyzing time-series data has become a crucial part of modern software systems. Tools like InfluxDB and Grafana are popular for handling and visualizing such data. InfluxDB is an open-source time-series database, and Grafana is an open-source platform for data visualization. In this blog post, we will walk through the process of setting up InfluxDB and Grafana using Docker Compose, and then show how to write data into InfluxDB using a Python script.
InfluxDB: A time-series database optimized for storing high volumes of time-stamped data. It is designed for handling metrics and events such as application performance metrics, sensor data, and more.
Grafana: A popular open-source platform for visualizing time-series data. Grafana integrates with various data sources, including InfluxDB, to create powerful and customizable dashboards for monitoring and alerting.
Together, InfluxDB and Grafana form a powerful monitoring solution for your applications and infrastructure.
Docker Compose simplifies the deployment and management of multi-container Docker applications. With Docker Compose, you can define multiple services (like InfluxDB and Grafana) in a single YAML file, which can be version-controlled and easily shared with others. In this case, we’ll use Docker Compose to set up and manage InfluxDB and Grafana.
Let’s start by creating a docker-compose.yml file. This file will define both InfluxDB and Grafana containers.
docker-compose.yml:
version: "3.8"
services:
grafana:
image: grafana/grafana
container_name: grafana
ports:
- "3000:3000"
restart: unless-stopped
volumes:
- grafana-data:/var/lib/grafana
environment:
# - GF_INSTALL_PLUGINS=
- GF_SECURITY_ADMIN_USER=username
- GF_SECURITY_ADMIN_PASSWORD=password
influxdb2:
image: influxdb:latest
ports:
- 8086:8086
environment:
DOCKER_INFLUXDB_INIT_MODE: setup
DOCKER_INFLUXDB_INIT_USERNAME: username
DOCKER_INFLUXDB_INIT_PASSWORD: password
DOCKER_INFLUXDB_INIT_ADMIN_TOKEN_FILE: /run/secrets/influxdb2-admin-token
DOCKER_INFLUXDB_INIT_ORG: organization-name
DOCKER_INFLUXDB_INIT_BUCKET: bucket-name
secrets:
- influxdb2-admin-username
- influxdb2-admin-password
- influxdb2-admin-token
volumes:
- type: volume
source: influxdb2-data
target: /var/lib/influxdb2
- type: volume
source: influxdb2-config
target: /etc/influxdb2
secrets:
influxdb2-admin-username:
file: ./secrets/influxdb2-admin-username
influxdb2-admin-password:
file: ./secrets/influxdb2-admin-password
influxdb2-admin-token:
file: ./secrets/influxdb2-admin-token
volumes:
influxdb2-data:
influxdb2-config:
grafana-data:
Step 2: Running Docker Compose
To start the services, navigate to the directory containing your docker-compose.yml file and run: docker-compose up -d
Now that we have InfluxDB and Grafana running, let’s move on to writing data into InfluxDB using a Python script.
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
bucket = "bucket-name"
org = "organization-name"
token = "token-name"
url = "http://localhost:8086"
client = influxdb_client.InfluxDBClient(
url=url,
token=token,
org=org
)
# Write script
write_api = client.write_api(write_options=SYNCHRONOUS)
p = influxdb_client.Point("service1_status").tag("service1", "Up").field("status", 77)
write_api.write(bucket=bucket, org=org, record=p)
client.close()
Access Grafana: After the containers are running, you can access Grafana by navigating to http://localhost:3000. Use the following credentials to log in:
Username: username
Password: password
Add InfluxDB as a Data Source:
In Grafana, go to Configuration > Data Sources.
Add InfluxDB as a new data source.
Set the URL to http://influxdb:8086, which refers to the InfluxDB service running in Docker.
Enter the username and password, along with the bucket and organization.
Create a Dashboard:
After configuring the data source, you can create a dashboard.
Add a Panel and use the query editor to fetch data from InfluxDB, such as:
SELECT "status" FROM "service1_status" WHERE "service1" = 'Up'
You can visualize the status field for the service1_status measurement.