LinuxÉϵÄDockerÈÝÆ÷¼à¿Ø£ºÔõÑùʵʱ¼à²âÈÝÆ÷µÄÔËÐÐ״̬£¿
linuxÉϵÄdockerÈÝÆ÷¼à¿Ø£ºÔõÑùʵʱ¼à²âÈÝÆ÷µÄÔËÐÐ״̬£¿
СÐò£º
Ëæ×ÅÈÝÆ÷ÊÖÒÕµÄÆÕ±éÓ¦Óã¬DockerÒѳÉΪ×îÊܽӴýµÄÈÝÆ÷»¯Æ½Ì¨Ö®Ò»¡£È»¶ø£¬½ö½ö½¨ÉèºÍÔËÐÐÈÝÆ÷ÊDz»·óµÄ£¬¶ÔÈÝÆ÷¾ÙÐмà¿ØÒÔÈ·±£ÆäÎȹÌÔËÐÐÊÇÖÁ¹ØÖ÷ÒªµÄ¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚLinuxƽ̨ÉÏʵʱ¼à²âDockerÈÝÆ÷µÄÔËÐÐ״̬£¬²¢ÌṩÏìÓ¦µÄ´úÂëʾÀý¡£
Ò»¡¢DockerÈÝÆ÷¼à¿ØµÄÖ÷ÒªÐÔ
ͨ¹ý¼à¿ØDockerÈÝÆ÷£¬ÎÒÃÇ¿ÉÒÔʵʱ׷×ÙÈÝÆ÷µÄCPUʹÓÃÂÊ¡¢ÄÚ´æÕ¼Óá¢ÍøÂçÁ÷Á¿¡¢´ÅÅÌIOµÈÖ¸±ê£¬ÒÔ±ãʵʱ·¢Ã÷²¢½â¾öDZÔÚµÄÎÊÌ⡣ͬʱ£¬¼à¿Ø»¹¿ÉÒÔ×ÊÖúÎÒÃÇÓÅ»¯×ÊԴʹÓã¬Ìá¸ßÕûϵһÇеÄÐÔÄÜ¡£
¶þ¡¢Ê¹ÓÃcAdvisor¼à¿ØDockerÈÝÆ÷
cAdvisorÊÇÒ»¸ö¿ªÔ´¹¤¾ß£¬×¨ÃÅÓÃÓÚ¼à¿ØÈÝÆ÷µÄ×ÊԴʹÓÃÇéÐΡ£Ëü¿ÉÒÔÔÚDockerÖ÷»úÉÏͨ¹ýDocker APIÓëDockerÒýÇæ¾ÙÐÐͨѶ£¬²¢ÍøÂçÈÝÆ÷Äڵĸ÷ÏîÖ¸±ê¡£
×°ÖÃcAdvisor
ÔÚLinuxϵͳÉÏ×°ÖÃcAdvisorºÜÊǼòÆÓ¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
docker run --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --publish=8080:8080 --detach=true --name=cadvisor google/cadvisor:latest
µÇ¼ºó¸´ÖÆ
ÕâÌõÏÂÁî»áÔÚÍâµØÆô¶¯Ò»¸öcAdvisorÈÝÆ÷£¬²¢½«ÆäÓ³Éäµ½Ö÷»úµÄ8080¶Ë¿Ú¡£
Éó²é¼à¿ØЧ¹û
ÔÚä¯ÀÀÆ÷Öлá¼ûhttp://localhost:8080¼´¿ÉÉó²écAdvisorµÄ¼à¿Ø½çÃæ¡£¸Ã½çÃæÄܹ»ÏÔʾÈÝÆ÷µÄ¸÷ÏîÖ¸±ê£¬°üÀ¨CPU¡¢ÄÚ´æ¡¢ÍøÂç¡¢´ÅÅ̵ȡ£
Èý¡¢Ê¹ÓÃDocker API¼à¿ØÈÝÆ÷
³ýÁËʹÓÃcAdvisorÍ⣬ÎÒÃÇ»¹¿ÉÒÔͨ¹ýDocker APIÀ´¼à¿ØÈÝÆ÷µÄÔËÐÐ״̬¡£Docker APIÌṩÁËһϵÁнӿڣ¬¿ÉÒÔÅÌÎʺÍÖÎÀíÈÝÆ÷µÄÐÅÏ¢¡£
×°ÖÃDocker SDK for Python
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃDocker SDK for Python£¬ËüÊÇDocker¹Ù·½ÌṩµÄPython¿â£¬ÓÃÓÚÓëDocker API¾ÙÐн»»¥¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
pip install docker
µÇ¼ºó¸´ÖÆ
ʹÓôúÂë¼à¿ØÈÝÆ÷
ÏÂÃæÊÇÒ»¸öʹÓÃDocker SDK for PythonÀ´¼à¿ØÈÝÆ÷µÄʾÀý´úÂ룺
import docker def monitor_container(container_id): client = docker.from_env() container = client.containers.get(container_id) stats = container.stats(stream=True) for stat in stats: # ´¦ÀíÈÝÆ÷µÄͳ¼ÆÊý¾Ý print(stat)
µÇ¼ºó¸´ÖÆ
¸Ã´úÂëÊ×ÏÈʹÓÃdocker.from_env()½¨ÉèÁËÒ»¸öDocker¿Í»§¶ËʵÀý£¬È»ºóͨ¹ýclient.containers.get(container_id)»ñÈ¡Ö¸¶¨µÄÈÝÆ÷¹¤¾ß¡£½Ó×Å£¬Í¨¹ýcontainer.stats(stream=True)»ñÈ¡ÈÝÆ÷µÄʵʱͳ¼ÆÊý¾Ý£¬²¢¾ÙÐÐÏìÓ¦µÄ´¦Àí¡£
ËÄ¡¢×ܽá
±¾ÎÄÏÈÈÝÁËÔÚLinuxÉÏÔõÑùʵʱ¼à¿ØDockerÈÝÆ÷µÄÔËÐÐ״̬¡£Í¨¹ýʹÓÃcAdvisorºÍDocker API£¬ÎÒÃÇ¿ÉÒÔÀû±ãµØ»ñÈ¡ÈÝÆ÷µÄ¸÷ÏîÖ¸±ê£¬²¢¾Ý´Ë¾ÙÐÐÐÔÄÜÓÅ»¯ºÍ¹ÊÕÏÅŲ顣ϣÍû±¾ÎĶÔÄãÏàʶDockerÈÝÆ÷¼à¿ØÓÐËù×ÊÖú¡£
´úÂëʾÀýºÍAPIÎĵµ¿ÉÒԲο¼ÒÔÏÂÁ´½Ó£º
cAdvisor: https://github.com/google/cadvisor
Docker SDK for Python: https://docker-py.readthedocs.io/
ÒÔÉϾÍÊÇLinuxÉϵÄDockerÈÝÆ÷¼à¿Ø£ºÔõÑùʵʱ¼à²âÈÝÆ÷µÄÔËÐÐ״̬£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡