In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to deal with the log of Docker container". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to deal with the log of Docker container" can help you solve the problem.
Docker has many log plug-ins. Json-file is used by default. Only when json-file is used, sudo docker logs-f can be displayed. Enter the following command to view the docker log plug-in:
$sudo docker info | grep logging
First of all, when the container is running, docker will create a file related to the container on the host, and then transfer the logs generated by the container to this file. The docker logs-f command finds the contents of the file and displays it on the terminal.
We all know that docker logs-f will output all the corresponding service logs to the terminal. No matter which node the service is deployed on, I now ask a question: will the container file corresponding to each node save the full log backup of the service, or only the logs generated by the corresponding container of the node service?
Because this problem involves that if each node uses filebeat to monitor the container log file of the host, then if the container log of each node is a complete backup, the log will be repeated, and if only the container log on that node is saved, it will not.
The answer is to keep only the container logs on that node, and the docker logs-f command simply takes a layer of protocol over the overlay network model to aggregate the same container logs on other nodes.
Json-file of docker is used by default. Configure daemon first:
$sudo dockerd\-log-driver=json-file\-log-opt labels=servicename
You need to add the following parameters to start the container:
$sudo docker service update-label servicename=test
Or mark it directly in docker-compose.yml:
Version: "3" services: go-gin-demo: image: chenghuizhang/go-gin-demo:v3 ports:-8081 chenghuizhang/go-gin-demo:v3 ports:-overlay deploy: mode: replicated replicas: 3 labels: servicename: go-gin-demoxxxxxxx logging: options: labels: "servicename" networks: overlay:
Install filebeat on each node, and the filebeat.yml configuration is as follows:
Filebeat.prospectors:- type: log paths: # container log directory-/ var/lib/docker/containers/*/*.log # because the log driver used by docker is json-file, so the collected log format is json format. After setting it to true, filebeat will json_decode the log json.keys_under_root: true tail_files: trueoutput.logstash: hosts: ["172.17.10.114docker 5044"]
Configure the index in logstash.conf:
Output {elasticsearch {action = > "index" hosts = > ["172.17.10.114action 9200"] # get log label index = > "% {attrs.servicename} -% {+ log}"}
The dockerfile file needs to print the output log of the project to stdout and stderr, otherwise the json-file log driver will not collect the output log in the container, and sudo docker logs-f will not be able to display the container log on the terminal. You need to add the following command in dockerfile:
Run ln-sf / dev/stdout / xx/xx.log\ # info & & ln-sf / dev/stderr / xx/xx.log # error
Or configure the output console in the log4j of the project:
If the log needs to record the container id name and image name, you can add the following parameters when running the container:
-- log-opt tag= "/"
Finally, the json-file log plug-in generates logs printed from the container to the console to the local / var/lib/docker/containers/*/ directory in the following format:
{"log": "[gin-debug] [warning] now gin requires go 1.6 or later and go 1.7 will be required soon.", "stream": "stderr", "attrs": {"tag": "chenghuizhang/go-gin-demo:v3@sha256:e6c0419d64e5eda510056a38cfb803750e4ac2f0f4862d153f7c4501f576798b/mygo.2.jhqptjugfti2t4emf55sehamo/647eaa4b3913", "servicename": "test"}, "time": "2019-01-29t10:08:59.780161908z"}
Format the log in logstash:
Filter {grok {patterns_dir = > "/ etc/logstash/conf.d/patterns" match = > {"message" = > "% {timestamp_iso8601:time}% {servicename:attr.servicename}% {docker_tag:attr.tag}"} about "how to handle Docker container logs" ends here. Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.