Log is a very important part of the system, through the log can be found in time the problems in the system, but also to provide clues to repair problems. Docker provides a variety of plug-in ways to manage logs, and this article records the process of using MongoDB to store Docker logs.Data FlowCreated with Raphaël 2.1.2 Start Docker container fluentd MongoDB End
The date produced by the Docker is sent to the FLUENTD, and then FLUENTD is transferred to MongoDB. Configure Fluentd
Prerequisite: Operating system is CentOS7, installed MongoDB3.4 1. Install Fluentd
The CentOS 7 software library does not contain FLUENTD installation packages, so you need to install them manually. The software td-agent used in CentOS.
# curl-l Https://toolbelt.treasuredata.com/sh/install-redhat-td-agent3.sh | Sh
The above command creates a Td-agent installation library Td.repo in the/etct/yum.repo.d/directory, and then automatically installs Td-agent. Set to start automatically when the installation is complete.
# Systemctl start td-agent
# Systemctl Enable Td-agent
If a problem occurs during startup, you can view the log file located under/var/log/td-agent/td-agent.log 2. Configure to dump Docker logs to MongoDB
Plug-in support is required for the dump to MongoDB, but Td-agent is not installed by default and needs to be installed.
# yum-y Install td-agent-gemt
# Td-agent-gem Install Fluent-plugin-mongo
Edit/etc/td-agent/td-agent.conf, add
# # Match tag=docker.* and dump to MongoDB
# # When the Docker starts the container, the tag of the log should conform to the docker.* form
<match docker.*>
@ Type MONGO
# database address
host 127.0.0.1
# database ports Port
22017
# Store log database and dataset name db
Docker
Collection Log
</match>
Restart Td-agent. Start Container
The command to start the container is changed to:
$ docker Run--mount Type=bind,source=/home/demo/app,destination=/app--log-driver=fluentd--log-opt tag= "Docker.log" Python3:latest python3/app/test.py
After you have finished running, you can view the log in MongoDB.
Reference Documentation:
Https://docs.treasuredata.com/articles/td-agent
https://docs.docker.com/engine/admin/logging/fluentd/
Https://docs.fluentd.org/v1.0/articles/out_mongo