fluentd
kubesphere配置 (opens new window)
由于内存消耗低,KubeSphere 选择 Fluent Bit。Fluentd 一般在 Kubernetes 中以守护进程集的形式部署,在每个节点上收集容器日志。此外,Fluentd 支持多个插件。因此,Fluentd 会以部署的形式在 KubeSphere 中创建,将从 Fluent Bit 接收到的日志发送到多个目标,例如 S3、MongoDB、Cassandra、MySQL、syslog 和 Splunk 等。
# K8S安装配置Fluentd
# 创建 Fluentd 部署
- 以下命令将在默认命名空间
default
中创建 Fluentd 部署、服务和配置字典,并为该 Fluentd 配置字典添加filter
以排除default
命名空间中的日志,避免 Fluent Bit 和 Fluentd 重复日志收集。- 如果您想要将 Fluentd 部署至其他命名空间,请修改以下命令中的命名空间名称。
# ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: fluentd-config
namespace: default
data:
fluent.conf: |-
# Receive logs sent from Fluent Bit on port 24224
<source>
@type forward
port 24224
</source>
# Because this will send logs Fluentd received to stdout,
# to avoid Fluent Bit and Fluentd loop logs collection,
# add a filter here to avoid sending logs from the default namespace to stdout again
<filter **>
@type grep
<exclude>
key $.kubernetes.namespace_name
pattern /^default$/
</exclude>
</filter>
# Send received logs to stdout for demo/test purpose only
# Various output plugins are supported to output logs to S3, MongoDB, Cassandra, MySQL, syslog, Splunk, etc.
<match **>
@type stdout
</match>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: fluentd
name: fluentd
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: fluentd
template:
metadata:
labels:
app: fluentd
spec:
containers:
- image: fluentd:v1.9.1-1.0
imagePullPolicy: IfNotPresent
name: fluentd
ports:
- containerPort: 24224
name: forward
protocol: TCP
- containerPort: 5140
name: syslog
protocol: TCP
volumeMounts:
- mountPath: /fluentd/etc
name: config
readOnly: true
volumes:
- configMap:
defaultMode: 420
name: fluentd-config
name: config
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Service-ClusterIP
apiVersion: v1
kind: Service
metadata:
labels:
app: fluentd
name: fluentd
namespace: default
spec:
ports:
- name: forward
port: 24224
protocol: TCP
targetPort: forward
selector:
app: fluentd
sessionAffinity: None
type: ClusterIP
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 添加 Fluentd 作为日志接收器
以
admin
身份登录 KubeSphere 的 Web 控制台。点击左上角的平台管理,然后选择集群管理。如果您启用了多集群功能 (opens new window),您可以选择一个集群。
在集群管理页面,选择集群设置下的日志接收器。
点击添加日志接收器并选择 Fluentd。
输入 Fluentd 服务地址和端口信息。
Fluentd 会显示在日志接收器页面的接收器列表中,状态为收集中。
# 验证 Fluentd 能否从 Fluent Bit 接收日志
- 在集群管理页面点击应用负载。
- 点击工作负载,并在部署选项卡中选择
default
项目。 - 点击 fluentd 项目并选择 fluentd-xxxxxxxxx-xxxxx 容器组。
- 点击 fluentd 容器。
- 在 fluentd 容器页面,选择容器日志选项卡。
- 您可以看到日志持续滚动输出。
# 使用Kafka协议上传日志到阿里云
# 配置方式
使用Kafka协议上传日志时,您需要配置以下参数。
参数 | 说明 |
---|---|
连接类型 | 为保证日志传输安全性,连接协议必须为SASL_SSL。 |
hosts | 初始连接的集群地址,格式为project名称.Endpoint ,请根据Project所在的Endpoint进行配置。更多信息,请参见服务入口 (opens new window)。阿里云内网:端口号为10011,例如test-project-1.cn-hangzhou-intranet.log.aliyuncs.com:10011。公网:端口号为10012,例如test-project-1.cn-hangzhou.log.aliyuncs.com:10012。 |
topic | 配置为日志服务Logstore名称。使用Kafka生产者(produce)或Beats系列软件上传日志且指定输出格式为JSON时,您可以将topic的值设置为Logstore名称.json 格式,实现JSON日志自动展开。更多信息,请参见示例六:通过Kafka生产者(produce)上传日志 (opens new window)。 |
headers | 使用Kafka生产者(produce)或Beats系列软件上传日志且指定输出格式为JSON时,您可以将headers配置为如下内容后,实现JSON日志自动展开。headers: - key: "data-parse-format" value: "json" 更多信息,请参见示例一:通过Beats系列软件上传日志 (opens new window)。 |
username | 配置为日志服务Project名称。 |
password | 配置为阿里云AK,格式为**${access-key-id}#${access-key-secret}。请根据实际情况,将${access-key-id}替换为您的AccessKey ID,将${access-key-secret}**替换为您的AccessKey Secret。建议使用RAM用户的AK。更多信息,请参见授权 (opens new window)。 |
证书文件 | 日志服务的域名均具备可信任证书,您只需使用服务器自带的根证书即可,例如:/etc/ssl/certs/ca-bundle.crt。 |
# 使用Fluentd上传日志
Fluentd (opens new window)是一个开源的日志收集器,是云端原生计算基金会(CNCF)的成员项目之一,遵循Apache 2 License协议。
Fluentd支持众多输入、处理、输出插件,支持通过Kafka插件将日志上传到日志服务,您只需安装并配置Kafka插件即可。更多信息,请参见fluent-plugin-kafka (opens new window)。
配置示例
示例中将日志输出格式(Format)设置为JSON,除此之外还支持数十种Format类型。更多信息,请参见Fluentd Formatter (opens new window)。
<match **> @type kafka # Brokers: you can choose either brokers or zookeeper. brokers test-project-1.cn-hangzhou.log.aliyuncs.com:10012 default_topic test-logstore-1 default_message_key content output_data_type json output_include_tag true output_include_time true sasl_over_ssl true username yourusername //请根据真实值,替换yourusername。 password yourpassword //请根据真实值,替换yourpassword。 ssl_ca_certs_from_system true # ruby-kafka producer options max_send_retries 10000 required_acks 1 compression_codec gzip </match>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18日志样例
使用JSON模式输出日志后,您可以给content字段创建JSON类型的索引。更多信息,请参见JSON类型 (opens new window)。
# 附录:
fluent.conf
<source>
@type forward
port 24224
<parse>
@type "multiline"
format_firstline "/\\d{4}-\\d{1,2}-\\d{1,2}/"
format1 /^(?<timestamp>[^ ]* [^ ]*) (?<level>[^\s]+) (?<pid>[^s+]+) \[TID:(?<tid>[,a-z0-9A-Z./]+)\] --- \[(?<thread>.*)\] (?<message>[\s\S]*)/
unmatched_lines
</parse>
</source>
<filter **>
@type grep
<exclude>
key $.kubernetes.namespace_name
pattern /^default$|^kube-system$|^kubesphere-system$|^kubesphere-logging-system$|^kubesphere-monitoring-system$|^kubesphere-devops-worker$|^kubesphere-devops-system$|^argocd$|^birenchong-prod$/
</exclude>
</filter>
<match **>
@type kafka
brokers eu-central-1.log.aliyuncs.com:10012
default_topic kubernetes_log
default_message_key content
output_data_type json
output_include_tag true
output_include_time true
sasl_over_ssl true
username birenchonglive
password LxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxY
ssl_ca_certs_from_system true
max_send_retries 10000
required_acks 1
compression_codec gzip
</match>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36