LinuxºÍDocker£ºÔõÑùʵÏÖÈÝÆ÷µÄ¿çÖ÷»úǨáãºÍͬ²½£¿
linuxºÍdocker£ºÔõÑùʵÏÖÈÝÆ÷µÄ¿çÖ÷»úǨáãºÍͬ²½£¿
ÕªÒª£ºDockerÊÇÒ»ÖÖÊ¢ÐеÄÈÝÆ÷»¯ÊÖÒÕ£¬ËüÌṩÁËÒ»ÖÖÇáÁ¿¼¶µÄÐéÄ⻯½â¾ö¼Æ»®¡£ÔÚ¶àÖ÷»úÇéÐÎÖУ¬ÊµÏÖÈÝÆ÷µÄ¿çÖ÷»úǨáãºÍͬ²½ÊǷǾ³£¼ûµÄÐèÇó¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃLinuxºÍDockerÀ´ÊµÏÖÈÝÆ÷µÄ¿çÖ÷»úǨáãºÍͬ²½£¬²¢ÌṩһЩʾÀý´úÂ빩²Î¿¼¡£
СÐò
ÈÝÆ÷»¯ÊÖÒÕµÄÐËÆðʹµÃÓ¦Óð²ÅźÍǨáã±äµÃÔ½·¢ÎÞаºÍ¸ßЧ¡£ÔÚ¶àÖ÷»úÇéÐÎÖУ¬ÈÝÆ÷µÄ¿çÖ÷»úǨáãºÍͬ²½ÊDZز»¿ÉÉٵĹ¦Ð§£¬¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖ¸ºÔØƽºâ¡¢¸ß¿ÉÓÃÐÔÒÔ¼°×ÊÔ´µÄ×îÓÅʹÓá£LinuxÌṩÁËһЩ¹¤¾ßºÍ¹¦Ð§À´Ö§³ÖÈÝÆ÷µÄ¿çÖ÷»úǨáãºÍͬ²½£¬¶øDockerÔò»ùÓÚLinuxÌṩÁËÔ½·¢±ã½ÝµÄ·½·¨À´ÖÎÀíºÍ²Ù×÷ÈÝÆ÷¡£
LinuxµÄÈÝÆ÷ǨáãºÍͬ²½
ÔÚLinuxÖУ¬ÈÝÆ÷µÄǨáãºÍͬ²½Ö÷ÒªÒÀÀµÓÚÁ½ÏîÊÖÒÕ£ºÇ¨áã´æ´¢ºÍÍøÂ硣Ǩáã´æ´¢ÊÇÖ¸½«ÈÝÆ÷µÄÊý¾ÝºÍÎļþϵͳ´ÓÔ´Ö÷»úǨá㵽ĿµÄÖ÷»úµÄÀú³Ì£¬¶øÍøÂçÔòÊÇÔÚÈÝÆ÷ǨáãÀú³ÌÖмá³ÖÍøÂçÅþÁ¬µÄÒªº¦¡£
2.1 Ǩáã´æ´¢
¹ØÓÚÈÝÆ÷µÄǨáã´æ´¢£¬Óм¸ÖÖ³£ÓõÄÊÖÒտɹ©Ñ¡Ôñ£¬Èç¹Å°åµÄ¸´ÖƺÍͬ²½Îļþϵͳ¡¢ÂþÑÜʽÎļþϵͳÒÔ¼°ÂþÑÜʽ¿é´æ´¢¡£¸´ÖƺÍͬ²½ÎļþϵͳÊÇ×î³£¼ûµÄ·½·¨£¬ËüÃÇÊÊÓÃÓÚС¹æÄ£ÇéÐΣ¬µ«ÔÚ´ó¹æÄ£ÇéÐÎÖпÉÄ᷺ܻÆðÐÔÄÜÆ¿¾±¡£ÂþÑÜʽÎļþϵͳºÍÂþÑÜʽ¿é´æ´¢Ôò¿ÉÒÔÌṩ¸ü¸ßµÄÐÔÄܺͿÉÀ©Õ¹ÐÔ£¬µ«ÉèÖúÍÖÎÀíÏà¶ÔÖØ´ó¡£ÔÚÕâÀÎÒÃǽ«ÒÔ¸´ÖƺÍͬ²½ÎļþϵͳΪÀý¾ÙÐÐ˵Ã÷¡£
¼ÙÉèÎÒÃÇÓÐÁ½Ì¨Ö÷»ú£¬Ô´Ö÷»úºÍÄ¿µÄÖ÷»ú£¬Òª½«ÈÝÆ÷´ÓÔ´Ö÷»úǨá㵽ĿµÄÖ÷»ú£¬ÎÒÃÇ¿ÉÒÔÖ´ÐÐÒÔÏ°취£º
°ì·¨1£º×èÖ¹ÈÝÆ÷ÔÚÔ´Ö÷»úÉϵÄÔËÐС£
$ docker stop container_id
µÇ¼ºó¸´ÖÆ
°ì·¨2£ºµ¼³öÈÝÆ÷µÄÎļþϵͳ¡£
$ docker export container_id > container.tar
µÇ¼ºó¸´ÖÆ
°ì·¨3£º½«ÈÝÆ÷µÄÎļþϵͳ´«Ê䵽ĿµÄÖ÷»ú¡£
$ scp container.tar user@target_host:/path/
µÇ¼ºó¸´ÖÆ
°ì·¨4£ºÔÚÄ¿µÄÖ÷»úÉϵ¼ÈëÈÝÆ÷µÄÎļþϵͳ¡£
$ docker import /path/container.tar
µÇ¼ºó¸´ÖÆ
°ì·¨5£ºÆô¶¯ÈÝÆ÷ÔÚÄ¿µÄÖ÷»úÉÏÔËÐС£
$ docker run -d --name container_name image_name
µÇ¼ºó¸´ÖÆ
2.2 ÍøÂçͬ²½
ÔÚÈÝÆ÷ǨáãÀú³ÌÖУ¬¼á³ÖÍøÂçÅþÁ¬µÄÎȹÌÐÔÊǺÜÊÇÖ÷ÒªµÄ¡£LinuxÌṩÁËһЩ¹¤¾ßºÍÊÖÒÕÀ´ÊµÏÖÍøÂçµÄͬ²½£¬Èçiptables¹æÔò¡¢ÍøÂçÃüÃû¿Õ¼äºÍmacvlan¡£ÏêϸµÄʵÏÖ·½·¨È¡¾öÓÚÍøÂç¼Ü¹¹ºÍÐèÇó¡£
¼ÙÉèÎÒÃÇÓÐÁ½Ì¨Ö÷»ú£¬Ô´Ö÷»úºÍÄ¿µÄÖ÷»ú£¬Òª¼á³ÖÈÝÆ÷ÔÚǨáãÀú³ÌÖеÄÍøÂçÅþÁ¬£¬ÎÒÃÇ¿ÉÒÔÖ´ÐÐÒÔÏ°취£º
°ì·¨1£ºÔÚÔ´Ö÷»úÉϽ¨ÉèÍøÂçÃüÃû¿Õ¼ä£¬²¢½«ÈÝÆ÷µÄÍøÂç½Ó¿ÚÒƶ¯µ½¸ÃÃüÃû¿Õ¼ä¡£
$ ip link set dev eth0 netns container_ns
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
°ì·¨2£ºÔÚÄ¿µÄÖ÷»úÉϽ¨ÉèÍøÂçÃüÃû¿Õ¼ä£¬²¢½«ÈÝÆ÷µÄÍøÂç½Ó¿ÚÒƶ¯µ½¸ÃÃüÃû¿Õ¼ä¡£
$ ip link set dev eth0 netns container_ns
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
°ì·¨3£ºÔÚÔ´Ö÷»úÉÏÉèÖÃiptables¹æÔò£¬½«ÈÝÆ÷µÄÍøÂçÁ÷Á¿Öض¨Ïòµ½Ä¿µÄÖ÷»ú¡£
$ iptables -t nat -A PREROUTING -i eth0 -j DNAT --to-destination target_ip
µÇ¼ºó¸´ÖÆ
°ì·¨4£ºÔÚÄ¿µÄÖ÷»úÉÏÆô¶¯ÈÝÆ÷µÄÍøÂç½Ó¿Ú¡£
$ ip link set dev eth0 up
µÇ¼ºó¸´ÖÆ
°ì·¨5£ºÔÚÄ¿µÄÖ÷»úÉÏÉèÖÃiptables¹æÔò£¬½«ÈÝÆ÷µÄÍøÂçÁ÷Á¿×ª·¢µ½ÈÝÆ÷µÄÍøÂçÃüÃû¿Õ¼ä¡£
$ iptables -t nat -A PREROUTING -i eth0 -j DNAT --to-destination container_ip
µÇ¼ºó¸´ÖÆ
DockerµÄÈÝÆ÷ǨáãºÍͬ²½
ÔÚDockerÖУ¬ÈÝÆ÷µÄ¿çÖ÷»úǨáãºÍͬ²½Ö÷ÒªÒÀÀµÓÚDocker Swarm¡£Docker SwarmÊÇÒ»¸öÔÉúµÄDocker¼¯ÈººÍ±àÅŹ¤¾ß£¬Ëü¿ÉÒÔÖÎÀíºÍµ÷Àí¿ç¶à¸öÖ÷»úµÄÈÝÆ÷¡£
¼ÙÉèÎÒÃÇÒѾÉèÖúÃÁËDocker Swarm£¬ÔÚ¶àÖ÷»úÇéÐÎÖÐʵÏÖÈÝÆ÷µÄ¿çÖ÷»úǨáãºÍͬ²½¿ÉÒÔÖ´ÐÐÒÔÏ°취£º
°ì·¨1£º½«ÈÝÆ÷¼ÓÈëµ½Docker Swarm¼¯Èº¡£
$ docker swarm join --token SWMTKN-abcdefg1234567890 manager_ip:2377
µÇ¼ºó¸´ÖÆ
°ì·¨2£ºÔÚÔ´Ö÷»úÉϱê¼ÇÈÝÆ÷£¬ÒÔָʾËüÐèÒª¿çÖ÷»úǨáã¡£
$ docker service update --label-add com.docker.ucp.mesh.http.ports=80 container_name
µÇ¼ºó¸´ÖÆ
°ì·¨3£ºÔÚÄ¿µÄÖ÷»úÉÏÆô¶¯Í¬ÃûµÄЧÀÍ¡£
$ docker service create --name container_name image_name
µÇ¼ºó¸´ÖÆ
°ì·¨4£ºDocker Swarm»á×Ô¶¯½«Ô´Ö÷»úÉϵÄÈÝÆ÷Ǩá㵽ĿµÄÖ÷»úÉÏ¡£
½áÂÛ
ͨ¹ýLinuxºÍDocker£¬ÎÒÃÇ¿ÉÒÔºÜÊÇÀû±ãµØʵÏÖÈÝÆ÷µÄ¿çÖ÷»úǨáãºÍͬ²½¡£ÎÞÂÛÊÇÔÚLinuxÇéÐÎÏÂÖ±½ÓʹÓø´ÖƺÍͬ²½Îļþϵͳ£¬ÕÕ¾ÉʹÓÃDocker SwarmÀ´ÖÎÀíºÍµ÷Àí¿çÖ÷»úµÄÈÝÆ÷£¬¶¼¿ÉÒÔÖª×ãÎÒÃÇÔÚ¶àÖ÷»úÇéÐÎÖеÄÐèÇó¡£Ï£Íû±¾ÎĶԶÁÕßÔÚʵÏÖÈÝÆ÷µÄ¿çÖ÷»úǨáãºÍͬ²½·½ÃæÌṩÁËһЩ×ÊÖú¡£
²Î¿¼ÎÄÏ×£º
Docker documentation: https://docs.docker.com/
Linux documentation: https://www.kernel.org/doc/html/latest/
¸½Â¼£º´úÂëʾÀý
# ʾÀý´úÂë1£º¸´ÖƺÍͬ²½Îļþϵͳ # °ì·¨1£º×èÖ¹ÈÝÆ÷ÔÚÔ´Ö÷»úÉϵÄÔËÐС£ $ docker stop container_id # °ì·¨2£ºµ¼³öÈÝÆ÷µÄÎļþϵͳ¡£ $ docker export container_id > container.tar # °ì·¨3£º½«ÈÝÆ÷µÄÎļþϵͳ´«Ê䵽ĿµÄÖ÷»ú¡£ $ scp container.tar user@target_host:/path/ # °ì·¨4£ºÔÚÄ¿µÄÖ÷»úÉϵ¼ÈëÈÝÆ÷µÄÎļþϵͳ¡£ $ docker import /path/container.tar # °ì·¨5£ºÆô¶¯ÈÝÆ÷ÔÚÄ¿µÄÖ÷»úÉÏÔËÐС£ $ docker run -d --name container_name image_name # ʾÀý´úÂë2£ºÍøÂçͬ²½ # °ì·¨1£ºÔÚÔ´Ö÷»úÉϽ¨ÉèÍøÂçÃüÃû¿Õ¼ä£¬²¢½«ÈÝÆ÷µÄÍøÂç½Ó¿ÚÒƶ¯µ½¸ÃÃüÃû¿Õ¼ä¡£ $ ip link set dev eth0 netns container_ns # °ì·¨2£ºÔÚÄ¿µÄÖ÷»úÉϽ¨ÉèÍøÂçÃüÃû¿Õ¼ä£¬²¢½«ÈÝÆ÷µÄÍøÂç½Ó¿ÚÒƶ¯µ½¸ÃÃüÃû¿Õ¼ä¡£ $ ip link set dev eth0 netns container_ns # °ì·¨3£ºÔÚÔ´Ö÷»úÉÏÉèÖÃiptables¹æÔò£¬½«ÈÝÆ÷µÄÍøÂçÁ÷Á¿Öض¨Ïòµ½Ä¿µÄÖ÷»ú¡£ $ iptables -t nat -A PREROUTING -i eth0 -j DNAT --to-destination target_ip # °ì·¨4£ºÔÚÄ¿µÄÖ÷»úÉÏÆô¶¯ÈÝÆ÷µÄÍøÂç½Ó¿Ú¡£ $ ip link set dev eth0 up # °ì·¨5£ºÔÚÄ¿µÄÖ÷»úÉÏÉèÖÃiptables¹æÔò£¬½«ÈÝÆ÷µÄÍøÂçÁ÷Á¿×ª·¢µ½ÈÝÆ÷µÄÍøÂçÃüÃû¿Õ¼ä¡£ $ iptables -t nat -A PREROUTING -i eth0 -j DNAT --to-destination container_ip # ʾÀý´úÂë3£ºDocker SwarmµÄÈÝÆ÷ǨáãºÍͬ²½ # °ì·¨1£º½«ÈÝÆ÷¼ÓÈëµ½Docker Swarm¼¯Èº¡£ $ docker swarm join --token SWMTKN-abcdefg1234567890 manager_ip:2377 # °ì·¨2£ºÔÚÔ´Ö÷»úÉϱê¼ÇÈÝÆ÷£¬ÒÔָʾËüÐèÒª¿çÖ÷»úǨáã¡£ $ docker service update --label-add com.docker.ucp.mesh.http.ports=80 container_name # °ì·¨3£ºÔÚÄ¿µÄÖ÷»úÉÏÆô¶¯Í¬ÃûµÄЧÀÍ¡£ $ docker service create --name container_name image_name # °ì·¨4£ºDocker Swarm»á×Ô¶¯½«Ô´Ö÷»úÉϵÄÈÝÆ÷Ǩá㵽ĿµÄÖ÷»úÉÏ¡£
µÇ¼ºó¸´ÖÆ
£¨×Ü×ÖÊý£ºÔÎÄÔ¼1306×Ö£¬Ê¾Àý´úÂë607×Ö£©
ÒÔÉϾÍÊÇLinuxºÍDocker£ºÔõÑùʵÏÖÈÝÆ÷µÄ¿çÖ÷»úǨáãºÍͬ²½£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡