LinuxÉϵÄDockerÈÝÆ÷¼à¿Ø£ºÔõÑùÆÊÎöºÍÓÅ»¯ÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ£¿
linuxÉϵÄdockerÈÝÆ÷¼à¿Ø£ºÔõÑùÆÊÎöºÍÓÅ»¯ÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ£¿
СÐò£º
DockerÊÇÒ»ÖÖÊ¢ÐеÄÈÝÆ÷»¯ÊÖÒÕ£¬¿ÉÒÔÔÚLinux²Ù×÷ϵͳÉÏÆô¶¯ºÍÖÎÀíÈÝÆ÷¡£Ê¹ÓÃDocker¿ÉÒÔ¿ìËÙ°²ÅźÍÖÎÀíÓ¦ÓóÌÐò£¬Ìá¸ß¿ª·¢ºÍ°²ÅÅЧÂÊ¡£È»¶ø£¬Ëæ×ÅÓ¦ÓóÌÐòÊýÄ¿µÄÔöÌíÒÔ¼°ÈÝÆ÷»¯ÇéÐεÄÖØ´óÐÔ£¬ÈÝÆ÷µÄ×ÊԴʹÓÃÂʳÉΪһ¸öÖ÷ÒªµÄÎÊÌâ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ì½ÌÖÔõÑùÆÊÎöºÍÓÅ»¯DockerÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ¡£
Ò»¡¢¼à¿ØDockerÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ
ÔÚÆÊÎöºÍÓÅ»¯ÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ֮ǰ£¬ÎÒÃÇÊ×ÏÈÐèÒª¼à¿ØÈÝÆ÷µÄ×ÊԴʹÓÃÇéÐΡ£DockerÌṩÁËһЩÏÂÁîºÍAPIÀ´¼à¿ØÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÕâЩ¹¤¾ßÀ´ÍøÂçºÍÆÊÎöÈÝÆ÷µÄÐÔÄÜÊý¾Ý¡£
ʹÓÃDockerÏÂÁî¼à¿ØÈÝÆ÷×ÊԴʹÓÃÂÊ
DockerÌṩÁËһЩÊÊÓõÄÏÂÁîÀ´¼à¿ØÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ¡£ÒÔÏÂÊÇһЩ³£ÓõÄÏÂÁîʾÀý£º
Éó²éÈÝÆ÷µÄCPUʹÓÃÂÊ£º
$ docker stats
µÇ¼ºó¸´ÖÆ
Éó²éÈÝÆ÷µÄÄÚ´æʹÓÃÂÊ£º
$ docker stats --format "table {{.Container}} {{.CPUPerc}} {{.MemUsage}} {{.MemPerc}}"
µÇ¼ºó¸´ÖÆ
Éó²éÈÝÆ÷µÄÍøÂçʹÓÃÂÊ£º
$ docker stats --format "table {{.Container}} {{.NetIO}} {{.BlockIO}}"
µÇ¼ºó¸´ÖÆ
ʹÓÃÕâЩÏÂÁÎÒÃÇ¿ÉÒÔʵʱ¼à¿ØÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ£¬²¢Æ¾Ö¤ÐèÒª½ÓÄÉÏìÓ¦µÄ²½·¥À´ÓÅ»¯ÈÝÆ÷µÄ×ÊԴʹÓá£
ʹÓÃDocker API¼à¿ØÈÝÆ÷×ÊԴʹÓÃÂÊ
³ýÁËÏÂÁîÐй¤¾ßÍ⣬Docker»¹ÌṩÁËÒ»Ì×ÍêÕûµÄAPIÀ´¼à¿ØÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ¡£Í¨¹ýʹÓÃDocker API£¬ÎÒÃÇ¿ÉÒÔ½«ÈÝÆ÷µÄÐÔÄÜÊý¾Ýµ¼Èëµ½ÆäËûϵͳÖоÙÐÐÆÊÎöºÍ´¦Öóͷ£¡£
ÒÔÏÂÊÇÒ»¸öʹÓÃDocker API¼à¿ØÈÝÆ÷CPUʹÓÃÂʵÄʾÀý´úÂ룺
import docker def monitor_container_resource_usage(container_id): client = docker.from_env() container = client.containers.get(container_id) stats = container.stats(stream=False) cpu_usage = stats['cpu_stats']['cpu_usage']['total_usage'] cpu_limit = stats['cpu_stats']['cpu_usage']['percpu_usage'] cpu_percent = round((cpu_usage / sum(cpu_limit) * 100), 2) print(f"Container {container_id} CPU utilization: {cpu_percent}%") if __name__ == "__main__": container_id = "d6d39e8dc22f" # ÊäÈëÈÝÆ÷ID monitor_container_resource_usage(container_id)
µÇ¼ºó¸´ÖÆ
ͨ¹ýʹÓÃDocker API£¬ÎÒÃÇ¿ÉÒÔ»ñÈ¡ÈÝÆ÷µÄÐÔÄÜÊý¾Ý£¬½ø¶ø¶ÔÈÝÆ÷µÄ×ÊԴʹÓÃÂʾÙÐмà¿ØºÍÆÊÎö¡£
¶þ¡¢ÓÅ»¯ÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ
µ±ÎÒÃÇÏàʶÁËÈÝÆ÷µÄ×ÊԴʹÓÃÂÊÖ®ºó£¬ÎÒÃÇ¿ÉÒÔƾ֤ÐèÒª½ÓÄÉһЩ²½·¥À´ÓÅ»¯ÈÝÆ÷µÄ×ÊԴʹÓá£ÏÂÃæÊÇһЩ³£¼ûµÄÓÅ»¯ÒªÁì¡£
µ÷½âÈÝÆ÷µÄCPUºÍÄÚ´æÏÞÖÆ
ͨ¹ýµ÷½âÈÝÆ÷µÄCPUºÍÄÚ´æÏÞÖÆ£¬ÎÒÃÇ¿ÉÒÔ¿ØÖÆÈÝÆ÷µÄ×ÊԴʹÓÃÇéÐΡ£¿ÉÒÔͨ¹ýÔÚÔËÐÐÈÝÆ÷ʱʹÓÖcpus²ÎÊýÏÞÖÆÈÝÆ÷µÄCPUʹÓÃÁ¿£¬Ê¹ÓÖmemory²ÎÊýÏÞÖÆÈÝÆ÷µÄÄÚ´æʹÓÃÁ¿¡£
ÀýÈ磬ÒÔÏÂÏÂÁ½¨ÉèÒ»¸öÃûΪmycontainerµÄÈÝÆ÷£¬ÏÞÖÆÈÝÆ÷µÄCPUʹÓÃÁ¿Îª1¸ö½¹µã£¬²¢ÏÞÖÆÈÝÆ÷µÄÄÚ´æʹÓÃÁ¿Îª1 GB£º
$ docker run --name mycontainer --cpus 1 --memory 1g -d myimage:latest
µÇ¼ºó¸´ÖÆ
ͨ¹ýµ÷½âÈÝÆ÷µÄ×ÊÔ´ÏÞÖÆ£¬ÎÒÃÇ¿ÉÒÔ×èÖ¹ÈÝÆ÷Ì«¹ýʹÓÃϵͳ×ÊÔ´£¬´Ó¶øÓÅ»¯ÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ¡£
ºÏÀí·ÖÅÉÈÝÆ÷µÄЧÀͺ͹¦Ð§
¶ÔÈÝÆ÷ÖеÄЧÀͺ͹¦Ð§¾ÙÐкÏÀíµÄ·ÖÅÉ£¬¿ÉÒÔÌá¸ßÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ¡£ÀýÈ磬¿ÉÒÔ½«ÏàËƵÄЧÀͺ͹¦Ð§·ÅÔÚͳһ¸öÈÝÆ÷ÖУ¬ÒÔïÔÌÈÝÆ÷Ö®¼äµÄ×ÊÔ´ÈßÓàʹÓá£
±ðµÄ£¬ÎÒÃÇ»¹¿ÉÒÔͨ¹ýʹÓöà¸öÈÝÆ÷À´Æ½ºâ¸ºÔغÍÌá¸ßÈÝÆ÷µÄ×ÊԴʹÓá£ÀýÈ磬¿ÉÒÔʹÓÃÈÝÆ÷±àÅŹ¤¾ßÈçKubernetesÀ´ÖÎÀí¶à¸öÈÝÆ÷£¬Æ¾Ö¤ÐèÇó×Ô¶¯µ÷½âÈÝÆ÷µÄ×ÊԴʹÓá£
½áÂÛ£º
ͨ¹ý¼à¿ØDockerÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ£¬²¢½ÓÄÉÏìÓ¦µÄÓÅ»¯²½·¥£¬ÎÒÃÇ¿ÉÒÔÌá¸ßÈÝÆ÷µÄ×ÊԴʹÓÃЧÂÊ£¬ÓÅ»¯Ó¦ÓóÌÐòµÄÐÔÄܺͿÉÀ©Õ¹ÐÔ¡£ÔÚ¾ÙÐÐÈÝÆ÷»¯°²ÅÅʱ£¬ÎñÐëÒªÖØÊÓÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ£¬ÒÔÌá¸ßÕûϵһÇеÄЧÂʺÍÐÔÄÜ¡£
²Î¿¼ÎÄÏ×£º
Docker Documentation: https://docs.docker.com/
Docker SDK for Python Documentation: https://docker-py.readthedocs.io/
¸½Â¼£º
ÒÔÉϾÍÊÇLinuxÉϵÄDockerÈÝÆ÷¼à¿Ø£ºÔõÑùÆÊÎöºÍÓÅ»¯ÈÝÆ÷µÄ×ÊԴʹÓÃÂÊ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡