docker logs
docker默认配置下日志会发送到容器的标准输出设备和标准错误设备
例子:
docker run -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
[Mon Jun 22 05:06:05.111332 2020] [mpm_event:notice] [pid 1:tid 140520454775936] AH00489: Apache/2.4.43 (Unix) configured -- resuming normal operations
[Mon Jun 22 05:06:05.122645 2020] [core:notice] [pid 1:tid 140520454775936] AH00094: Command line: 'httpd -D FOREGROUND'
##此时访问这个容器就会有实时的变化
192.168.1.1 - - [22/Jun/2020:05:07:17 +0000] "GET /favicon.ico HTTP/1.1" 404 196
运行容器时没有加入 -d选项,日志会直接打印在当前的终端界面上,如果加入了-d选项容器后台运行就看不到日志了
方法1:attach到容器中
docker run -d -p 80:80 httpd
4fd9b5c22f231ebf24906ac1b6ffa0c81596621cb5b7b72ec3905d63bc92b65e
docker attach 4fd9
再打开一个终端
curl localhost
查看attach的终端
172.17.0.1 - - [22/Jun/2020:05:10:30 +0000] "GET / HTTP/1.1" 200 45
方法2:使用docker logs命令
docker logs -f 4fd9
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
[Mon Jun 22 05:09:39.701168 2020] [mpm_event:notice] [pid 1:tid 140174998008960] AH00489: Apache/2.4.43 (Unix) configured -- resuming normal operations
[Mon Jun 22 05:09:39.701293 2020] [core:notice] [pid 1:tid 140174998008960] AH00094: Command line: 'httpd -D FOREGROUND'
172.17.0.1 - - [22/Jun/2020:05:10:30 +0000] "GET / HTTP/1.1" 200 45
[Mon Jun 22 05:11:13.956207 2020] [mpm_event:notice] [pid 1:tid 140174998008960] AH00491: caught SIGTERM, shutting down
##logs命令可以打开完整日志,-f的作用为继续打印出新的日志
docker提供了多种日志机制帮助用户从正在运行的容器查询日志信息,这些机制被称为logging driver
docker info | grep "Logging Driver"
Logging Driver: json-file
容器在启动时没有指明的情况下默认使用这个Driver,json-file会将容器的日志保存在json文件中,在容器的目录中
docker container inspect 4fd9 | grep "LogPath"
"LogPath": "/var/lib/docker/containers/4fd9b5c22f231ebf24906ac1b6ffa0c81596621cb5b7b72ec3905d63bc92b65e/4fd9b5c22f231ebf24906ac1b6ffa0c81596621cb5b7b72ec3905d63bc92b65e-json.log"
docker支持多种日志驱动
| 驱动程序 | 描述 |
|---|---|
| none | 容器没有日志可用,docker logs 什么都不返回 |
| json-file | 日志格式化为 JSON。这是 Docker 默认的日志驱动程序。 |
| syslog | 将日志消息写入 syslog 工具。syslog 守护程序必须在主机上运行。 |
| journald | 将日志消息写入 journald。journald 守护程序必须在主机上运行。 |
| gelf | 将日志消息写入 Graylog Extended Log Format (GELF) 终端,例如 Graylog 或 Logstash。 |
| fluentd | 将日志消息写入 fluentd(forward input)。fluentd 守护程序必须在主机上运行。 |
| awslogs | 将日志消息写入 Amazon CloudWatch Logs。 |
| splunk | Writes log messages to splunk using the HTTP Event Collector. |
| etwlogs | 将日志消息写为 Windows 的 Event Tracing 事件。仅在Windows平台上可用。 |
| gcplogs | 将日志消息写入 Google Cloud Platform (GCP) Logging。 |
| logentries | 将日志消息写入 Rapid7 Logentries。 |
容器启动时可以通过–log-driver指定驱动,修改默认的驱动则需要修改docker启动脚本
vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --log-driver=none
systemctl daemon-reload
systemctl restart docker
docker run -d -p 80:80 httpd
63c08074183b19f65ddac0bc12972d9170f1c9ea97313df385554640d8491c38
docker container inspect 63c0 | grep "LogPath"
"LogPath": "",
docker elk的安装
ELK”是三个开源项目的首字母缩写,这三个项目分别是:Elasticsearch、Logstash 和 Kibana。Elasticsearch 是一个搜索和分析引擎。Logstash 是服务器端数据处理管道,能够同时从多个来源采集数据,转换数据,然后将数据发送到诸如 Elasticsearch 等“存储库”中。Kibana 则可以让用户在 Elasticsearch 中使用图形和图表对数据进行可视化。
docker elk使用文档:https://elk-docker.readthedocs.io/
ELK帮助手册:https://hub.docker.com/r/sebp/elk/
docker主机分配4G以上内存
##下载镜像,时间较长。。
docker pull sebp/elk
修改vm.max_map_count必须262144以上
cat /etc/sysctl.conf
vm.max_map_count = 262144
sysctl -p
##启动容器
docker run -p 5601:5601 -p 9200:9200 -p 5044:5044 -it -e ES_HEAP_SIZE="2g" -e LS_HEAP_SIZE="1g" --name elk sebp/elk
##访问IP:5601

2782




被折叠的 条评论
为什么被折叠?



