目录
前言
一、制作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:
@type tcp
@id debug-input
port 4560
tag debug
@type json
@type tcp
@id error-input
port 4561
tag error
@type json
@type tcp
@id business-input
port 4562
tag business
@type json
@type tcp
@id record-input
port 4563
tag record
@type json
@type parser
key_name message
reserve_data true
remove_key_name_field true
@type json
@type stdout
output_type json
@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
2.配置说明(可忽略不看)
">
定义了日志收集的来源,可以有tcp、udp、tail(文件)、forward(tcp+udp)、http等方式。
">
定义了日志收集的来源,可以有tcp、udp、tail(文件)、forward(tcp+udp)、http等方式。
这里我们从tcp请求收集日志,端口为4560,并且设置了tag为debug。
@type tcp @id debug-input port 24221 tag debug @type json
">
定义对原始数据的解析方式,可以将日志转化为JSON。
将debug日志转化为JSON可以进行如下配置。
@type tcp @id debug-input port 4560 tag debug @type json
">
可以对收集的日志进行一系列的处理,比如说将日志打印到控制台或者对日志进行解析。
对于tag为record来源的日志,我们将其中的message属性转化为JSON格式,如果不进行转化的话,message属性将会是一个字符串。
@type parser key_name message reserve_data true remove_key_name_field true @type json
">
定义了收集到的日志最后输出到哪里,可以输出到stdout(控制台)、file、elasticsearch、mongo等里面。
这里我们使用elasticsearch来存储日志信息,logstash_format、logstash_prefix、logstash_dateformat主要用来控制日志索引名称的生成,当前配置生成debug日志的索引格式为docker-debug-logs-2021-10-23,flush_interval用来控制日志输出到elasticsearch的时间间隔。
@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
3.logback-spring.xml的配置
这里面没啥说的,就是将上面的端口配置到里面,好让日志能够输出到fluentd
${LOG_STASH_HOST}:4560
三、部署fluentd
kubesphere部署无状态服务。
使用自定义经常仓库地址 这里用我自己制作上传的带es插件的镜像:
registry.cn-shanghai.aliyuncs.com/samaritan/fluentd:1.14-1。
选择使用默认端口
设置日志搜集端口。
挂载配置文件,选择之前编写的配置文件,挂载到/fluentd/etc 目录
点击下一步,创建。
查看启动日志,确定载入的是我们自定义的配置文件,且无报错。



