本文永久链接: https://www.xtplayer.cn/rancher/rancher-logging-v2-configuration-optimization/

从 Rancher v2.6.x 开始,原来的 rancher logging v1 功能将被弃用, Banzai Cloud Logging operator 将取代原来的日志搜集功能。

Banzai Cloud Logging Operator 工作原理

Logging Operator 自动部署和配置 Kubernetes 日志流水线。它会在每个节点上部署和配置一个 Fluent Bit DaemonSet,从而收集节点文件系统中的容器和应用程序日志。

Fluent Bit 查询 Kubernetes API 并使用 pod 的元数据来丰富日志,然后将日志和元数据都传输到 Fluentd。Fluentd 会接收和过滤日志并将日志传输到多个Output

以下自定义资源用于定义了如何过滤日志并将日志发送到 Output

  • Flow 是一个命名空间自定义资源,它使用过滤器和选择器将日志消息路由到对应的 Output
  • ClusterFlow 用于路由集群级别的日志消息。
  • Output 是一个命名空间资源,用于定义发送日志消息的位置。
  • ClusterOutput 定义了一个所有 FlowClusterFlow 都可用的 Output

每个 Flow 都必须引用一个 Output,而每个 ClusterFlow 都必须引用一个 ClusterOutput

Banzai 文档中的下图显示了新的 Logging 架构:

Banzai Cloud Logging Operator 如何与 Fluentd 和 Fluent Bit 一起使用

启用 rancher logging v2

注意:因为 rancher logging v1 与 rancher logging v2 之间不兼容,并且也无法直接从rancher logging v1 迁移到 rancher logging v2,因此需要在 rancher 旧版 ui 中禁用 rancher logging v1功能。

rancher logging v2的安装与安装注意事项,可以访问以下文档

https://docs.ranchermanager.rancher.io/zh/pages-for-subheaders/logging

https://docs.ranchermanager.rancher.io/zh/integrations-in-rancher/logging/logging-helm-chart-options

配置优化

场景一,集群中有 pvc

  • 给指定节点打上 app=rancher-logging-fluentd 标签,以供 fluentd 运行。
  • 需要为 fluend 单独做内核调优,所以建议用标签调度的方式让 fluentd 运行在指定节点上。
  • 如果运行了多个 fluentd pod 副本,则需要提前给多个节点打上标签。
loggingOverlay:
spec:
fluentbit:
tolerations:
- operator: Exists
logLevel: debug
targetHost: {}
targetPort: {}
podPriorityClassName: system-node-critical
# 性能参数
flush: 1
coroStackSize: 40960 # Default 24576
inputTail:
storage.type: filesystem
Buffer_Chunk_Size: 64k
Buffer_Max_Size: 1024k
Refresh_Interval: "3"
Mem_Buf_Limit: 1024M
Multiline_Flush: "3"
# 开启负载均衡模式
enableUpstream: true
# TCP连接参数
network:
connectTimeout: 30 # Default 10
keepaliveIdleTimeout: 60 # Default 30
# 开启forward转发上游应答响应
forwardOptions:
Require_ack_response: true
storage.total_limit_size: 1024M
positiondb:
hostPath:
path: "/fluent-bit/positiondb"
bufferStorage:
storage.backlog.mem_limit: 1024M
bufferStorageVolume:
hostPath:
path: "/fluent-bit/buffers"
resources:
limits: {} # 禁用 fluent-bit 的 resources limit
# cpu: 2
# memory: 4Gi
fluentd:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- podAffinityTerm:
labelSelector:
matchExpressions:
- key: app.kubernetes.io/name
operator: In
values:
- fluentd
topologyKey: "kubernetes.io/hostname"
weight: 100
logLevel: debug
scaling:
replicas: 1
drain:
enabled: true
tolerations: {}
bufferStorageVolume:
pvc:
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 40Gi
storageClassName: xxxxx
volumeMode: Filesystem
nodeSelector:
# 给节点打上标签,以供 fluentd 调度
app: rancher-logging-fluentd
# fluentd 日志默认输出到 /fluentd/log/out,通过以下配置让日志通过标准输出显示 https://banzaicloud.com/docs/one-eye/logging-operator/configuration/fluentd/#fluentoutlogrotate
fluentOutLogrotate:
enabled: false
livenessProbe:
initialDelaySeconds: 60
periodSeconds: 30
tcpSocket:
port: 24240
resources:
limits: {} # 禁用 fluentd 的 resources limit
# cpu: 2
# memory: 4Gi
global:
cattle:
systemDefaultRegistry: registry.cn-hangzhou.aliyuncs.com

场景二,集群中没有 pvc

  • 给指定节点打上 app=rancher-logging-fluentd 标签,以供 fluentd 运行。
  • 为了保证 fluentd buffer 数据持久, 把 buffer 映射到主机目录。fluentd 以Stateful Set模式运行,需要使用标签调度的方式让 fluentd pod 运行在指定节点上。
  • 如果运行了多个 fluentd pod 副本,则需要提前给多个节点打上标签 app=rancher-logging-fluentd。
loggingOverlay:
spec:
fluentbit:
metrics:
serviceMonitor: true
tolerations:
- operator: Exists
logLevel: debug
targetHost: {}
targetPort: {}
podPriorityClassName: system-node-critical
# 性能参数
flush: 1
coroStackSize: 40960 # Default 24576
inputTail:
storage.type: filesystem
Buffer_Chunk_Size: 64k
Buffer_Max_Size: 1024k
Refresh_Interval: "3"
Mem_Buf_Limit: 1024M
Multiline_Flush: "3"
# 开启负载均衡模式
enableUpstream: true
# TCP连接参数
network:
connectTimeout: 30 # Default 10
keepaliveIdleTimeout: 60 # Default 30
# 开启forward转发上游应答响应
forwardOptions:
Require_ack_response: true
storage.total_limit_size: 1024M
positiondb:
hostPath:
path: "/fluent-bit/positiondb"
bufferStorage:
storage.backlog.mem_limit: 1024M
bufferStorageVolume:
hostPath:
path: "/fluent-bit/buffers"
resources:
limits: {} # 禁用 fluent-bit 的 resources limit
# cpu: 2
# memory: 4Gi
fluentd:
logLevel: debug
metrics:
serviceMonitor: true
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- podAffinityTerm:
labelSelector:
matchExpressions:
- key: app.kubernetes.io/name
operator: In
values:
- fluentd
topologyKey: "kubernetes.io/hostname"
weight: 100
scaling:
replicas: 1
drain:
enabled: true
tolerations: {}
disablePvc: true
bufferStorageVolume:
hostPath:
path: "/fluentd/buffers" # 指定 buffers 主机映射路径,因为 fluentd 容器内以非 root 运行,所以需要在主机上给挂载的路径添加权限:chmod -R 757 /fluentd/buffers/, 默认权限为 755
nodeSelector:
# 给节点打上标签,以供 fluentd 调度
app: rancher-logging-fluentd
# fluentd 日志默认输出到 /fluentd/log/out,通过以下配置让日志通过标准输出显示 https://banzaicloud.com/docs/one-eye/logging-operator/configuration/fluentd/#fluentoutlogrotate
fluentOutLogrotate:
enabled: false
livenessProbe:
initialDelaySeconds: 60
periodSeconds: 30
tcpSocket:
port: 24240
resources:
limits: {} # 禁用 fluentd 的 resources limit
# cpu: 2
# memory: 4Gi
global:
cattle:
systemDefaultRegistry: registry.cn-hangzhou.aliyuncs.com

内核调优

# 建议优化以下内核参数,有些参数值可以在这些参数的基础上增加

# https://docs.fluentd.org/installation/before-install#increase-the-maximum-number-of-file-descriptors
ulimit -n

# Kernel, https://docs.fluentd.org/installation/before-install#optimize-the-network-kernel-parameters
net.core.somaxconn = 1024
net.core.netdev_max_backlog = 5000
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_wmem = 4096 12582912 16777216
net.ipv4.tcp_rmem = 4096 12582912 16777216
net.ipv4.tcp_max_syn_backlog = 8096
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10240 65535
# If forward uses port 24224, reserve that port number for use as an ephemeral port.
# If another port, e.g., monitor_agent uses port 24220, add a comma-separated list of port numbers.
# net.ipv4.ip_local_reserved_ports = 24220,24224
net.ipv4.ip_local_reserved_ports = 24224
# https://docs.fluentd.org/installation/before-install#use-sticky-bit-symlink-hardlink-protection
fs.protected_hardlinks = 1
fs.protected_symlinks = 1

clusteroutput 调优

fluentd 日志发送逻辑

  • 日志会保存在 chunk 中,每个 chunk 有大小限制(chunk_limit_size),或者记录条数(chunk_limit_records)限制。要想日志发送快一些,则降低 chunk_limit_size 大小。

  • chunk 保存在 buffer 中,通过 total_limit_size 限制总的 buffer 大小。

  • 在 chunk 装满之后会被转发到发送队列中,默认发送队列只有 1 个 chunk,可通过 queued_chunks_limit_size 设置队列中 chunk 数量。

  • 通过 flush_thread_count 设置单次发送的 chunk 数量,默认 1 个 chunk。

  • 通过 flush_mode 设置发送模式,设置为 interval 表示每隔多久发送一次,默认 60s。

以 kafka 为例:

kafka:
brokers: 'xxx:xxx'
max_send_retries: 5
kafka_agg_max_bytes: 409600
slow_flush_log_threshold: 20 #Default: 20.0 (seconds)。如果出现 ‘2016-12-19 12:00:00 +0000 [warn]: buffer flush took longer time than slow_flush_log_threshold: elapsed_time=15.0031226690043695 slow_flush_log_threshold=10.0 plugin_id="foo"’ 则增加这个值 https://docs.fluentd.org/output#slow_flush_log_threshold
buffer:
tags: topic
timekey: 1m
timekey_wait: 30s
timekey_use_utc: true
chunk_limit_size: 8M
chunk_limit_records: 100000
total_limit_size: 10GB
queued_chunks_limit_size: 10
flush_thread_count: 8
flush_at_shutdown: false
flush_interval: 1s
flush_mode: interval
retry_forever: true
retry_type: periodic
# compress: gzip
default_topic: test1
format:
type: json