コンテナは友だち! in RailsDM /container-ga-tokui-na-friends-in-railsdm

2cf373725ded741824c50fd571eda6e1?s=47 KONDO Uchio
November 16, 2017

コンテナは友だち! in RailsDM /container-ga-tokui-na-friends-in-railsdm

Rails Developers Meetup #7
https://techplay.jp/event/631428

2cf373725ded741824c50fd571eda6e1?s=128

KONDO Uchio

November 16, 2017
Tweet

Transcript

  1. ා͘ͳ͍γεςϜϓϩάϥϛϯάGPS3BJMT%. ۙ౻Ӊஐ࿕(.01FQBCP *OD 3BJMT%FWFMPQFST.FFUVQ ίϯςφ͸༑ͩͪʂ

  2. ΤϯδχΞ ۙ౻Ӊஐ࿕!VE[VSB ٕज़෦ٕज़ج൫νʔϜ ޷͖ͳϥʔϝϯ͸ΒʔΊΜ޻๪ཾ (.0ϖύϘॴଐ

  3. ུྺ w&$αΠτɺιʔγϟϧήʔϜͷ։ൃͳͲΛܦͯɺ
 ೥ΑΓ(.0ϖύϘٕज़ج൫νʔϜɺಉ೥෱ԬʹҠॅɻ
 ޷͖ͳϥʔϝϯ͸ΒʔΊΜ޻๪ཾɺ޷͖ͳΧϨʔ͸5JLJɻ wஶॻʹʰύʔϑΣΫτ3VCZPO3BJMTʱ΄͔ɻ w3VCZ,BJHJʹࣗ࡞ίϯςφʮ)BDPOJXBʯͷ࿩͕࠾୒ɻ w3BJMT(JSMT'VLVPLBΦʔΨφΠβɺ෱Ԭ3VCZձ࣮ٞߦҕһ௕ɻ w΄͔ɺ3VCZ΍ίϯςφʹؔ͢Δ׆ಈଟ਺ɻ

  4. ࠷ۙͷ࢓ࣄͷએ఻Ͱ͢ w3VCZPO3BJMT͕࢖͑ΔϗεςΟϯάαʔϏεͷ
 ΞʔΩςΫτʢࣗশʣΛ΍͍ͬͯ·͢ɻ wϑϩϯτΤϯυ͸/VYUKT &YQSFTTɺ443Ͱ͢  w಺෦"1*͸(Pͷ&DIPͰ͢  wྶΔͣ wࠓͷॴЋఏڙதɻ͓଴ͪΛ

  5. ෱Ԭ3VCZձٞ! IUUQSFHJPOBMSVCZLBJHJPSHGVLVPLB ചΓ੾Ε·ͨ͠

  6. ؓ࿩ ٳ୊

  7. ࠓ೔ͷΞδΣϯμ w ϓϩηεͱ͸ԿͩΖ͏ʁ w ίϯςφͱ͸ԿͩΖ͏ʁ w ΈΜͳ΋ίϯςφʹͳͬͯΈΑ͏ʁʁʁ

  8. ϓϩηεͱ͸ʁ

  9. 'SPN෼ް͍ຊ w-*/69ϓϩάϥϛϯάΠϯλʔϑΣʔε
  ΦϥΠϦʔ ͜ͷதͰ΋Ұ൪෼ް͍ຊ

  10. ϓϩηεͱ͸ϓϩάϥϜͷ࣮ߦதͷ࢟Ͱ͢ ʮϓϩηεͱ͸ɺϓϩάϥϜͷ࣮ߦʹඞཁ ͳγεςϜϦιʔεΛׂΓ౰ͯΔͨΊɺΧʔ ωϧ͕ఆٛ͢Δந৅ΤϯςΟςΟͰ͋Δʯͱ ΋ݴ͍׵͑ΒΕ·͢ɻ ʰ-JOVYϓϩάϥϛϯάΠϯλʔϑΣʔεʱষϓϩηε

  11. ϓϩηεͱ৮Ε߹͏ wʮϓϩάϥϜͷ࣮ߦʹඞཁͳϦιʔεʯΛ೷͍ͯΈΔ wͦΕ͕ɺ͢ͳΘͪϓϩηεଐੑͰ΋͋Δ wྫ w 1*%ɺ਌1*% w εςʔλεʢ࣮ߦՄೳɺ*0଴ͪɺʣ w ؀ڥม਺

    w ϑΝΠϧσΟεΫϦϓλςʔϒϧ w 6TFS*%(SPVQ*% w ݖݶʢ,FSOFM$BQBCJMJUZʣ w ΧϨϯτσΟϨΫτϦ w ϝϞϦϚοϐϯά
  12. TUSVDUUBTL@TUSVDU

  13. QSPDQSPDGT wγεςϜ΍ϓϩηεͷ৘ใΛऔಘͰ͖ΔಛघϑΝΠϧγεςϜ wQSPDͷԼʹ࠷ॳ͔ΒϚ΢ϯτ͞Ε͍ͯΔ৔߹͕ଟ͍ɻ wϚ΢ϯτ͞Ε͍ͯͳ͍ͱ࢖͑ͳ͍πʔϧ΋͋ΔʢQTɺUPQͦͷଞʣ

  14. த਎ΛݟͯΈΑ͏ wԋश  ௕͘ଘࡏ͢ΔΑ͏ͳϓϩηεΛ࡞ͬͯΈΑ͏ɻ w  ͦͷϓϩηεͷ1*%Λಛఆ͠Α͏ɻ w  ͦͷϓϩηεʹ͍ͭͯɺҎԼͷ৘ใΛऔಘͯ͠ΈΑ͏ɻ

    w  ΧϨϯτσΟϨΫτϦ IJOUQSPD1*%DXE  w  ؀ڥม਺ IJOUQSPD1*%FOWJSPO  w  ϝϞϦϚοϐϯά IJOUQSPD1*%NBQT  w  εϨουͷ਺ IJOUQSPD1*%UBTL
  15. ղ౴ྫ wσϞ͠·͢ w(JTUIUUQCJUMZSENDPOUBJOFST

  16. ίϯςφͱ͸ʁ

  17. Ծ૝ԽͷछྨʢҰྫʣ wϋΠύʔόΠβܕʢωΠςΟϒϋΠύʔόΠβʣ wઐ༻ͷ04΍ɺ-JOVY,FSOFMࣗମΛϋΠύʔόΠβͱͯ͠࢖͍ɺͦͷ ্Ͱ04Λ૸ΒͤΔ wϗετ04ܕʢϗετϋΠύʔόΠβʣ w൚༻తͳ04ͷ্ʹɺ7JSUVBM#PYͷΑ͏ͳԾ૝ԽͷͨΊͷઐ༻ιϑτ ΢ΣΞΛೖΕͯ૸ΒͤΔ wίϯςφܕˠ

  18. ίϯςφͱ͸ wখ͞ͳಠཱͨ͠؀ڥΛɺҰͭͷ04ͷ্ʹ࡞Δٕज़ɻ wͨͩ͠ɺԾ૝Խٕज़ͷΑ͏ʹѻΘΕΔ͕ɺԿ͔ΛԾ૝తʹ࡞͍ͬͯΔ ͱ͍͏͜ͱͰ͸ͳ͍

  19. ίϯςφ͸ ಛघͳϓϩηεͰ͋Δ

  20. ͜Ε͚֮ͩ͑ͯؼͬͯ͘Εʂʂ̍

  21. ίϯςφͷ࣮૷ w%PDLFS(PMBOH੡ɻ,VCFSOFUFTͳͲͱ࿈ܞ͠ɺঃʑʹ׆༻γʔϯ ͕޿·͍ͬͯΔ w-9$-9%ΧʔωϧͷίϯςφϨϑΝϨϯε࣮૷తཱͪҐஔɻ
 $ (PMBOH੡ w)BDPOJXBNSVCZ $ݴޠͰͰ͖ͨίϯςφɻ3VCZͷ%4-Ͱίϯ ςφΛఆٛͰ͖Δɻ(.0ϖύϘͱ͍͏ձ͕ࣾϚωʔδυΫϥ΢υͷ όοΫΤϯυʹ࠾༻͠࿩୊ʹɻ

  22. ίϯςφ͸Ͳ͏΍ͬͯ࡞ΒΕ͍ͯΔ͔ wࠓ͋͛ͨ-JOVYίϯςφͷ࣮૷Ͱ͸ɺجຊతʹಉ͡ΧʔωϧͷػೳΛ ૊Έ߹Θͤͯɺʮখ͘͞ಠཱͨ͠؀ڥʯΛ࡞͍ͬͯΔɻ wͦͷػೳ͕ద༻͞Ε͍ͯΔ͔͍ͳ͍͔͸ɺϓϩηεͷଐੑͰ΋͋Δͷ ͰɺQSPDͷԼͳͲ͔Β֬ೝ͢Δ͜ͱ͕Ͱ͖·͢

  23. %PDLFSͰίϯςφΛ࡞ͬͯΈΑ͏

  24. QSPD1*%OTͷԼΛݟͯΈΑ͏

  25. ͜Ε͸ɺଞͷϓϩηεͱҧ͍ͬͯΔ wͱ͍͏͜ͱΛ֬ೝ͢Δ

  26. ͜Ε͸ɺଞͷϓϩηεͱҧ͍ͬͯΔ wͱ͍͏͜ͱΛ֬ೝ͢Δ ◀ ◀ ◀ ◀ ◀

  27. -JOVY/BNFTQBDF

  28. -JOVY/BNFTQBDFͱ͸ wΧʔωϧ͸ɺάϩʔόϧͰ؅ཧ͍ͯ͠ΔϦιʔε͕ͨ͘͞Μ͋Δɻ wͦ͏͍͏Ϧιʔεͷ͏ͪҰ෦͸ɺ໊લۭؒΛ੾ͬͯɺಛఆͷϓϩηε ͨͪͷͨΊʹผͷϦιʔεΛ֬อ͢Δ͜ͱ͕Ͱ͖Δ w654/BNFTQBDFϗετ໊ͳͲ w1*%/BNFTQBDFϓϩηε*% w/FUXPSL/BNFTQBDFωοτϫʔΫઃఆʢ/*$ଞʣ wͳͲͳͲɻ࠷৽ͷ-JOVYͰ͸ਓͷࣆͭͷ/BNFTQBDF

  29. QSPD1*%OTͱ͸ wʮͦͷϓϩηε͕ॴଐ͢Δ/BNFTQBDFΛදݱ͢ΔϑΝΠϧʯΛ
 ݟ͚ͭΔ͜ͱ͕Ͱ͖ΔσΟϨΫτϦ w͋Δϓϩηεͱɺผͷϓϩηε͕ҧ͏ϑΝΠϧʢJOPEF൪߸ʣΛ
 ͍ࠩͯ͠Ε͹ɺͦͷϓϩηεͨͪ͸ผʑͷ໊લۭؒʹ͍ΔͶɺ
 ͱ֬ೝͰ͖Δ

  30. DHSPVQ

  31. QSPD1*%DHSPVQͱ͍͏ϑΝΠϧ wͳʹ΍Βಛघͳจࣈྻ͕ॻ͔Ε͍ͯΔ

  32. ࣮ࡍͷσΟϨΫτϦʹରԠ͢Δ wTZTGTDHSPVQͱ͍͏ͱ͜ΖΛௐ΂Δͱ

  33. ͜Ε͕DHSPVQ wDHSPVQ͸ɺ΋ͱ΋ͱϓϩηεΛάϧʔϐϯά͢ΔͨΊͷػೳɻ wϓϩηεͷάϧʔϓຖʹɺ$16ͷར༻཰Ͱ͋ͬͨΓɺϝϞϦͰ͋ͬͨ Γɺ1*%ͷ਺Ͱ͋ͬͨΓɺͷ੍ݶΛ͔͚Δ͜ͱ͕Ͱ͖Δɻ͋Δ͍͸ɺ άϧʔϓผͷར༻ঢ়گΛ֬ೝͨ͠Γ΋Ͱ͖Δ wDHSPVQʹؔ͢Δૢ࡞͸ɺDHSPVQGTͱ͍͏ಛघϑΝΠϧγεςϜΛ ܦ༝ͯ͠Ͱ͖Δ

  34. ࣮ࡍͷ੍ݶΛ֬ೝ •docker run --pids-limit=128 -ti debian:jessie /bin/bash wͷΑ͏ͳίϚϯυͰɺϓϩηε਺ͷDHSPVQ੍ݶΛ͔͚ΒΕΔ w͜ͷγΣϧͰɺGPSLCPNC߈ܸΛͯ͠΋ɺؼͬͯ͘Δ

  35. None
  36. ͦͷଞͷػೳ wDISPPU  Ͱɺಠཱͨ͠ϑΝΠϧγεςϜπϦʔʹʮೖΓࠐΉʯ wηΩϡϦςΟपΓͷػೳ੍ݶ w,FSOFM$BQBCJMJUZͰSPPUͷػೳΛߜΓࠐΉʢٯ΋વΓʣ wTFDDPNQͰɺγεςϜίʔϧ୯Ґͷݺͼग़͠ΛϑΟϧλʔ w."$ .BOEBUPSZ"DDFTT$POUSPM 4&-JOVY

    "QQ"SNPS
  37. ΈΜͳ΋ίϯςφʹ ͳͬͯΈΑ͏ʁʁʁ

  38. ͜͜·Ͱͷ·ͱΊ wίϯςφ͸ͨͩͷ ϓϩηεɻා͘ͳ͍Αʂ

  39. ͱ͍͏͜ͱͰ wίϯςφΛܰʙ͘ϋϯυϝΠυͯ͠Έ·͠ΐ͏͔ɻ

  40.  ಠཱͨ͠SPPUʹ͸͍Δ wEFCPPUTUSBQͱݴ͏ίϚϯυͰɺ%FCJBOͷϑΝΠϧγεςϜ͚ͩΛ
 ४උͰ͖Δɻ wDISPPU  ͱݴ͏ίϚϯυͰɺͦͷதʹೖΔ͜ͱ͕Ͱ͖·͢ w ࣮͸%PDLFSͳ͠Ͱ΋ɺ6CVOUV౳ͷதͰ%FCJBO؀ڥΛಘΔ͜ͱ͸Ͱ͖Δ mkdir

    /root/railsdm-7th debootstrap --variant=minbase \ jessie \ /root/railsdm-7th \ http://ftp.jp.debian.org/debian
  41. None
  42.  /BNFTQBDFΛ෼཭͢Δ wVOTIBSFͱݴ͏ίϚϯυΛ࢖͏ͱͰ͖Δ •unshare --fork --pid --mount --uts wͷΑ͏ʹͯ͠ɺ1*%/BNFTQBDFɺ.PVOU/BNFTQBDFɺ654 /BNFTQBDFΛ෼཭Ͱ͖Δ


    ʢGPSL͸1*%/BNFTQBDFͷ࢓༷ʹΑΔ੍ݶʣ
  43.  DHSPVQͷ੍ݶΛ͔͚Δ w৽͍͠DHSPVQ͸ɺ
 NLEJSͳͲͰ؆୯ʹ࡞ΕΔ wࣗ෼Λॴଐͤ͞Δʹ͸ɺUBTLTͱ͍͏ϑΝΠϧʹࣗ෼ͷ1*%Λॻ͖ࠐ Ί͹0,ɻ •echo $$ > /sys/fs/cgroup/cpu/7th-engineer/tasks

  44.  Ұ࿈ͷॲཧΛͭͳ͛Δ $ cat after-unshare.rb #!/usr/bin/env ruby container_name = ARGV[0]

    raise unless container_name Dir.mkdir "/sys/fs/cgroup/cpu/#{container_name}" rescue puts("skip") File.write "/sys/fs/cgroup/cpu/#{container_name}/tasks", $$.to_s Dir.chroot "/root/#{container_name}" Dir.chdir "/" system "mount -t proc proc /proc" system "hostname #{container_name}.example.jp" exec "bash -l" ࠷ॳʹɺʮVOTIBSFޙʯͷॲཧΛ3VCZͰॻ͘
  45.  Ұ࿈ͷॲཧΛͭͳ͛Δɻ $ sudo unshare \ > --fork \ >

    --pid \ > --mount \ > --uts \ > `pwd`/after-unshare.rb railsdm-7th ઌ΄ͲͷεΫϦϓτʹ࣮ߦݖݶΛ͚ͭͯVOTIBSFͷҾ਺ʹ
  46. ͜Ε͸ίϯςφʁ wγεςϜ͕%FCJBOͰ͋Δ wϓϩηε͕ಠཱ͍ͯ͠Δɺϗετ໊΋ҧ͏

  47. ԋशͦͷ w  ઌ΄Ͳͷʮࣗ࡞ίϯςφʯʹ͍ͭͯɺ
 ผͷλʔϛφϧ͔ΒϗετʹೖΓɺҎԼΛ͔֬ΊͯΈΑ͏ɻ w  ϗετ͔Βݟͯίϯςφ͕ʮϓϩηεʯͰ͋Δ͜ͱ w 

    1*%ɺ.PVOUɺ654/BNFTQBDF͕෼཭͍ͯ͠Δ͜ͱ w  ίϯςφ಺Ͱ$16Λ͍͘Β࢖ͬͯ΋ɺϗετͷ$16Λ͔͠ ࢖Θͳ͘ͳΔΑ͏ɺDHSPVQΛઃఆͯ͠ΈΑ͏ɻ·ͨɺ֬ೝ͠Α͏
 IJOUIUUQTBDDFTTSFEIBUDPNEPDVNFOUBUJPOKB+13FE@)BU@&OUFSQSJTF@-JOVY IUNM3FTPVSDF@.BOBHFNFOU@(VJEFTFDDQVIUNM
  48. ॓୊

  49. ·ͱΊ

  50. ֮͑ͱ͘͜ͱͭ w-JOVYʹ͓͍ͯ͸ɺϓϩηεʹؔ͢Δͨ͘͞Μͷ৘ใ͕QSPDͷԼʹ ͋Δ͜ͱ w-JOVYίϯςφ͸ɺΧʔωϧͷϦιʔεʮ෼཭ʯͱʮ੍ݶʯͷػೳΛ ࢖ͬͨϓϩηεͰ͋Δ͜ͱ

  51. ֮͑ͱ͘͜ͱͭ w-JOVYʹ͓͍ͯ͸ɺϓϩηεʹؔ͢Δͨ͘͞Μͷ৘ใ͕QSPDͷԼʹ ͋Δ͜ͱ w-JOVYίϯςφ͸ɺΧʔωϧͷϦιʔεʮ෼཭ʯͱʮ੍ݶʯͷػೳΛ ࢖ͬͨϓϩηεͰ͋Δ͜ͱ wͲΜͳʹ೉ͦ͠͏ͳπʔϧͰ΋ɺ୭͔͕ॻ͍ͨ΋ͷͰ͋Γɺ
 ͦͯ͠044Ͱ͋ΔݶΓ͸த਎Λਂ͘஌Δ͜ͱ͕Ͱ͖Δͱ͍͏͜ͱ

  52. ࠓ೔࿩͞ͳ͔ͬͨ͜ͱ wίϯςφؔ࿈ͷͦͷଞͷػೳͷօ͞Μ wQJWPU@SPPU w,FSOFM$BQBCJMJUZ w3FTPVSDF-JNJU SMJNJU  wTFDDPNQ#1' w."$ 4&-JOVY"QQ"SNPS

    ΍-JOVY4FDVSJUZ.PEVMFT -4. 
  53. ѹ౗తࢀߟจݙ w ϖʔδͷຊΛಡΉલʹಡΜͰ͓͖͍ͨ wʮͳΔ΄Ͳ6OJYϓϩηεʯ wIUUQTUBUTV[JOFDPNCPPLTOBSVIPVOJY

  54. ͞ΒͳΔਂΈ w(.0ϖύϘڈ೥ͷ࠲ֶࢿྉ wIUUQTTQFBLFSEFDLDPNVE[VSBUIFTLFMUPOPGXIBMFT

  55. 13

  56. ίϯςφ௿ϨΠϠͷ࢓ࣄ ෱ԬͰͷࠊΛਾٕ͑ͨज़త੒௕ ͝ڵຯ͕͋Ε͹૬ஊʹ৐Γ·͢ GPMMPX!QC@SFDSVJU ΋͘͠͸!VE[VSBʹ௚઀