目前,我们正在将所有应用程序日志从多个容器重定向到 stdout,并通过主机中的 rsyslog 收集 /var/log/message 到 ELK 堆栈。
所有 docker 容器日志都显示为 docker/xxxxxxxx,我们无法判断该日志是哪个应用程序,无论如何我们可以轻松地区分来自 docker stdout 的多个容器日志中的应用程序?
(适用于 OS X 的说明,但应该适用于 Linux)
似乎没有办法使用 docker 命令来执行此操作,但是在 bash 中,您可以同时运行多个命令,并且使用sed
您可以使用容器名称作为前缀。
docker logs -f --tail=30 container1 | sed -e 's/^/[-- containerA1 --]/' &
docker logs -f --tail=30 container2 | sed -e 's/^/[-- containerM2 --]/' &
您将同时看到两个容器的输出。
[-- containerA1 --] :: logging line
[-- containerA1 --] :: logging line
[-- containerM2 --] :: logging line
[-- containerM2 --] :: logging line
[-- containerA1 --] :: logging line
[-- containerA1 --] :: logging line
[-- containerM2 --] :: logging line
[-- containerM2 --] :: logging line
要立即尾随所有容器:
#!/bin/bash
names=$(docker ps --format "{{.Names}}")
echo "tailing $names"
while read -r name
do
# eval to show container name in jobs list
eval "docker logs -f --tail=5 \"$name\" | sed -e \"s/^/[-- $name --] /\" &"
# For Ubuntu 16.04
#eval "docker logs -f --tail=5 \"$name\" |& sed -e \"s/^/[-- $name --] /\" &"
done <<< "$names"
function _exit {
echo
echo "Stopping tails $(jobs -p | tr '\n' ' ')"
echo "..."
# Using `sh -c` so that if some have exited, that error will
# not prevent further tails from being killed.
jobs -p | tr '\n' ' ' | xargs -I % sh -c "kill % || true"
echo "Done"
}
# On ctrl+c, kill all tails started by this script.
trap _exit EXIT
# For Ubuntu 16.04
#trap _exit INT
# Don't exit this script until ctrl+c or all tails exit.
wait
并阻止他们逃跑fg
然后按ctrl+c
对于每个容器。
更新:感谢 @Flo-Woo 对 Ubuntu 16.04 的支持
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)