{"id":46460,"date":"2017-02-24T13:07:45","date_gmt":"2017-02-24T07:37:45","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=46460"},"modified":"2017-02-24T13:07:45","modified_gmt":"2017-02-24T07:37:45","slug":"docker-logging-methods-that-fit-your-deployment-strategy","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/docker-logging-methods-that-fit-your-deployment-strategy\/","title":{"rendered":"Docker Logging Methods that Fit your Deployment Strategy"},"content":{"rendered":"<p>With the huge success of micro-services and container technologies, most of the enterprises are migrating their architecture to the container-based solution which is more reliable and flexible as compared to monolithic architecture.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-46492\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/fluentd_docker.png\" alt=\"fluentd_docker\" width=\"399\" height=\"140\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/fluentd_docker.png 399w, \/blog\/wp-ttn-blog\/uploads\/2017\/02\/fluentd_docker-300x105.png 300w\" sizes=\"(max-width: 399px) 100vw, 399px\" \/><\/p>\n<p><span style=\"font-weight: 400\">Moving to containerized solution like <a title=\"What is Docker and Why You Must Use It?\" href=\"http:\/\/www.tothenew.com\/blog\/what-is-docker-and-why-use-it\/\">Docker<\/a>, logging each container and its insights is a new challenge. Analyzing logs provides insight into stability, performance, and reliability of each container and its application which is deployed over them.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Due to dynamic and flexible nature of Docker, there are multiple approaches for storing and analyzing logs. There are a variety of solutions, each has its own drawbacks and benefits. So I am going to discuss some of the solutions which I have faced while logging containerized architecture.<\/span><\/p>\n<p><strong>Key considerations before moving to Docker logging<\/strong><\/p>\n<ol>\n<li style=\"font-weight: 400\"><b>Containers are transient<\/b><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">As running Docker containers are just a process. Containers start, stop, destroy and rebuilt on regular basis. So, storing the application data inside the container is not at all a good practice, as data will be lost when the process is completed or you might end up losing the data when it becomes dead. So it is highly recommended to export your data to persistent storage.<\/span><\/li>\n<li style=\"font-weight: 400\"><b>Containers are generally known by its container ID<\/b><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">As we learned in last point that containers are transient, so every time container start, it comes with unique container ID so maintaining logs of the\u00a0same type of containers with the different container ID can be slightly difficult, so it\u2019s better to provide some label or tag to a\u00a0running container.<\/span><\/li>\n<\/ol>\n<p><strong>Methods of Logging in Docker<\/strong><\/p>\n<p><strong>1) Log directly from your application:<\/strong><\/p>\n<p><span style=\"font-weight: 400\">In this method, the application handles its own log using some logging framework and send it to the remote destinations like <a href=\"http:\/\/www.tothenew.com\/blog\/elasticsearch-cluster-with-aws-spot-instances\/\">Elasticsearch<\/a>. For example, a Python application might use logging module for formatting and send logs to some remote destinations. This is the easiest way for developers to control logs but it creates extra load on the application process.<\/span><\/p>\n<p><span style=\"font-weight: 400\">However, this type of logging method is limited to application, means under the head of the container, so if we are storing any logs in the\u00a0container it will be lost if a\u00a0container was terminated or was shut down in any manner. To overcome this data loss, we will have to configure a persistent storage or add to add some log forwarder in the container itself which send logs to the\u00a0remote destination. Also, it will be very difficult to identify logs when we deploy same application in multiple containers to know which log belongs to which container.<\/span><\/p>\n<p><strong><strong>\u00a0<img decoding=\"async\" loading=\"lazy\" class=\"wp-image-46475 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/docker1a.jpg\" alt=\"docker1a\" width=\"395\" height=\"213\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/docker1a.jpg 395w, \/blog\/wp-ttn-blog\/uploads\/2017\/02\/docker1a-300x161.jpg 300w\" sizes=\"(max-width: 395px) 100vw, 395px\" \/><\/strong><\/strong><\/p>\n<p><b>2) Logging using data volumes<\/b><\/p>\n<p><span style=\"font-weight: 400\">In this method, we have to map a log directory of a container to a directory on the host machine.\u00a0<\/span><span style=\"font-weight: 400\">We can also share single volume across multiple containers for centralized logging.\u00a0<\/span>However, it is difficult to move containers to different hosts without any loss of log data.<\/p>\n<p>Follow this <a href=\"http:\/\/www.tothenew.com\/blog\/aws-cost-optimization-series-blog-3-leveraging-ec2-container-service-ecs\/\">link<\/a>, if you want to refer more about implementation on data volumes<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-46485 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/docker1b.jpg\" alt=\"docker1b\" width=\"606\" height=\"212\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/docker1b.jpg 606w, \/blog\/wp-ttn-blog\/uploads\/2017\/02\/docker1b-300x104.jpg 300w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><\/p>\n<p><b>3) Logging using Docker logging driver<\/b><\/p>\n<p><span style=\"font-weight: 400\">In this method, first, we have to setup any log forwarder in the host or in any dedicated docker container. After setting up this log forwarder, we can tell Docker container to send the log to the logging driver which we are <a href=\"http:\/\/www.tothenew.com\/blog\/collecting-tomcat-logs-using-fluentd-and-elasticsearch\/#sthash.zG9Mc32x.dpuf\">using as the forwarder such as <\/a>fluentd<a href=\"http:\/\/www.tothenew.com\/blog\/collecting-tomcat-logs-using-fluentd-and-elasticsearch\/#sthash.zG9Mc32x.dpuf\">, Logstash)<\/a>. We can also add tags in this method, which segregate containers from each other and maintain consistency if the container is terminated.<\/span><\/p>\n<p><span style=\"font-weight: 400\">However, in this method, Docker container only sends its stdout and stderr logs to logging driver. So this method will be best if your application is running as the main process inside the docker container which is sending its log to Docker logs.<\/span><\/p>\n<p>[js]<\/p>\n<p>docker run -dit &#8211;log-driver=fluentd &#8211;log-opt fluentd-address=localhost:24224 &#8211;log-opt tag=&quot;docker.app1&quot;\u00a0app1 [\/js]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-46491 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/docker1c.jpg\" alt=\"docker1c\" width=\"606\" height=\"239\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/docker1c.jpg 606w, \/blog\/wp-ttn-blog\/uploads\/2017\/02\/docker1c-300x118.jpg 300w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><\/p>\n<p><span style=\"font-weight: 400\">As discussed, there is no single approach to logging with Docker. It depends upon the use-cases we have, like in orchestration tool <a title=\"How to Install Kubernetes on CentOS?\" href=\"http:\/\/www.tothenew.com\/blog\/how-to-setup-kubernetes-master-ha-on-centos\/\">Kubernetes<\/a>, we can not implement logging driver as there is no support for that, so either we move to data volumes or installing forwarder in each container. So every use cases have its own solution. Still, there\u2019s is no clearly defined best solution but that doesn\u2019t mean that you should stop logging and managing the Docker logs.<\/span><\/p>\n<p><span style=\"color: #333333;font-family: Consolas, 'Liberation Mono', Courier, monospace;font-size: 13px;background-color: #f5f5f5\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the huge success of micro-services and container technologies, most of the enterprises are migrating their architecture to the container-based solution which is more reliable and flexible as compared to monolithic architecture. Moving to containerized solution like Docker, logging each container and its insights is a new challenge. Analyzing logs provides insight into stability, performance, [&hellip;]<\/p>\n","protected":false},"author":917,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1174,2348,1],"tags":[2651,3389,4445,4446,3965,288,1782,3390],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/46460"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/917"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=46460"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/46460\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=46460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=46460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=46460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}