087、日志管理之 Docker logs (2019-05-09)

 
高效的监控和日志管理对保持生产系统只需稳定的运行以及排查问题至关重要。
 
在微服务架构中,由于容器的数量众多以及快速变化的特性,使得记录日志和监控变的重要起来。考虑到容器短暂和不固定的生命周期,我们需要debug问题时有些容器可能已经不在了。因此,一套集中式的日志管理系统是生产环境中不可缺少的组成部分。
 
本章我们将学习监控容器的各种可用技术和方案,首先会介绍 Docker 自带的 logs 子命令,然后讨论 Docker 的 logging driver ,接下来通过实践学习几个已经广泛应用的日志管理方案:ELK、Fluentd和Graylog。
 
 
Dokcer logs
 
我们先来看一下默认配置下的docker日志功能。
 
对于一个运行的容器,Docker 会将日志发送到容器的标准输出设备(STDOUT)和标准错误设备(STDERR),STDOUT和STDERR实际上就是容器的控制台终端。
 
比如我们运行一个httpd容器,查看日志有三种方式:
 
1、在当前终端运行容器,不放到后台运行,日志直接打印在屏幕上
 
[root@ubuntu ~]# docker run --name web01 -p 80:80 httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Thu May 09 01:04:43.943040 2019] [mpm_event:notice] [pid 1:tid 139694789120064] AH00489: Apache/2.4.39 (Unix) configured -- resuming normal operations
[Thu May 09 01:04:43.943184 2019] [core:notice] [pid 1:tid 139694789120064] AH00094: Command line: 'httpd -D FOREGROUND'
10.12.28.253 - - [09/May/2019:01:04:50 +0000] "GET / HTTP/1.1" 200 45
10.12.28.253 - - [09/May/2019:01:05:05 +0000] "GET /httpd_access_testweb01 HTTP/1.1" 404 220
 
2、容器放到后台运行,然后使用 docker attach 进入容器查看,但是退出不方便,容器kill掉容器(理论上ctrl+p 、ctrl+q 可以退出,实际上很多时候不好用)
 
[root@ubuntu ~]# docker run --name web02 -d -p 81:80 httpd
6c86c63d729e776efe49b8a305f0dc8ed8e7261c99c1a3c6c96b288a2fc9e8de
[root@ubuntu ~]# docker attach web02
10.12.28.253 - - [09/May/2019:01:06:42 +0000] "GET /httpd_access_testweb02 HTTP/1.1" 404 220
^P^P^P^P^P^P^Pp^P^P^P^P^P^P^P^P^P^P^P^P^P^P^P^P^P
 
3、容易放到后台运行,使用今天的主角 docker logs 查看
 
[root@ubuntu ~]# docker run --name web03 -d -p 82:80 httpd
d777fe241f27b541a776e4e0eca4e86754e61d7fe324bb6720e46711707a5a30
[root@ubuntu ~]# docker logs web03
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.4. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.4. Set the 'ServerName' directive globally to suppress this message
[Thu May 09 01:07:37.761772 2019] [mpm_event:notice] [pid 1:tid 140651667042368] AH00489: Apache/2.4.39 (Unix) configured -- resuming normal operations
[Thu May 09 01:07:37.761911 2019] [core:notice] [pid 1:tid 140651667042368] AH00094: Command line: 'httpd -D FOREGROUND'
10.12.28.253 - - [09/May/2019:01:07:44 +0000] "GET /httpd_access_testweb03 HTTP/1.1" 404 220
[root@ubuntu ~]# docker logs -f web03
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.4. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.4. Set the 'ServerName' directive globally to suppress this message
[Thu May 09 01:07:37.761772 2019] [mpm_event:notice] [pid 1:tid 140651667042368] AH00489: Apache/2.4.39 (Unix) configured -- resuming normal operations
[Thu May 09 01:07:37.761911 2019] [core:notice] [pid 1:tid 140651667042368] AH00094: Command line: 'httpd -D FOREGROUND'
10.12.28.253 - - [09/May/2019:01:07:44 +0000] "GET /httpd_access_testweb03 HTTP/1.1" 404 220
10.12.28.253 - - [09/May/2019:01:08:09 +0000] "GET /httpd_access_testweb03 HTTP/1.1" 404 220
10.12.28.253 - - [09/May/2019:01:08:09 +0000] "GET /httpd_access_testweb03 HTTP/1.1" 404 220
10.12.28.253 - - [09/May/2019:01:08:11 +0000] "GET /httpd_access_testweb03_01 HTTP/1.1" 404 223
10.12.28.253 - - [09/May/2019:01:08:12 +0000] "GET /httpd_access_testweb03_02 HTTP/1.1" 404 223
10.12.28.253 - - [09/May/2019:01:08:13 +0000] "GET /httpd_access_testweb03_03 HTTP/1.1" 404 223
 
 

猜你喜欢

转载自www.cnblogs.com/www1707/p/10836546.html