Splunk 日志记录驱动程序
日志记录驱动程序splunk
将容器日志发送到
Splunk Enterprise 和 Splunk Cloud 中的HTTP 事件收集器。
用法
您可以将 Docker 日志记录配置为splunk
默认使用驱动程序或基于每个容器。
要将splunk
驱动程序用作默认日志记录驱动程序,请在配置文件中将键
log-driver
和log-opts
设置为适当的值daemon.json
,然后重新启动 Docker。例如:
{
"log-driver": "splunk",
"log-opts": {
"splunk-token": "",
"splunk-url": "",
...
}
}
daemon.json 文件位于/etc/docker/
Linux 主机或
C:\ProgramData\docker\config\daemon.json
Windows Server 上。有关使用配置 Docker 的更多信息daemon.json
,请参阅
daemon.json。
笔记
log-opts
配置文件中的配置选项daemon.json
必须以字符串形式提供。因此,布尔值和数值(例如splunk-gzip
或 的值splunk-gzip-level
)必须用引号 ( ) 括起来"
。
要将splunk
驱动程序用于特定容器,请使用命令行标志
--log-driver
和log-opt
with docker run
:
$ docker run --log-driver=splunk --log-opt splunk-token=VALUE --log-opt splunk-url=VALUE ...
Splunk 选项
以下属性允许您配置 Splunk 日志记录驱动程序。
splunk
要在 Docker 环境中 配置驱动程序,daemon.json
请使用键进行编辑"log-opts": {"NAME": "VALUE", ...}
。- 要
splunk
为单个容器配置驱动程序,请docker run
与标志 一起使用--log-opt NAME=VALUE ...
。
选项 | 必需的 | 描述 |
---|---|---|
splunk-token | 必需的 | Splunk HTTP 事件收集器令牌。 |
splunk-url | 必需的 | Splunk Enterprise、自助服务 Splunk Cloud 实例或 Splunk Cloud 托管集群的路径(包括 HTTP 事件收集器使用的端口和方案),采用以下格式之一:https://your_splunk_instance:8088 、https://input-prd-p-XXXXXXX.cloud.splunk.com:8088 或https://http-inputs-XXXXXXXX.splunkcloud.com 。 |
splunk-source | 选修的 | 事件源。 |
splunk-sourcetype | 选修的 | 事件源类型。 |
splunk-index | 选修的 | 事件索引。 |
splunk-capath | 选修的 | 根证书的路径。 |
splunk-caname | 选修的 | 用于验证服务器证书的名称;默认情况下splunk-url 使用 的主机名。 |
splunk-insecureskipverify | 选修的 | 忽略服务器证书验证。 |
splunk-format | 选修的 | 消息格式。可以是inline ,json 或者raw 。默认为inline . |
splunk-verify-connection | 选修的 | 启动时验证 Docker 是否可以连接到 Splunk 服务器。默认为 true。 |
splunk-gzip | 选修的 | 启用/禁用 gzip 压缩以将事件发送到 Splunk Enterprise 或 Splunk Cloud 实例。默认为 false。 |
splunk-gzip-level | 选修的 | 设置 gzip 的压缩级别。有效值为 -1(默认)、0(无压缩)、1(最佳速度)... 9(最佳压缩)。默认为 DefaultCompression。 |
tag | 选修的 | 指定消息的标签,它解释一些标记。默认值为{{.ID}} (容器 ID 的 12 个字符)。有关自定义日志标签格式的信息,请参阅
日志标签选项文档。 |
labels | 选修的 | 以逗号分隔的标签键列表,如果为容器指定了这些标签,则应将其包含在消息中。 |
labels-regex | 选修的 | 与 类似并兼容labels 。用于匹配与日志记录相关的标签的正则表达式。用于高级
日志标记选项。 |
env | 选修的 | 以逗号分隔的环境变量键列表,如果为容器指定了这些变量,则应将其包含在消息中。 |
env-regex | 选修的 | 与 类似并兼容env 。用于匹配与日志记录相关的环境变量的正则表达式。用于高级
日志标记选项。 |
label
如果和键之间存在冲突env
,则 的值env
优先。这两个选项都会向日志消息的属性添加附加字段。
以下是为 Splunk Enterprise 实例指定的日志记录选项的示例。该实例本地安装在运行 Docker 守护程序的同一台计算机上。
根证书和公用名的路径是使用 HTTPS 方案指定的。这用于验证。由SplunkServerDefaultCert
Splunk 证书自动生成。
$ docker run \
--log-driver=splunk \
--log-opt splunk-token=176FCEBF-4CF5-4EDF-91BC-703796522D20 \
--log-opt splunk-url=https://splunkhost:8088 \
--log-opt splunk-capath=/path/to/cert/cacert.pem \
--log-opt splunk-caname=SplunkServerDefaultCert \
--log-opt tag="{{.Name}}/{{.FullID}}" \
--log-opt labels=location \
--log-opt env=TEST \
--env "TEST=false" \
--label location=west \
your/application
Splunk Cloud 上托管的 Splunk实例splunk-url
的格式类似于https://http-inputs-XXXXXXXX.splunkcloud.com
且不包含端口说明符。
消息格式
日志记录驱动程序消息传递格式共有三种:(inline
默认)、json
和raw
。
默认格式是inline
每条日志消息作为字符串嵌入。例如:
{
"attrs": {
"env1": "val1",
"label1": "label1"
},
"tag": "MyImage/MyContainer",
"source": "stdout",
"line": "my message"
}
{
"attrs": {
"env1": "val1",
"label1": "label1"
},
"tag": "MyImage/MyContainer",
"source": "stdout",
"line": "{\"foo\": \"bar\"}"
}
要将消息格式化为json
对象,请设置--log-opt splunk-format=json
。驱动程序尝试将每一行解析为 JSON 对象并将其作为嵌入对象发送。如果无法解析消息,则会发送该消息inline
。例如:
{
"attrs": {
"env1": "val1",
"label1": "label1"
},
"tag": "MyImage/MyContainer",
"source": "stdout",
"line": "my message"
}
{
"attrs": {
"env1": "val1",
"label1": "label1"
},
"tag": "MyImage/MyContainer",
"source": "stdout",
"line": {
"foo": "bar"
}
}
要将消息格式raw
设置为--log-opt splunk-format=raw
.属性(环境变量和标签)和标签作为消息的前缀。例如:
MyImage/MyContainer env1=val1 label1=label1 my message
MyImage/MyContainer env1=val1 label1=label1 {"foo": "bar"}
高级选项
Splunk 日志记录驱动程序允许您通过设置 Docker 守护程序的环境变量来配置一些高级选项。
环境变量名称 | 默认值 | 描述 |
---|---|---|
SPLUNK_LOGGING_DRIVER_POST_MESSAGES_FREQUENCY | 5s | 等待更多消息批处理的时间。 |
SPLUNK_LOGGING_DRIVER_POST_MESSAGES_BATCH_SIZE | 1000 | 一批发送之前应累积的消息数。 |
SPLUNK_LOGGING_DRIVER_BUFFER_MAX | 10 * 1000 | 缓冲区中保留的用于重试的最大消息数。 |
SPLUNK_LOGGING_DRIVER_CHANNEL_SIZE | 4 * 1000 | 用于将消息发送到后台记录器工作线程(对消息进行批处理)的通道中可存在的最大挂起消息数。 |