ÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄˮƽÀ©Õ¹ºÍ¸ºÔØƽºâ
ÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄˮƽÀ©Õ¹ºÍ¸ºÔØƽºâ
СÐò£º
Ëæ×ÅÔÆÅÌËãÊÖÒÕµÄÒ»Ö±Éú³¤£¬ÈÝÆ÷»¯ÊÖÒÕÏÖÔÚÒѾ³ÉΪÁ˹¹½¨¡¢°²ÅźÍÖÎÀíÓ¦ÓóÌÐòµÄÖ÷Á÷·½·¨Ö®Ò»¡£Docker×÷ΪÏÖÔÚ×îΪʢÐеÄÈÝÆ÷»¯Æ½Ì¨£¬²»µ«ÌṩÁ˱ã½ÝµÄÓ¦ÓóÌÐò´ò°üºÍ°²ÅÅ·½·¨£¬»¹Ö§³ÖÈÝÆ÷µÄˮƽÀ©Õ¹ºÍ¸ºÔØƽºâ¡£±¾ÎĽ«ÏêϸÏÈÈÝÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄˮƽÀ©Õ¹ºÍ¸ºÔØƽºâ£¬²¢¸ø³öÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢DockerÈÝÆ÷µÄˮƽÀ©Õ¹
ÈÝÆ÷µÄˮƽÀ©Õ¹ÊÇָͨ¹ýÔöÌíÈÝÆ÷µÄÊýÄ¿À´Ìá¸ßÓ¦ÓóÌÐòµÄÍÌÍÂÁ¿ºÍ²¢·¢ÄÜÁ¦¡£DockerÌṩÁ˶àÖÖ·½·¨À´ÊµÏÖÈÝÆ÷µÄˮƽÀ©Õ¹£¬ÏÂÃæÏÈÈÝÁ½ÖÖ³£Óõķ½·¨¡£
ʹÓÃDocker ComposeʵÏÖÈÝÆ÷µÄˮƽÀ©Õ¹
Docker ComposeÊÇDocker¹Ù·½ÍƳöµÄÒ»ÖÖ¹¤¾ß£¬¿ÉÒÔͨ¹ýÒ»¸öYAMLÎļþ½ç˵¶à¸öÈÝÆ÷µÄÉèÖú͹Øϵ£¬²¢ÊµÏÖËüÃǵÄÅúÁ¿ÖÎÀí¡£Í¨¹ýÐ޸ĸÃÎļþÖеÄÈÝÆ÷ÊýÄ¿£¬¿ÉÒÔ¼òÆÓ¿ì½ÝµØʵÏÖÈÝÆ÷µÄˮƽÀ©Õ¹¡£ÏÂÃæÊÇÒ»¸öʾÀýµÄDocker ComposeÎļþ£¬ÆäÖаüÀ¨ÁËÒ»¸öWebÓ¦ÓúÍÒ»¸öÊý¾Ý¿âµÄÉèÖãº
version: '3' services: web: build: . ports: - "80:80" depends_on: - db db: image: mysql:5.7 environment: - MYSQL_ROOT_PASSWORD=root
µÇ¼ºó¸´ÖÆ
¼ÙÉèÎÒÃÇÏ£Íû½«WebÓ¦ÓõÄÈÝÆ÷ÊýÄ¿´Ó1¸öÀ©Õ¹µ½3¸ö£¬Ö»ÐèÒª½«ÉÏÊöÎļþÖеÄwebЧÀ͵ÄreplicasÊôÐÔÐÞ¸ÄΪ3¼´¿É£º
version: '3' services: web: build: . ports: - "80:80" depends_on: - db replicas: 3 db: image: mysql:5.7 environment: - MYSQL_ROOT_PASSWORD=root
µÇ¼ºó¸´ÖÆ
È»ºóʹÓÃÒÔÏÂÏÂÁîÆô¶¯ºÍÖÎÀíÈÝÆ÷£º
$ docker-compose up -d
µÇ¼ºó¸´ÖÆ
Docker Compose»á×Ô¶¯°ïÎÒÃǽ¨ÉèºÍÖÎÀí3¸öWebÓ¦ÓõÄÈÝÆ÷£¬´Ó¶øʵÏÖÁËÈÝÆ÷µÄˮƽÀ©Õ¹¡£
ʹÓÃDocker SwarmʵÏÖÈÝÆ÷µÄˮƽÀ©Õ¹
Docker SwarmÊÇDocker¹Ù·½ÌṩµÄÒ»ÖÖÈÝÆ÷±àÅźͼ¯ÈºÖÎÀí¹¤¾ß£¬Í¨¹ýÔÚ¶à¸ö½ÚµãÉϵ÷ÀíºÍÖÎÀíDockerÈÝÆ÷£¬ÊµÏÖÁËÈÝÆ÷µÄˮƽÀ©Õ¹¡£ÏÂÃæÊÇÒ»¸öʾÀýµÄDocker SwarmÉèÖÃÎļþ£º
version: '3' services: web: image: nginx:latest deploy: replicas: 3 restart_policy: condition: on-failure
µÇ¼ºó¸´ÖÆ
ͨ¹ýʹÓÃDocker SwarmÌṩµÄÏÂÁ¿ÉÒÔ¿ìËÙ½¨ÉèºÍÖÎÀíÈÝÆ÷£¬²¢ÊµÏÖÈÝÆ÷µÄˮƽÀ©Õ¹£º
$ docker stack deploy -c docker-compose.yml myapp
µÇ¼ºó¸´ÖÆ
ʹÓÃÒÔÉÏÏÂÁî¿ÉÒÔ½«ÉÏÊöÉèÖÃÎļþÖнç˵µÄwebЧÀͽ¨Éè³ÉÒ»¸öÃûΪmyappµÄЧÀÍ£¬¸ÃЧÀÍ°üÀ¨3¸öÈÝÆ÷¡£Docker Swarm»á×Ô¶¯ÔÚ¼¯ÈºÖеIJî±ð½ÚµãÉϽ¨Éè²¢ÖÎÀíÕâ3¸öÈÝÆ÷£¬ÊµÏÖÁËÈÝÆ÷µÄˮƽÀ©Õ¹¡£
¶þ¡¢DockerÈÝÆ÷µÄ¸ºÔØƽºâ
¸ºÔØƽºâÊÇÖ¸ÔÚ¶à¸öÈÝÆ÷Ö®¼äÔȳƷÖÅÉÇëÇó£¬ÒÔÌá¸ßÓ¦ÓóÌÐòµÄ´¦ÀíÄÜÁ¦ºÍ¿ÉÓÃÐÔ¡£DockerÌṩÁ˶àÖÖ·½·¨À´ÊµÏÖÈÝÆ÷µÄ¸ºÔØƽºâ£¬ÏÂÃæÏÈÈÝÁ½ÖÖ³£Óõķ½·¨¡£
ʹÓÃDockerÄÚÖõĸºÔØƽºâÆ÷
DockerÄÚÖÃÁËÒ»ÖÖ»ùÓÚround-robinËã·¨µÄ¸ºÔØƽºâÆ÷£¬¿ÉÒÔ×Ô¶¯½«ÇëÇóÔȳƵطÖÅɵ½¶à¸öÈÝÆ÷ÉÏ¡£Ö»Ð轫¶à¸öÏàͬµÄÈÝÆ÷Ó³É䵽ͳһ¸ö¶Ë¿ÚÉÏ£¬¼´¿ÉʵÏÖ¸ºÔØƽºâ¡£ÏÂÃæÊÇÒ»¸öʾÀýµÄDocker ComposeÎļþ£¬ÆäÖаüÀ¨ÁËÁ½¸öWebÓ¦ÓõÄÉèÖãº
version: '3' services: web1: build: . ports: - "8080:80" web2: build: . ports: - "8081:80"
µÇ¼ºó¸´ÖÆ
ͨ¹ýÒÔÉÏÉèÖã¬WebÓ¦ÓõÄÇëÇ󽫻ᱻÔȳƵطÖÅɵ½web1ºÍweb2ÈÝÆ÷ÉÏ¡£
ʹÓõÚÈý·½µÄÈÝÆ÷±àÅŹ¤¾ß
³ýÁËDocker×Ô´øµÄ¸ºÔØƽºâÆ÷£¬»¹¿ÉÒÔʹÓÃһЩµÚÈý·½µÄÈÝÆ÷±àÅŹ¤¾ßʵÏÖÔ½·¢Ç¿Ê¢ºÍÎÞаµÄ¸ºÔØƽºâ¡£ÀýÈ磬¿ÉÒÔʹÓÃNginx×÷Ϊ·´ÏòÊðÀíЧÀÍÆ÷£¬½«ÇëÇó·Ö·¢µ½¶à¸öÈÝÆ÷ÉÏ¡£ÏÂÃæÊÇÒ»¸öʾÀýµÄNginxÉèÖÃÎļþ£º
http { upstream backend { server web1:80; server web2:80; } server { listen 80; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏÉèÖÃÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪbackendµÄ¸ºÔØƽºâ¼¯Èº£¬°üÀ¨ÁËweb1ºÍweb2Á½¸öÈÝÆ÷¡£Nginx»áƾ֤ÉèÖý«ÇëÇóÔȳƵطַ¢µ½Á½¸öÈÝÆ÷ÉÏ¡£
½áÂÛ£º
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÎÒÃÇ¿ÉÒÔ¿´µ½£¬DockerÌṩÁ˸»ºñµÄ¹¦Ð§ºÍ¹¤¾ßÀ´ÊµÏÖÈÝÆ÷µÄˮƽÀ©Õ¹ºÍ¸ºÔØƽºâ¡£ÎÞÂÛÊÇʹÓÃDocker ComposeÕÕ¾ÉDocker Swarm£¬¶¼¿ÉÒÔÇáËɵØʵÏÖÈÝÆ÷µÄˮƽÀ©Õ¹¡£¶øͨ¹ýʹÓÃDockerÄÚÖõĸºÔØƽºâÆ÷»òÕßµÚÈý·½µÄÈÝÆ÷±àÅŹ¤¾ß£¬Ò²¿ÉÒÔʵÏÖÈÝÆ÷µÄ¸ºÔØƽºâ¡£ÕâЩ¹¦Ð§ºÍ¹¤¾ßµÄʹÓã¬Ê¹µÃÎÒÃÇ¿ÉÒÔÔ½·¢Àû±ãµØ¹¹½¨ºÍÖÎÀíÈÝÆ÷»¯µÄÓ¦ÓóÌÐò£¬Ìá¸ßÁËÓ¦ÓóÌÐòµÄÐÔÄܺͿÉÓÃÐÔ¡£
²Î¿¼ÎÄÏ×£º
Docker¹Ù·½Îĵµ£ºhttps://docs.docker.com/
Docker Compose¹Ù·½Îĵµ£ºhttps://docs.docker.com/compose/
Docker Swarm¹Ù·½Îĵµ£ºhttps://docs.docker.com/engine/swarm/
ÒÔÉϾÍÊÇÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄˮƽÀ©Õ¹ºÍ¸ºÔØƽºâµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡