ÔõÑùʹÓÃDocker¹¹½¨¸ß¿ÉÀ©Õ¹µÄÂþÑÜʽϵͳ£¿
ÔõÑùʹÓÃdocker¹¹½¨¸ß¿ÉÀ©Õ¹µÄÂþÑÜʽϵͳ£¿
СÐò£º
ÔÚµ±½ñÔÆÅÌËãʱ´ú£¬¹¹½¨¸ß¿ÉÀ©Õ¹µÄÂþÑÜʽϵͳÊÇÿ¸öÈí¼þ¹¤³Ìʦ¶¼ÐèÒªÃæÁÙµÄÌôÕ½¡£¶øDocker×÷ΪһÖÖÇáÁ¿¼¶µÄÈÝÆ÷»¯ÊÖÒÕ£¬ÔÚ¹¹½¨ÂþÑÜʽϵͳ·½Ãæ¾ßÓкܴóµÄÓÅÊÆ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃDocker¹¹½¨¸ß¿ÉÀ©Õ¹µÄÂþÑÜʽϵͳ£¬²¢Ìṩ´úÂëʾÀý¡£
Docker¼ò½é£º
DockerÊÇÒ»¸ö¿ªÔ´µÄÈÝÆ÷»¯Æ½Ì¨£¬¿ÉÒÔÇáËɵؽ«Ó¦ÓóÌÐòºÍÆäÒÀÀµµÄËùÓÐ×ÊÔ´´ò°üµ½Ò»¸ö¿ÉÒÆÖ²µÄÈÝÆ÷ÖС£DockerʹÓÃÈÝÆ÷»¯ÊÖÒÕµÄÌØÕ÷£¬ÊµÏÖÁË×ÊÔ´¸ôÀë¡¢¿ìËÙ°²Åźͼò»¯ÖÎÀíµÄÓÅÊÆ¡£Ëü¿ÉÒÔʹÓòÙ×÷ϵͳ¼¶±ðµÄÐéÄ⻯ÊÖÒÕ£¬ÊµÏÖ¸ßЧµÄ×ÊԴʹÓúͿìËÙµÄÓ¦ÓóÌÐòÆô¶¯¡£
¸ß¿ÉÀ©Õ¹µÄÂþÑÜʽϵͳ¼Ü¹¹£º
Ò»¸ö¸ß¿ÉÀ©Õ¹µÄÂþÑÜʽϵͳӦ¸Ã¾ß±¸ÒÔϼ¸¸öÌص㣺
¿ÉÒÔÔöÌí¸ü¶àµÄ½ÚµãÒÔÖ§³Ö¸ü¸ßµÄ¸ºÔØ¡£
¾ß±¸×Ô¶¯»¯µÄ×ÊÔ´·ÖÅɺ͸ºÔØƽºâ»úÖÆ¡£
¿ÉÒÔƾ֤ÐèÇóÎÞаµØµ÷½âϵͳ×ÊÔ´¡£
ÔÚʹÓÃDocker¹¹½¨ÂþÑÜʽϵͳʱ£¬¿É½ÓÄÉÒÔϼܹ¹£º
ʹÓÃÒ»¸ö»ò¶à¸öÖ÷½Úµã×÷Ϊ¼¯ÖÐʽµÄÖÎÀíÆ÷£¬ÈÏÕæ·ÖÅÉʹÃüºÍ¼à¿Øϵͳ״̬¡£
ÿ¸öÊÂÇé½Úµãͨ¹ý»ñȡʹÃü²¢Ö´ÐУ¬½«Ð§¹û·µ»Ø¸øÖ÷½Úµã¡£
Ö÷½Úµã¿ÉÒÔƾ֤ϵͳ×ÊÔ´µÄ¸ºÔØÇéÐΣ¬¶¯Ì¬µØµ÷½âʹÃü·ÖÅɺÍÊÂÇé½ÚµãµÄÊýÄ¿¡£
ʹÓÃDocker¹¹½¨ÂþÑÜʽϵͳµÄ°ì·¨£º
ÏÂÃ潫ÏÈÈÝÔõÑùʹÓÃDocker¹¹½¨Ò»¸ö¼òÆÓµÄÂþÑÜʽϵͳ£¬²¢ÌṩÏìÓ¦µÄ´úÂëʾÀý¡£
°ì·¨1£º½¨ÉèDocker¾µÏñ
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öDocker¾µÏñ£¬ÓÃÓÚ¹¹½¨ÊÂÇé½Úµã¡£
FROM ubuntu:latest RUN apt-get update && apt-get install -y python3 COPY worker.py . CMD ["python3", "worker.py"]
µÇ¼ºó¸´ÖÆ
°ì·¨2£º½¨ÉèÖ÷½Úµã
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÖ÷½ÚµãÈÏÕæ·ÖÅÉʹÃüºÍ¼à¿Øϵͳ״̬¡£
import docker client = docker.from_env() # ½¨ÉèÒ»¸öÖ÷½ÚµãÈÝÆ÷ master = client.containers.run( image="master-image", detach=True, ports={ '5000/tcp': ('127.0.0.1', 5000) # ÉèÖÃÖ÷½Úµã¼àÌýµÄ¶Ë¿Ú } ) # »ñÈ¡Ö÷½ÚµãµÄIPµØµãºÍ¶Ë¿ÚºÅ ip_address = master.attrs['NetworkSettings']['IPAddress'] port = master.attrs['NetworkSettings']['Ports']['5000/tcp'][0]['HostPort'] print("Master node is running at {}:{}".format(ip_address, port))
µÇ¼ºó¸´ÖÆ
°ì·¨3£º½¨ÉèÊÂÇé½Úµã
×îºó£¬ÎÒÃÇ¿ÉÒÔ½¨Éè¶à¸öÊÂÇé½Úµã£¬ÓÃÓÚÖ´ÐÐʹÃü²¢½«Ð§¹û·µ»Ø¸øÖ÷½Úµã¡£
import docker client = docker.from_env() # ½¨ÉèÒ»¸öÊÂÇé½ÚµãÈÝÆ÷ worker = client.containers.run( image="worker-image", detach=True ) # »ñÈ¡ÊÂÇé½ÚµãµÄIPµØµã ip_address = worker.attrs['NetworkSettings']['IPAddress'] print("Worker node is running at {}".format(ip_address))
µÇ¼ºó¸´ÖÆ
°ì·¨4£ºÊµÏÖʹÃü·Ö·¢ºÍЧ¹ûÍøÂç
Ö÷½ÚµãʹÓÃËù¼àÌýµÄ¶Ë¿Ú£¬ÏòÊÂÇé½Úµã·¢ËÍʹÃü£¬²¢ÍøÂçÊÂÇé½ÚµãµÄÖ´ÐÐЧ¹û¡£
import requests # ÏòÊÂÇé½Úµã·¢ËÍʹÃü response = requests.post("http://<worker-ip>:<worker-port>/task", json={"task": "example-task"}) # ÍøÂçÊÂÇé½ÚµãµÄÖ´ÐÐЧ¹û result = requests.get("http://<worker-ip>:<worker-port>/result") print("Result: ", result.json())
µÇ¼ºó¸´ÖÆ
½áÂÛ£º
ʹÓÃDocker¹¹½¨¸ß¿ÉÀ©Õ¹µÄÂþÑÜʽϵͳ¿ÉÒÔ¼«´óµØ¼ò»¯ÏµÍ³µÄ°²ÅźÍÖÎÀí¡£Í¨¹ýºÏÀíµÄ¼Ü¹¹Éè¼ÆºÍʹÓÃDockerµÄÈÝÆ÷»¯ÊÖÒÕ£¬ÎÒÃÇ¿ÉÒÔʵÏÖµ¯ÐÔÉìËõµÄÂþÑÜʽϵͳ£¬Ìṩ¸ß¿ÉÓÃÐԺ͸ßÐÔÄܵÄЧÀÍ¡£Ï£Íû±¾ÎĹØÓÚÄÇЩÏëҪʹÓÃDocker¹¹½¨¸ß¿ÉÀ©Õ¹µÄÂþÑÜʽϵͳµÄ¶ÁÕßÓÐËù×ÊÖú¡£
²Î¿¼×ÊÁÏ£º
Docker¹Ù·½Îĵµ£ºhttps://docs.docker.com/
Docker Python SDKÎĵµ£ºhttps://docker-py.readthedocs.io/zh_CN/latest/
ÒÔÉϾÍÊÇÔõÑùʹÓÃDocker¹¹½¨¸ß¿ÉÀ©Õ¹µÄÂþÑÜʽϵͳ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡