ÔõÑùʹÓÃDocker¾ÙÐÐÓ¦Óõļà¿ØºÍÈÕÖ¾ÖÎÀí
DockerÒѾ³ÉΪÁËÏÖ´ú»¯Ó¦ÓÃÖеÄÒ»Ïî±Ø±¸ÊÖÒÕ£¬¿ÉÊÇʹÓÃDocker¾ÙÐÐÓ¦Óüà¿ØºÍÈÕÖ¾ÖÎÀíÈ´ÊÇÒ»ÏîÌôÕ½¡£Ëæ×ÅDockerÍøÂ繦Ч£¬ÈçService DiscoveryºÍLoad BalancingµÄÒ»Ö±ÔöÇ¿£¬ÎÒÃÇÔ½À´Ô½ÐèÒªÒ»¸öÍêÕû¡¢Îȹ̣¬ÒÔ¼°¸ßЧµÄÓ¦Óüà¿Øϵͳ¡£
ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«¼òÆÓµØÏÈÈÝʹÓÃDocker¾ÙÐÐÓ¦Óüà¿ØºÍÈÕÖ¾ÖÎÀíµÄͬʱ¸ø³öÏêϸµÄ´úÂëʾÀý¡£
ʹÓÃPrometheus¾ÙÐÐÓ¦Óüà¿Ø
PrometheusÊÇÒ»¿î¿ªÔ´£¬»ùÓÚPullÄ£×ÓµÄЧÀͼà²âºÍÖÒÑÔ¹¤¾ß£¬ÓÉSoundCloud¿ª·¢¡£ËüʹÓÃGoÓïÑÔ±àд£¬±»ÆÕ±éÓ¦ÓÃÓÚ΢ЧÀͼƻ®ºÍÔÆÇéÐÎÖС£×÷Ϊһ¿î¼à¿Ø¹¤¾ß£¬Ëü¿ÉÒÔ¶ÔDockerµÄCPU¡¢ÄÚ´æ¡¢ÍøÂçºÍ´ÅÅ̵ȾÙÐмà¿Ø£¬²¢ÇÒ»¹Ö§³Ö¶àάÊý¾ÝÇл»¡¢ÎÞаµÄÅÌÎÊ¡¢±¨¾¯ÒÔ¼°¿ÉÊÓ»¯µÈ¹¦Ð§£¬ÈÃÄã¿ÉÒÔ¿ìËÙ×ö³ö·´Ó¦£¬²¢¿ìËÙ×ö³ö¾öÒé¡£
ÉÐÓÐÒ»µãÐèҪעÖصÄÊÇ£¬PrometheusÐèҪͨ¹ýPull·½·¨µÄ²ÉÑù£¬Ò²¾ÍÊÇ»á¼û±»¼à¿ØÓ¦ÓÃÖеÄ/metrics½Ó¿Ú»ñÈ¡¼à¿ØÊý¾Ý¡£ÒÔÊÇ£¬ÔÚÆô¶¯±»¼à¿ØÓ¦ÓþµÏñʱ£¬ÐèÒªÏȽ«¿ÉÒÔ»á¼ûµ½PrometheusµÄIPºÍ¶Ë¿ÚÉèÖõ½/metrics½Ó¿ÚÖС£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄNode.jsÓ¦Óá£
const express = require('express') const app = express() app.get('/', (req, res) => { res.send('Hello World!') }) app.get('/metrics', (req, res) => { res.send(` # HELP api_calls_total Total API calls # TYPE api_calls_total counter api_calls_total 100 `) }) app.listen(3000, () => { console.log('Example app listening on port 3000!') })
µÇ¼ºó¸´ÖÆ
ÔڸôúÂëÖУ¬ÎÒÃÇͨ¹ý/metrics½Ó¿Ú£¬·µ»ØÁËÒ»¸öapi_calls_totalµÄ¼à¿ØÖ¸±ê¡£
½Ó×Å£¬ÔÚ¹ÙÍøÉÏÏÂÔØPrometheusµÄDocker¾µÏñ£¬²¢½¨ÉèÒ»¸ödocker-compose.ymlÎļþ£¬²¢ÇÒÔÚ¸ÃÎļþÖУ¬ÎÒÃÇ»ñÈ¡¸ÃNode.jsÓ¦ÓõÄÊý¾Ý¡£
version: '3' services: node: image: node:lts command: node index.js ports: - 3000:3000 prometheus: image: prom/prometheus:v2.25.2 volumes: - ./prometheus:/etc/prometheus command: - '--config.file=/etc/prometheus/prometheus.yml' - '--storage.tsdb.retention.time=15d' ports: - 9090:9090
µÇ¼ºó¸´ÖÆ
¸Ãdocker-compose.ymlÎļþÖУ¬ÎÒÃǽç˵ÁËÁ½¸öЧÀÍ£¬Ò»¸öÊÇÔËÐÐNode.jsÓ¦ÓõÄNodeЧÀÍ£¬ÁíÒ»¸öÊÇÓÃÓÚ¼à¿ØµÄPrometheusЧÀÍ¡£ÆäÖУ¬NodeЧÀÍÐû²¼µÄ¶Ë¿ÚΪ3000¶Ë¿Ú£¬Í¨¹ý¶Ë¿ÚÓ³É䣬¿ÉÒÔͨ¹ýdocker-compose.ymlÖеÄIPºÍ3000¶Ë¿Ú»á¼ûµ½¸ÃNodeÓ¦ÓõÄ/metrics½Ó¿Ú¡£¶øPrometheusÔò¿ÉÒÔͨ¹ý9090¶Ë¿Ú»á¼û¶ÔÓ¦µÄ¼à¿ØÖ¸±êÊý¾Ý¡£
×îºó£¬ÔÚprometheus.ymlÎļþÖУ¬ÎÒÃÇÐèÒª½ç˵Ҫ»ñÈ¡µÄÊý¾ÝȪԴ¡£
global: scrape_interval: 15s evaluation_interval: 15s scrape_configs: - job_name: 'node-exporter' static_configs: - targets: ['node:9100'] - job_name: 'node-js-app' static_configs: - targets: ['node:3000']
µÇ¼ºó¸´ÖÆ
ÔÚ¸ÃÎļþÖУ¬ÎÒÃǽç˵ÁËÒªÊÕÂÞµÄËùÓеÄNode.jsÓ¦ÓõÄÖ¸±ê£¬ÆäÖÐtargets²ÎÊýÊÇNode.jsÓ¦ÓõÄIPµØµã¼°Æä¶ÔÓ¦¶Ë¿ÚºÅ¡£ÔÚÕâÀÎÒÃÇʹÓõÄÊÇnodeºÍ3000¶Ë¿Ú¡£
×îºó£¬ÔËÐÐdocker-compose upÏÂÁ¼´¿ÉÆô¶¯Õû¸öÓ¦Óü°Æä¼à¿ØЧÀÍ£¬²¢ÔÚPrometheusÖÐÉó²é¸Ã»áÔ±Ö¸±ê¡£
ʹÓÃElasticSearchºÍLogstash¾ÙÐÐÈÕÖ¾ÖÎÀí
ÔÚDockerÖУ¬Ó¦ÓõÄÈÕÖ¾Êý¾ÝÂþÑÜÔÚ²î±ðµÄDockerÈÝÆ÷ÖС£ÈôÊÇÄãÒªÔÚ¼¯Öеĵط½¶ÔÕâЩÈÕÖ¾¾ÙÐÐÖÎÀí£¬ÄÇô¿ÉÒÔͨ¹ýʹÓÃELKÖеÄElasticSearchºÍLogstash,½«ÈÕÖ¾ÖÐÐÄ»¯ÖÎÀí£¬ÒÔ±ã¸üÇáËɵØʵÏÖÅÌËã»ú×ÊÔ´µÄ¼à¿ØºÍÆÊÎö¡£
ÔÚ×îÏÈ֮ǰ£¬ÐèÒªÏÈÏÂÔØLogstashºÍElasticSearchµÄDocker¾µÏñ£¬²¢½¨ÉèÒ»¸ödocker-compose.ymlÎļþ¡£
ÔÚ¸ÃÎļþÖУ¬ÎÒÃǽç˵ÁËÈý¸öЧÀÍ£¬ÆäÖÐblsÊÇÓÃÀ´Ä£ÄâÓªÒµÈÕÖ¾µÄAPIЧÀÍ£¬Æäÿ´ÎÏìÓ¦ºó£¬½«¼Í¼һÌõÈÕÖ¾µ½stdoutºÍÈÕÖ¾ÎļþÖС£logstashЧÀÍÊÇÓÉLogstash¹Ù·½ÌṩµÄDocker¾µÏñ¹¹½¨£¬ÓÃÓÚÍøÂç¡¢¹ýÂ˺ʹ«ÊäÈÕÖ¾¡£ElasticSearchЧÀÍÓÃÓÚ´æ´¢ºÍ¼ìË÷ÈÕÖ¾¡£
version: '3' services: bls: image: nginx:alpine volumes: - ./log:/var/log/nginx - ./public:/usr/share/nginx/html:ro ports: - "8000:80" logging: driver: "json-file" options: max-size: "10m" max-file: "10" logstash: image: logstash:7.10.1 volumes: - ./logstash/pipeline:/usr/share/logstash/pipeline environment: - "ES_HOST=elasticsearch" depends_on: - elasticsearch elasticsearch: image: elasticsearch:7.10.1 environment: - "http.host=0.0.0.0" - "discovery.type=single-node" volumes: - ./elasticsearch:/usr/share/elasticsearch/data
µÇ¼ºó¸´ÖÆ
ÔÚÉèÖÃÎļþÖУ¬ÎÒÃÇÓ³ÉäÁËÈÝÆ÷Äڵķ¾¶µ½ËÞÖ÷»úµÄÈÕÖ¾ÎļþϵͳÖС£Í¬Ê±Í¨¹ýloggingµÄoption£¬Ôò½ç˵ÁËÈÕÖ¾µÄ¾í¾ÞϸºÍÊýÄ¿£¬ÒÔÏÞÖÆÈÕÖ¾µÄÕ¼Óô洢¡£
ÔÚÉèÖÃÎļþµÄlogstashÖУ¬ÎÒÃǽç˵ÁËÒ»¸öеÄpipeline£¬ÆäÃûΪnginx_pipeline.conf£¬¸ÃÎļþÓÃÓÚ´¦Öóͷ£nginxÈÕÖ¾µÄÍøÂç¡¢¹ýÂ˺ʹ«Êä¡£ÓëELKµÄÊÂÇéÔÀíÏàͬ£¬logstash½«Æ¾Ö¤²î±ðµÄÌõ¼þ£¬¶ÔÎüÊÕµ½µÄÈÕÖ¾¾ÙÐд¦Öóͷ££¬²¢½«Æä·¢Ë͵½ÒѾ½¨ÉèµÄ Elasticsearch ¼¯ÈºÖС£ÔÚ¸ÃÉèÖÃÎļþÖУ¬ÎÒÃǽç˵ÁËÈçÏ´¦Öóͷ£Âß¼£º
input { file { path => "/var/log/nginx/access.log" } } filter { grok { match => { "message" => "%{COMBINEDAPACHELOG}" } } } output { elasticsearch { hosts => [ "${ES_HOST}:9200" ] index => "nginx_log_index" } }
µÇ¼ºó¸´ÖÆ
ÔÚ¸ÃÉèÖÃÎļþÖУ¬ÎÒÃǽç˵ÁËÒ»¸önameΪfileµÄÊäÈ룬ÌåÏÖÒª´ÓÍâµØLogÎļþÖжÁÈ¡Êý¾Ý¡£½Ó×Å£¬ÎÒÃÇÒýÈëÁËʹÓÃgrok¿âÀ´ÆÊÎöÇкÏÌض¨Ä£°åµÄÈÕÖ¾µÄfilter¡£×îºó£¬ÎÒÃǽç˵ÁËÊä³ö£¬Æ佫Êý¾Ý´«Êäµ½ Elasticsearch ¼¯ÈºµÄµØµã£¬Í¬Ê±½«¼ìË÷ºÍ±¨¸æͨ¹ýÇéÐαäÁ¿ES_HOSTת´ïµ½ÈÝÆ÷ÖС£
ÔÚ×îºó£¬ÈçÉÏÍê³ÉÕû¸öELKµÄÉèÖúó£¬ÎÒÃÇ»á»ñµÃÒ»¸ö¸ßЧµÄÈÕÖ¾ÖÎÀíϵͳ£¬Ã¿ÌõÈÕÖ¾¶¼½«·¢Ë͵½¼¯Öеĵط½£¬²¢±»ÕûºÏÔÚÒ»Æ𣬿ÉÒÔʵÏÖÇáÓ¯µÄËÑË÷£¬¹ýÂ˺ͿÉÊÓ»¯²Ù×÷¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃDocker¾ÙÐÐÓ¦Óõļà¿ØºÍÈÕÖ¾ÖÎÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡