目前,我们将所有应用程序日志从多个容器重定向到stdout,并通过主机中的rsyslog将/ var/log/message收集到ELK堆栈.
所有docker容器日志显示为docker/xxxxxxxx,我们无法分辨这个日志是哪个应用程序,无论如何我们可以轻松地将应用程序与docker stdout中的多个容器日志区分开来?
Nat*_*ate 20
(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 --]/' &
Run Code Online (Sandbox Code Playgroud)
并且您将同时看到两个容器的输出.
[-- containerA1 --] :: logging line
[-- containerA1 --] :: logging line
[-- containerM2 --] :: logging line
[-- containerM2 --] :: logging line
[-- containerA1 --] :: logging line
[-- containerA1 --] :: logging line
[-- containerM2 --] :: logging line
[-- containerM2 --] :: logging line
Run Code Online (Sandbox Code Playgroud)
要立即拖尾所有容器:
#!/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
Run Code Online (Sandbox Code Playgroud)
并阻止它们运行fg,然后按下ctrl+c每个容器.
更新:感谢@ Flo-Woo支持Ubuntu 16.04
这是一个跟踪所有 docker 容器的脚本。
基于@nate的回答,但要短一些。在 CentOS 上测试。
#!/bin/bash
function _exit {
kill $(jobs -p)
}
trap _exit EXIT
for name in $(docker ps --format "{{.Names}}"); do
eval "docker logs -f --tail=5 \"$name\" | sed -e \"s/^/[-- $name --] /\" &";
done
wait
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7513 次 |
| 最近记录: |