美文网首页
docker 通过 fluentd 输出日志测试

docker 通过 fluentd 输出日志测试

作者: 偷油考拉 | 来源:发表于2022-11-22 23:06 被阅读0次

    一、创建一个运行 fluentd 的 docker容器

    docker pull fluent/fluentd:edge-debian
    

    创建配置文件 /root/tmp/fluentd.conf

    <source>
      @type http
      port 9880
      bind 0.0.0.0
    </source>
    
    <match **>
      @type stdout
    </match>
    

    启动

    [root@svr1 ~]# docker run -p 9880:9880 -v /root/tmp:/fluentd/etc fluent/fluentd:edge-debian -c /fluentd/etc/fluentd.conf
    fluentd -c /fluentd/etc/fluentd.conf
    2022-10-09 15:18:19 +0000 [info]: parsing config file is succeeded path="/fluentd/etc/fluentd.conf"
    2022-10-09 15:18:19 +0000 [info]: gem 'fluentd' version '1.15.2'
    2022-10-09 15:18:19 +0000 [warn]: define <match fluent.**> to capture fluentd logs in top level is deprecated. Use <label @FLUENT_LOG> instead
    2022-10-09 15:18:19 +0000 [info]: using configuration file: <ROOT>
      <source>
        @type http
        port 9880
        bind "0.0.0.0"
      </source>
      <match **>
        @type stdout
      </match>
    </ROOT>
    2022-10-09 15:18:19 +0000 [info]: starting fluentd-1.15.2 pid=6 ruby="3.1.2"
    2022-10-09 15:18:19 +0000 [info]: spawn command to main:  cmdline=["/usr/local/bin/ruby", "-Eascii-8bit:ascii-8bit", "/usr/local/bundle/bin/fluentd", "-c", "/fluentd/etc/fluentd.conf", "--plugin", "/fluentd/plugins", "--under-supervisor"]
    2022-10-09 15:18:20 +0000 [info]: adding match pattern="**" type="stdout"
    2022-10-09 15:18:20 +0000 [info]: adding source type="http"
    2022-10-09 15:18:20 +0000 [warn]: #0 define <match fluent.**> to capture fluentd logs in top level is deprecated. Use <label @FLUENT_LOG> instead
    2022-10-09 15:18:20 +0000 [info]: #0 starting fluentd worker pid=15 ppid=6 worker=0
    2022-10-09 15:18:20 +0000 [info]: #0 fluentd worker is now running worker=0
    2022-10-09 15:18:20.507753396 +0000 fluent.info: {"pid":15,"ppid":6,"worker":0,"message":"starting fluentd worker pid=15 ppid=6 worker=0"}
    2022-10-09 15:18:20.512374850 +0000 fluent.info: {"worker":0,"message":"fluentd worker is now running worker=0"}
    

    查看容器

    [root@svr1 ~]# docker ps
    CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                                         NAMES
    d79fa9288e00        fluent/fluentd:edge-debian   "tini -- /bin/entr..."   15 seconds ago      Up 13 seconds       5140/tcp, 24224/tcp, 0.0.0.0:9880->9880/tcp   stupefied_einstein
    

    测试

    curl -X POST -d 'json={"json":"message"}' http://127.0.0.1:9880/sample.test
    

    容器打印如下日志

    2022-10-09 15:21:49.094392263 +0000 sample.test: {"json":"message"}
    

    二、创建docker容器,发送日志到fluentd

    需要docker版本不低于1.8

    1. 本地 fluentd 实例

    创建配置文件/root/demo.conf

    <source>
      @type forward
      port 24224
      bind 0.0.0.0
    </source>
    
    <match *>
      @type stdout
    </match>
    

    启动一个 fluentd 实例

    docker run -it -p 24224:24224 -v /root/demo.conf:/fluentd/etc/demo.conf -e FLUENTD_CONF=demo.conf fluent/fluentd:latest
    

    启动一个docker实例,并加载 --log-driver=fluentd

    [root@localhost ~]# docker run --log-driver=fluentd ubuntu echo "Hello Fluentd"
    Hello Fluentd
    

    在上面的fluentd 实例,会输出如下日志

    2022-10-09 08:18:59.000000000 +0000 071b0e3e0c06: {"log":"Hello Fluentd","container_id":"071b0e3e0c063cafe3d503d11926910cfe6b4531ac315036d93874fe172646ad","container_name":"/stoic_mendeleev","source":"stdout"}
    

    2. 远程 fluentd 实例

    修改上面创建的远程fluentd配置文件如下

    <source>
      @type http
      port 9880
      bind 0.0.0.0
    </source>
    
    <source>
      @type forward
      port 24224
      bind 0.0.0.0
    </source>
    
    <match **>
      @type stdout
    </match>
    

    启动远程fluentd实例

    docker run -p 9880:9880 -p 24224:24224 -v /root/tmp:/fluentd/etc fluent/fluentd:edge-debian -c /fluentd/etc/fluentd.conf
    

    注意:
    docker转发的 input type 是 forward

    测试发送日志到远程fluentd实例

    [root@localhost ~]# docker run --log-driver=fluentd --log-opt fluentd-address=10.0.31.70:24224 ubuntu echo "test log message for remote fluentd"
    test log message for remote fluentd
    

    远程 fluentd 实例显示如下

    2022-10-09 08:52:36.000000000 +0000 465d12eb1df3: {"container_id":"465d12eb1df39e6de2d9ada56fce60ff384d5d6c753a019eed8f2540526d64d1","container_name":"/unruffled_aryabhata","source":"stdout","log":"test log message for remote fluentd"}
    

    相关文章

      网友评论

          本文标题:docker 通过 fluentd 输出日志测试

          本文链接:https://www.haomeiwen.com/subject/hkwgartx.html