Pro Yearly is on sale from $80 to $50! »

個人で使ってみた Docker と CoreOSとか

個人で使ってみた Docker と CoreOSとか

7/11社内勉強会の資料 by hoshinotsuyoshi at feedforce
http://tech.feedforce.jp/docker_coreos.html
図らずもCentos7もdemoで使ってみた

B122e77de860c4307034f0a041dabc8e?s=128

hoshino tsuyoshi

July 17, 2014
Tweet

Transcript

  1. ݸਓͰ࢖ͬͯΈͨ Docker'ͱ'CoreOSͱ͔ ٕज़νʔϜษڧձ!#199 2014/7/11 ٕज़νʔϜ!੕໺߶ࢤ

  2. ໨࣍ ংষ ຊ೔ͷ໰୊ҙࣝ ຊ୊ *"Docker"࢖͍ํ *"CoreOSͰͷBootstrapʙDeploy *"Dockerfile"CIͷఏҊ

  3. =begin ংষ (ʙϓϩϩʔάʙ)

  4. ݸਓͷ࿩

  5. 2009೥ࠒ

  6. ॳΊͯݸਓͰ͘͞ΒVPSΛआΓ·͠ ͨ

  7. आΓͨVPSʹ͋Γ͕ͪͳ͜ͱ in#Զ

  8. develop'>'(trial&error)'>'deploy

  9. deploy'>'(..਺ϲ݄ޙ..)'>'deploy

  10. ͳΜ͔ࢮ͵

  11. (ཧ༝͸Θ͔ΒΜ)

  12. (ཧ༝͸ຊ౰ʹΘ͔ΒΜ)

  13. ৭ʑ͋Γ͗ͯ͢Α͘Θ͔Βͳ͍

  14. ௚઀ͳݪҼ͸Α͘Θ͔ΒΜ͚Ͳ ؒ઀తͳݪҼ͸Α͘Θ͔ͬͯΔ

  15. ϛυϧ΢ΣΞ!΁ͷมߋ MySQL/Apache

  16. ౓ॏͳΔ trial&error

  17. ཁ͸ɺ్தͰߦͬͨมߋΛه࿥ͯ͠ ͍ͳ͔ͬͨ

  18. Πϯϑϥߏங΋ه࿥Λͱ͓ͬͯ͘ (ίʔυʹམͱ͓ͯ͘͠) ͷ͕େࣄͩͱૉਓ໨ʹ௧ײ

  19. ͨͩɺ్தͰग़ձͬͨਆπʔϧ

  20. σϓϩΠͷੈ୅ͷ؅ཧ

  21. ηοτΞοϓͷํ๏͸๨Ε͕ͨ ͦͷޙͷӡ༻͕҆৺ʹͳͬͨ(ؾ͕ ͢Δ)

  22. !!!

  23. None
  24. 2014೥ࠒ

  25. ฐࣾೖࣾ

  26. ৬৔Ͱͷ࿩

  27. Cͷͭ͘ΞϨ"ʹՃ͑

  28. Cͷͭ͘ΞϨ"΋

  29. None
  30. ࢥ͑͹ฐࣾ

  31. rubyΛ࢖͏ਓ%/%ٕज़νʔϜͷਓ

  32. ໿115% (੍࡞͕erb࢖͍ͬͯΔ) (஫ɿԶ໨ઢ)

  33. ͋Δछͷඞવ

  34. None
  35. ʮϓϩϏδϣχϯάͷ̏૚ʯ h"p:/ /mizzy.org/blog/2013/10/29/1/ h"p:/ /en.oreilly.com/velocity2mar2010/public/schedule/detail/ 14180

  36. Orchestra)on Fabric,(Capistrano,(MCollec2ve Configura)on Puppet,'Chef,'AWS'OpsWorks Bootstrapping Kickstart,*Cobbler,*OpenStack,*AWS

  37. ؾ෼ͷ໰୊ (஫ɿग़యԶ)

  38. Orchestra)on (ʆ˜´) Configura)on (;´ρ`) Bootstrapping (ʆ˜´)

  39. Configura)on (;´ρ`)

  40. ႈ౳.͕Μ͹Δ!

  41. ႈ౳.͕Μ͹ΕΔ?

  42. ͋ΔͭΒΈͷ͋ΔPR

  43. None
  44. None
  45. None
  46. None
  47. ͋ΔͭΒΈͷ͋ΔPR2

  48. None
  49. None
  50. ࠓͭΒ͍࿩͕͍ͬͺ͍ग़ͨؾ͕͠· ͢ (ͳΜͰݹ͍nkfೖΕΔͷɺͱ͔͸๨Εͯͩ͘ ͍͞)

  51. ႈ౳.͕Μ͹ΕΔ?.!.!.!

  52. ୭͕ѱ͍ͷ͔

  53. ϓϩϏδϣχϯάͷ̏૚

  54. Orchestra)on (ʆ˜´) Configura)on (;´ρ`) Bootstrapping (ʆ˜´)

  55. ͯ͞ɺ·ͨݸਓͷ࿩

  56. ͠ΐ΅͍Τϯλϝܥ()WebΞϓϦΛ ӡ༻ heroku͕৸ͳ͍ఔ౓ͷPV rails4'/'MongoDB'/'heroku

  57. Ұࡢ೥͋ͨΓ͔Β ӡ༻ͩͯ͠͠ deployʹ͍ͭͯࢸͬͨ݁࿦

  58. heroku࠷ڧઆ

  59. None
  60. $"git"push"heroku"master

  61. ͭ·Γ"Orchestra*on"͚ͩ

  62. "Configura*on"/"Bootstrapping"͸ରԠෆཁ

  63. جຊແྉ

  64. ϛυϧ΢ΣΞ͸ΞυΦϯ(ແྉ/༗ྉ)

  65. database logging metrix worker

  66. ͍͟ͱͳΕ͹͓ۚͰղܾͰ͖Δ (͠ɺ͍͍ͨͯͳΜͱ͔ͳΔ)

  67. (ʆ˜´)

  68. ݸਓͰ༡Ϳ෼ʹ͸࠷ڧ͕ͩɻɻɻ ආ͚ΒΕͳ͍໰୊͕͋Δ

  69. ϨΠςϯγ

  70. (ແྉΞυΦϯͰ)MySQLαʔόήοτ΍ʔ ! ೔ຊͷWEBαʔόʔ͔ΒΞΫηε ! ೔͕฻ΕΔ

  71. GoogleSpeedInsight ! ੒੷ΘΖ͠ (ͨͩͬͪ͜͸ͲͪΒ͔ͱ͍͏ͱύϑΥʔϚϯ ε?)

  72. ΞϨͷTokyoϦʔδϣϯ࢖͑͹͑͑ ΍Ζ

  73. VPS࢖͑͹͑͑΍Ζ

  74. (Herokuָ͡Όͳ͍Ͱ͔͢...)

  75. None
  76. ʙϓϩϩʔά =end

  77. ຊ೔ͷ໰୊ҙࣝ

  78. C͕ͭ͘ΞϨ͕ͭΒ͗͢Δ গͳ͘ͱ΋ݸਓͰ͸Ϡμ heroku͕ྑ͍ͷ͸ɺOrchestra,on͚͔ͩͩΒ ΋͠VPSͰӡ༻͢Δͱͨ͠Β? ϨΠςϯγղܾͯ͠Έ͍ͨ

  79. TRY:%VPS΁ͷճؼ(ͭΒͦ͏)

  80. ࡞ۀ͸ίʔυͰ࢒͍ͨ͠ Inflastructure+as+Code chefݏ͍ͳࢲ͸ɺͲ͏͢Ε͹ྑ͍͔

  81. ຊ୊ *"Docker"࢖͍ํ(·ͣ࠷ॳʹઆ໌) *"CoreOSͰͷBootstrapʙDeploy *"Dockerfile"CIͷఏҊ

  82. CoreOSΛVPSͰ࢖͏

  83. CoreOS

  84. Docker'on'CoreOS

  85. Configura)on/Orchestra)on systemd/Docker Bootstrapping ipxe%+%VultrVPS

  86. Docker'νϡʔτϦΞϧ

  87. Dockerͱ͸

  88. ୺తͳઆ໌ h"p:/ /www.ryuzee.com/contents/ blog/6952

  89. Ծ૝Խٕज़ "ίϯςφ୯ҐͰύοέʔδϯά VirtualBoxͱ͔ͱҧͬͯߴ଎ɺΦʔόʔϔου͕গͳ͍ɻchrootʹ ͍ۙɻLXCʹ͸ґଘ͠ͳ͘ͳ͍ͬͯΔ ίϯςφ͝ͱʹID͕ৼΒΕΔ ίϯςφ͸ࠩ෼อଘͳͷͰϩʔϧόοΫ΋؆୯ Ұճ࡞Ε͹Ͳ͜Ͱ΋ಈ͘ DockerfileͰίϯςφΛ࡞੒͢Δ Dockerfileͷ1ߦ͝ͱʹίϯςφID͕ϑϥΕΔ"

  90. Կ͕͏Ε͍͠ͷʁ "ߴ଎ʹىಈ͢ΔɻैͬͯCI༻ʹ࢖ͬͨΓɺChefͷΫοΫϒοΫͷ ࣮ݧ؀ڥʹ࢖ͬͨΓɺ։ൃ؀ڥʹ࢖ͬͨΓ৭ʑศརɻ΋͏ຊ൪؀ ڥͰ࢖͍ͬͯΔྫ΋ଟ਺ ίϯςφ಺ʹด͡ࠐΊΔ͜ͱͰϙʔλϏϦςΟ͕͕͋ΔɻΞϓϦ έʔγϣϯͷσϓϩΠઓָུ͕ͳํʹେ͖͘มΘΓ͏Δ ࠩ෼Ͱ؅ཧ͞ΕΔͷͰ഑෍ͱ࠶ར༻͕؆୯ ͍ΖΜͳ؀ڥͰಈ͘ DockerfileΛ࢖ͬͯίʔυͰΠϯϑϥΛ؅ཧͰ͖Δ"

  91. None
  92. (΍Γ·͠ΐ͏)

  93. centos(vagrant)Ͱ΍ͬͯΈ·͢

  94. $"vi"Vagrantfile Vagrant.configure('2') do |config| config.vm.box = "hfm4/centos7" config.vm.network :private_network, ip:

    "192.168.100.101" config.vm.network :forwarded_port, guest: 10080, host: 10080 end
  95. $"vagrant"up"&&"vagrant"ssh

  96. Πϯετʔϧɾىಈ֬ೝ $"sudo"yum")y"install"docker)io $"sudo"service"docker"start $"sudo"docker"ps

  97. άϧʔϓͷ௥Ճ $"sudo"groupadd"docker $"sudo"gpasswd"+a"vagrant"docker $"sudo"service"docker"restart

  98. ίϯςφΛ࡞Δ·Ͱͷ࡞ۀ ஫ɿग़యԶ

  99. None
  100. $"mkdir"mydocker $"cd"mydocker $"vi"Dockerfile

  101. First&Stage

  102. The$first$Dockerfile$instruc1on "FROM"

  103. ͳʹ͸ͳ͘ͱ΋FROMͱॻ͘

  104. FROM debian:jessie

  105. ͓ΊͰͱ͏! ͜ΕͰ׬ᘳͳDockerfile͕ग़དྷ্͕ Γ·ͨ͠! ɹ ɹ (photo:'zarpie.deviantart.com/art/Twilight8Sparkle8is8Happy8280187353)

  106. ͭ͗͸ docker buildͯ͠Έ·͠ΐ͏

  107. $ docker build -t hoshinotsuyoshi/perfect . (࠷ޙͷ.͕ΧϨϯτσΟϨΫτϦͷDockerfile ୳ͯ͠Δ)

  108. $ docker images ͍ͤ͘͞ɹ͔͘ʹΜὑ

  109. ͓ΊͰͱ͏! ͜ΕͰ͸͡Ίͯimage͕ग़དྷ্͕Γ ·ͨ͠! ɹ ɹ (photo:'minnie,themousekid.deviantart.com/art/Happy,Birthday,Cake,181149361)

  110. run͠·͢ $"docker"run"hoshinotsuyoshi/ perfect

  111. ಈ͍͔ͨ? $"docker"ps

  112. $"docker"ps"+a "Exit(0)"

  113. docker'runͷUSAGE Usage:'docker'run'[OPTIONS]' IMAGE'[COMMAND]'[ARG...]

  114. ࠶ͼrun͠·͢ $"docker"run" hoshinotsuyoshi/perfect//bin/bash

  115. $"docker"ps"+a "Exit(0)"

  116. docker'runͷΦϓγϣϯ !i,$!!interac+ve=false$(Keep$stdin$ open$even$if$not$a6ached) !t,$!!%y=false$(Allocate$a$pseudo!%y)

  117. ࠶ͼrun͠·͢ $"docker"run"+it"hoshinotsuyoshi/ perfect"/bin/bash

  118. ͓ΊͰͱ͏! jessieͷίϯςφ͕ಈ͍ͨ sshΈ͍ͨʹೖΕͨ! ɹ (photo:'h(ps:/ /www.flickr.com/photos/goodncrazy/9680244249)

  119. $"cat"/etc/debian_version

  120. ͜ͷdebianίϯςφൈ͚·͢ $"exit"100

  121. ֎͔ΒݟΔͱɻ100͕ه࿥͞Εͯ Δɻ $"docker"ps"+a

  122. Next%Stage...

  123. "CMD"

  124. "CMD" ... docker'run'ͨ͠ͱ͖ͷ σϑΥϧτͷಈ͖Λఆٛ͢Δ

  125. None
  126. FROM%debian:jessie CMD%["/bin/bash",%"6c",%"for((i=1;i<=1000;i+=1));%do%sleep%1%;%echo% \"hello%debian%$i%Gmes\";%done"]

  127. $"docker"build"-t"hoshinotsuyoshi/ perfect2". ͦͯ͠ $"docker"images

  128. ͜ΕΛrunͯ͠Έ·͢ $"docker"run"++name"perfect2" hoshinotsuyoshi/perfect2 (ࠓճ͸"##nameΦϓγϣϯͰίϯςφ໊ࢦఆ ͯ͠Έ·͢)

  129. (ࢭΊํΘ͔Μͳ͍)

  130. (ผ୺຤ͱ͔ͰࢭΊ·͢)

  131. ࠓ౓͸!dΦϓγϣϯ(detach)Ͱrun͠ ·͢ $"docker"run"+d"++name"perfect2" hoshinotsuyoshi/perfect2

  132. (ίϯςφID͕͔͖͑ͬͯͨ)

  133. த਎ΛݟΔʹ͸docker'a)ach $"docker"a*ach",,sig,proxy=false" perfect2

  134. (docker(rm(*fͰstop(+(rm)

  135. ࣮͸ run࣌ͷ࠷ޙʹίϚϯυΛ༩͑Δͱ CMDͷ಺༰্͕ॻ͖͞ΕΔ $"docker"run"++name"perfect2" hoshinotsuyoshi/perfect2"/bin/echo" hey

  136. (docker(rm(*fͰstop(+(rm)

  137. Next%Stage...

  138. "RUN"

  139. "RUN" ... Ϗϧυதʹ࣮ߦ͍ͨ͠ίϚϯυΛࢦ ఆ͢Δɻ

  140. DockerfileʹRUNΛॻ͍ͯΈΔ

  141. FROM%debian:jessie RUN%apt3get%update RUN%apt3get%3y%install%apache2

  142. $"docker"build"-t"hoshinotsuyoshi/ apache". $"docker"images

  143. (apacheಈ͔͢ʹ͸Ͳ͏͢Ε͹͍͍ Μ͚ͩͬ)

  144. $"docker"run"+it"hoshinotsuyoshi/ apache"/bin/bash $"which"apache2 $"service"apache2"start

  145. None
  146. ͪΐͬͱ࿬ಓ $"vagrant"share

  147. None
  148. ...Ҏ্ɺجຊͷinstruc)on+3ͭΛݟ ͖ͯ·ͨ͠ FROM CMD RUN

  149. ͦͷଞͷDockerfile)instruc/on

  150. ADD#...#file΍dirΛίϯςφʹίϐʔ͢ΔΑ MAINTAINER#...#ϝϯςφΛೖΕΔ EXPOSE#...#ۭ͚Δϙʔτ൪߸Λࢦఆ ENV#...#؀ڥม਺Λࢦఆ COPY#...#΄΅ADDͱҰॹ?(URLࢦఆͰ͖ͳ͍Β͍͠) ENTRYPOINT#...#CMDͷ਌ੰɺrunͰΦʔόʔϥΠτ͞Εͳ͍ VOLUME#...#ϗετ΍ଞίϯςφͱͷؒͷϚ΢ϯτϙΠϯτ USER#...#Ϗϧυͷ్தͰϢʔβม͑Δ WORKDIR#...#Ϗϧυͷ్தͰσΟϨΫτϦม͑Δ ONBUILD#...#docker0.8Ͱొ৔ɻࢠϏϧυ޲͚ͷ໋ྩΛॻ͚Δɻ

  151. ...͜ΕͰDockerfileͷinstruc/onશ ෦!

  152. Dockerfileͷઆ໌ऴΘΓ

  153. Docker'دΓಓ

  154. ϝϞϦࢦఆͱ͔΋ग़དྷΔΒ͍͠ $"docker"run"+c"256"+m"512m"+it" hoshinotsuyoshi/perfect2"/bin/bash

  155. ϓϥΠϕʔτͳϨδετϦʹ͍ͭͯ $"docker"pull"registry ͱ͔΍ΔͱͰ͖ΔΒ͍͠

  156. Docker'دΓಓ͓ΘΓ

  157. Dockerfile)σϓϩΠͷͨΊͷςΫχ οΫ

  158. Dockerfileͱ ࠩ෼Ϗϧυ

  159. ࣗ෼ͷϕʔεΠϝʔδΛ࡞͓ͬͯ͘ ͱศརͱ͍͏࿩

  160. None
  161. None
  162. ͭ·Γ̎ͭͷΠϝʔδΛ༻ҙ͢Δ [Configura*onࡁΈͷ΍ͭ] [Orchestra*on༻ͷ΍ͭ]

  163. [Configura*onࡁΈ] ϛυϧ΢ΣΞΠϯετʔϧࡁΈ hoshinotsuyoshi/ruby3mongo h"ps:/ /registry.hub.docker.com/u/hoshinotsuyoshi/mongo6ruby/ h"ps:/ /github.com/hoshinotsuyoshi/Dockerfiles/blob/master/ mongo9ruby/Dockerfile

  164. [Orchestra*on༻] WEBΞϓϦͷσϓϩΠ hoshinotsuyoshi/actress h"ps:/ /registry.hub.docker.com/u/hoshinotsuyoshi/actress/ h"ps:/ /github.com/hoshinotsuyoshi/Dockerfiles/blob/master/ actress/Dockerfile

  165. Configura)onࡁΈͷ΍ͭΛ࠶ར༻ registry.hub.docker.comʹΞοϓͰ ͖Δ Ϗϧυ͕ૣ͍ͷ͕͏Ε͍͠

  166. ͰɺͲ͏σϓϩΠ͢Δͷ͕͔ͬ͜ྑ ͍͔ (docker(runͰ΋͍͍Μ͚ͩͲ)

  167. CoreOSྲྀʹsystemdͰ΍ͬͯΈΔ

  168. ܰ͘ɹCoreOSͷઆ໌

  169. খͯ͘͞ݎ࿚ͳίΞ Ϋϥελ!+!ίϯςφ

  170. ࠼Δཁૉ docker'...'ίϯςφͷ؅ཧ systemd'...'init.dͷ୅ସ etcd%...%ઃఆ৘ใΛڞ༗Ͱ͖ΔKVS fleet$...$Ϋϥελ؅ཧ

  171. cent7ʹ͸systemd͸ೖ͍ͬͯΔ

  172. ࠓ೔͸͔ͤͬ͘ͳͷͰsystemd cent7Ͱ΍Γ͍ͨͱࢥ͍·͢!!

  173. (systemd͋·Γௐ΂ͳ͔ͬͨͷͰ ͱ͍͏͔͘͢͝೉͔ͬͨ͠ͷͰ͋·Γ ͭͬ͜·Εͯ΋౴͑ΒΕ·ͤΜ)

  174. طଘͷUnitϑΝΠϧ $"ls"%alF"/etc/systemd/system

  175. ͜Μͳײ͡ʹฤू $"sudo"vi"/etc/systemd/system/ docker1actress.service

  176. [Unit] Description=Actress Service After=docker.service Requires=docker.service [Service] User=root ExecStart=/bin/sh -c \

    "/usr/bin/docker run --name actress -p 10081:3000 hoshinotsuyoshi/actress" ExecStop=/usr/bin/docker rm -f actress [Install] WantedBy=local.target
  177. [Unit] [Unit] Description=Actress Service After=docker.service Requires=docker.service

  178. [Service] [Service] User=root ExecStart=/bin/sh -c \ "/usr/bin/docker run --name actress

    -p 10081:3000 hoshinotsuyoshi/actress" ExecStop=/usr/bin/docker rm -f actress
  179. [Install] [Install] WantedBy=local.target

  180. $"sudo"systemctl"daemon/reload $"sudo"systemctl"start"docker0 actress"#"Start! $"sudo"systemctl"stop"docker0 actress"#"Stop! ·ΔͰserviceͷΑ͏ʹѻ͑Δ

  181. (σϞ)

  182. ·͞ʹImmutable

  183. ۘࠂ ͔͜͜Β͋·Γࢿྉͳ͍

  184. (σϞ) ΦϨΦϨεΫϦϓτىಈ͠·͢ CoreOSαʔόʔௐୡσϞ ࠷ऴతʹ͸Jenkins͓ͬ͞ΜΛಈ͔ ͍ͨ͠

  185. ΦϨΦϨεΫϦϓτͨͪ script1:)VPSങͬͯscript2/3Λىಈ script2:)ipxe script3:)cloud.config

  186. script1:)VPSങͬͯscript2/3Λىಈ

  187. ͜ͷͨΊʹvultr&APIୟ͘Gem࡞ͬͨ

  188. script1:)VPSങͬͯscript2/3Λىಈ gistʹ͓͍ͨ h"ps:/ /gist.github.com/hoshinotsuyoshi/627362efc554e2fd9a5f

  189. script2:)ipxe herokuʹ͓͍ͨ (Vultrͷipxechainurl͸ɺh,psͰ͸͏·͍͔͘ͳ͔ͬ ͨɻ) h"p:/ /young+dawn+7740.herokuapp.com/script.txt CoreOSެࣜϖʔδͷ಺༰Λࣸܦ

  190. script3:)cloud.config secret&gistʹஔ͍ͨ h"ps:/ /gist.githubusercontent.com/hoshinotsuyoshi/ 4421f1d7754db9629903/raw/ 7b005f4e8438434f48b7f03b50946c9c6731c0a1/testB config.config CoreOSެࣜϖʔδͷ಺༰͔Β࡞੒

  191. ruby%setup.rb͢Δ͚ͩͰ αʔό͕ىಈͯ͠ Jenkinsಈ͍ͨ! (σϞ͓ΘΓ)

  192. ͰɺϨΠςϯγͷ໰୊

  193. before h"p:/ /xn((o1q8h80u333a.xn((u9jw89o.com/

  194. a"er h"p:/ /108.61.201.67

  195. before

  196. a"er

  197. (ʆ˜´)

  198. None
  199. ·ͱΊ(ʆ˜´) CoreOS'+'cloud-configͰBootstrapʙ Orchestra7on·ͰͷશͯΛهड़Ͱ͖Δ! VultrͰϨΠςϯγͷ໰୊͸ղܾͨ͠!

  200. ࡶײ Bootstrap͔ΒOrchestra,on·ͰΛίʔυԽ! CI༻్ʹ͸޲͍ͯΔ ͋ͱɺηοτΞοϓ͕೉͍͠αʔϏεͱ͔͕ ͍͍͔΋ ex.$Jenkinsɺ؂ࢹαʔόʔ

  201. thank&you

  202. ʢεϥΠυऴΘΓʣ