【问题标题】:How do I run a docker compose file with modified telegraf config file?如何使用修改后的电报配置文件运行 docker compose 文件?
【发布时间】:2020-03-03 15:18:08
【问题描述】:

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

docker compose 文件目前如下所示:

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

我可以运行构建命令,并且 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 来设置配置文件,但这似乎有点过头了,因为我的理解是撰写文件的卷部分应该允许使用此配置文件。

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

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

【问题讨论】:

    标签: docker docker-compose influxdb telegraf


    【解决方案1】:

    如果没有您的 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:
    
    

    我的电报配置文件如下所示:

    [[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 地址。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 2019-07-14
      • 2014-05-15
      • 2021-02-10
      相关资源
      最近更新 更多