1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-28 13:48:49 +08:00

MF-296 - Create docker-compose with InfluxDB and Grafana (#306)

* Add docker-compose

Update README.md accordingly.

Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>

* Move writer compose files to root docker dir

Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>

* Remove Point from InfluxDB writer config

Update docs.

Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
This commit is contained in:
Dušan Borovčanin 2018-06-01 11:20:44 +02:00 committed by Nikola Marčetić
parent 781d358f38
commit 9d7f4544c7
5 changed files with 72 additions and 14 deletions

View File

@ -33,7 +33,6 @@ const (
envNatsURL = "MF_NATS_URL"
envPort = "MF_INFLUX_WRITER_PORT"
envDBName = "MF_INFLUX_WRITER_DB_NAME"
envDBPoint = "MF_INFLUX_WRITER_DB_POINT"
envDBHost = "MF_INFLUX_WRITER_DB_HOST"
envDBPort = "MF_INFLUX_WRITER_DB_PORT"
envDBUser = "MF_INFLUX_WRITER_DB_USER"
@ -44,7 +43,6 @@ type config struct {
NatsURL string
Port string
DBName string
DBPoint string
DBHost string
DBPort string
DBUser string
@ -69,7 +67,7 @@ func main() {
}
defer client.Close()
repo, err := influxdb.New(client, cfg.DBName, cfg.DBPoint)
repo, err := influxdb.New(client, cfg.DBName)
if err != nil {
logger.Error(fmt.Sprintf("Failed to create InfluxDB writer: %s", err.Error()))
os.Exit(1)
@ -99,7 +97,6 @@ func loadConfigs() (config, influxdata.HTTPConfig) {
NatsURL: mainflux.Env(envNatsURL, defNatsURL),
Port: mainflux.Env(envPort, defPort),
DBName: mainflux.Env(envDBName, defDBName),
DBPoint: mainflux.Env(envDBPoint, defPointName),
DBHost: mainflux.Env(envDBHost, defDBHost),
DBPort: mainflux.Env(envDBPort, defDBPort),
DBUser: mainflux.Env(envDBUser, defDBUser),

View File

@ -0,0 +1,49 @@
###
# This docker-compose file contains optional InfluxDB, InfluxDB-writer and Grafana services
# for Mainflux platform. Since these are optional, this file is dependent of docker-compose file
# from <project_root>/docker. In order to run these optional service, execute command:
# docker-compose -f docker/docker-compose.yml -f docker/addons/influxdb/docker-compose.yml up
# from project root.
###
version: "3"
services:
influxdb:
image: influxdb:1.5.2-alpine
container_name: mainflux-influxdb
restart: on-failure
environment:
INFLUXDB_DB: mainflux
INFLUXDB_ADMIN_USER: mainflux
INFLUXDB_ADMIN_PASSWORD: mainflux
influxdb-writer:
image: mainflux/influxdb:latest
container_name: mainflux-influxdb-writer
depends_on:
- influxdb
- nats
expose:
- 8900
restart: on-failure
environment:
MF_NATS_URL: nats://nats:4222
MF_INFLUX_WRITER_PORT: 8900
MF_INFLUX_WRITER_DB_NAME: mainflux
MF_INFLUX_WRITER_DB_HOST: influxdb
MF_INFLUX_WRITER_DB_PORT: 8086
MF_INFLUX_WRITER_DB_USER: mainflux
MF_INFLUX_WRITER_DB_PASS: mainflux
ports:
- 8900:8900
grafana:
image: grafana/grafana:5.1.3
container_name: mainflux-grafana
depends_on:
- influxdb
restart: on-failure
ports:
- 3001:3000

View File

@ -13,7 +13,6 @@ default values.
| MF_NATS_URL | NATS instance URL | nats://localhost:4222 |
| MF_INFLUX_WRITER_PORT | Service HTTP port | 8180 |
| MF_INFLUX_WRITER_DB_NAME | InfluxDB database name | mainflux |
| MF_INFLUX_WRITER_DB_POINT | InfluxDB point to write data to | messages |
| MF_INFLUX_WRITER_DB_HOST | InfluxDB host | localhost |
| MF_INFLUX_WRITER_DB_PORT | Default port of InfluxDB database | 8086 |
| MF_INFLUX_WRITER_DB_USER | Default user of InfluxDB database | mainflux |
@ -33,7 +32,6 @@ default values.
MF_NATS_URL: [NATS instance URL]
MF_INFLUX_WRITER_PORT: [Service HTTP port]
MF_INFLUX_WRITER_DB_NAME: [InfluxDB database name]
MF_INFLUX_WRITER_DB_POINT: [point name]
MF_INFLUX_WRITER_DB_HOST: [InfluxDB database host]
MF_INFLUX_WRITER_DB_PORT: [InfluxDB port]
MF_INFLUX_WRITER_DB_USER: [InfluxDB admin user]
@ -58,10 +56,23 @@ make influxdb
make install
# Set the environment variables and run the service
MF_NATS_URL=[NATS instance URL] MF_INFLUX_WRITER_PORT=[Service HTTP port] MF_INFLUX_WRITER_DB_NAME=[InfluxDB database name] MF_INFLUX_WRITER_DB_POINT=[point name] MF_INFLUX_WRITER_DB_HOST=[InfluxDB database host] MF_INFLUX_WRITER_DB_PORT=[InfluxDB port] MF_INFLUX_WRITER_DB_USER=[InfluxDB admin user] MF_INFLUX_WRITER_DB_PASS=[InfluxDB admin password] $GOBIN/mainflux-influxdb
MF_NATS_URL=[NATS instance URL] MF_INFLUX_WRITER_PORT=[Service HTTP port] MF_INFLUX_WRITER_DB_NAME=[InfluxDB database name] MF_INFLUX_WRITER_DB_HOST=[InfluxDB database host] MF_INFLUX_WRITER_DB_PORT=[InfluxDB port] MF_INFLUX_WRITER_DB_USER=[InfluxDB admin user] MF_INFLUX_WRITER_DB_PASS=[InfluxDB admin password] $GOBIN/mainflux-influxdb
```
### Using docker-compose
This service can be deployed using docker containers.
Docker compose file is available in <project_root>/docker/addons/influxdb/docker-compose.yml. Besides database
and writer service, it contains [Grafana platform](https://grafana.com/) which can be used for database
exploration and data visualization and analytics. In order to run all Mainflux core services, as well as mentioned optional ones, execute following command:
```bash
docker-compose -f docker/docker-compose.yml -f docker/addons/influxdb/docker-compose.yml up -d
```
_Please note that order matters here. You need to start core services before additional ones, i. e. core services compose file needs to be the first param of the command. Since all services need to be in the same network and writer services are dependent of core ones, you need to start all of them using single command._
## Usage
Starting service will start consuming normalized messages in SenML format.

View File

@ -10,20 +10,21 @@ import (
"github.com/mainflux/mainflux"
)
const pointName = "messages"
var _ writers.MessageRepository = (*influxRepo)(nil)
type influxRepo struct {
database string
pointName string
client influxdata.Client
database string
client influxdata.Client
}
type fields map[string]interface{}
type tags map[string]string
// New returns new InfluxDB writer.
func New(client influxdata.Client, database, pointName string) (writers.MessageRepository, error) {
return &influxRepo{database, pointName, client}, nil
func New(client influxdata.Client, database string) (writers.MessageRepository, error) {
return &influxRepo{database, client}, nil
}
func (repo *influxRepo) Save(msg mainflux.Message) error {
@ -35,7 +36,7 @@ func (repo *influxRepo) Save(msg mainflux.Message) error {
}
tags, fields := repo.tagsOf(&msg), repo.fieldsOf(&msg)
pt, err := influxdata.NewPoint(repo.pointName, tags, fields, time.Now())
pt, err := influxdata.NewPoint(pointName, tags, fields, time.Now())
if err != nil {
return err
}

View File

@ -64,7 +64,7 @@ func TestSave(t *testing.T) {
client, err := influxdata.NewHTTPClient(clientCfg)
assert.Nil(t, err, fmt.Sprintf("Creation of InfluxDB client expected to succeed.\n"))
repo, err := writer.New(client, testDB, "messages")
repo, err := writer.New(client, testDB)
assert.Nil(t, err, fmt.Sprintf("InfluxDB repo creation expected to succeed.\n"))
err = repo.Save(msg)