NginxЧÀÍÆ÷µÄÈÝÆ÷»¯°²Åźͼ¯ÈºÖÎÀíÏêϸָÄÏ
nginxЧÀÍÆ÷µÄÈÝÆ÷»¯°²Åźͼ¯ÈºÖÎÀíÏêϸָÄÏ
СÐò£º
Ëæ×ÅÔÆÅÌËãºÍÈÝÆ÷ÊÖÒÕµÄÉú³¤£¬ÈÝÆ÷»¯°²ÅÅÒѳÉΪÆóÒµÓ¦Óÿª·¢ºÍ°²Åŵij£¼û·½·¨¡£Nginx×÷Ϊһ¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬Ò²¿ÉÒÔͨ¹ýÈÝÆ÷»¯À´ÊµÏÖ°²ÅźÍÖÎÀí¡£±¾ÎĽ«ÏêϸÏÈÈÝÔõÑù½«NginxЧÀÍÆ÷¾ÙÐÐÈÝÆ÷»¯°²ÅÅ£¬²¢Í¨¹ý¼¯ÈºÖÎÀíÀ´Ìá¸ß¸ß¿ÉÓÃÐÔ¡£
Ò»¡¢×¼±¸ÊÂÇé
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃDockerÇéÐΣ¬²¢ÇÒÈ·±£Æô¶¯DockerЧÀÍ¡£½Ó×Å£¬ÎÒÃÇÐèÒª±àдDockerfileÎļþ£¬ÓÃÓÚ¹¹½¨NginxµÄDocker¾µÏñ¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄDockerfileʾÀý£º
FROM nginx:latest COPY nginx.conf /etc/nginx/nginx.conf COPY default.conf /etc/nginx/conf.d/default.conf EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
µÇ¼ºó¸´ÖÆ
Õâ¸öDockerfileÊ×ÏÈÑ¡ÔñÁË×îеÄNginx¾µÏñ×÷Ϊ»ù´¡¾µÏñ£¬È»ºó¸´ÖÆÁËÎÒÃÇÊÂÏÈ×¼±¸ºÃµÄNginxÉèÖÃÎļþºÍĬÈϵÄÐéÄâÖ÷»úÉèÖÃÎļþ¡£×îºó£¬Ì»Â¶ÁËÈÝÆ÷µÄ80¶Ë¿Ú£¬²¢ÇÒÒÔǰ̨ģʽÔËÐÐNginxЧÀÍÆ÷¡£
¶þ¡¢¹¹½¨Docker¾µÏñ
ÔÚ×¼±¸ºÃDockerfileºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃdocker buildÏÂÁîÀ´¹¹½¨Docker¾µÏñ¡£¼ÙÉèÎÒÃǽ«DockerfileÉúÑÄÔÚÄ¿½ñĿ¼Ï£¬¿ÉÒÔͨ¹ýÏÂÃæµÄÏÂÁî¾ÙÐй¹½¨²Ù×÷£º
docker build -t my_nginx .
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁî»áƾ֤Dockerfile¹¹½¨³öÒ»¸öÃûΪmy_nginxµÄDocker¾µÏñ¡£¹¹½¨Íê³Éºó£¬¿ÉÒÔͨ¹ýdocker imagesÏÂÁîÀ´Éó²éÒÑÓеľµÏñÁÐ±í£¬È·ÈÏmy_nginx¾µÏñÒѾÀֳɹ¹½¨¡£
Èý¡¢ÔËÐе¥¸öNginxÈÝÆ÷
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔ»ùÓÚmy_nginx¾µÏñÀ´½¨ÉèÒ»¸öNginxÈÝÆ÷£¬²¢ÔËÐÐÆðÀ´¡£¿ÉÒÔʹÓÃdocker runÏÂÁîÀ´Ö´ÐÐÕâ¸ö²Ù×÷£º
docker run -d -p 80:80 my_nginx
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁî»áÔÚºǫ́ÔËÐÐÒ»¸öеÄNginxÈÝÆ÷£¬²¢½«ÈÝÆ÷µÄ80¶Ë¿ÚÓ³Éäµ½Ö÷»úµÄ80¶Ë¿Ú¡£¿ÉÒÔͨ¹ýä¯ÀÀÆ÷»á¼ûhttp://localhostÀ´ÑéÖ¤NginxЧÀÍÆ÷ÊÇ·ñÕý³£ÊÂÇé¡£
ËÄ¡¢¹¹½¨Nginx¼¯Èº
ΪÁËÌá¸ßNginxЧÀÍÆ÷µÄ¸ß¿ÉÓÃÐÔ£¬ÎÒÃÇ¿ÉÒÔʹÓÃDockerµÄ¼¯ÈºÖÎÀí¹¤¾ßÀ´¹¹½¨Ò»¸öNginx¼¯Èº¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇʹÓÃDocker SwarmÀ´ÊµÏÖ¼¯ÈºÖÎÀí¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒª³õʼ»¯Ò»¸öSwarmÖÎÀí½Úµã¡£¿ÉÒÔͨ¹ýÏÂÃæµÄÏÂÁîÀ´½«Ä¿½ñ½ÚµãÉèÖÃΪSwarmÖÎÀí½Úµã£º
docker swarm init
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÏÂÃæµÄÏÂÁÉèÁ½¸öÊÂÇé½Úµã£¨Ö÷»ú£©£º
docker swarm join-token worker
µÇ¼ºó¸´ÖÆ
ÔËÐÐÒÔÉÏÏÂÁîºó£¬»áÌìÉúÒ»¸öÀàËÆÏÂÃæµÄÊä³ö£º
docker swarm join --token xxxxxxxxxxxxxxxx
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÎÒÃÇÐèҪʹÓÃÕâ¸öÊä³öÀ´½«Á½¸öÊÂÇé½Úµã¼ÓÈëµ½Swarm¼¯ÈºÖУº
docker swarm join --token xxxxxxxxxxxxxxxx
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÕâÑù£¬ÎÒÃǾÍÀֳɽ«Á½¸öÊÂÇé½Úµã¼ÓÈëµ½Swarm¼¯ÈºÖС£½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öNginxЧÀÍ¡£¿ÉÒÔʹÓÃÏÂÃæµÄÏÂÁîÀ´½¨ÉèNginxЧÀÍ£º
docker service create --name nginx --replicas 3 -p 80:80 my_nginx
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁî»áÔÚ¼¯ÈºÖн¨ÉèÒ»¸öÃûΪnginxµÄЧÀÍ£¬²¢Ö¸¶¨ÁË3¸ö¸±±¾¡£Ð§ÀÍ»á×Ô¶¯ÔÚ¼¯ÈºÖеIJî±ð½ÚµãÉϽ¨ÉèºÍ·ÖÅÉÕâЩ¸±±¾£¬´Ó¶ø¹¹½¨Ò»¸öNginx¼¯Èº¡£¿ÉÒÔʹÓÃdocker service lsÏÂÁîÀ´Éó²é¼¯ÈºÖÐËùÓеÄЧÀÍÒÔ¼°Æä״̬¡£
Îå¡¢¼¯ÈºÖÎÀí²Ù×÷
Ò»µ©ÎÒÃǽ¨ÉèÁËNginx¼¯Èº£¬¾Í¿ÉÒÔ¾ÙÐÐһЩ»ù±¾µÄ¼¯ÈºÖÎÀí²Ù×÷¡£
À©ÈݺÍËõÈÝ
¿ÉÒÔͨ¹ýÏÂÃæµÄÏÂÁîÀ´ÊµÏÖ¶ÔNginxЧÀ͵ÄÀ©ÈݺÍËõÈÝ£º
docker service scale nginx=5 docker service scale nginx=2
µÇ¼ºó¸´ÖÆ
µÚÒ»¸öÏÂÁnginxЧÀ͵ĸ±±¾ÊýÀ©Õ¹Îª5¸ö£¬µÚ¶þ¸öÏÂÁ¸±±¾ÊýËõ¼õΪ2¸ö¡£
ЧÀ͸üÐÂ
µ±ÎÒÃÇÐèÒª¸üÐÂNginxµÄ¾µÏñ»òÉèÖÃÎļþʱ£¬¿ÉÒÔʹÓÃÏÂÃæµÄÏÂÁîÀ´¾ÙÐÐЧÀ͸üУº
docker service update --image my_nginx:latest nginx
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁî»á¸üÐÂnginxЧÀ͵ľµÏñΪ×îеİ汾¡£Í¬ÑùµØ£¬ÎÒÃÇÒ²¿ÉÒÔͨ¹ýdocker service updateÏÂÁîÀ´¸üÐÂЧÀ͵ÄÆäËûÉèÖòÎÊý¡£
ЧÀÍÀ©Õ¹ÐÔÖÎÀí
¿ÉÒÔͨ¹ýÏÂÃæµÄÏÂÁîÀ´Éó²éºÍÖÎÀíЧÀ͵ÄÀ©Õ¹ÐÔ£º
docker service ps nginx docker service inspect --pretty nginx
µÇ¼ºó¸´ÖÆ
µÚÒ»¸öÏÂÁî»áÏÔʾnginxЧÀ͵ÄËùÓи±±¾µÄ״̬ºÍÐÅÏ¢£¬µÚ¶þ¸öÏÂÁî»áչʾnginxЧÀ͵ÄÏêϸÐÅÏ¢£¬°üÀ¨½Úµã·ÖÅɺ͸±±¾ÔËÐÐ״̬µÈ¡£
½áÂÛ£º
ͨ¹ý½«NginxЧÀÍÆ÷¾ÙÐÐÈÝÆ÷»¯°²Åźͼ¯ÈºÖÎÀí£¬ÎÒÃÇ¿ÉÒÔʵÏÖ¸ü¸ßµÄ¿ÉÓÃÐÔºÍÎÞаÐÔ¡£±¾ÎÄÏêϸÏÈÈÝÁËʹÓÃDockerÀ´¹¹½¨Nginx¾µÏñ¡¢ÔËÐе¥¸öÈÝÆ÷£¬²¢Í¨¹ýDocker SwarmÀ´ÊµÏÖNginx¼¯ÈºµÄ´î½¨ºÍÖÎÀí¡£Ï£Íû¶ÁÕßÄܹ»Í¨¹ý±¾ÎÄѧϰµ½ÓйØNginxÈÝÆ÷»¯°²Åźͼ¯ÈºÖÎÀíµÄ֪ʶ£¬²¢Äܹ»ÔÚÏÖʵ³¡¾°ÖÐÓ¦ÓúÍÀ©Õ¹¡£
ÒÔÉϾÍÊÇNginxЧÀÍÆ÷µÄÈÝÆ÷»¯°²Åźͼ¯ÈºÖÎÀíÏêϸָÄϵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡