Fluentd直接传输日志给MongoDB副本集 (replset)

官方文档地址:https://docs.fluentd.org/output/mongo_replset

td-agent版本默认没有包含out_mongo插件,需要安装这个插件才能使用

使用的是td-agent,安装这个插件:

$ /usr/sbin/td-agent-gem install gems
$ /usr/sbin/td-agent-gem install fluent-plugin-mongo

使用的是Fluentd,安装这个插件:

$ fluent-gem install fluent-plugin-mongo

有关插件管理可以看这篇文章,地址:https://www.cnblogs.com/sanduzxcvbnm/p/13936280.html

配置示例

# Single MongoDB
<match mongo.**>
  @type mongo_replset
  database fluentd
  collection test
  nodes localhost:27017,localhost:27018,localhost:27019

  # The name of the replica set
  replica_set myapp

  <buffer>
    # flush
    flush_interval 10s
  </buffer>
</match>

参数说明

  • @type:必填mongo_replset
  • nodes:必填,MongoDB的URI连接字符串,类型是string,举例:host1:27017,host2:27017,host3:27017
  • database:必填,MongoDB数据库,类型是string,默认是nil
  • collection:必填,集合名称,类型是string,如果没有设置tag_mapped则默认是'untagged' ,但是实际没有用到
  • tag_mapped:是否允许out_mongo使用Fluentd的标记来确定目标集合,类型是bool,默认是false
  • capped:启用capped集合,类型是string
  • capped_size:上限集合大小,类型是size
  • user:用于身份验证的用户名,类型是string
  • password:用于身份验证的密码,类型是string
  • replica_set:必填,mongodb副本集标识
  • read:副本集读取首选项,类型是string,默认是nil
  • num_retries:复制集故障转移阈值。默认阈值为60。如果重试计数达到此阈值,则插件将引发异常。类型是integer,默认60

connection_string参数官方文档上是必填,但是实际使用的时候没找到这个咋用的,官方示例上也没有用这个

如果没有配置tag_mapped,则说使用配置文件中的中的database(数据库)+collection(集合/表)

如下这个示例,正常情况下是fluentd+misc,但是设置了tag_mapped,tag是mongo.foo,又因为设置了remove_tag_prefix,去掉了mongo.,只保留了foo,最终使用得是fluentd+foo

<match mongo.*>
  @type mongo_replset
  database fluentd
  nodes localhost:27017,localhost:27018,localhost:27019

  # Set 'tag_mapped' if you want to use tag mapped mode.
  tag_mapped

  # If the tag is "mongo.foo", then the prefix "mongo." is removed.
  # The inserted collection name is "foo".
  remove_tag_prefix mongo.

  # This configuration is used if the tag is not found. The default is 'untagged'.
  collection misc
</match>

用法示例

mongodb副本集安装参考:https://www.cnblogs.com/sanduzxcvbnm/p/13937264.html
这个示例设置tag_mapped了,最终的是fluentd+nginx

<source>
  @type tail
  @id input_tail
  <parse>
    @type nginx
  </parse>
  path /usr/local/openresty/nginx/logs/host.access.log
  tag mongo.nginx
</source>


<match mongo.nginx>
  @type mongo_replset
  database mongo
  collection test # 设置了tag_mapped,这个就不生效了
  nodes 192.168.0.253:27027,192.168.0.253:27028,192.168.0.253:27029

  user mongo # 事先创建好的数据库
  password 12345
  
  replica_set rs0 # 创建副本集时使用的副本集标识
  tag_mapped
  
  remove_tag_prefix mongo.
  
  capped
  capped_size 1024m
  
  <buffer>
    flush_interval 10s
  </buffer>
</match>

启动openresty,浏览器访问,然后使用Navicat查看数据

原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/13937428.html