Upgrade to Pro — share decks privately, control downloads, hide ads and more …

コンテナ開発入門 1回目/Introduction to Container Developm...

muttan
October 24, 2018

コンテナ開発入門 1回目/Introduction to Container Development 1

muttan

October 24, 2018
Tweet

More Decks by muttan

Other Decks in Technology

Transcript

  1. Ծ૝Խٕज़ • ͦ΋ͦ΋ͳͥԾ૝Խٕज़Λಋೖ͢Δͷ͔ ‣ ίετύϑΥʔϚϯεͷ޲্ → Ϧιʔε࢖༻཰ͷ޲্ɾӡ༻ͷ୯७Խ ‣ ؀ڥͷִ཭ →

    ࣮ߦ͢Δ؀ڥΛִ཭͢Δ • ༷ʑͳཧ༝͕͋Δ͕, DockerΛϩʔΧϧͰ࢖͏৔߹, ࣗ ਎ͷ؀ڥΛԚͣ͞ʹ(ίϯςφ಺͚ͩͰดͨ͡)؀ڥͱ͠ ͯ࢖༻͢Δ͜ͱ͕ଟ͍
  2. Ծ૝Խٕज़ • Ծ૝؀ڥͷ෼ྨ ‣ ϋΠύʔόΠβܕ(XenͳͲ)
 ϋΠύʔόΠβͱݴΘΕΔιϑτ΢ΣΞΛ༻͍ͯ, ϚγϯશମΛԾ૝ Խ͢Δ(ڱٛ) ‣ ϗετOSܕ(VirtualBox,

    VMWareͳͲ)
 Ծ૝ԽΞϓϦέʔγϣϯΛϗετOS্ʹΠϯετʔϧ͠, ͦͷ্Ͱ ϋʔυ΢ΣΞͷಈ͖ΛγϛϡϨʔτ ‣ ίϯςφܕ(DockerͳͲ)
 ϗετOS্ʹίϯςφϥϯλΠϜ͕ଘࡏ͠, ͦͷ্Ͱίϯςφ͕Ք ಇ. ϗετOSͱΧʔωϧΛڞ༗͍ͯ͠Δ
  3. Ծ૝Խٕज़ ௨ৗͷγεςϜ ϋΠύʔόΠβܕ ϗετOSܕ ίϯςφܕ(Docker) ϗετ04 8JOEPXTͳͲ "QQ "QQ "QQ

    ϗετ04 8JOEPXTͳͲ "QQ "QQ "QQ Ծ૝Խιϑτ ήετ04 ήετ04 ϋΠύʔόΠβ "QQ ήετ04 "QQ ήετ04 "QQ ήετ04 ϗετ04 8JOEPXTͳͲ ίϯςφ ίϯςφ "QQ %PDLFS ɿԾ૝ԽྖҬ
  4. ίϯςφܕԾ૝Խ • ίϯςφܕԾ૝Խ͸Ҏલ͔Βଘࡏ ‣ LXC(Linux Containers) - Dockerొ৔લ͔Βଘࡏ͍ͯ͠Δ - ϦιʔεΛִ཭͠,

    ίϯςφΛ૸ΒͤΔ • Docker΋͔ͭͯ͸LXCΛ࢖༻͍ͯͨ͠ ‣ ݱࡏ͸runCͱ͍͏ϥϯλΠϜΛ࢖༻͠Ծ૝ԽΛ࣮ݱ
  5. ίϯςφܕԾ૝Խ LXC ϗετ04 8JOEPXTͳͲ -9$ Docker ϗετ04 8JOEPXTͳͲ %PDLFS LFSOFM

    ίϯςφ ίϯςφ ίϯςφ ίϯςφ ίϯςφ ίϯςφ LFSOFM SVO$ • DockerͷίϯςφܕԾ૝Խٕज़ͷਪҠ ‣ ͔ͭͯ͸LXCܦ༝ͰίϯςφΛ࣮ߦ ‣ ݱࡏ͸, cgourps, namespace౳Λ௚઀ୟ͘libcontainer ܦ༝Ͱ࣮ߦ
  6. ΞϓϦέʔγϣϯʹϑΥʔΧε • DockerͰ͸, ΞϓϦέʔγϣϯΛίʔυͰ؅ཧ͢Δ ‣ DSL(Dockerfile)ʹΑΔ؅ཧ - ίʔυͰߏ੒Λ؅ཧͰ͖Δ(Infrastructure as code)

    - ߏ੒Λόʔδϣϯ؅ཧ͢Δ͜ͱ͕Ͱ͖Δ • ΞϓϦέʔγϣϯ͚ͩͰͳ͘, ࣮ߦ؀ڥ(os)΋ಉ࣌ʹσ ϓϩΠ ‣ ࣮ߦ؀ڥ͝ͱ഑෍͢Δ͜ͱͰґଘؔ܎Λղফ DSL : domain-specific language
  7. Dockerͷߟ͑ํʹ;ΕΔ • DockerfileΛ࡞੒͢Δ ‣ ࢖༻͢ΔosΠϝʔδ΍, Πϯετʔϧ͕ඞཁͳύο έʔδ, ίϐʔ͢ΔϑΝΠϧͳͲΛهड़ ‣ γΣϧεΫϦϓτͱಉ͡֊૚ʹҎԼͷ಺༰

    Λ”Dockerfile”ͱ͍͏໊લͰอଘ FROM ubuntu:16.04 COPY helloworld /usr/local/bin RUN chmod +x /usr/local/bin/helloworld CMD ["helloworld"]
  8. Dockerͷߟ͑ํʹ;ΕΔ FROM ubuntu:16.04 COPY helloworld /usr/local/bin RUN chmod +x /usr/local/bin/helloworld

    CMD ["helloworld"] %PDLFSΠϝʔδ 04 Λࢦఆ ࠓճ͸VCVOUVͷΛ࢖༻ ϑΝΠϧΛίϐʔ γΣϧίϚϯυΛ࣮ߦ ίϯςφͱ࣮ͯ͠ߦ͢Δલʹ ࣮ߦ͍ͨ͠ίϚϯυΛهड़ • DockerϑΝΠϧͷߏ੒
  9. Dockerͷߟ͑ํʹ;ΕΔ • ࣮ߦ
 ·ͣ͸ίϯςφΛϏϧυ͢Δ 1 $ docker image build -t

    helloworld:latest . 2 Sending build context to Docker daemon 3.072kB 3 Step 1/4 : FROM ubuntu:16.04 4 ---> 5e8b97a2a082 5 Step 2/4 : COPY helloworld /usr/local/bin 6 ---> f21bf7b10524 7 Step 3/4 : RUN chmod +x /usr/local/bin/helloworld 8 ---> Running in 915c2aeac8fa 9 Removing intermediate container 915c2aeac8fa 10 ---> 879dda6427e0 11 Step 4/4 : CMD ["helloworld"] 12 ---> Running in fa5dee022556 13 Removing intermediate container fa5dee022556 14 ---> 6b9e6c2ffc9e 15 Successfully built 6b9e6c2ffc9e 16 Successfully tagged helloworld:latest docker image build -t helloworld:latest . ϐϦΦυ๨Εʹ஫ҙ ͏·͘Ͱ͖͍ͯͦ͏ʁ ࣮ߦ݁Ռ
  10. Dockerͷߟ͑ํʹ;ΕΔ • ࣮ߦ
 ίϯςφΛ૸ΒͤΔ docker container run helloworld:latest 1 $

    docker container run helloworld:latest 
 2 Hello, world ࣮ߦ݁Ռ l)FMMP XPSMEz͕ग़ྗ͞Ε͍ͯΔ ࣮ߦʹඞཁͳϑΝΠϧ΍ΞϓϦέʔγϣϯΛDockerΠϝʔδʹಉ࣮ࠝͯ͠ߦ͢Δ.
  11. ؀ڥࠩҟ͔Βͷ୤٫ • ΞϓϦέʔγϣϯ͸ৗʹԿ͔ʹґଘ͍ͯ͠Δ ‣ OS, CPU, ϝϞϦ, ݴޠϥϯλΠϜ, ϥΠϒϥϦ… •

    ͜ͷґଘ౓ΛͳΔ΂͘Լ͛Δ͜ͱͰ͜ͷ໰୊Λղܾ • ղܾํ๏ ‣ Infrastructure as CodeʢΠϯϑϥͷߏ੒؅ཧʣ ‣ Immutable InfrastructureʢෆมͳΠϯϑϥʣ
  12. Infrastructure as Code • Infrastructure as Code(IaC, Πϯϑϥͷߏ੒؅ཧ) ‣ ΠϯϑϥΛίʔυϕʔεͰ؅ཧ͠Α͏ͱ͍͏ߟ͑

    • ҎԼͷ಺༰ΛίʔυͰهड़ ‣ ͲͷΑ͏ͳαʔόߏ੒ʹ͢Δ͔ ‣ ͲͷύοέʔδΛΠϯετʔϧ͢Δͷ͔ • ਓ͕खಈͰߦ͏ͷͰ͸ͳ͘, Chef΍Ansibleͱ͍ͬͨ πʔϧΛ࢖ͬͯࣗಈԽΛߦ͏
  13. Infrastructure as Code • ͨͩ͠, IaC΋ສೳͰ͸ͳ͍ ‣ ྫ͑͹, ͋Δπʔϧͷstable൛Λಋೖ͍ͨ͠ͱ͖ʹ, λ

    ΠϛϯάʹΑͬͯ͸ಋೖ͞ΕΔόʔδϣϯ͕ҟͳΔ - ͜ΕΛ͚͞Δʹ͸, ႈ౳ੑ͕อূ͞ΕΔίʔυΛॻ ͘ඞཁ͕͋Δ - ႈ౳ੑ
  14. Infrastructure as Code • ႈ౳ੑͱ͸
 
 
 
 
 •

    ௚ײతͳཧղͱͯ͠͸, ಉ͡ૢ࡞ΛԿ౓ߦͬͯ΋ಉ͡ঢ়ଶͰ͋Δ ͜ͱ • ෳ਺ճࣗಈԽεΫϦϓτΛ࣮ߦͯ͠΋, ৗʹαʔόͷঢ়ଶ͕ಉ͡ Ͱ͋Δ৔߹, ႈ౳ੑ͕อͨΕ͍ͯΔͱߟ͑ΒΕΔ ႈ౳ੑͱ͸ʮԿ౓ಉ͡ૢ࡞Λͨ͠ͱͯ͠΋ಉ݁͡ՌΛಘΒΕΔʯͱ͍͏΋ͷ Ͱ͢ɻྫ͑͹ʮabcͱ͍͏ϢʔβʔΛࢦఆͷଐੑͰ࡞੒ͤΑʯͱ͍͏ʮϓϩ άϥϜʯΛ͋ͬͨͱ͠·͢ɻ͜ͷʮϓϩάϥϜʯΛԿճ࣮ߦͨ͠ͱͯ͠΋ɺ ʮabcʯͱ͍͏Ϣʔβʔ͕ࢦఆͷଐੑͰ࡞੒͞Ε͍ͯΔͱ͍͏݁ՌʹมΘΓ ͸ͳ͍ͱ͍͏ࣄͰ͢ɻͭ·ΓઌͷྫͰݴ͑͹ɺύοέʔδ͕Πϯετʔϧࡁ ΈͳΒ͹ʮԿ΋͠ͳ͍ʯͷ͕๬·͍͠ಈ͖ʹͳΓ·͢ɻ Ҿ༻ɿhttp://www.intellilink.co.jp/article/column/devops02.html
  15. Infrastructure as Code • Chef΍AnsibleͰ΋ႈ౳ੑΛอͬͨΠϯϑϥΛߏ੒͢Δ ͜ͱ͸Մೳ ‣ ࣮ࡍ͸࣮૷࣍ୈͳͱ͜Ζ͕͋Γ, ׬શʹอূ͸͞Εͯ ͍Δͱ͍͏Θ͚Ͱ͸ͳ͍

    ‣ ৗʹႈ౳ੑ͕อূ͞Ε͍ͯΔίʔυΛॻ͖ଓ͚Δͷ͸ ӡ༻্େ͖ͳෛ୲ͱͳΓಘΔ - ͦ͜ͰImmutable Infrastructure͕ొ৔
  16. Immutable Infrastructure • Immutable Infrastructure(ෆมͳΠϯϑϥ)
 ͋Δ࣌఺ͷαʔόͷঢ়ଶΛอଘ͓͖ͯ͠, ͍ͭͰ΋ෳ੡ ΛՄೳʹ͓ͯ͘͠ ‣ ਖ਼͘͠ಈ࡞͍ͯ͠Δঢ়ଶΛอଘ͓͚ͯ͠͹,

    ෳ੡ͨ͠ αʔό΋ಉ༷ʹಈ࡞͢Δ͜ͱ͕อূ͞ΕΔ ‣ αʔόͷมߋ͸, طଘͷαʔόʹखΛՃ͑ΔͷͰ͸ͳ ͯ͘৽͍͠Πϝʔδͱͯ͠อଘ͢Δ
  17. IaCͱImmutable Infrastructure • Docker͸, IaCͱImmutable InfrastructureΛ௿ίετ͔ ͭ؆୯ʹ࣮ݱ͢Δ͜ͱ͕Մೳ ‣ DockerfileʹΑͬͯߏ੒Λ؅ཧ(IaC) ‣

    OS෦෼ͷଟ͘ΛϗετOSͱڞ༗͍ͯ͠ΔͨΊ, ىಈ ͕ඇৗʹߴ଎ͳͨΊ, ৽͘͠Πϯϑϥߏ੒Λ࡞Γ௚͢ Immutable Infrastructureͱ૬ੑ͕ྑ͍
  18. DockerͷϙʔλϏϦςΟ • ௨ৗ, ࡞੒ͨ͠ΞϓϦέʔγϣϯΛผͰ༻ҙͨ͠αʔό (Πϯϑϥ)্ʹల։͢Δ͜ͱͰαʔϏεΛӡ༻ ‣ ΞϓϦέʔγϣϯͱΠϯϑϥ͕׬શʹ෼཭ ‣ αʔό෦෼ͷઃఆͷࠩҟͳͲ͕ੜ·ΕΔՄೳੑ •

    Dockerίϯςφʹ͸, OS(Πϯϑϥ)ͱΞϓϦέʔγϣϯ ΛηοτͰϏϧυՄೳ ‣ Ϗϧυͨ͠Πϝʔδ͸ৗʹಉ͡ͳͷͰ, Docker͕Πϯε τʔϧ͞Ε͍ͯΔϚγϯͰ͋Ε͹ಉ͡؀ڥΛ࠶ݱՄೳ
  19. ΞϓϦέʔγϣϯߏ੒؅ཧͷ͠΍͢͞ • Docker Compose
 ෳ਺ͷίϯςφΛ࢖͏DockerΞϓϦέʔγϣϯΛఆ ٛɾ࣮ߦ͢ΔͨΊͷπʔϧ ‣ yamlܗࣜͰΞϓϦέʔγϣϯΛఆٛ 1 version:

    "3" 2 services: 3 web: 4 image: gihyodocker/web 5 ports: 6 - "3000:3000" 7 environment: 8 REDIS_TARGET: redis 9 depends_on: 10 - redis 11 12 redis: 13 image: "redis:alpine"
  20. ΞϓϦέʔγϣϯߏ੒؅ཧͷ͠΍͢͞ 1 version: "3" 2 services: 3 web: 4 image:

    gihyodocker/web 5 ports: 6 - "3000:3000" 7 environment: 8 REDIS_TARGET: redis 9 depends_on: 10 - redis 11 12 redis: 13 image: "redis:alpine" ←࢖༻͢ΔDockerΠϝʔδ ←ϙʔτϑΥϫʔσΟϯά ←؀ڥม਺ ←ґଘؔ܎
 (webͷલʹredisΛىಈ) ←KVSͷىಈ
  21. ΞϓϦέʔγϣϯߏ੒؅ཧͷ͠΍͢͞ • ࣮ࡍ͸, ͜ͷߏ੒Λෳ਺୆༻ҙ͢Δ͜ͱ΋͋Δ ‣ ෛՙରࡦͰεέʔϧΞ΢τ͢Δ৔߹ͳͲ LoadBalancer αʔό
 (Dockerϊʔυ) ←docker-compose.ymlΛద༻

    ←docker-compose.ymlΛద༻ ←docker-compose.ymlΛద༻ ͨͩ͠, Docker Compose୯ମ͸, 1ͭͷαʔόͰͷ࢖༻Λ૝ఆ →ෳ਺୆࢖͑ΔΑ͏ʹ͍ͨ͠