×ðÁú¿­Ê±

ÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄ¼à¿ØºÍ¸æ¾¯´¦Öóͷ£

ÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄ¼à¿ØºÍ¸æ¾¯´¦Öóͷ£

Ò»¡¢Ð¡Ðò

Ëæ×ÅÈÝÆ÷ÊÖÒÕµÄÆÕ±éÓ¦Óà £¬ÈÝÆ÷µÄ¼à¿ØºÍ¸æ¾¯´¦Öóͷ£±äµÃÓú·¢Ö÷Òª ¡£DockerÊÇÏÖÔÚ×îÊ¢ÐеÄÈÝÆ÷ÖÎÀíƽ̨֮һ £¬±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄ¼à¿ØºÍ¸æ¾¯´¦Öóͷ£ £¬²¢¸ø³öÏêϸµÄ´úÂëʾÀý ¡£

¶þ¡¢¼à¿ØDockerÈÝÆ÷

ʹÓÃDocker Stats API

Docker Stats APIÊÇDockerÌṩµÄÒ»¸öÓÃÓÚ»ñÈ¡ÈÝÆ÷ͳ¼ÆÐÅÏ¢µÄAPI ¡£ÎÒÃÇ¿ÉÒÔͨ¹ýŲÓøÃAPI»ñÈ¡ÈÝÆ÷µÄ¸÷ÏîÖ¸±ê £¬²¢¾ÙÐмà¿Ø ¡£

Ïêϸ´úÂëʾÀýÈçÏ£º

import docker

client = docker.DockerClient(base_url='unix://var/run/docker.sock')

def monitor_container(container_id):
    container = client.containers.get(container_id)
    stats = container.stats(stream=False)
    print(stats)

if __name__ == '__main__':
    monitor_container('CONTAINER_ID')

µÇ¼ºó¸´ÖÆ

ʹÓÃPrometheusºÍcAdvisor

PrometheusÊÇÒ»¸ö¿ªÔ´µÄ¼à¿Øϵͳ £¬¶øcAdvisorÊÇÓÃÓÚ¼à¿ØÈÝÆ÷µÄ¹¤¾ß ¡£ÍŽáÕâÁ½¸ö¹¤¾ß £¬ÎÒÃÇ¿ÉÒÔʵÏÖ¶ÔÈÝÆ÷µÄÖÜÈ«¼à¿Ø ¡£

Ïêϸ´úÂëʾÀýÈçÏ£º

Ê×ÏÈ £¬ÎÒÃÇÐèҪװÖò¢Æô¶¯PrometheusºÍcAdvisor ¡£È»ºóÔÚPrometheusµÄÉèÖÃÎļþprometheus.ymlÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º

scrape_configs:
  - job_name: 'cadvisor'
    scrape_interval: 5s
    static_configs:
    - targets: ['cadvisor:8080']

µÇ¼ºó¸´ÖÆ

½ÓÏÂÀ´ £¬ÔÚPythonÖÐʹÓÃPrometheusÌṩµÄ¿Í»§¶Ë¿âÀ´ÅÌÎʲ¢´¦Öóͷ£ÈÝÆ÷µÄ¼à¿ØÊý¾Ý ¡£Ïêϸ´úÂëʾÀýÈçÏ£º

from prometheus_api_client import PrometheusConnect

prometheus = PrometheusConnect(url='http://localhost:9090')

def get_container_cpu_usage(container_id):
    query = 'sum(rate(container_cpu_usage_seconds_total{container_label_com_docker_swarm_service_id="%s"}[5m]))' % (container_id)
    result = prometheus.custom_query(query)
    return result['data']['result']

if __name__ == '__main__':
    container_id = 'CONTAINER_ID'
    cpu_usage = get_container_cpu_usage(container_id)
    print(cpu_usage)

µÇ¼ºó¸´ÖÆ

Èý¡¢¸æ¾¯´¦Öóͷ£

ʹÓÃDocker Stats APIºÍÓʼþ¸æ¾¯

ʹÓÃDocker Stats API»ñÈ¡ÈÝÆ÷µÄ¼à¿ØÊý¾Ý £¬²¢Æ¾Ö¤ÎÒÃÇÉ趨µÄãÐÖµ¾ÙÐи澯´¦Öóͷ£ ¡£ÈôÊÇÈÝÆ÷µÄijÏîÖ¸±êÁè¼ÝÁËÉ趨µÄãÐÖµ £¬ÎÒÃÇ¿ÉÒÔͨ¹ýÓʼþ·¢Ë͸澯ÐÅÏ¢ ¡£

Ïêϸ´úÂëʾÀýÈçÏ£º

import docker
import smtplib
from email.mime.text import MIMEText

client = docker.DockerClient(base_url='unix://var/run/docker.sock')

def monitor_container(container_id):
    container = client.containers.get(container_id)
    stats = container.stats(stream=False)
    
    # ¼ì²éij¸öÖ¸±êÊÇ·ñÁè¼ÝãÐÖµ£¬ÕâÀïÒÔCPUʹÓÃÂÊΪÀý
    cpu_usage = stats['cpu_stats']['cpu_usage']['total_usage']
    cpu_limit = stats['cpu_stats']['cpu_usage']['percpu_usage'].size
    cpu_usage_percent = cpu_usage / cpu_limit * 100

    if cpu_usage_percent > 80:
        send_alert_email(container_id, cpu_usage_percent)

def send_alert_email(container_id, cpu_usage_percent):
    msg = MIMEText('ÈÝÆ÷ %s µÄCPUʹÓÃÂÊÁè¼Ý80%%£¬Ä¿½ñʹÓÃÂÊΪ%.2f%%' % (container_id, cpu_usage_percent), 'plain', 'utf-8')
    msg['Subject'] = 'ÈÝÆ÷¸æ¾¯'
    msg['From'] = 'alert@example.com'
    msg['To'] = 'admin@example.com'
    
    server = smtplib.SMTP('smtp.example.com')
    server.login('username', 'password')
    server.sendmail('alert@example.com', ['admin@example.com'], msg.as_string())
    server.quit()

if __name__ == '__main__':
    monitor_container('CONTAINER_ID')

µÇ¼ºó¸´ÖÆ

ʹÓÃPrometheusºÍAlertmanager

PrometheusÌṩÁËÒ»¸öÃûΪAlertmanagerµÄ×é¼þ £¬ÓÃÓÚ´¦Öóͷ£ºÍ·¢Ë͸澯֪ͨ ¡£ÎÒÃÇ¿ÉÒÔʹÓÃËüÀ´¼à¿ØÈÝÆ÷µÄÖ¸±ê²¢Æ¾Ö¤É趨µÄ¹æÔò·¢ËÍÏìÓ¦µÄ¸æ¾¯Í¨Öª ¡£

Ïêϸ´úÂëʾÀýÂÔ ¡£

ËÄ¡¢×ܽá

±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄ¼à¿ØºÍ¸æ¾¯´¦Öóͷ£ £¬²¢¸ø³öÁËÏêϸµÄ´úÂëʾÀý ¡£ÈÝÆ÷µÄ¼à¿ØºÍ¸æ¾¯´¦Öóͷ£¹ØÓÚ°ü¹ÜÈÝÆ÷ÔËÐеÄÎȹÌÐԺͿɿ¿ÐÔºÜÊÇÖ÷Òª £¬Ï£Íû±¾ÎĶÔÄúÓÐËù×ÊÖú ¡£

ÒÔÉϾÍÊÇÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄ¼à¿ØºÍ¸æ¾¯´¦Öóͷ£µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

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

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

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿