kubesphere k8s 安装Fluentd,带elasticsearch插件

目录

前言

一、制作Fluentd镜像

二、编写配置文件

1.编辑配置

2.配置说明(可忽略不看)

 3.logback-spring.xml的配置

三、部署fluentd




前言

Fluentd是一款开源的日志收集功能,和Elasticsearch、Kibana一起使用可以搭建EFK日志收集系统。好处就是Fluentd比Logstash轻量化的多。内存占用连Logstash的十分之一都不到。本文将演示如何在kubesphere k8s上部署Fluentd




一、制作Fluentd镜像

dockerhub上有官方的镜像,但是里面不内置elasticsearch插件。这样的话在k8s上会有些问题,没法安装啊!!

制作方法也很简单官方教程:

fluent/fluentd-docker-image: Docker image for Fluentd (github.com)

如果实在是懒得麻烦,用我做好的也行。

aliyun:           docker pull registry.cn-shanghai.aliyuncs.com/samaritan/fluentd:1.14-1



二、编写配置文件

1.编辑配置

在kubesphere上新建一个配置文件

 key:fluent.conf

value:

<source>
  @type  tcp
  @id    debug-input
  port  4560
  tag debug
  <parse>
    @type json
  </parse>
</source>

<source>
  @type  tcp
  @id    error-input
  port  4561
  tag error
  <parse>
    @type json
  </parse>
</source>

<source>
  @type  tcp
  @id    business-input
  port  4562
  tag business
  <parse>
    @type json
  </parse>
</source>

<source>
  @type  tcp
  @id    record-input
  port  4563
  tag record
  <parse>
    @type json
  </parse>
</source>

<filter record>
  @type parser
  key_name message
  reserve_data true
  remove_key_name_field true
  <parse>
    @type json
  </parse>
</filter>

<match fluent.**>
  @type stdout
  output_type json
</match>

<match **>
  @type elasticsearch
  host elastic-9g8m25-elasticsearch-master.mall-swarm
  port 9200
  type_name docker
  logstash_format true
  logstash_prefix docker-${tag}-logs
  logstash_dateformat %Y-%m-%d
  flush_interval 5s
  include_tag_key true
</match>
 

2.配置说明(可忽略不看)

 <source>

定义了日志收集的来源,可以有tcp、udp、tail(文件)、forward(tcp+udp)、http等方式。

这里我们从tcp请求收集日志,端口为4560,并且设置了tag为debug

<source>
  @type  tcp
  @id    debug-input
  port  24221
  tag debug
  <parse>
    @type json
  </parse>
</source>

 <parse>

定义对原始数据的解析方式,可以将日志转化为JSON。

 将debug日志转化为JSON可以进行如下配置。

<source>
  @type  tcp
  @id    debug-input
  port  4560
  tag debug
  <parse>
    @type json
  </parse>
</source>

 <filter xxx>

可以对收集的日志进行一系列的处理,比如说将日志打印到控制台或者对日志进行解析。

对于tag为record来源的日志,我们将其中的message属性转化为JSON格式,如果不进行转化的话,message属性将会是一个字符串。

<filter record>
  @type parser
  key_name message
  reserve_data true
  remove_key_name_field true
  <parse>
    @type json
  </parse>
</filter>

 <match>

定义了收集到的日志最后输出到哪里,可以输出到stdout(控制台)、file、elasticsearch、mongo等里面。

 这里我们使用elasticsearch来存储日志信息,logstash_formatlogstash_prefixlogstash_dateformat主要用来控制日志索引名称的生成,当前配置生成debug日志的索引格式为docker-debug-logs-2021-10-23flush_interval用来控制日志输出到elasticsearch的时间间隔。

<match **>
  @type elasticsearch
  host elastic-9g8m25-elasticsearch-master.samaritan
  port 9200
  type_name docker
  logstash_format true
  logstash_prefix docker-${tag}-logs
  logstash_dateformat %Y-%m-%d
  flush_interval 5s
  include_tag_key true
</match>

 3.logback-spring.xml的配置

这里面没啥说的,就是将上面的端口配置到里面,好让日志能够输出到fluentd

<appender name="LOG_STASH_DEBUG" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
    <destination>${LOG_STASH_HOST}:4560</destination>
</appender>

三、部署fluentd

kubesphere部署无状态服务。

 使用自定义经常仓库地址 这里用我自己制作上传的带es插件的镜像:

registry.cn-shanghai.aliyuncs.com/samaritan/fluentd:1.14-1。

选择使用默认端口

设置日志搜集端口。

 挂载配置文件,选择之前编写的配置文件,挂载到/fluentd/etc 目录

 点击下一步,创建。

查看启动日志,确定载入的是我们自定义的配置文件,且无报错。


猜你喜欢

转载自blog.csdn.net/qq_31277409/article/details/120922878