asobannã®AWS ECSãããã¤ã§ãã£ããã¨ã¨å¼ã£ããã£ãã㨠ãã®1
asobannã¯ç¾å¨herokuã«ãããã¤ãã¦MongoDBã使ã£ã¦ãããããherokuã®MongoDBã11æã§ä½¿ããªããªããã¼ãã¨ããéç¥ãæ¥ãããªãã»ã©ãã¾ãããµã¼ãã¼ãã¹ã±ã¼ã«ããããã«ã¯Flask-SocketIOãè¤æ°ããã»ã¹èµ·åãã¦ãã¼ããã©ã³ãµãç½®ããªãã¨ãããªãã®ã ãã©ãherokuã§ãããå®ç¾ã§ããªãããããã¯ããããéãããã(herokuã®ã¬ã¤ãéãã«1ããã»ã¹1Dynoã«ããã¨ããã¼ããã©ã³ãµ1ã¤ï¼ã¡ãã»ã¼ã¸ãã¥ã¼1ã¤ï¼ã¢ããªnåã¶ãã®Dynoãå¿ è¦ã«ãªããå¦çè² è·ã¯ããããå°ããã®ã«ãã£ãããªã)ã
ãã¨ãã¨ããã©ã¼ãã³ã¹æ¹åãèãã¦ãã¦ãè¤æ°ããã»ã¹æ§æã«è¼ãæ¿ãããã£ãã®ã§ããããæ©ä¼ã«åãçµãã§ã¿ããã¨ã«ããããã¡ããéçºç°å¢ã¬ãã«ã§ã¯åãããã«ãªã£ãã®ã§ãããã§ãã£ããã¨ã主ã«èªåã®ã¡ã¢ã¨ãã¦ããããããã誰ãã®åèã«ãªãæ å ±ã¨ãã¦ãæ¸ãã¦ãããã©ãããä»ããï¼ã¨æãããããªæ°ãããªãè¦ç´ ã ããåãã¦ã®äººã¯ãã¤ã§ãããã(âèªå)ãããã£ãã¨ãããããããããã調æ»ä¸è¶³ãåéããå¤ãããã©ãããã誰ãã®å½¹ã«ç«ã¤ã¨ãããªã
åãã¦ã®Docker Composeã§ãnginxã使ããªãã¨ç¥ã
Flask-SocketIOãè¤æ°ä¸¦åã§åããã«ã¯ãgunicornã®workerãå¢ããã®ã§ã¯å¯¾å¿ã§ãããããã»ã¹èªä½ãå¢ãããªãã¨ãªããªããããã»ã¹ãå¢ããã¨ãããã¨ã¯ãªãã¼ã¹ãããã·ãããã¯ãã¼ããã©ã³ãµãå¿ è¦ã«ãªããã¾ããµã¼ãã¼å士ã調æ´ããããã®ã¡ãã»ã¼ã¸ãã¥ã¼ãå¿ è¦(RedisãRabbitMQãªã©)ãå ¬å¼ã§ã¯nginxã®è¨å®æ¹æ³ãæ¸ãã¦ããã®ã§ãããã«åãçµããã¨ã«ããã
AWSã«ä¸ããåã«ãã¾ãæå ã§ãªãã¼ã¹ãããã·ï¼è¤æ°ããã»ã¹ã®æ§æãå®é¨ãã¹ããDocker Composeã使ããã¨ã«ãããdocker-compose.ymlãã¡ã¤ã«ãæ¸ãã®ã¯ä»åãåãã¦ããã®ã¸ãã®ææã¯æ®ã£ã¦ããããçµè«ã¨ãã¦ã¯nginxããã¼ããã©ã³ãµã«ããã¨ã©ããã¦ãåããªãã£ããSocketIOãWebSocketã«åãæ¿ãããã¨ãã¦ãåãæ¿ããããçµæã¨ãã¦ã¾ã£ããéä¿¡ãã§ããªããeventletãgeventã«å¤ããããgevent-websocketãå ¥ããããnginx.confã®æ¸ãæ¹ããã¡ãã¡èª¿ã¹ãããããã©ããããªããããã·ã³ãã«ãªãµã³ãã«ãæ¸ãã¦è©¦ãããã©åãç¾è±¡ãåç¾ããã®ã§ã諦ãããã¨ã«ãAWS ALB (Application Load Balancer)ã使ãã°ãªã«ããããã®ã¾ã¾åããã®ã§ãçµæçã«ã¯ããã§åé¡ãªãã£ãã
ããã«ã¡ã¯AWS ECSããåã¯ãã¤ãããã ãªSecurity Group
å段ã®å®é¨ã§Dockerã³ã³ããã§åãããã«ãã¦ããã®ã§ãAWS ECS (Elastic Container Service)ã使ããã¨ã«ãããECSã¯ããã©ã¼ãã³ã¹ãã¹ããä½ã£ãã¨ãã«ã使ã£ã¦ããã®ã§ãåãã¦ã§ã¯ãªããDocker Composeã§ä¸çºãããã¤ãã§ãããå¬ããæ°ãããããã©ãã¾ã ããã¾ã§ECSã®æ°æã¡ãããããªãã®ã§ãã¾ãã¯Webã³ã³ã½ã¼ã«ããã²ã¨ã¤ãã¤æä½ããããã¨ã«ãããClusterãVPCãTask DefinitionãServiceãLoad Balancerã¨ä½ã£ã¦ããã
èµ·åã¿ã¤ãã¯EC2ã«ãFargateã§ã¯ãããã¯ã¼ã¯ã awsvpcã«ãªã£ã¦ãããã¯ã¼ã¯ã®è¨å®ãã·ã³ãã«ã«ãªããã軽ãã¿ã¹ã¯ãããããä¸ããã¨herokuã®Dynoæ°ã¨åãåé¡ã§ãéãããããEC2 Launch Typeã§1ã¤ã³ã¹ã¿ã³ã¹ã«è»½ãã¿ã¹ã¯ãè¤æ°å ¥ãã¦å®ä¸ããã«ããããæçµçã«ã¯EC2ã®t3.smallã¤ã³ã¹ã¿ã³ã¹1åã«MongoDBï¼Redisï¼ã¢ããª3ã¤ãåã¾ã£ã¦ãæ15USDããã(us-east-1ãªã¼ã¸ã§ã³)ã
ã¨ãããEC2ã§awsvpcã«è¨å®ããã¨ãEC2ã¤ã³ã¹ã¿ã³ã¹ã®ã¿ã¤ãã«ãã£ã¦æã¦ããããã¯ã¼ã¯ã¤ã³ã¿ã¼ãã§ã¼ã¹(ENI)ã®æ°ã«å¶éããã£ã¦ãããããå®è¡ã§ããªãã¨å¶éããããä»åã¯ããã«å¼ã£ããã£ãããããå¾ ã£ã¦ãTaskãèµ·åãããã³ã³ã½ã¼ã«ä¸ã§Serviceã®ã¤ãã³ããè¦ãã¨ãæ¡ä»¶ã®åãã¤ã³ã¹ã¿ã³ã¹ããªãããã¿ã¹ã¯ãèµ·åããªããã¨ããã¨ã©ã¼ã«ãªã£ã¦ããã調ã¹ã¦ããããENIã®æ°ã«å¶éãããã¨ããããã¥ã¡ã³ãã«ãã©ãçãã¦åå ãããã£ãããã£ããawsvpcã§ä½ã£ã¦ãã¾ã£ãã®ã§ããã¾ã¯ç·´ç¿ã¨ãã¦æ¾ç½®ããEC2ã¤ã³ã¹ã¿ã³ã¹ã2ã¤ã«ãã¦ãã®ããããªããã¾èª¿ã¹ãã2019å¹´ã«ENIå¶éãç·©åããããããããã©ãNITRO以éã®ã¿ã¤ãã®ã¿ã ã£ãã®ã§é¢ä¿ãªãã
ä»åã¯ç·´ç¿ã¨ãã¦awsvpcã®ã¾ã¾ã ããæ¬çªã®ã¨ãã¯ãããã¯ã¼ã¯ãbridgeã«è¨å®ãããããããã¨ENIæ°ã®å¶ç´ã¯ãªããªããåãIPã«è¤æ°ã®Taskãåå¨ãã¦ããã¼ãçªå·ã§å¼ã³åãããã¨ã«ãªããService Discoveryã使ãã¨ããAã¬ã³ã¼ãã§ã¯ãªãSRVã¬ã³ã¼ãã使ãå¿ è¦ãåºã¦ããã®ã ããããããã¨ã§ä¸æéå¢ããåå ã¨ãªã£ãã
ALB (Application Load Balancer)ãServiceãä½ãã¨ãã«è¨å®ãããALBã¯ãã®ã¾ã¾ã§WebSocketã«å¯¾å¿ãã¦ãã¦è¿½å è¨å®ä¸è¦ã ããSocketIOã«ã¯ã¹ãã£ããã¼ã»ãã·ã§ã³ãå¿ è¦ãªã®ã§ããã ãè¨å®ãONã«ãããè¨å®ã¯ããã®ã ãã©ãALBã®URLãã¢ã¯ã»ã¹ãã¦ã503ã«ãªã£ã¦ã¢ããªã«å°éã§ããªããããã¯Security Groupã®è¨å®ãæªãã¦ãALBèªä½ã«ãSecurity Groupãåãã®ã§ããã¼ã80çªãéãã¦ãããªãã¨ãããªããSecurity Groupã¯ããããªã¨ããã«å½±é¿ããã®ã«ãè¨å®ãæªãã¨æ°ã¥ããæ¹æ³ããªãã¦ããã¤ããã¡ããã¦ããæ°ããããããã®æ°æã¡ãããããªããã
ç¶ã
ããã§ãã£ããAWS ECSä¸ã§asobannãåãããã«ãªã£ããåºå®URLããªãããMongoDBãã³ã³ããã§åãã¦ããã®ã§ä½ãããã¨ãã¼ã¿ãæ¶ãã¦ãã¾ãããéçºç°å¢ã¨ããä½ç½®ã¥ããªãã¨ãããã大ä¸å¤«ã ãhttpsããªãã®ã¯ã¡ãã£ã¨ãã¡ããã
ä»åº¦ã¯ä»åæä½ããããã®ãåèã«ãCloudFormationã§èªåæ§ç¯ã§ããããã«ããã®ã ãâ¦â¦æ¬¡åã«ç¶ãã
Â
Â
Â
Â