ÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄÈÕÖ¾ÆÊÎöºÍÒì³£ÅŲé
Ëæ×ÅÈÝÆ÷ÊÖÒÕµÄÆÕ¼°ºÍÓ¦Óã¬DockerÒѾ³ÉΪÐí¶àÆóÒµÖв»¿É»òȱµÄÒ»²¿·Ö¡£ÔÚʹÓÃDocker¾ÙÐпª·¢ºÍ°²ÅÅÖУ¬ÈÝÆ÷µÄÈÕÖ¾ÆÊÎöºÍÒì³£ÅŲéÊǺÜÊÇÖ÷ÒªµÄÒ»»·¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄÈÕÖ¾ÆÊÎöºÍÒì³£ÅŲ飬ÒÔ¼°ÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢DockerÈÕÖ¾¼ò½é
DockerÈÕÖ¾Ö¸µÄÊÇÈÝÆ÷µÄÊä³ö£¬°üÀ¨ÈÝÆ÷µÄ±ê×¼Êä³öºÍ¹ýʧÊä³ö£¬ÔÚÈÝÆ÷ÄÚ²¿£¬¿ÉÒÔͨ¹ýstdoutºÍstderr½«Êä³öдÈëµ½¿ØÖÆ̨¡£Docker½«ÕâЩÊä³ö²¶»ñ£¬ÉúÑĵ½ËÞÖ÷»úÉϵÄÒ»¸öÎļþÖС£
ÔÚDockerÖУ¬Ã¿¸öÈÝÆ÷¶¼ÓÐ×Ô¼ºµÄÈÕÖ¾£¬¿ÉÒÔʹÓÃDockerÏÂÁîÉó²éÈÝÆ÷µÄÈÕÖ¾£º
docker logs [CONTAINER ID]
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÆäÖÐCONTAINER IDÖ¸µÄÊÇÈÝÆ÷µÄID¡£
¶þ¡¢Ê¹ÓÃDocker¾ÙÐÐÈÝÆ÷ÈÕÖ¾ÆÊÎö
ʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄÈÕÖ¾ÆÊÎö£¬¿ÉÒÔ×ÊÖúÎÒÃÇÏàʶÈÝÆ÷µÄÔËÐÐÇéÐΣ¬ÕÒµ½±£´æµÄÎÊÌâ²¢¾ÙÐÐÐÞ¸´¡£ÏÂÃæÊÇʹÓÃDocker¾ÙÐÐÈÝÆ÷ÈÕÖ¾ÆÊÎöµÄÏêϸ°ì·¨£º
1. Éó²éÈÝÆ÷ÈÕÖ¾
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÉó²éÈÝÆ÷µÄÈÕÖ¾£¬ÅжÏÈÝÆ÷ÊÇ·ñ±£´æÒì³£¡£Ê¹ÓÃDockerÏÂÁîÉó²éÈÝÆ÷µÄÈÕÖ¾£º
docker logs [CONTAINER ID]
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÈôÊÇÏëÉó²éÈÝÆ÷×î½üµÄ10ÌõÈÕÖ¾£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
docker logs --tail 10 [CONTAINER ID]
µÇ¼ºó¸´ÖÆ
2. ƾ֤Ҫº¦×Ö¹ýÂËÈÕÖ¾
ÔÚÉó²éÈÝÆ÷ÈÕ־ʱ£¬ÎÒÃÇ¿ÉÒÔƾ֤Ҫº¦×ÖÀ´¹ýÂËÈÕÖ¾£¬ÒÔ±ãÔ½·¢×¼È·µØÏàʶÈÝÆ÷µÄÔËÐÐÇéÐΡ£Ê¹ÓÃÈçÏÂÏÂÁ¿ÉÒÔƾ֤Ҫº¦×Öɸѡ³öÈÕÖ¾£º
docker logs [CONTAINER ID] | grep [KEYWORD]
µÇ¼ºó¸´ÖÆ
ÀýÈ磬²éÕÒ°üÀ¨¡°error¡±µÄÈÕÖ¾£º
docker logs [CONTAINER ID] | grep error
µÇ¼ºó¸´ÖÆ
3. Êä³öÈÝÆ÷ÈÕÖ¾µ½Îļþ
Docker»á½«ÈÝÆ÷µÄÈÕÖ¾ÉúÑĵ½ËÞÖ÷»úÉϵÄÒ»¸öÎļþÖС£ÎÒÃÇ¿ÉÒÔ½«ÈÝÆ÷µÄÈÕÖ¾Êä³öµ½ÎļþÖУ¬ÒÔ±ãºóÐøÆÊÎö¡£Ê¹ÓÃÈçÏÂÏÂÁ¿ÉÒÔ½«ÈÝÆ÷µÄÈÕÖ¾Êä³öµ½Ò»¸öÎļþÖУº
docker logs [CONTAINER ID] > [LOG FILE]
µÇ¼ºó¸´ÖÆ
ÀýÈ磬½«ÈÝÆ÷µÄÈÕÖ¾Êä³öµ½Îļþ¡°container.log¡±£º
docker logs [CONTAINER ID] > container.log
µÇ¼ºó¸´ÖÆ
4. ʹÓõÚÈý·½¹¤¾ß¾ÙÐÐÈÕÖ¾ÆÊÎö
ÈôÊÇÏëÔ½·¢ÉîÈëµØ¾ÙÐÐÈÝÆ÷ÈÕÖ¾ÆÊÎö£¬¿ÉÒÔʹÓÃһЩµÚÈý·½¹¤¾ß¡£ÀýÈ磬ʹÓÃELK£¨Elasticsearch + Logstash + Kibana£©¾ÙÐÐÈÕÖ¾ÆÊÎö¡£ÏÂÃæ¼òÆÓÏÈÈÝÒ»ÏÂÔõÑùʹÓÃELK¾ÙÐÐÈÝÆ÷ÈÕÖ¾ÆÊÎö¡£
4.1 ×°ÖÃELK
ÎÒÃÇ¿ÉÒÔʹÓÃDockerÀ´×°ÖÃELK£¬Ïêϸ°ì·¨ÈçÏ£º
docker pull sebp/elk docker run -p 5601:5601 -p 9200:9200 -p 5044:5044 -it --name elk sebp/elk
µÇ¼ºó¸´ÖÆ
ÉÏÊöÏÂÁîÖУ¬ÀÈ¡ÁËsebp/elk¾µÏñ£¬²¢Æô¶¯ÁËÒ»¸öÃûΪ¡°elk¡±µÄÈÝÆ÷¡£ÎÒÃÇ¿ÉÒÔͨ¹ýµØµãhttp://localhost:5601/À´»á¼ûKibanaÃæ°å¡£
4.2 ÉèÖÃLogstash
ÔÚLogstashÖУ¬ÐèÒªÉèÖÃÊäÈë¡¢¹ýÂËÆ÷ºÍÊä³ö¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄLogstashÉèÖÃÎļþ£¬ÓÃÓÚ½«ÈÝÆ÷µÄÈÕÖ¾ÊäÈëµ½ElasticsearchÖУº
input { file { type => "docker" path => "/var/lib/docker/containers/*/*.log" exclude => "*.gz" } } filter { if [type] == "docker" { grok { match => { "message" => "[%{TIMESTAMP_ISO8601:timestamp}] %{LOGLEVEL:loglevel} %{GREEDYDATA:message}" } overwrite => [ "message" ] } date { match => [ "timestamp", "ISO8601" ] timezone => "UTC" } } } output { stdout { codec => rubydebug } elasticsearch { hosts => ["localhost:9200"] index => "docker-%{+YYYY.MM.dd}" document_type => "docker" } }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÎļþÖУ¬½ç˵ÁËÒ»¸öÃûΪ¡°docker¡±µÄÊäÈ룬ÊäÈë·¾¶Îª/var/lib/docker/containers/ÏÂËùÓеÄ.logÎļþ¡£ÔÚ¹ýÂËÆ÷ÖУ¬Ê¹ÓÃgrokģʽƥÅäÈÕÖ¾£¬²¢½«Ê±¼ä´Áת»»ÎªISO8601ʱ¼äÃûÌá£ÔÚÊä³öÖУ¬½«ÈÕÖ¾Êä³öµ½ElasticsearchÖС£
4.3 Éó²éÈÝÆ÷ÈÕÖ¾
ÔÚÍê³ÉÒÔÉÏÉèÖúó£¬ÎÒÃÇ¿ÉÒÔÉó²éÈÝÆ÷µÄÈÕÖ¾²¢¾ÙÐÐÆÊÎö¡£ÔÚKibanaÃæ°åÖУ¬Ñ¡Ôñ¡°Discover¡±Ò³Ã棬¿ÉÒÔ¿´µ½ËùÓеÄÈÕÖ¾ÐÅÏ¢¡£
Èý¡¢Ê¹ÓÃDocker¾ÙÐÐÈÝÆ÷Òì³£ÅŲé
DockerÖÐÈÝÆ÷µÄÒì³£ÅŲéÒ²ÊǺÜÊÇÖ÷ÒªµÄ¡£ÈÝÆ÷µÄÒì³£¿ÉÄܵ¼ÖÂÓ¦ÓóÌÐòÎÞ·¨Õý³£ÔËÐУ¬ÉõÖÁ»áµ¼ÖÂÕû¸öϵͳµÄÍ߽⡣ÏÂÃæÏÈÈÝÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄÒì³£ÅŲ顣
1. Éó²éÈÝÆ÷״̬
ÎÒÃÇ¿ÉÒÔʹÓÃÈçÏÂÏÂÁîÀ´Éó²éÈÝÆ÷µÄ״̬£º
docker ps -a
µÇ¼ºó¸´ÖÆ
´ËÏÂÁÁгöËùÓеÄÈÝÆ÷¼°Æä״̬¡£
2. ½øÈëÈÝÆ÷¾ÙÐе÷ÊÔ
ÔÚÉó²éÈÝÆ÷״̬ºó£¬ÎÒÃÇ¿ÉÒÔ½øÈëÈÝÆ÷ÖоÙÐе÷ÊÔ¡£Ê¹ÓÃÈçÏÂÏÂÁ¿ÉÒÔ½øÈëµ½ÈÝÆ÷ÖУº
docker exec -it [CONTAINER ID] /bin/bash
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬CONTAINER IDÖ¸µÄÊÇÈÝÆ÷µÄID¡£
3. Éó²éÈÕÖ¾²¢ÆÊÎö
ÔÚ½øÈëµ½ÈÝÆ÷Öкó£¬ÎÒÃÇ¿ÉÒÔÉó²éÈÝÆ÷µÄÈÕÖ¾£¬²¢Æ¾Ö¤ÈÕÖ¾ÐÅÏ¢¾ÙÐе÷ÊÔÆÊÎö¡£ÏêϸÏÂÁîÈçÏ£º
docker logs [CONTAINER ID]
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
4. ʹÓù¤¾ß¾ÙÐÐÆÊÎö
ÔÚ¾ÙÐÐÈÝÆ÷Òì³£ÅŲéʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃһЩ¹¤¾ß¾ÙÐÐ×ÊÖú¡£ÀýÈ磬ÔÚÈÝÆ÷ÖÐ×°ÖÃDebug¹¤¾ß£¬ÒÔÀû±ãÎÒÃǾÙÐе÷ÊÔ¡£ÏÂÃæÊÇÒ»¸öÀý×Ó£º
docker run -d --name nginx-debug --entrypoint /usr/bin/sleep nginx 9d docker pause nginx-debug docker network connect [NETWORK] nginx-debug docker attach --sig-proxy=false nginx-debug
µÇ¼ºó¸´ÖÆ
ÉÏÊöÏÂÁîÖУ¬Ê×ÏȽ¨ÉèÁËÒ»¸öÃûΪ¡°nginx-debug¡±µÄÈÝÆ÷£¬²¢¾ÙÐÐÁËһЩÉèÖá£ÎÒÃÇ¿ÉÒÔʹÓôËÈÝÆ÷À´¾ÙÐе÷ÊÔ¡£
ËÄ¡¢×ܽá
ÔÚʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄÈÕÖ¾ÆÊÎöºÍÒì³£ÅŲéʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃDockerÏÂÁî¾ÙÐÐÉó²éºÍÆÊÎö£¬Ò²¿ÉÒÔʹÓõÚÈý·½¹¤¾ßÀ´×ÊÖúÎÒÃÇÔ½·¢ÉîÈëµØ¾ÙÐÐÈÝÆ÷ÈÕÖ¾µÄÆÊÎöºÍÒì³£ÅŲ顣ϣÍûÉÏÊöÄÚÈÝÄܹ»¶ÔÄúÔÚʹÓÃDocker¾ÙÐпª·¢ºÍ°²ÅÅʱÓÐËù×ÊÖú£¬Í¬Ê±±¾ÎÄÒ²ÌṩÁËÏêϸµÄ´úÂëʾÀý£¬¿ÉÒÔʹ¶ÁÕ߸üÒ×ÓÚÃ÷È·²¢Ñ§Ï°Ïà¹ØÊÖÒÕ¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄÈÕÖ¾ÆÊÎöºÍÒì³£ÅŲéµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡