ÔõÑùʹÓÃDocker¾ÙÐи߿ÉÓú͸ºÔØƽºâµÄÉèÖÃ
DockerÊÇÒ»ÖÖÇáÁ¿¼¶µÄÐéÄ⻯ÊÖÒÕ£¬ËüÄܹ»¿ìËÙ½¨Éè¡¢ÔËÐкͰ²ÅÅÓ¦ÓóÌÐò£¬Ê¹µÃÓ¦ÓóÌÐòµÄ°²ÅźÍÖÎÀí±äµÃÔ½·¢¼òÆӺ͸ßЧ¡£ÔÚÏÖʵµÄÓ¦Óó¡¾°ÖУ¬ÎÒÃÇͨ³£ÐèҪʹÓÃDocker¾ÙÐи߿ÉÓú͸ºÔØƽºâµÄÉèÖã¬ÒÔÈ·±£Ó¦ÓóÌÐò¾ßÓÐÎȹÌÐԺͿÉÉìËõÐÔ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃDocker¾ÙÐи߿ÉÓú͸ºÔØƽºâµÄÉèÖ㬲¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
ʹÓÃDocker¾ÙÐи߿ÉÓõÄÉèÖÃ
¸ß¿ÉÓÃÊÇÖ¸Ò»¸öϵͳ»òЧÀÍÔÚÓöµ½Ó²¼þ»òÈí¼þ¹ÊÕÏʱ£¬ÈÔÄܹ»¼á³ÖÎȹÌÔËÐеÄÄÜÁ¦¡£ÔÚDockerÖУ¬ÎÒÃÇ¿ÉÒÔʹÓöàÖÖ·½·¨ÊµÏָ߿ÉÓã¬ÆäÖÐ×î³£Óõķ½·¨ÊÇʹÓÃDocker SwarmºÍDocker Compose¡£
Docker SwarmÊÇDockerÄÚÖõÄÈÝÆ÷±àÅŹ¤¾ß£¬Ëü¿ÉÒÔ½«Ò»×éDockerÖ÷»ú×é³ÉÒ»¸ö¼¯Èº£¬ÊµÏÖÈÝÆ÷µÄ¸ß¿ÉÓÃÐÔºÍÉìËõÐÔ¡£Ê¹ÓÃDocker SwarmÐèÒªÏȳõʼ»¯Ò»¸öSwarm¼¯Èº£¬²¢Ö¸¶¨SwarmµÄÖÎÀí½Úµã¡£È»ºóÎÒÃÇ¿ÉÒÔʹÓÃDocker CLI»òDocker APIÀ´ÖÎÀíSwarm¼¯ÈºÖеĸ÷¸ö½ÚµãºÍЧÀÍ¡£
ÏÂÃæÊÇÒ»¸öʹÓÃDocker SwarmʵÏָ߿ÉÓõÄʾÀý£º
³õʼ»¯Docker Swarm¼¯Èº
docker swarm init
°²ÅÅÒ»¸öNginxЧÀÍ
docker service create –name nginx –replicas 3 -p 80:80 nginx
Éó²éÔËÐÐÖеÄЧÀÍ
docker service ls
ʹÓÃDocker Swarm¿ÉÒÔ¿ìËÙ¡¢¼òÆÓµØʵÏÖÈÝÆ÷µÄ¸ß¿ÉÓÃÐÔºÍÉìËõÐÔ£¬¿ÉÊÇËüµÄ¹¦Ð§Ïà¶ÔÓÐÏÞ£¬¹ØÓÚÖØ´óµÄ³¡¾°¿ÉÄÜÎÞ·¨Öª×ãÐèÇó¡£´ËʱÎÒÃÇ¿ÉÒÔ˼Á¿Ê¹ÓÃDocker Compose¡£
Docker ComposeÊÇÒ»ÖÖÓÃÓÚ½ç˵ºÍÔËÐжà¸öDockerÈÝÆ÷µÄ¹¤¾ß£¬¿ÉÒÔ×Ô¶¯·ÖÅÉÍøÂç¡¢´æ´¢¾íµÈ×ÊÔ´£¬²¢Ö§³ÖÈÝÆ÷µÄÀ©ÈÝ¡¢»Ø¹öµÈ²Ù×÷¡£Ê¹ÓÃDocker ComposeÐèÒªÏȽç˵һ¸öÓ¦ÓóÌÐòµÄÉèÖÃÎļþ£¬È»ºóʹÓÃdocker-composeÏÂÁîÀ´Æô¶¯ºÍÖÎÀíÓ¦ÓóÌÐòÈÝÆ÷¡£
ÏÂÃæÊÇÒ»¸öʹÓÃDocker ComposeʵÏָ߿ÉÓõÄʾÀý£º
½ç˵һ¸öNginxЧÀÍ
version: ‘2’
services:
nginx:
image: nginx restart: always ports: - "80:80" environment: - VIRTUAL_HOST=www.example.com - VIRTUAL_PORT=80 volumes: - /data/nginx/conf:/etc/nginx - /data/nginx/logs:/var/log/nginx
µÇ¼ºó¸´ÖÆ
Æô¶¯NginxЧÀÍ
docker-compose up -d
Éó²éÔËÐÐÖеÄЧÀÍ
docker-compose ps
ʹÓÃDocker Compose¿ÉÒÔÔ½·¢ÎÞаµØ½ç˵ÈÝÆ÷ºÍÓ¦ÓóÌÐòµÄÉèÖã¬Í¬Ê±Ö§³Ö¸ü¶àµÄ¸ß¼¶ÌØÕ÷¡£
ʹÓÃDocker¾ÙÐиºÔØƽºâµÄÉèÖÃ
¸ºÔØƽºâÊÇÖ¸½«ÍøÂçÇëÇó·Ö·¢¸ø¶à¸öЧÀÍÆ÷¾ÙÐд¦Öóͷ££¬ÒÔÌá¸ßϵͳµÄÈÝ´íÄÜÁ¦ºÍ´¦Öóͷ£ÄÜÁ¦¡£ÔÚDockerÖУ¬ÎÒÃÇ¿ÉÒÔʹÓöàÖÖ·½·¨ÊµÏÖ¸ºÔØƽºâ£¬ÆäÖÐ×î³£Óõķ½·¨ÊÇʹÓÃDocker Swarm¡¢Docker ComposeºÍNginx¡£
ʹÓÃDocker SwarmºÍDocker ComposeʵÏÖ¸ºÔØƽºâÐèҪʹÓÃÆäÄÚÖõĸºÔØƽºâÆ÷£¬ÔÚSwarm¼¯Èº»òComposeµØÀíÉÏ×Ô¶¯·Ö·¢ÇëÇó¡£Ê¹ÓÃNginxʵÏÖ¸ºÔØƽºâÐèÒªÉèÖÃNginxµÄ·´ÏòÊðÀí¹¦Ð§£¬½«ÇëÇó·Ö·¢µ½¶à¸öºǫ́ЧÀÍÆ÷ÉÏ¡£
ÏÂÃæÊÇÒ»¸öʹÓÃNginxʵÏÖ¸ºÔØƽºâµÄʾÀý£º
×°ÖÃNginxºÍÒÀÀµÄ£¿é
apt-get install nginx
apt-get install libnginx-mod-http-upstream-hash
apt-get install libnginx-mod-http-upstream-fair
ÉèÖÃNginxµÄ·´ÏòÊðÀí
upstream backend {
hash $remote_addr consistent; server node1.example.com:80; server node2.example.com:80; server node3.example.com:80;
µÇ¼ºó¸´ÖÆ
}
server {
listen 80; server_name www.example.com; location / { proxy_pass http://backend; }
µÇ¼ºó¸´ÖÆ
}
ÖØÆôNginxЧÀÍ
systemctl restart nginx
ʹÓÃNginx¿ÉÒÔºÜÊÇÀû±ãµØʵÏÖÈÝÆ÷µÄ¸ºÔØƽºâ£¬Í¬Ê±»¹Ö§³Ö¸ü¶àµÄ¸ß¼¶ÌØÕ÷£¬ÀýÈ翵½¡¼ì²é¡¢ÈÝ´í´¦Öóͷ£µÈ¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃDocker¾ÙÐи߿ÉÓú͸ºÔØƽºâµÄÉèÖ㬲¢ÌṩÁËÏêϸµÄ´úÂëʾÀý¡£ÔÚÏÖʵӦÓÃÖУ¬ÎÒÃÇÐèҪƾ֤ÏÖʵÐèÇóÑ¡ÔñÊʺϵŤ¾ßºÍÊÖÒÕ£¬Í¬Ê±Ò²ÐèҪעÖØÇå¾²¡¢¿É¿¿ºÍ¿ÉÀ©Õ¹ÐԵȷ½ÃæµÄÎÊÌâ¡£Docker×÷ΪһÖÖÐÂÐ˵ÄÐéÄ⻯ÊÖÒÕ£¬½«ÔÚδÀ´µÄÓ¦Óó¡¾°ÖÐÊ©Õ¹Ô½À´Ô½Ö÷ÒªµÄ×÷Óá£
ÒÔÉϾÍÊÇÔõÑùʹÓÃDocker¾ÙÐи߿ÉÓú͸ºÔØƽºâµÄÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡