æè¡é¨ã®é´æ¨ (id:eagletmt) ã§ãã ã¯ãã¯ãããã§ã¯ä»¥åããã¢ããªã±ã¼ã·ã§ã³ã®å®è¡ç°å¢ã¨ã㦠Docker ãå©ç¨ãã¦ãã¾ããããæè¿ã¯å¾ã ã« Amazon EC2 Container Service (ECS) ãå©ç¨ãå§ãã¦ãã¾ãã å»å¹´ã®æç¹ã§ã® Web ã¢ããªã±ã¼ã·ã§ã³ã®ãããã¤ææ³ *1 ããæè¿ ECS ãå©ç¨ãã¦ã©ã Web ã¢ããªã±ã¼ã·ã§ã³ããããã¤ãã¦ããã *2 ã«ã¤ãã¦ã¯ç´¹ä»ãããã¨ãããã®ã§ãä»åã¯å®æçãªãããå¦çãã¸ã§ããã¥ã¼ãä»ãã¦éåæã«å®è¡ããããããªãªãã©ã¤ã³ã®å¦çã«ã¤ãã¦ãã©ã®ãããªç°å¢ãæ§ç¯ãã¦ãããç´¹ä»ãããã¨æãã¾ãã
Docker ã使ãå
Docker ãå©ç¨ãå§ããããåãã社å ã§ã¯ kuroko2 *3 ã¨ããã¸ã§ã管çã·ã¹ãã ã稼åãã¦ãããè¤æ°ã®ã¢ããªã±ã¼ã·ã§ã³ããå©ç¨ããã¦ãã¾ããã kuroko2 ã¯å®æçã«ã¸ã§ããå®è¡ããããã«å¿ è¦ãªæ©è½ãååããªãã¦ããä¸æ¹ã§ãè¤æ°ã®ã¢ããªã±ã¼ã·ã§ã³åãã«ã¯ã¼ã«ã¤ã³ã¹ã¿ã³ã¹ããããã¸ã§ãã³ã°ããå¿ è¦ããããã¨ãã¸ã§ããå®è¡ããã¯ã¼ã«ãæè»ã«å¢æ¸ã§ããªããã¨ãã¨ãã£ãæ¬ ç¹ãããã¾ããã
Docker ã®å°å ¥
Docker ãå©ç¨ãããã¨ã«ãããè¤æ°ã®ã¢ããªã±ã¼ã·ã§ã³ããå ±éã§ä½¿ããã¤ã¤ãã¤ã³ã¹ã¿ã³ã¹å´ã«å¿ è¦ãªãããã¸ã§ãã³ã°ãæä½éã«æãããã¨ãã§ããããã«ãªãã¾ããã ã¤ã³ã¹ã¿ã³ã¹å´ã®æ§æã¯ã·ã³ãã«ã«ãªããæ°ããã¢ããªã±ã¼ã·ã§ã³ã追å ããã¨ãã«å¿ è¦ãªæéãé常ã«å°ããæ¸ã¿ã¾ãã ãããã¸ã§ãã³ã°ã®æéã ãã§ãªããAPI ãã¼ããã¹ã¯ã¼ãã®ãããªç§å¿ãã¹ãã¯ã¬ãã³ã·ã£ã«ãã¤ã³ã¹ã¿ã³ã¹ã«ç´æ¥ç½®ããããã¨ãé²ããã¯ã¼ã«ã¤ã³ã¹ã¿ã³ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãããã¡ã¤ã«ã«ãã¹ã¦ã®ã¢ããªã±ã¼ã·ã§ã³ã«å¿ è¦ãªæ¨©éãã¤ããå¿ è¦ããªããªããå ±éã®ã¤ã³ã¹ã¿ã³ã¹ãè¤æ°ã®ã¢ããªã±ã¼ã·ã§ã³ã§ä½¿ãåãã¤ã¤ã権éãé©åã«åé¢ã§ããããã«ãªãã¾ããã
社å ã§ã¯å®æçãªã¸ã§ããå®è¡ããã¨ã㯠kuroko2 ã使ãã®ãæ¨æºã¨ãªã£ã¦ãããããkuroko2 ãã Docker ãå©ç¨ããããã®æ©è½è¿½å ãã¤ã³ã¹ã¿ã³ã¹ã®æ´åãªã©ãè¡ãã¾ããã ããã«ãã Web ã¢ããªã±ã¼ã·ã§ã³ãåããããã® Docker ã¤ã¡ã¼ã¸ãä½ã£ã¦ããã°èªåçã« kuroko2 ã§å®è¡ããç°å¢ãæã«å ¥ãããã«ãªããã¢ããªã±ã¼ã·ã§ã³éçºè ã«ã¨ã£ã¦ãªãã©ã¤ã³ã®ã¸ã§ãå®è¡ãããéã®éå£ã大ãã軽æ¸ããã¾ããã
ECS ã®å°å ¥
Web ã¢ããªã±ã¼ã·ã§ã³ã®ãããã¤ã« ECS ãå©ç¨ãå§ããã®ã¨åæã«ããªãã©ã¤ã³ã¸ã§ãã®å®è¡ã«ã ECS ãå©ç¨ãå§ãã¾ããã
Web ã¢ããªã±ã¼ã·ã§ã³ã®ãããã¤ã«ä½¿ã£ã¦ãããã¼ã«ã§ãã hako ã使ã£ã¦ãkuroko2 ãã hako oneshot awesome-app.yml -- bundle exec rake some:heavy:task
ã®ãããªã³ãã³ããå®è¡ããããã«ãã¦ãã¾ãã
hako oneshot
㯠YAML ã®å®ç¾©ã«å¾ã£ã¦ ECS ã® RunTask API ãå¼ã³åºãã³ãã³ãã§ããªãã©ã¤ã³ã®ã¸ã§ãã bin/rails db:migrate
ã®ãããªåçºã®ã³ãã³ããå®è¡ããããã«ä½¿ãã¾ãã
ç´æ¥ Docker ã使ã£ã¦ããã¨ãã¨æ¯è¼ãã¦ãããªã£ãç¹ã®ä¸ã¤ã« IAM ãã¼ã«ãå©ç¨ã§ããããã«ãªã£ããã¨ãããã¾ãã ECS ã§ã¯ã¤ã³ã¹ã¿ã³ã¹åä½ã§ã¯ãªãã¿ã¹ã¯åä½ã§ IAM ãã¼ã«ãå©ç¨ã§ããããã«ãã AWS ã®ã¢ã¯ã»ã¹ãã¼ãçºè¡ãããã¨ãªã AWS ã® API ãå©ç¨ããã¸ã§ããå®è¡ã§ããããã«ãªãã¾ããã
ã¸ã§ããã¥ã¼ããã®å©ç¨
å
æ¥ã® RubyKaigi 2016 ã§ç´¹ä»ãããããã«ãç¾å¨ barbeque ã¨ããã¸ã§ããã¥ã¼ã®ããã¿ãæ´åããã¦ãã¾ãã
barbeque ããã¸ã§ããå®è¡ããã¨ãã«ã¯ç¤¾å
ã§ã¯ hako oneshot
ã使ããã¦ãã¾ãã
https://speakerdeck.com/k0kubun/scalable-job-queue-system-built-with-docker
barbeque ã¸ã®ã¨ã³ãã¥ã¼ã¯ Web ã¢ããªã±ã¼ã·ã§ã³ã¸ã®ãªã¯ã¨ã¹ãèµ·å ã§ãããã¨ãå¤ããããã¸ã§ãã®å®è¡åæ°ãã¤ã¾ãã¸ã§ãã®å®è¡ã«å¿ è¦ãªãªã½ã¼ã¹ãäºåã«äºæ³ãããã¨ãé£ãããã¾ãæé帯ã«ãã£ã¦å»ã ã¨å¤åããã¨ããç¹å¾´ãããã¾ãã ãããã£ã¦ã常ã«å®å®çã«ã¸ã§ããå®è¡ãã¤ã¤ã³ã¹ããæããããã«ã¯ããªã¼ãã¹ã±ã¼ã«ãéè¦ã«ãªã£ã¦ãã¾ãã
ãªã¼ãã¹ã±ã¼ãªã³ã°
ECS ã®å°å
¥ã«ãã£ã¦ãã¯ã¼ã«ã¤ã³ã¹ã¿ã³ã¹ã®ã¹ã±ã¼ãªã³ã°ã容æã«ãªãã¾ããã
ã¯ã¼ã«ã¤ã³ã¹ã¿ã³ã¹ã¨ãã¦ä½¿ãããã® ECS ã¯ã©ã¹ã¿ãä½æãããã®ã¯ã©ã¹ã¿ã®ã¤ã³ã¹ã¿ã³ã¹ã¯ãã¹ã¦ AutoScaling ã°ã«ã¼ãã§ç®¡çããããã«ãã¾ããã
hako oneshot
㯠RunTask API ã使ã£ã¦ãã¾ããããã® API ã¯ããå®è¡ã«å¿
è¦ãªãªã½ã¼ã¹ãã¯ã©ã¹ã¿å
ã«ä¸è¶³ãã¦ããå ´åã«ãªã½ã¼ã¹ä¸è¶³ãç¥ãããã¨ã©ã¼ãè¿ãã®ã§ããã®ã¨ã©ã¼ãçºçããå ´å㯠AutoScaling ã°ã«ã¼ãã® desired capacity ãå¼ãä¸ããå度å®è¡ã試ã¿ãããã«ãã¦ãã¾ãã
ãã®æå㯠hako ã®å®ç¾©ãã¡ã¤ã«ã§ autoscaling_group_for_oneshot
ã« AutoScaling ã°ã«ã¼ããæå®ãããã¨ã§æå¹åã§ãã¾ãã
https://github.com/eagletmt/hako/blob/v0.20.0/examples/hello-autoscaling-group.yml
ä¸æ¹ãã¹ã±ã¼ã«ã¤ã³ã®å®è¡ã¯ CloudWatch ã®ã¡ããªã¯ã¹ãå®æçã«ãã§ãã¯ãããã¨ã§è¡ã£ã¦ãã¾ãã
ECS ã¯ã¯ã©ã¹ã¿æ¯ã«ã©ãããã CPUãã¡ã¢ãªã使ããã¦ãããã®å²åãèªåçã« CloudWatch ã«ä¿åãã¦ãããããç´è¿ã®å®ç¸¾å¤ãåç
§ãããã¨ã§ç¾å¨å¿
è¦ãªãªã½ã¼ã¹éãè¦ç©ãããã¨ãã§ãã¾ãã
ã¯ã©ã¹ã¿ãæä¾ãããªã½ã¼ã¹ã®ãã¡ P % ãå©ç¨ãã¦ãããããªç¶æ³ãç¶æãããå ´åãç¾å¨ã®ã¤ã³ã¹ã¿ã³ã¹æ°ã N ã¨ãã㨠n å°æ¸ããããã©ããã®é¾å¤ã¯ (N - n) * 100 / N * P
ã«ãªãã¾ãã
ãªã½ã¼ã¹éã®è¦ç©ããã¨é¾å¤ã®è¨ç®ãæ¯æå®è¡ããè¦ç©ãããé¾å¤ãä¸åã£ã¦ããã AutoScaling ã°ã«ã¼ãã® desired capacity ãä¸ãããã¨ã§ã¹ã±ã¼ã«ã¤ã³ãå®ç¾ãã¦ãã¾ãã
ã¹ã±ã¼ã«ã¤ã³ããã¨ãã«ã¯ãå®è¡ä¸ã®ã¿ã¹ã¯ãä¸æãããªãããã«æ³¨æããªããã°ãªãã¾ããã AutoScaling ã«ã¯ã©ã¤ããµã¤ã¯ã«ããã¯ã¨ããããã¿ããããAutoScaling ã«ãã£ã¦ããã¤ã³ã¹ã¿ã³ã¹ã terminating ç¶æ ã«ãªã£ã¦ããå®éã« terminate ãããã¾ã§ã®éã«çµäºå¦çãè¡ããããã«ãªã£ã¦ãã¾ãã ã©ã¤ããµã¤ã¯ã«ããã¯ã«ãã£ã¦ terminate ã決å®ããã¨ã㯠AWS Lambda çµç±ã§ãã®ã¤ã³ã¹ã¿ã³ã¹ã«ç¹å¥ãªã¿ã°ãã¤ããããã«ãã¦ãã¾ãã åã¤ã³ã¹ã¿ã³ã¹ã¯å®æçã«èªåèªèº«ã®ã¿ã°ããã§ãã¯ããããç¹æ®ãªã¿ã°ãã¤ãã¦ããã°èªåèªèº«ã DeregisterContainerInstance ãã¦ãµã¼ãã¹ã¢ã¦ããããã®å¾ã©ã¤ããµã¤ã¯ã«ãç¶ç¶ããã¾ãã ãããã®ã¤ã³ã¹ã¿ã³ã¹å ã§ã¿ã¹ã¯ãå®è¡ä¸ã§ããã° DeregisterContainerInstance ã¯å¤±æããã®ã§ãæåããã¾ã§ DeregisterContainerInstance ãå®è¡ãç¶ããããã«ãã¦ãã¾ãã
ä»å¾ã®èª²é¡
ECS ãå©ç¨ãã¦ããã¨ã³ã³ããã¤ã³ã¹ã¿ã³ã¹ã®ã¹ã±ã¼ã«ã¢ã¦ãã¯ããããä¸æ¹ã§ãã¹ã±ã¼ã«ã¤ã³ãããã«ã¯ç¾ç¶ããã¤ãã®å·¥å¤«ãå¿ è¦ã«ãªã£ã¦ãã¾ãã æ¯è¼ççæéã§çµäºããã¿ã¹ã¯å°ç¨ã®ã¯ã©ã¹ã¿ã§ããã° DeregisterContainerInstance ãæåããã¾ã§å©ãç¶ããã¨ããæ¹æ³ã§ã¹ã±ã¼ã«ã¤ã³ã§ããããã«ãªãã¾ãããã é·æéå®è¡ãç¶ããããªã¿ã¹ã¯ãå®è¡ããã¦ãå ´åã ECS ã® service ã使ã£ã¦ããå ´åãªã©ããã¹ã¦ã®å ´åã§èªåçã«ã¹ã±ã¼ã«ã¤ã³ãããã¨ã¯ã¾ã ã§ãã¦ãã¾ããã ãã®ãããªã±ã¼ã¹ã§ãã¹ã±ã¼ã«ã¤ã³ããæ¹æ³ãèããå¿ è¦ãããã¾ãã
ã¾ããã¹ã±ã¼ã«ã¢ã¦ããããã¿ã¤ãã³ã°ãç¾ç¶ã¯å®éã«ãªã½ã¼ã¹ãä¸è¶³ããã¨ãã«ãªã£ã¦ãããä¸æçã«ã¸ã§ãã®å®è¡ãé ãããã¨ã«ãªãã¾ãã AutoScaling ã°ã«ã¼ãã® desired capacity ãå¤ãã¦ããã¤ã³ã¹ã¿ã³ã¹ãèµ·åãã¦ã³ã³ããã¤ã³ã¹ã¿ã³ã¹ã¨ãã¦ç»é²ãããã¾ã§3åã»ã©ããã£ã¦ãã¾ãã ã¸ã§ãã®å®è¡ãã§ããã ãé ããªãããã«ããããã«ã¯ãã¤ã³ã¹ã¿ã³ã¹ã®èµ·åãé«éåããä»ã«ããªã½ã¼ã¹ã®ä½¿ç¨ç¶æ³ã®å±¥æ´ããå¿ è¦ãããªã¨ãã«ãããããã¹ã±ã¼ã«ã¢ã¦ããã¦ããæ¹æ³ãããã¾ãã ãã°ããã¯ã©ãããç¶æ³ã®ã¨ãã«å®éã«ãªã½ã¼ã¹ãä¸è¶³ãã¦ã¹ã±ã¼ã«ã¢ã¦ããã¦ãããã観å¯ãããã®çµæããã¨ã«ã¹ã±ã¼ã«ã¢ã¦ãããåºæºãã©ããããã決ãããã¨èãã¦ãã¾ãã
ã¾ã¨ã
Docker ã ECS ããªãã©ã¤ã³ã®ã¸ã§ãã§å©ç¨ããã¡ãªããã¨ãå®éã«ã©ãå©ç¨ãã¦ãããã«ã¤ãã¦ç´¹ä»ãã¾ããã ä»å¾ã ECS èªä½ã®æ¹åã注è¦ãã¤ã¤ãã¢ããªã±ã¼ã·ã§ã³å®è¡ç°å¢ã®å©ä¾¿æ§ãé«ããªãããã§ããã ãã³ã¹ããæããããããã«æ¹åãã¦ããããã¨æã£ã¦ãã¾ãã