×ðÁú¿­Ê±¹ÙÍøµÇ¼

LinuxϵÄDocker£ºÔõÑù¾ÙÐÐÈÝÆ÷µÄ×Ô¶¯»¯²âÊԺͼà¿Ø£¿

linuxϵÄdocker£ºÔõÑù¾ÙÐÐÈÝÆ÷µÄ×Ô¶¯»¯²âÊԺͼà¿Ø£¿

Ëæ×ÅÈÝÆ÷ÊÖÒÕµÄѸÃÍÉú³¤£¬Docker³ÉΪÁË×îΪʢÐеÄÈÝÆ÷»¯Æ½Ì¨Ö®Ò»¡£¶øÔÚʹÓÃDocker¾ÙÐÐÓ¦Óð²ÅźÍÖÎÀíµÄÀú³ÌÖУ¬ÈÝÆ÷µÄ×Ô¶¯»¯²âÊԺͼà¿ØÏÔµÃÓÈΪÖ÷Òª¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃLinuxϵÄDocker¾ÙÐÐÈÝÆ÷µÄ×Ô¶¯»¯²âÊԺͼà¿Ø£¬²¢ÌṩÏìÓ¦µÄ´úÂëʾÀý¡£

Ò»¡¢DockerµÄ×Ô¶¯»¯²âÊÔ

½¨ÉèDockerfile

Ê×ÏÈ£¬ÐèҪΪ´ý²âÊÔµÄÓ¦ÓóÌÐò½¨ÉèDockerfile¡£DockerfileÊÇÒ»¸öÎı¾Îļþ£¬ÆäÖаüÀ¨ÁËһϵÁÐÓÃÓÚ¹¹½¨Docker¾µÏñµÄÖ¸Áî¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀýDockerfile£º

FROM python:3.8-alpine

WORKDIR /app

COPY requirements.txt ./ 
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD [ "python", "./app.py" ]

µÇ¼ºó¸´ÖÆ

ÉÏÊöDockerfileÊÇΪһ¸ö»ùÓÚPythonµÄÓ¦ÓóÌÐò½¨ÉèµÄ£¬Ê×ÏÈ»ùÓÚpython:3.8-alpine¾µÏñ¹¹½¨Ò»¸öеľµÏñ¡£È»ºóÉèÖÃÊÂÇéĿ¼Ϊ/app£¬½«Ó¦ÓóÌÐòËùÐèµÄÒÀÀµÎļþrequirements.txt¸´ÖƵ½ÈÝÆ÷ÖУ¬²¢×°ÖÃÒÀÀµ¡£½Ó׎«Ä¿½ñĿ¼ÖеÄËùÓÐÎļþ¶¼¸´ÖƵ½ÈÝÆ÷ÖУ¬²¢Í¨¹ýCMDÖ¸ÁîÖ¸¶¨ÈÝÆ÷Æô¶¯Ê±Ö´ÐеÄÏÂÁî¡£

¹¹½¨ºÍÔËÐÐÈÝÆ÷

Íê³ÉDockerfileµÄ±àдºó£¬¿ÉÒÔʹÓÃdocker buildÏÂÁî¹¹½¨Docker¾µÏñ£¬ÈçÏÂËùʾ£º

$ docker build -t myapp:latest .

µÇ¼ºó¸´ÖÆ

ÉÏÊöÏÂÁ»ùÓÚÄ¿½ñĿ¼ÖеÄDockerfile¹¹½¨ÃûΪmyappµÄ×îа汾¾µÏñ¡£

½ÓÏÂÀ´£¬Ê¹ÓÃdocker runÏÂÁîÔËÐÐÈÝÆ÷£¬²¢Ö¸¶¨ÏìÓ¦µÄ¶Ë¿ÚÓ³ÉäµÈÉèÖã¬ÈçÏÂËùʾ£º

$ docker run -d -p 8080:8080 --name myapp-container myapp:latest

µÇ¼ºó¸´ÖÆ

ÉÏÊöÏÂÁÔËÐÐÃûΪmyapp-containerµÄÈÝÆ÷£¬²¢½«ÈÝÆ÷ÄÚµÄ8080¶Ë¿ÚÓ³Éäµ½Ö÷»úµÄ8080¶Ë¿Ú¡£

±àд×Ô¶¯»¯²âÊԾ籾

ΪÁ˾ÙÐÐ×Ô¶¯»¯²âÊÔ£¬ÐèÒª±àдÏìÓ¦µÄ²âÊԾ籾¡£ÒÔPythonΪÀý£¬¿ÉÒÔʹÓÃunittestÄ£¿é±àд²âÊÔÓÃÀý¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀý£º

import unittest
import requests

class TestApp(unittest.TestCase):
    def setUp(self):
        self.url = 'http://localhost:8080/'

    def tearDown(self):
        pass

    def test_hello(self):
        response = requests.get(self.url + 'hello')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.text, 'Hello, world!')

if __name__ == '__main__':
    unittest.main()

µÇ¼ºó¸´ÖÆ

ÉÏÊöʾÀýÖУ¬setUpÒªÁìÓÃÓÚ³õʼ»¯²âÊÔÇéÐΣ¬tearDownÒªÁìÓÃÓÚÕûÀí²âÊÔÇéÐΡ£test_helloÒªÁìÊÇÒ»¸öÏêϸµÄ²âÊÔÓÃÀý£¬Ê¹ÓÃrequests¿â·¢ËÍHTTPÇëÇ󣬲¢¾ÙÐжÏÑÔÅжϷµ»ØЧ¹ûÊÇ·ñÇкÏÔ¤ÆÚ¡£

ÔËÐÐ×Ô¶¯»¯²âÊÔ

Íê³É²âÊԾ籾µÄ±àдºó£¬¿ÉÒÔͨ¹ýÔÚÖ÷»úÉÏÔËÐвâÊÔ½ÅÔ­À´¶ÔÈÝÆ÷¾ÙÐÐ×Ô¶¯»¯²âÊÔ¡£¼ÙÉè²âÊԾ籾ÉúÑÄΪtest_app.py£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÔËÐвâÊԾ籾£º

$ python test_app.py

µÇ¼ºó¸´ÖÆ

¶þ¡¢DockerµÄ¼à¿Ø

ʹÓÃPrometheus¼à¿Ø

PrometheusÊÇÒ»¿î¿ªÔ´µÄ¼à¿Øϵͳ£¬ËüÔÚDockerÉú̬ϵͳÖб»ÆÕ±éʹÓá£Prometheusͨ¹ýÊÕÂ޺ʹ洢ʱ¼äÐòÁÐÊý¾Ý£¬²¢ÌṩÎÞаµÄÅÌÎʺͿÉÊÓ»¯¹¦Ð§£¬ÎªÈÝÆ÷µÄ¼à¿ØÌṩÁËÇ¿Ê¢µÄÖ§³Ö¡£

Ê×ÏÈ£¬ÐèÒªÔÚÈÝÆ÷ÖÐ×°ÖúÍÉèÖÃPrometheus¡£Õâ¿ÉÒÔͨ¹ýÔÚDockerfileÖÐÌí¼ÓÏìÓ¦µÄÖ¸ÁîÀ´ÊµÏÖ£¬Ïêϸ°ì·¨ÈçÏ£º

ÏÂÔز¢×°ÖÃPrometheus

FROM prom/prometheus:v2.26.0

COPY prometheus.yml /etc/prometheus/

µÇ¼ºó¸´ÖÆ

½¨ÉèPrometheusÉèÖÃÎļþprometheus.yml

global:
  scrape_interval: 5s

scrape_configs:
  - job_name: 'myapp'
    static_configs:
      - targets: ['myapp-container:8080']

µÇ¼ºó¸´ÖÆ

ÉÏÊöÉèÖÃÎļþÖУ¬scrape_intervalÖ¸¶¨ÁËÊý¾ÝÊÕÂ޵ľàÀëʱ¼ä£¬scrape_configs½ç˵ÁËÒª¼à¿ØµÄÄ¿µÄ¡£

Æô¶¯PrometheusÈÝÆ÷

Íê³ÉDockerfileºÍÉèÖÃÎļþµÄ±àдºó£¬¿ÉÒÔʹÓÃdocker runÏÂÁîÆô¶¯PrometheusÈÝÆ÷£¬ÈçÏÂËùʾ£º

$ docker run -d -p 9090:9090 -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml --name prometheus prom/prometheus:v2.26.0

µÇ¼ºó¸´ÖÆ

ÉÏÊöÏÂÁÔËÐÐÃûΪprometheusµÄÈÝÆ÷£¬²¢½«ÈÝÆ÷ÄÚµÄ9090¶Ë¿ÚÓ³Éäµ½Ö÷»úµÄ9090¶Ë¿Ú£¬Í¬Ê±½«Ö÷»úÉϵÄprometheus.ymlÎļþ¹ÒÔص½ÈÝÆ÷ÖС£

»á¼ûPrometheus Web½çÃæ

Íê³ÉPrometheusÈÝÆ÷µÄÆô¶¯ºó£¬¿ÉÒÔͨ¹ýä¯ÀÀÆ÷»á¼ûhttp://localhost:9090À´·­¿ªPrometheusµÄWeb½çÃæ¡£ÔڸýçÃæÖУ¬¿ÉÒÔͨ¹ýPromQLÅÌÎÊÓïÑÔ¾ÙÐÐÊý¾ÝµÄÅÌÎʺͿÉÊÓ»¯¡£

×ܽá

±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃLinuxϵÄDocker¾ÙÐÐÈÝÆ÷µÄ×Ô¶¯»¯²âÊԺͼà¿Ø¡£ÔÚ¾ÙÐÐ×Ô¶¯»¯²âÊÔʱ£¬ÐèÒª½¨ÉèDockerfile¡¢¹¹½¨ºÍÔËÐÐÈÝÆ÷£¬²¢±àдÏìÓ¦µÄ²âÊԾ籾¾ÙÐвâÊÔ¡£¶øÔÚ¾ÙÐÐÈÝÆ÷¼à¿Øʱ£¬¿ÉÒÔʹÓÃPrometheus¾ÙÐÐʱ¼äÐòÁÐÊý¾ÝµÄÊÕÂ޺ʹ洢£¬²¢Í¨¹ýPromQL¾ÙÐÐÅÌÎʺͿÉÊÓ»¯¡£Í¨¹ýÒÔÉÏÒªÁ죬¿ÉÒÔ¸üºÃµØÖÎÀíºÍ¼à¿ØDockerÈÝÆ÷£¬È·±£Ó¦ÓóÌÐòµÄÎȹÌÐԺͿɿ¿ÐÔ¡£

²Î¿¼ÎÄÏ×£º

Docker¹Ù·½Îĵµ£ºhttps://docs.docker.com/

Prometheus¹Ù·½Îĵµ£ºhttps://prometheus.io/docs/

ÒÔÉϾÍÊÇLinuxϵÄDocker£ºÔõÑù¾ÙÐÐÈÝÆ÷µÄ×Ô¶¯»¯²âÊԺͼà¿Ø£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ