我的EC2容器由亚马逊网络服务上的ECS ( Docker容器服务)管理。ECS集群决定容器将在哪个EC2实例上运行。
我看到有一种方法可以配置Splunk驱动程序将Docker容器Id和/或容器名称与每个日志条目一起发送到Splunk,但我找不到发送EC2实例Id的方法。对于这一点似乎没有一个变量(参见https://docs.docker.com/engine/admin/logging/log_tags)。
发布于 2016-08-30 16:46:34
此similar question for fluentd介绍了通过日志选项设置标签的详细信息。
在ECS上,您可以将自定义的“日志选项”添加到任务或服务的“日志配置”中:
密钥= tag
值= {{ index .ContainerLabels "com.amazonaws.ecs.container-name" }}
实例ID
看起来ECS不会将EC2实例ID发送给Docker和it's really hard in ECS to populate anything from the hosts environment。
主机名可以通过{{ .Hostname }}在Docker中作为--log-opt tag使用,因此将主机名设置为实例ID并使用它可能是最简单的方法。
实例ID可通过http://instance-data/latest/meta-data/instance-id获取
您可以使用user-data脚本在构建时设置主机名,也可以使用启动脚本在每次实例引导时设置主机名。
#!/bin/bash
set -ue
INSTANCE_ID=$(curl -s --retry 5 --retry-delay 1 http://instance-data/latest/meta-data/instance-id)
# Set the network hostname
sed -ri "s/^HOSTNAME=.*/HOSTNAME=$INSTANCE_ID/" /etc/sysconfig/network
# Set the local host lookup
if ! grep -Eq "127.0.0.1.*$INSTANCE_ID" /etc/hosts; then
sed -ri "s/^(127.0.0.1.*)/\1 $INSTANCE_ID/" /etc/hosts
fi如果您在每个EC2实例上运行一个local splunk forwarder并在其中添加标记或字段,我认为您可以执行类似的操作。
https://stackoverflow.com/questions/39219486
复制相似问题