ÔõÑùÔÚLinuxÉÏ°²ÅŸ߿ÉÓõÄ΢ЧÀͼܹ¹
ÔõÑùÔÚlinuxÉÏ°²ÅŸ߿ÉÓõÄ΢ЧÀͼܹ¹
СÐò£º
Ëæ×ÅÏÖ´úÈí¼þ¿ª·¢µÄÒ»Ö±Éú³¤£¬Î¢Ð§ÀͼÜ×é³ÉΪÁ˹¹½¨ÎÞа¡¢¿ÉÀ©Õ¹ºÍ¿Éά»¤µÄÓ¦ÓóÌÐòµÄÒ»ÖÖÊ¢Ðз½·¨¡£ÔÚ΢ЧÀͼܹ¹ÖУ¬Ó¦ÓóÌÐò±»²ð·Ö³ÉһϵÁÐСÐÍ×ÔÁ¦µÄЧÀÍ£¬Ã¿¸öЧÀÍÈÏÕæÒ»¸öÌض¨µÄ¹¦Ð§£¬²¢Í¨¹ýÍøÂç¾ÙÐÐͨѶ¡£ÓÉÓÚЧÀÍÓëЧÀÍÖ®¼äµÄ×ÔÁ¦ÐÔ£¬ÎÒÃÇ¿ÉÒÔ¸üÈÝÒ׵ؾÙÐа²ÅÅ¡¢À©Õ¹ºÍά»¤¡£
±¾ÎĽ«ÏÈÈÝÔõÑùÔÚLinuxÉÏ°²ÅÅÒ»¸ö¸ß¿ÉÓõÄ΢ЧÀͼܹ¹£¬²¢ÌṩһЩÊÊÓõĴúÂëʾÀý¡£
µÚÒ»²¿·Ö£º×¼±¸ÊÂÇé
ÔËÐÐÇéÐΣºÊ×ÏÈÈ·±£ÄãÓÐһ̨ÔËÐÐLinuxµÄЧÀÍÆ÷£¬¿ÉÒÔÊÇÐéÄâ»ú»òÎïÀíЧÀÍÆ÷¡£ÎÒÃÇÍƼöʹÓÃһЩʢÐеÄLinux¿¯Ðа棬ÀýÈçUbuntu»òCentOS¡£
×°ÖÃDocker£ºDockerÊÇÒ»¸ö¿ªÔ´µÄÈÝÆ÷»¯Æ½Ì¨£¬¿ÉÒÔ×ÊÖúÎÒÃÇ¿ìËÙ°²ÅźÍÖÎÀí΢ЧÀÍ¡£ÔÚLinuxÉÏ×°ÖÃDockerºÜÊǼòÆÓ£¬¿ÉÒÔͨ¹ý¹Ù·½Îĵµ¾ÙÐÐ×°Öá£
µÚ¶þ²¿·Ö£º¹¹½¨Î¢Ð§À;µÏñ
½¨ÉèDockerfile£ºÔÚÿ¸öЧÀ͵ĸùĿ¼Ï½¨ÉèÒ»¸öDockerfileÎļþ£¬ÓÃÓÚ¹¹½¨¾µÏñ¡£DockerfileÊÇÒ»¸ö´¿Îı¾Îļþ£¬½ç˵ÁËһϵÁÐÖ¸ÁîÀ´¹¹½¨¾µÏñ¡£
±àдDockerfile£ºÀýÈ磬ÎÒÃǼÙÉèÎÒÃÇÓÐÒ»¸öÃûΪ”userservice”µÄ΢ЧÀÍ£¬×ðÁú¿Ê±DockerfileÈçÏÂËùʾ£º
# »ùÓÚJavaµÄ¾µÏñ FROM openjdk:8-jdk-alpine # ÉèÖÃÊÂÇéĿ¼ WORKDIR /app # ½«Ó¦ÓóÌÐò¸´ÖƵ½¾µÏñ COPY target/userservice.jar . # ½ç˵ÈÝÆ÷̻¶µÄ¶Ë¿Ú EXPOSE 8080 # ÉèÖÃÇéÐαäÁ¿ ENV JAVA_OPTS="" # Æô¶¯Ó¦ÓóÌÐò ENTRYPOINT exec java $JAVA_OPTS -jar userservice.jar
µÇ¼ºó¸´ÖÆ
¹¹½¨¾µÏñ£ºÊ¹ÓÃÒÔÏÂÏÂÁî¹¹½¨¾µÏñ£¨¼ÙÉèDockerfileºÍÓ¦ÓóÌÐòÔÚͳһĿ¼Ï£©£º
docker build -t userservice .
µÇ¼ºó¸´ÖÆ
µÚÈý²¿·Ö£º°²ÅÅ΢ЧÀͼ¯Èº
½¨ÉèDocker Swarm£ºÔËÐÐÒÔÏÂÏÂÁ×ðÁú¿Ê±Ð§ÀÍÆ÷ת»»ÎªÒ»¸öDocker Swarm¼¯ÈºµÄÖÎÀí½Úµã¡£
docker swarm init
µÇ¼ºó¸´ÖÆ
°²ÅÅЧÀÍ£º½¨ÉèÒ»¸ödocker-compose.yamlÎļþ£¬ÓÃÓÚ½ç˵×ðÁú¿Ê±Î¢Ð§Àͼܹ¹¡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖÃÎļþ£º
version: '3' services: userservice: image: userservice deploy: replicas: 3 restart_policy: condition: on-failure
µÇ¼ºó¸´ÖÆ
´ËÉèÖÃÎļþÖ¸¶¨ÁË×ðÁú¿Ê±userserviceЧÀÍÓ¦¸ÃÔËÐÐ3¸ö¸±±¾£¬²¢ÔÚ±¬·¢¹ÊÕÏʱ×Ô¶¯ÖØÆô¡£
ʹÓÃdocker stackÏÂÁî°²ÅÅЧÀÍ£º
docker stack deploy -c docker-compose.yaml myservice
µÇ¼ºó¸´ÖÆ
Õ⽫ÔÚ×ðÁú¿Ê±Docker Swarm¼¯ÈºÖа²ÅÅ×ðÁú¿Ê±Î¢Ð§Àͼ¯Èº¡£
µÚËIJ¿·Ö£º¼à¿ØÓëÀ©Õ¹
ʹÓÃDocker Swarm¾ÙÐÐЧÀÍÀ©Õ¹£ºÈôÊÇÎÒÃÇÐèÒª¸ü¶àµÄЧÀÍʵÀýÀ´´¦Öóͷ£¸ü¸ßµÄ¸ºÔØ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ©Õ¹Ð§ÀÍ£º
docker service scale myservice_userservice=5
µÇ¼ºó¸´ÖÆ
Õ⽫½«userserviceЧÀ͵ĸ±±¾ÊýÀ©Õ¹µ½5¸ö¡£
ʹÓÃPrometheusºÍGrafana¾ÙÐмà¿Ø£ºPrometheusÊÇÒ»¸ö¿ªÔ´µÄ¼à¿Øϵͳ£¬¶øGrafanaÊÇÒ»¸ö¿ÉÊÓ»¯¹¤¾ß¡£ÎÒÃÇ¿ÉÒÔʹÓÃÕâÁ½¸ö¹¤¾ßÀ´¼à¿Ø×ðÁú¿Ê±Î¢Ð§Àͼ¯Èº¡£
ʹÓÃÒÔÏÂÏÂÁîÆô¶¯PrometheusºÍGrafanaÈÝÆ÷£º
docker run -d -p 9090:9090 --name prometheus prom/prometheus docker run -d -p 3000:3000 --name grafana grafana/grafana
µÇ¼ºó¸´ÖÆ
ÉèÖÃPrometheusÒÔ¼à¿Ø×ðÁú¿Ê±Î¢Ð§Àͼ¯Èº£¬²¢Ê¹ÓÃGrafana½¨ÉèÒDZí°åÀ´Éó²é¼à¿ØÊý¾Ý¡£
½áÂÛ£º
ÒÔÉÏÊÇÔÚLinuxÉÏ°²ÅŸ߿ÉÓÃ΢ЧÀͼܹ¹µÄ°ì·¨ºÍʾÀý´úÂ롣ͨ¹ýʹÓÃDockerºÍDocker Swarm£¬ÎÒÃÇ¿ÉÒÔÇáËɵع¹½¨¡¢°²ÅźÍÀ©Õ¹Î¢Ð§Àͼ¯Èº¡£Í¬Ê±£¬Ê¹ÓÃPrometheusºÍGrafana¿ÉÒÔ×ÊÖúÎÒÃǼà¿Ø΢ЧÀ͵ÄÐÔÄܺͿµ½¡×´Ì¬¡£Ï£Íû±¾ÎĶÔÄú¹¹½¨¸ß¿ÉÓÃ΢ЧÀͼܹ¹ÌṩÁËһЩ×ÊÖúºÍÖ¸µ¼¡£
ÒÔÉϾÍÊÇÔõÑùÔÚLinuxÉÏ°²ÅŸ߿ÉÓõÄ΢ЧÀͼܹ¹µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡