二、spec 内容说明
采集规则 spec 部分主要定义了日志的采集源以及日志输出端的配置信息。
注意:clsDetail 和 kafkaDetail 在同一个采集规则中只能配置一种。
| 字段 | 是否必填 | 说明 |
| inputDetail | 是 | 定义采集源。 |
| clsDetail | 否 | CLS 采集器配置,该配置信息将由日志组件 cls-provisioner 同步到 CLS 侧。 |
| kafkaDetail | 否 | Kafka 采集器配置,定义日志采集格式等信息。 |
三、inputDetail 说明
{ "type": "host_file", "containerStdout": {}, "containerFile": {}, "hostFile": {}}
| 字段 | 是否必填 | 类型 | 说明 |
| type | 是 | String | 采集类型,取值如下:container_file:容器内文件采集,该类型对应 containerFile 配置项。 container_stdout:采集容器标准输出。host_file:采集节点日志,超级节点不支持该配置。 |
| containerStdout | 否 | Object | 容器标准输出采集配置。配置采集容器标准输出时,需指定采集类型为 container_stdout。 |
| containerFile | 否 | Object | 容器内路径采集。配置采集容器标准输出时,需指定采集类型为 container_file。 |
| hostFile | 否 | Object | 节点路径采集。配置采集容器标准输出时,需指定采集类型为 container_file。 |
四、containerStdout 对象结构说明
{ "includeLabels": { "label1": "value1" }, "excludeLabels": { "label": "value" }, "metadataLabels": [ "label1" ], "metadataContainer": [ "namespace", "pod_name", "pod_ip", "pod_uid", "container_id", "container_name", "image_name", "cluster_id" ], "container": "container", "workloads": [ { "container": "kubernetes-proxy", "kind": "deployment", "name": "kubernetes-proxy", "namespace": "default" }, { "container": "testlog11", "kind": "deployment", "name": "testlog1", "namespace": "default" } ], "customLabels": { "xx": "xx" }}
| 字段 | 是否必填 | 类型 | 说明 |
| includeLabels | 否 | Map | 采集包含指定 label:value 的 pod,不能与 workloads 同时配置。 |
| excludeLabels | 否 | Map | 排除包含指定 label:value 的 pod,不能与 workloads 同时配置。 |
| metadataLabels | 否 | Array | 定义将 pod 的哪些 label 作为元数据上报。不指定则全部上报。 |
| metadataContainer | 否 | Array | 定义将 pod、container 的哪些信息作为元数据上报。不指定则全部上报。 |
| customLabels | 否 | Map | 用户自定义元数据。 |
| container | 否 | String | 指定采集哪些容器的日志。不指定或配置 *,则采集命中 pod 的所有容器的标准输出。不能与 workloads 同时配置。 |
| workloads | 否 | Array | 指定需要采集哪些 workload 的标准输出,同时指定采集的容器名称。 |
workload 对象说明
| 字段 | 是否必填 | 类型 | 说明 |
| container | 否 | String | 需要采集的容器名称。不指定,则采集所有容器标准输出。 |
| kind | 是 | String | workload 类型。 |
| name | 是 | String | workload 名称。 |
| namespace | 是 | String | workload 所在 namespace。 |
五、containerFile 对象结构说明
{ "logPath": "/var/logs", "filePattern": "*.log", "filePaths": [ { "path": "/var/logs", "file": "*.log" } ], "customLabels": { "key": "value1" }, "namespace": "default", "nsLabelSelector": "", "excludeNamespace": "xxx,xx", "includeLabels": { "label": "value" }, "excludeLabels": { "exLabel": "exvalue" }, "metadataLabels": [ "xx" ], "metadataContainer": [ "namespace", "pod_name", "pod_ip", "pod_uid", "container_id", "container_name", "image_name", "cluster_id" ], "container": "xxx", "workload": { "name": "xxx", "kind": "deployment" }}
| 字段 | 是否必填 | 类型 | 说明 |
| logPath | 否 | String | 采集路径。不可与 filePaths 同时配置。 |
| filePattern | 否 | String | 预期采集文件匹配模式,不可与 filePaths 同时配置。通配符支持 * 和 ?。 |
| filePaths | 是 | Array | 配置预期采集容器内的路径及文件匹配模式,可配置多个采集路径。 |
| includeLabels | 否 | Map | 采集包含指定 label:value 的 pod,不能与 workload 同时配置。 |
| excludeLabels | 否 | Map | 排除包含指定 label:value 的 pod,不能与 workload 同时配置。 |
| metadataLabels | 否 | Array | 定义将 pod 的哪些 label 作为元数据上报。不指定则全部上报。 |
| metadataContainer | 否 | Array | 定义将 pod、container 的哪些信息作为元数据上报。不指定则全部上报。 |
| customLabels | 否 | Map | 用户自定义元数据。 |
| container | 否 | String | 指定采集哪些容器的日志。 |
| workload | 否 | Object | 指定需要采集哪些 workload 的标准输出,同时指定采集的容器名称。 |
workload 字段说明
| 字段 | 是否必填 | 类型 | 说明 |
| kind | 是 | String | workload 类型。 |
| name | 是 | String | workload 名称。 |
filePath 对象字段说明
| 字段 | 是否必填 | 类型 | 说明 |
| path | 是 | String | 预期采集的日志所在路径。 |
| file | 是 | String | 预期采集的日志文件名称匹配模式,通配符支持 * 和 ?。 |
六、hostFile 对象结构说明
{ "logPath": "/var/logs", "filePattern": "*.log", "filePaths": [ { "path": "/var/log", "file": "*.log" } ], "customLabels": { "key": "value" }}
| 字段 | 是否必填 | 类型 | 说明 |
| logPath | 否 | String | 采集路径。不可与 filePaths 同时配置。 |
| filePattern | 否 | String | 预期采集文件匹配模式,不可与 filePaths 同时配置。通配符支持 * 和 ?。 |
| filePaths | 否 | Array | 配置预期采集容器内的路径及文件匹配模式,可配置多个采集路径。 |
| customLabels | 否 | Map | 用户自定义元数据。 |
七、clsDetail 对象结构说明
{ "topicId": "", "topicName": "", "logsetId": "", "logsetName": "", "logFormat": "", "logType": "", "hotPeriod": 7, "period": 30, "partitionCount": 1, "tags": [ { "key": "key", "value": "value" } ], "autoSplit": false, "maxSplitPartitions": 50, "storageType": "hot", "extractRule": {…}, "excludePaths": [ { "excludeType": "File", "value": "/var/log/1.log" }, { "excludeType": "Path", "value": "/var/log1/" } ], "fullTextIndex": {…}, "indexs": [ {…} ], "indexStatus": "off", "region": "ap-xxx", "advancedConfig": {…}}
| 字段名 | 是否必填 | 类型 | 说明 |
| topicId | 否 | String | 日志主题 ID。 |
| topicName | 否 | String | 日志主题名称。当 topicId 为空时,指定日志主题名称,可根据日志主题名称自动拉取日志主题 ID,或者自动创建日志主题。 |
| logsetId | 否 | String | 日志集 ID。 |
| logsetName | 否 | String | 日志集名称。当 logsetId 为空时,指定日志集名称,可根据日志集名称自动拉取日志集 ID,或者自动创建日志集。 |
| region | 否 | String | 日志主题所在 region。默认为集群所在 region。 |
| logType | 否 | String | 日志类型。可选值:json_log(json 格式)、delimiter_log(分隔符提取)、minimalist_log(单行全文格式)、multiline_log(多行全文格式)、fullregex_log(完全正则格式)。默认为 minimalist_log。 |
| logFormat | 否 | String | 日志格式化。 |
| storageType | 否 | String | 日志主题存储类型(仅在自动创建 topic 时生效)。可选值:hot(标准存储)、cold(低频存储)。默认 hot。 |
| hotPeriod | 否 | Number | 日志沉降开关(仅在自动创建 topic 时生效)。0:关闭日志沉降,非 0:开启日志沉降后标准存储的天数。HotPeriod 需要大于等于 7,且小于 Period。仅在 StorageType 为 hot 时生效。 |
| period | 否 | Number | 日志存储周期(单位天)(仅在自动创建 topic 时生效)。标准存储取值范围 1~3600,低频存储取值范围 7~3600 天。取值为 3640 时代表永久保存。默认 30 天。 |
| partitionCount | 否 | Number | 日志主题分区个数(仅在自动创建 topic 时生效)。默认创建 1 个,最大支持创建 10 个分区。 |
| tags | 否 | Array | 标签列表(仅在自动创建 topic 时生效)。 |
| autoSplit | 否 | Bool | 是否开启自动分裂,“false” 或 "true"(仅在自动创建 topic 时生效)。 |
| maxSplitPartitions | 否 | Number | 开启自动分裂后,每个日志主题能够容许的最大分区数,默认为 50 个(仅在自动创建 topic 时生效)。 |
| extractRule | 否 | Object | 采集规则相关配置。 |
| excludePaths | 否 | Array | 采集黑路径配置。 |
| fullTextIndex | 否 | Object | 全文索引配置(仅在自动创建 topic 时生效)。 |
| indexs | 否 | Array | 索引配置(仅在自动创建 topic 时生效)。 |
| indexStatus | 否 | String | 索引状态(仅在自动创建 topic 时生效)。 |
| autoIndex | 否 | String | 是否开启自动索引(仅在自动创建 topic 时生效)。 |
| advancedConfig | 否 | Object | 高级采集配置。 |
八、tag 对象结构说明
{ "key": "key", "value": "value" }
| 字段 | 类型 | 是否必填 | 说明 |
| key | String | 是 | 标签名称。 |
| value | String | 是 | 标签值。 |
九、extractRule 对象结构说明
{ "timeKey": "xxx", "timeFormat": "xxx", "delimiter": "xxx", "logRegex": "xxx", "beginningRegex": "xxx", "keys": [ "xx" ], "filterKeys": [ "xx" ], "filterRegex": [ "xx" ], "unMatchUpload": "true", "unMatchedKey": "parseFailed", "backtracking": "-1", "isGBK": "true", "jsonStandard": "true", "advancedFilters": [ { "key": "level", "rule": 0, "value": "info" } ]}
| 字段名 | 是否必填 | 类型 | 说明 |
| timeKey | 否 | String | 时间字段的 key 名字,timeKey 和 timeFormat 必须成对出现。 |
| timeFormat | 否 | String | 时间字段的格式,参考 C 语言的 strftime 函数对于时间的格式说明输出参数。 |
| delimiter | 否 | String | 分隔符类型日志的分隔符,只有 logType 为 delimiter_log 时有效。 |
| logRegex | 否 | String | 整条日志匹配规则,只有 logType 为 fullregex_log 时有效。 |
| beginningRegex | 否 | String | 行首匹配规则,只有 logType 为 multiline_log 或 fullregex_log 时有效。 |
| keys | 否 | Array | 取的每个字段的 key 名字,为空的 key 代表丢弃这个字段,只有 logType 为 delimiter_log 时有效,json_log 的日志使用 json 本身的 key。 |
| filterKeys | 否 | Array | 需要过滤日志的 key,与 filterRegex 按下标进行对应。 |
| filterRegex | 否 | Array | 需要过滤日志的 key 对应的 regex,与 filterKeys 按下标进行对应。 |
| unMatchUpload | 否 | String | 解析失败日志是否上传,true 表示上传,false 表示不上传。 |
| unMatchedKey | 否 | String | 失败日志的 key。 |
| backtracking | 否 | String | 增量采集模式下的回溯数据量,默认 -1(全量采集),0 表示增量。 |
| isGBK | 否 | String | 是否为 GBK 编码。可选值:"false":否;"true":是。注意:此字段可能返回 null,表示取不到有效值。 |
| jsonStandard | 否 | String | 是否为标准 JSON。可选值:"false":否;"true":是。注意:此字段可能返回 null,表示取不到有效值。 |
| advancedFilters | 否 | Array | 高级过滤规则,该字段仅适用于 v1.1.15 及以上的采集组件版本,低于 v1.1.15 请使用 filterKeys 与 filterRegex。 |
十、advancedFilters 对象结构说明
{ "key": "level", // 需要过滤的key "rule": 0, // 过滤规则 0(等于),1(字段存在),2(字段不存在), 3(不等于) "value": "info" // 需要过滤的值value。 其中当rule为1或2时, value无需指定 }
十一、excludePath 对象结构说明
{ "excludeType": "File", "value": "/var/log/1.log" }
| 字段 | 类型 | 是否必填 | 说明 |
| excludeType | String | 是 | 排除类型。取值 File、Path。 |
| excludeType | String | 是 | 排除的日志路径或者路径。 |
十二、fullTextIndex 对象结构说明
{ "caseSensitive": false, "containZH": false, "status": "on", "tokenizer": "@&()='\\",;:<>[]{}\\/ \\n\\t\\r" }
| 字段名 | 类型 | 是否必填 | 说明 |
| caseSensitive | bool | 否 | 是否大小写敏感。 |
| containZH | bool | 否 | 是否包含中文。 |
| status | String | 否 | 全文索引开关。如果不设置,则会默认打开全文索引;如果设置为 on 打开,不设置其他参数,则 caseSensitive 和 tokenizer 都会是默认值;如果设置为 off 关闭,则不打开全文索引。 |
| tokenizer | String | 否 | 全文索引的分词符。如果全文索引开关打开,则必须设置 tokenizer。可以设置为 "@&()='",;:<>[]{}/ \\n\\t\\r",为默认设置。 |
十三、index 对象结构说明
{ "indexName": "xxx", "indexType": "text", "tokenizer": "@&()='\\",;:<>[]{}/ \\n\\t\\r", "sqlFlag": true, "containZH": false }
| 字段名 | 类型 | 是否必填 | 说明 |
| indexName | String | 是 | 索引名称。 |
| indexType | String | 是 | 索引类型。可选值:long、text、double。 |
| tokenizer | String | 否 | 字段分词符。 |
| sqlFlag | bool | 否 | 字段是否开启分析功能。 |
| containZH | bool | 否 | 是否包含中文。 |
十四、advancedConfig 对象结构说明
{"ClsAgentMaxDepth": 1,"ClsAgentFileTimeout": 60,"ClsAgentParseFailMerge": false,}
| 字段名 | 类型 | 是否必填 | 说明 |
| ClsAgentMaxDepth | Number | 否 | 当前配置采集时下钻的深度。 |
| ClsAgentFileTimeout | Number | 否 | 当前配置在采集文件时,当文件在指定时间未写入日志时,采集器释放文件句柄。 |
| ClsAgentParseFailMerge | bool | 否 | 采集器解析文件失败后是否合并日志上报。 |
十五、kafkaDetail 对象结构说明
{ "brokers": "127.0.0.1:9092", "topic": "test_log", "timestampKey": "@timestamp", "timestampFormat": "double", "extractRule": { "beginningRegex": "xxxx" }, "logType": "minimalist_log", "metadata": { "formatType": "json" }}
| 字段名 | 类型 | 是否必填 | 说明 |
| brokers | String | 是 | Kafka brokers。 |
| topic | String | 是 | Kafka topic。 |
| timestampKey | String | 否 | 时间戳的 key,默认 @timestamp。 |
| timestampFormat | String | 否 | 时间戳格式。可选值:double、iso8601。默认 double。 |
| extractRule.beginningRegex | String | 否 | 行首匹配规则。logType 为 multiline_log 时,该项需要配置。 |
| logType | String | 否 | 日志提取模式。默认单行全文。可选值:minimalist_log(单行全文)、multiline_log(多行全文)、json(json 格式)。 |
| metadata.formatType | String | 否 | 元数据格式。可选值:fluent-bit(fluent-bit 原生格式)、fluent-bit-string(字符串类型)。 |
十七、容器标准输出
采集 default 命名空间中的所有容器的标准输出:
"apiVersion": "cls.cloud.tencent.com/v1", "kind": "LogConfig", "metadata": { "name": "test" }, "spec": { "inputDetail": { "containerStdout": { "allContainers": true, "namespace": "default" }, "type": "container_stdout" }, … }
采集 production 命名空间中属于 ingress-gateway deployment 的 pod 中的所有容器的标准输出:
"apiVersion": "cls.cloud.tencent.com\\/v1", "kind": "LogConfig", "spec": { "inputDetail": { "containerStdout": { "allContainers": false, "workloads": [ { "kind": "deployment", "name": "ingress-gateway", "namespace": "production" } ] }, "type": "container_stdout" }, … }
采集 production 命名空间中 pod 标签中包含 k8s-app=nginx 的 pod 中的所有容器的标准输出:
{ "apiVersion": "cls.cloud.tencent.com\\/v1", "kind": "LogConfig", "spec": { "inputDetail": { "containerStdout": { "allContainers": false, "includeLabels": { "k8s-app": "nginx" }, "namespace": "production" }, "type": "container_stdout" }, … }}
采集 production 命名空间中属于 ingress-gateway 工作负载的 pod 中,名称为 ingress 的容器的标准输出:
{ "apiVersion": "cls.cloud.tencent.com\\/v1", "kind": "LogConfig", "spec": { "inputDetail": { "containerStdout": { "allContainers": false, "includeLabels": { "k8s-app": "nginx" }, "namespace": "production" }, "type": "container_stdout" }, … }}
十八、容器文件采集
采集 production 命名空间中属于 ingress-gateway deployment 的 pod 中的 nginx 容器中 /data/nginx/log/ 路径下名为 access.log 的文件:
"apiVersion": "cls.cloud.tencent.com/v1", "kind": "LogConfig", "metadata": { "name": "test" }, "spec": { "inputDetail": { "containerFile": { "container": "nginx", "filePaths": [ { "file": "access.log", "path": "/data/nginx/log" } ], "namespace": "production", "workload": { "kind": "deployment", "name": "ingress-gateway" } }, "type": "container_file" }, … }
采集 production 命名空间中 pod 标签包含 k8s-app=ingress-gateway 的 pod 中的 nginx 容器中 /data/nginx/log/ 路径下名为 access.log 的文件:
{ "apiVersion": "cls.cloud.tencent.com/v1", "kind": "LogConfig", "spec": { "inputDetail": { "containerFile": { "container": "nginx", "filePaths": [ { "file": "access.log", "path": "/data/nginx/log" } ], "includeLabels": { "k8s-app": "ingress-gateway" }, "namespace": "production" }, "type": "container_file" } }}
十九、主机文件采集
采集主机 /data/ 路径下所有 .log 文件:
"apiVersion": "cls.cloud.tencent.com/v1", "kind": "LogConfig", "metadata":{ "name":"test" } "spec": { "inputDetail": { "containerStdout": { "allContainers": false, "includeLabels": { "k8s-app": "nginx" }, "namespace": "production" }, "type": "container_stdout" }, … }
20、完整示例
投递到 CLS 的日志采集规则,采集 default 命名空间下 deployment/testlog1 的标准输出:
{ "apiVersion": "cls.cloud.tencent.com\\/v1", "kind": "LogConfig", "metadata": { "name": "123" }, "spec": { "clsDetail": { "extractRule": { "backtracking": "0", "isGBK": "false", "jsonStandard": "false", "unMatchUpload": "false" }, "indexs": [ { "indexName": "namespace" }, { "indexName": "pod_name" }, { "indexName": "container_name" } ], "logFormat": "default", "logType": "minimalist_log", "maxSplitPartitions": 0, "region": "ap-chengdu", "storageType": "", "topicId": "c26b66bd-617e-4923-bea0-test" }, "inputDetail": { "containerStdout": { "metadataContainer": [ "namespace", "pod_name", "pod_ip", "pod_uid", "container_id", "container_name", "image_name", "cluster_id" ], "nsLabelSelector": "", "workloads": [ { "kind": "deployment", "name": "testlog1", "namespace": "default" } ] }, "type": "container_stdout" } }}
投递到 Kafka 的采集规则,采集 default 命名空间下的所有 pod 标准输出:
{ "apiVersion": "cls.cloud.tencent.com\\/v1", "kind": "LogConfig", "metadata": { "name": "321" }, "spec": { "inputDetail": { "containerStdout": { "allContainers": true, "namespace": "default", "nsLabelSelector": "" }, "type": "container_stdout" }, "kafkaDetail": { "brokers": "127.0.0.1:9092", "extractRule": {}, "instanceId": "", "kafkaType": "SelfBuildKafka", "logType": "minimalist_log", "messageKey": { "value": "", "valueFrom": { "fieldRef": { "fieldPath": "" } } }, "metadata": {}, "timestampFormat": "double", "timestampKey": "", "topic": "test" } }}