在同一主机上运行的多个容器的日志解决方案 [英] Logging solution for multiple containers running on same host

查看:239
本文介绍了在同一主机上运行的多个容器的日志解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我们将所有应用程序日志从多个容器重定向到stdout,并通过主机中的rsyslog收集/ var / log / message到ELK堆栈。

Currently we are redirecting all application logs to stdout from multiple containers and collect /var/log/message via rsyslog in host to ELK stack.

所有docker容器日志显示为docker / xxxxxxxx,我们无法确定哪个应用程序是此日志,无论如何,我们可以轻松地将应用程序与多个容器日志区分开,从docker stdout ?

All docker container logs shows as docker/xxxxxxxx, we can't tell which application is this log for, anyway we can easily differentiate applications from multiple container logs from docker stdout?

推荐答案

(操作系统X的说明,但应在Linux中工作)

(Instructions for OS X but should work in Linux)

似乎没有办法使用docker命令执行此操作,但是在bash中,您可以同时运行多个命令,并且使用 sed 可以前缀与您的容器名称。

There doesn't appear to be a way to do this with a docker command, however in bash you can run multiple commands at the same time, and with sed you can prefix with your container name.

docker logs -f --tail=30 container1 | sed -e 's/^/[-- container1 --]/' &
docker logs -f --tail=30 container2 | sed -e 's/^/[-- container2 --]/' &

同时您将看到两个容器的输出。

And you will see output from both containers at the same time.

container1 :: logging line
container1 :: logging line
container2 :: logging line
container2 :: logging line
container1 :: logging line
container1 :: logging line
container2 :: logging line
container2 :: logging line






一次拖曳所有容器:


To tail all your containers at once:

#!/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 为每个容器。

And to stop them run fg and then press ctrl+c for each container.

更新:感谢@ Flo-Woo Ubuntu 16.04支持

Update: Thanks to @Flo-Woo for Ubuntu 16.04 support

这篇关于在同一主机上运行的多个容器的日志解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆