我如何运行带有修改过的telegraf配置文件的docker compose文件?

问题描述 投票:2回答:1

我正在尝试在MacOS上运行docker compose文件以运行Telegraf,Mosquitto(MQTT),Grafana和InfluxDB。我正在尝试使用修改后的配置文件运行Telegraf。最终目的是存储和显示从arduino肌肉传感器发送的数据。

docker撰写文件当前看起来像这样:

version: '3'

services: 
  influxdb:
    container_name: influxdb
    image: influxdb
    ports:
        - "8086:8086"
    environment:
      - INFLUXDB_DB=sensors
      - INFLUXDB_ADMIN_USER=telegraf
      - INFLUXDB_ADMIN_PASSWORD=telegraf
    restart: always

  telegraf: 
    image: telegraf
    restart: always
    ports: 
      - "5050:5050"
    volumes:
    - $PWD/telegraf.conf:/etc/telegraf/telegraf.conf:ro  

  grafana: 
    container_name: grafana
    image: grafana/grafana
    links:
      - influxdb
    hostname: grafana
    ports:
        - "3000:3000"

  mosquitto:
    container_name: mosquitto
    image: eclipse-mosquitto
    ports:
      - "1883:1883"
      - "9001:9001"
    depends_on: 
      - influxdb
    restart: always

我可以运行build命令,而Mosquitto,Grafana和InfluxDB似乎都可以运行,但是Telegraf存在许多问题。无论在撰写文件中对卷进行了什么更改,都会使用Telegraf的默认配置文件,而不是我修改过的配置,这似乎阻止了数据发送到InfluxDB。

Telegraf发布到InfluxDB错误看起来如下:

telegraf     | 2020-03-03T11:40:49Z E! [outputs.influxdb] When writing to [http://localhost:8086]: Post http://localhost:8086/write?db=telegraf: dial tcp 127.0.0.1:8086: connect: connection refused
telegraf     | 2020-03-03T11:40:49Z E! [agent] Error writing to outputs.influxdb: could not write any address

Mosquitto似乎可以工作,因为MQTT.fx应用程序能够连接并发布/订阅该容器。但是,会建立常规连接并使用未知名称删除该连接。

以下连接错误连续记录:

mosquitto    | 1583247033: New connection from 172.25.0.1 on port 1883.
mosquitto    | 1583247033: Client <unknown> disconnected due to protocol error.

我曾考虑编写一个Telegraf dockerfile来设置配置文件,但是这似乎有点过头了,因为我的理解是撰写文件的volume部分应允许使用此配置文件。

我的telegraf.conf文件与docker-compose.yml文件位于同一目录。

问题是a)我是否认为容器使用默认的telegraf文件正确b)如何将更改后的telegraf.conf文件保存到容器中

docker docker-compose influxdb telegraf
1个回答
0
投票

没有您的telegraf配置文件,很难判断它是否正确加载,并且存在网络问题...

我发现不是使用:$PWD/telegraf.conf:/etc/telegraf/telegraf.conf:ro,而是将docker的配置文件包含在本地子目录中更有意义:.docker/telegraf/telegraf.conf:/etc/telegraf/telegraf.conf:ro

如果要对PWD使用完整路径,建议使用${PWD}。根据我的经验,$PWD正在使用终端中的变量(您可以测试是否将其分配给echo $PWD),其中${PWD}实际上会运行打印工作目录并输出结果(您可以使用echo ${PWD}

为了完整起见,该堆栈(基于BME280传感器和Ardunio)应该可以正常工作(为了安全起见-我已更改凭据,因此如果它不能正常工作就从那里开始!)。我已对其进行评论,仅供我参考,希望对您有所帮助:

version: '3'

# To Do:
# - Setup defined networks to protect influxdb and telegraf
# - Define a backup process for data
# - Monitor implications of version tags/docker container lifecycles

services: 

    # MQTT Broker, handles data from sensors
    # https://hub.docker.com/_/eclipse-mosquitto
    mosquitto:
        # Name this container so other containers can find it easily
        # Name used in: 
        # - Telegraf config
        container_name: mosquitto
        image: eclipse-mosquitto:1.6
        ports:
            - "1883:1883"
            - "9001:9001"
        depends_on: 
            - influxdb
        restart: always
        volumes:
            # Use a volume for storage
            # since influxdb stores data long term this might not be needed?
            - mosquitto-storage:/mosquitto/data

    # Data storage
    # https://hub.docker.com/_/influxdb
    influxdb:
        # Name this container so other containers can find it easily
        # Name used in: 
        # - Grafana data source 
        # - Telegraf config
        container_name: influxdb
        image: influxdb:1.5
        ports:
            - "8086:8086"
        environment:
            - INFLUXDB_DB=sensors
            - INFLUXDB_ADMIN_USER=admin-user
            - INFLUXDB_ADMIN_PASSWORD=telegraf-admin-password
            - INFLUXDB_USER=telegraf-username
            - INFLUXDB_PASSWORD=telegraf-password
        restart: always
        volumes:
          # Data persistence (could also be a bind mount: /srv/docker/influxdb/data:/var/lib/influxdb)
          - influxdb-storage:/var/lib/influxdb
          # Backups...
          - ./influxdb-backup:/backup
          # Host can run the following on a crontab, then rsnapshot can pickup:
          # docker exec -it influxdb influxd backup -database sensors /backup

    # Message formattet (MQTT -> InfluxDB)
    # https://hub.docker.com/_/telegraf
    telegraf: 
        image: telegraf:1.11
        restart: always
        ports: 
            - "5050:5050"
        depends_on: 
            - influxdb
        volumes:
            # This file needs edited with your MQTT topics, host, etc
            - .docker/telegraf/telegraf.conf:/etc/telegraf/telegraf.conf:ro

    # Dashboard/graphing
    # https://hub.docker.com/r/grafana/grafana
    grafana: 
        # Grafana tags are a mess! just use whatever...
        image: grafana/grafana
        links:
            - influxdb
        hostname: grafana
        ports:
            - "3000:3000"
        depends_on: 
            - influxdb
        volumes:
            # Grafana gets grumpy over bind mount permissions, use a volume
            - grafana-storage:/var/lib/grafana

volumes:
    mosquitto-storage:
    influxdb-storage:
    grafana-storage:

而且我的telegraf配置文件看起来像这样:

[[inputs.mqtt_consumer]]
  servers = ["tcp://mosquitto:1883"]

  topics = [
    "home/sensor/bme280/temperature",
  ]

  username = "mqttuser"
  password = "mqttpassword"
  data_format = "value"
  data_type = "float"

[[outputs.influxdb]]

  urls = ["http://influxdb:8086"]
  database = "sensors"
  skip_database_creation = true
  timeout = "5s"
  username = "telegraf-username"
  password = "telegraf-password" 
  user_agent = "telegraf"
  udp_payload = "512B"

注意,我使用的是容器名称而不是docker中的本地IP地址。

© www.soinside.com 2019 - 2024. All rights reserved.