Google Cloud 日志驱动
Google Cloud Logging 驱动程序将容器日志发送到 Google Cloud Logging 日志记录。
用法
要将gcplogs驱动程序设置为默认的日志记录驱动程序,请在daemon.json文件中将log-driver和log-opt键设置为适当的值,该文件位于Linux主机上的/etc/docker/或Windows Server上的C:\ProgramData\docker\config\daemon.json。有关使用daemon.json配置Docker的更多信息,请参阅daemon.json。
以下示例将日志驱动程序设置为gcplogs并设置gcp-meta-name选项。
{
"log-driver": "gcplogs",
"log-opts": {
"gcp-meta-name": "example-instance-12345"
}
}重启Docker以使更改生效。
您可以通过使用--log-driver选项为特定容器设置日志驱动程序:
$ docker run --log-driver=gcplogs ...
如果Docker检测到它在Google Cloud项目中运行,它会从实例元数据服务中获取配置。否则,用户必须使用--gcp-project日志选项指定要记录的项目,并且Docker会尝试从Google应用程序默认凭据中获取凭据。--gcp-project标志优先于从元数据服务器发现的信息,因此可以使用--gcp-project覆盖在Google Cloud项目中运行的Docker守护程序以记录到不同的项目。
Docker 从 Google Cloud 元数据服务器获取区域、实例名称和实例 ID 的值。如果元数据服务器不可用,可以通过选项提供这些值。它们不会覆盖元数据服务器中的值。
gcplogs 选项
您可以使用--log-opt NAME=VALUE标志来指定这些额外的Google Cloud Logging驱动程序选项:
| Option | Required | Description |
|---|---|---|
gcp-project | optional | Which Google Cloud project to log to. Defaults to discovering this value from the Google Cloud metadata server. |
gcp-log-cmd | optional | Whether to log the command that the container was started with. Defaults to false. |
labels | optional | Comma-separated list of keys of labels, which should be included in message, if these labels are specified for the container. |
labels-regex | optional | Similar to and compatible with labels. A regular expression to match logging-related labels. Used for advanced
日志标签选项. |
env | optional | Comma-separated list of keys of environment variables, which should be included in message, if these variables are specified for the container. |
env-regex | optional | Similar to and compatible with env. A regular expression to match logging-related environment variables. Used for advanced
日志标签选项. |
gcp-meta-zone | optional | Zone name for the instance. |
gcp-meta-name | optional | Instance name. |
gcp-meta-id | optional | Instance ID. |
如果label和env键之间存在冲突,env的值将优先。这两个选项都会向日志消息的属性添加额外的字段。
以下是记录到默认日志目的地所需的日志记录选项示例,该目的地通过查询Google Cloud元数据服务器发现。
$ docker run \
--log-driver=gcplogs \
--log-opt labels=location \
--log-opt env=TEST \
--log-opt gcp-log-cmd=true \
--env "TEST=false" \
--label location=west \
your/application
此配置还指示驱动程序在有效负载中包含标签
location,环境变量 ENV,以及用于启动容器的命令。
以下示例展示了在Google Cloud外部运行的日志记录选项。必须为守护进程设置GOOGLE_APPLICATION_CREDENTIALS环境变量,例如通过systemd设置:
[Service]
Environment="GOOGLE_APPLICATION_CREDENTIALS=uQWVCPkMTI34bpssr1HI"$ docker run \
--log-driver=gcplogs \
--log-opt gcp-project=test-project \
--log-opt gcp-meta-zone=west1 \
--log-opt gcp-meta-name=`hostname` \
your/application