ÔõÑùʹÓÃDocker¾ÙÐжà½Úµã¼¯ÈºµÄÖÎÀíºÍÀ©ÈÝ
ÔÚµ±½ñÔÆÅÌËãʱ´ú£¬ÈÝÆ÷»¯ÊÖÒÕÒѾ³ÉΪ¿ªÔ´½ç×îÊܽӴýµÄÊÖÒÕÖ®Ò»¡£DockerµÄ·ºÆðʹµÃÔÆÅÌËã±äµÃÔ½·¢±ã½Ý¡¢¸ßЧ£¬³ÉΪÁË¿ª·¢Ö°Ô±¡¢ÔËάְԱ²»¿É»òȱµÄ¹¤¾ß¡£¶ø¶à½Úµã¼¯ÈºÊÖÒÕµÄÓ¦ÓøüÊÇÔÚDockerµÄ»ù´¡Éϱ»ÆÕ±éʹÓá£Í¨¹ý¶à½Úµã¼¯Èº°²ÅÅ£¬ÎÒÃÇ¿ÉÒÔÔ½·¢ÓÐÓõØʹÓÃ×ÊÔ´£¬Ìá¸ß¿É¿¿ÐԺͿÉÀ©Õ¹ÐÔ£¬Í¬Ê±Ò²ÄÜÔ½·¢ÎÞаµØ¾ÙÐа²ÅźÍÖÎÀí¡£½ÓÏÂÀ´£¬ÎÒÃǽ«Îª¸÷ÈËÏÈÈÝÔõÑùʹÓÃDocker¾ÙÐжà½Úµã¼¯ÈºµÄÖÎÀíºÍÀ©ÈÝ¡£
µÚÒ»²½£º´î½¨¶à½Úµã¼¯ÈºÇéÐÎ
ΪÁËʵÏÖ¶à½Úµã¼¯ÈºµÄÖÎÀíºÍÀ©ÈÝ£¬ÎÒÃÇÐèÒªÏȴ¶à¸ö½Úµã¡£±¾ÎÄÖÐÎÒÃǽÓÄɵÄÊÇDocker SwarmÊÖÒÕ£¬Ëü¿ÉÒÔ×ÊÖúÎÒÃÇÔ½·¢Àû±ãµØʵÏÖ¶à½Úµã¼¯ÈºµÄÖÎÀí¡£
1£©ÔÚ¶ą̀»úеÉÏ×°ÖÃDockerºÍDocker Compose£¬²¢È·±£ËüÃÇÖ®¼ä¿ÉÒÔÏ໥ͨѶ¡£
2£©Ñ¡ÔñÆäÖÐһ̨»úе×÷Ϊ¡°ÖÎÀí½Úµã¡±£¬ÔËÐÐÒÔÏÂÏÂÁ
docker swarm init --advertise-addr <ÖÎÀí½ÚµãIP>
µÇ¼ºó¸´ÖÆ
Ö´ÐÐÍê±Ïºó£¬»á·µ»ØÒ»¸ötoken£¬Õâ¸ötokenÊÇÓÃÀ´½«ÆäËû½Úµã¼ÓÈëµ½Õâ¸öSwarmÖеġ£Í¬Ê±£¬Ö´ÐÐÍêÕâ¸öÏÂÁîºó£¬ÄãµÄ±¾»ú¾Í³ÉΪÁËSwarmµÄÖÎÀí½Úµã¡£
3£©½«ÆäËû»úе¼ÓÈëµ½SwarmÖС£ÔÚÆäËû»úеÉÏÖ´ÐÐÒÔÏÂÏÂÁ
docker swarm join --token <token> <ÖÎÀí½ÚµãIP>:2377
µÇ¼ºó¸´ÖÆ
´Ëʱ£¬ÆäËû»úе¾Í»á±»¼ÓÈëµ½SwarmÖС£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÉó²éSwarmÖеĽڵ㣺
docker node ls
µÇ¼ºó¸´ÖÆ
µÚ¶þ²½£º¹¹½¨Ð§ÀͲ¢À©ÈÝ
ÔڴºÜ¶à¶àÉٽڵ㼯ȺÇéÐÎÖ®ºó£¬ÎÒÃÇ¿ÉÒÔ×îÏȹ¹½¨Ð§ÀÍÁË¡£Ð§ÀÍÊÇÖ¸Ò»×éÔËÐÐÔÚSwarmÖеÄÈÝÆ÷ʵÀý£¬ËüÃǹ²ÏíÖ÷»ú¡¢ÍøÂçºÍ´æ´¢×ÊÔ´£¬¿ÉÒÔÒÔˮƽÀ©Õ¹µÄ·½·¨×Ô¶¯Ëõ·Å¡£ÔÚÕâÀÎÒÃÇÒÔÒ»¸ö¼òÆÓµÄwebÓ¦ÓÃ×÷ΪÀý×Ó£¬ÏÈÈÝÔõÑù¹¹½¨ºÍÀ©ÈÝЧÀÍ¡£
1£©±àдDockerfile¡£Ê×ÏÈ£¬ÎÒÃÇÐèÒª±àдDockerfileÎļþ£¬Ö¸¶¨Ó¦ÓóÌÐòµÄ»ù´¡¾µÏñºÍÔËÐÐÏÂÁîµÈ¡£
FROM node:latest WORKDIR /app COPY . /app RUN npm install CMD ["npm", "start"]
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬ÎÒÃÇÑ¡ÔñÁËnode:latest×÷Ϊ»ù´¡¾µÏñ£¬½«×ðÁú¿Ê±Ó¦ÓóÌÐòÎļþ¼ÓÈë/appĿ¼£¬×°ÖÃÒÀÀµ²¢Æô¶¯³ÌÐò¡£
2£©±àдdocker-compose.yml¡£docker-composeÊÇÒ»¸öÓÃÓÚ½ç˵ºÍÔËÐжàÈÝÆ÷DockerÓ¦ÓõŤ¾ß¡£ÔÚÕâÀÎÒÃÇ¿ÉÒÔʹÓÃdocker-compose½ç˵һ¸öÃûΪwebµÄЧÀÍ¡£
version: '3' services: web: build: . ports: - "8080:8080" environment: - NODE_ENV=production deploy: replicas: 5 resources: limits: cpus: "0.1" memory: 50M restart_policy: condition: on-failure
µÇ¼ºó¸´ÖÆ
Õâ¸ödocker-composeÎļþÖ¸¶¨ÁË×ðÁú¿Ê±Ó¦ÓóÌÐò¹¹½¨ÒªÁì¡¢¶Ë¿ÚÓ³Éä¡¢ÇéÐαäÁ¿¡¢¸±±¾ÊýÄ¿ºÍ×ÊÔ´ÏÞÖƵȡ£ÔÚ°ì·¨ÈýÖÐÎÒÃǽ«Ê¹ÓÃdocker stack deployÏÂÁîÀ´¹¹½¨²¢ÔËÐÐÕâ¸öЧÀÍ¡£
3£©¹¹½¨²¢À©ÈÝЧÀÍ¡£ÔÚÍê³ÉÒÔÉÏÁ½²½Ö®ºó£¬ÎÒÃÇ¿ÉÒÔ×îÏȹ¹½¨²¢À©ÈÝ×ðÁú¿Ê±Ð§ÀÍÁË¡£ÇëÖ´ÐÐÒÔÏÂÏÂÁ
docker-compose build docker stack deploy --compose-file docker-compose.yml web
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁî»á½«×ðÁú¿Ê±Ð§ÀÍ°²Åŵ½SwarmÖУ¬²¢Ö¸¶¨5¸öʵÀý¡£ÔÚä¯ÀÀÆ÷Öлá¼ûhttp:// :8080£¬¾Í¿ÉÒÔÉó²éµ½×ðÁú¿Ê±webÓ¦ÓÃÁË¡£
µÚÈý²½£ºÖÎÀíºÍÀ©ÈÝЧÀÍ
ÔÚÀֳɹ¹½¨²¢°²ÅÅЧÀÍÖ®ºó£¬ÎÒÃÇÐèÒª¶ÔÆä¾ÙÐÐÖÎÀíºÍÀ©ÈÝ¡£ÔÚSwarmÖоÙÐÐЧÀÍÖÎÀíºÍÀ©ÈݺÜÊǼòÆÓ£¬Ö»ÐèÒªÖ´ÐÐһЩÏÂÁî¼´¿É¡£
1£©Éó²éЧÀÍ״̬¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´Éó²éЧÀ͵Ä״̬£º
docker service ls docker service ps web
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁÏÔʾËùÓÐÔËÐÐÖеÄЧÀͺÍЧÀÍʵÀý¡£´Ëʱ£¬ÎÒÃÇÓ¦¸Ã¿ÉÒÔ¿´µ½5¸öwebЧÀÍʵÀýÕýÔÚÔËÐС£
2£©À©ÈÝЧÀÍ¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´À©ÈÝЧÀ͵ÄʵÀýÊýÄ¿£º
docker service scale web=8
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ°ÑwebЧÀ͵ÄʵÀýÊýÄ¿ÔöÌíµ½8¸ö¡£
3£©¸üÐÂЧÀÍ¡£ÎªÁ˸üÐÂЧÀÍ£¬ÎÒÃÇÐèÒªÐÞ¸Ä֮ǰµÄdocker-compose.ymlÎļþ£¬²¢ÔÙ´ÎÔËÐÐdocker stack deployÏÂÁî¡£ÔÚÐÞ¸ÄÍê³Éºó£¬Ö´ÐÐÒÔÏÂÏÂÁî¼´¿É¸üÐÂЧÀÍ£º
docker stack deploy --compose-file docker-compose.yml web
µÇ¼ºó¸´ÖÆ
×ܽá
¶à½Úµã¼¯ÈºÊÖÒÕÊÇDockerʹÓÃÖкÜÊÇÊÊÓõÄÊÖÒÕ£¬Äܹ»×ÊÖúÎÒÃÇÔ½·¢Àû±ãµØÖÎÀíºÍÀ©Õ¹×ðÁú¿Ê±Ó¦ÓóÌÐò¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÔõÑùʹÓÃDocker Swarm¹¹½¨¶à½Úµã¼¯ÈºÇéÐΣ¬²¢ÇÒÒÔÒ»¸öwebÓ¦ÓóÌÐòΪÀýÑÝʾÁËÔõÑù¹¹½¨¡¢°²ÅÅ¡¢ÖÎÀíºÍÀ©ÈÝЧÀÍ¡£ÐÅÍÐͨ¹ý±¾ÎĵÄÏÈÈÝ£¬¸÷ÈËÒѾ¶ÔDocker Swarm¶à½Úµã¼¯ÈºÊÖÒÕÓÐÁËÔ½·¢ÉîÈëµÄÏàʶ¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃDocker¾ÙÐжà½Úµã¼¯ÈºµÄÖÎÀíºÍÀ©ÈݵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡