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

コードを書くことに集中したい全てのアプリ開発者に贈るKubernetesの話 / Kube...

コードを書くことに集中したい全てのアプリ開発者に贈るKubernetesの話 / Kubernetes Tips for all application engineers who want to focus on coding

コンテナネイティブな時代、アプリケーションエンジニアに求められる技術領域はさらに広がりを見せつつあります。インフラとアプリの境界を変えたコンテナ技術にどこまで踏み込めばいいのか迷っているアプリケーションエンジニアは多いと思います。このセッションでは、アプリケーションエンジニアがコーディングに集中するために知っておきたいコンテナ技術のおさらいから、Kubernetesとその周辺ツールの基本をデモも交えて説明します。(Japan Container Days v18.12)

Yoshiaki Mizukura

December 05, 2018
Tweet

More Decks by Yoshiaki Mizukura

Other Decks in Technology

Transcript

  1. 2 αΠΦεςΫϊϩδʔגࣜձࣾ ٕज़2෦ ΞϓϦέʔγϣϯίϯαϧςΟϯάάϧʔϓ γχΞΞʔΩςΫτ ਫ૔ ྑ໌ About me •

    Ϋϥ΢υج൫த৺ͷΠϯϑϥߏங͔ΒΞϓϦ։ൃ • σʔλ෼ੳج൫ߏங • WebΞϓϦ։ൃʢΤϯλʔϓϥΠζܥʣ Powered by https://wordart.com/
  2. 3 • ΞϓϦ࣮ߦ؀ڥͷύοέʔδԽʹΑΔ؀ڥؒࠩҟͷۃখԽ • ίϯςφҎલɾɾɾ • ։ൃ؀ڥͰಈ͘ίʔυ͕ຊ൪؀ڥͰಈ͔ͳ͍ • ΞϓϦͱ࣮ߦ؀ڥͷߏ੒؅ཧͷ͠΍͢͞ •

    Immutableͳ࣮ߦ؀ڥʹΑΔႈ౳ੑͷ୲อ ։ൃऀʹͱͬͯͷίϯςφϝϦοτ͓͞Β͍ ΞϓϦ֎෦ͷ؀ڥґଘʹ೰·͞ΕΔ͜ͱ͕ݮΓɺਝ଎ͳσϦόϦ͕ՄೳʹͳΔ
  3. 5 ߏஙγεςϜ୯ҐͷมԽ γεςϜ͝ͱʹVMΛ࡞Δ γεςϜ͝ͱʹίϯςφΛ࡞Δ Ծ૝ج൫ H/W H/W H/W VM OS

    M/W App Ծ૝ج൫ H/W H/W H/W M/W App M/W App M/W App M/W App ίϯςφج൫ VM OS VM OS VM OS VM OS M/W App VM OS M/W App VM OS M/W App VM OS ैདྷͷΠϯϑϥ͕ΞϓϦͱηοτʹͳͬͨʢΠϯϑϥͱΞϓϦͷڥքมԽʣ VMʢඇίϯςφʣ ίϯςφ
  4. 6 ΠϯϑϥͱΞϓϦͷڥքมԽ Ծ૝ج൫ H/W H/W H/W M/W App M/W App

    M/W App M/W App ίϯςφج൫ VM OS VM OS VM OS VM OS ίϯςφߏ੒ఆٛ ΞϓϦʹՃ͑ͯϛυϧ΢ΣΞɺωοτϫʔΫɺετϨʔδߏ੒ͷཧղ σʔλӬଓԽ ωοτϫʔΫ Docker Compose Kubernetes ႈ౳ͳ࣮ߦ؀ڥΛखܰʹߏஙͰ͖ΔΑ͏ʹͳͬͨ
  5. 7 • ίϯςφͷϝϦοτ͸ཧղͰ͖Δ΋ͷͷɾɾɾ ΞϓϦ։ൃऀͷށ࿭͍ ͦ΋ͦ΋։ൃͷྲྀΕͲ͏ͳΔͷʁ Logging CI/CD HTTPηογϣϯ؅ཧ σϓϩΠ؀ڥͷࠩҟ ؂ࢹ

    ϚΠΫϩαʔϏε޲͖ͱ͸ฉ͚͘Ͳɾɾɾ ϦτϥΠ ෛՙ෼ࢄ ෼ࢄϩάͷτϨʔε ྲྀྔ੍ݶ αʔΩοτϒϨʔΧ ো֐ݕ஌ αʔϏεؒͷґଘؔ܎ ςετ
  6. 10 • ίϯςφͷϝϦοτ͸ཧղͰ͖Δ΋ͷͷɾɾɾ ΞϓϦ։ൃऀͷށ࿭͍ʢ࠶ܝʣ ͦ΋ͦ΋։ൃͷྲྀΕͲ͏ͳΔͷʁ Logging CI/CD HTTPηογϣϯ؅ཧ σϓϩΠ؀ڥͷࠩҟ ؂ࢹ

    ϚΠΫϩαʔϏε޲͖ͱ͸ฉ͚͘Ͳɾɾɾ ϦτϥΠ ෛՙ෼ࢄ ෼ࢄϩάͷτϨʔε ྲྀྔ੍ݶ αʔΩοτϒϨʔΧ ো֐ݕ஌ αʔϏεؒͷґଘؔ܎ ςετ
  7. 12 Delivery Pipeline base Image Image Coding Test Dockerfile /Manifest

    Release Legacy Container-based खॱॻ Param Sheet build & deploy Code M/W M/W APP खॱॻ Param Sheet Code Manifest Dockerfile ؀ڥߏங push ؀ڥߏங ؀ڥߏங ؀ڥߏங ؀ڥߏங build Image Registry Image Registry Code Repositry Code Repositry push pull push build push deploy deploy Manifest (for Test) Manifest (for Production) ؀ڥߏங build & deploy ҟͳΔ؀ڥ΁εϜʔζʹҠߦՄೳɻͲ͜Ͱ΋ͪΌΜͱಈ͘ΞϓϦ΁ Docker/Kubernetesͷ֓೦ɺؔ࿈CLIͷֶशίετ͸͔͔Δ (։ൃऀશһ͕શͯΛ೺Ѳ͢Δඞཁ͸ͳ͘ϙΠϯτ͸ߜΕΔ)
  8. 14 ઃఆϑΝΠϧ͕؀ڥม਺Λɾɾɾ • ಡΊΔ৔߹ • ManifestϑΝΠϧ͔ΒConfigMapΛࢀর͢Δ • ಡΊͳ͍৔߹ • ઃఆϑΝΠϧͷ௚઀ॻ͖׵͑

    • ؀ڥຖͷઃఆϑΝΠϧͰ্ॻ͖ • ςϯϓϨʔτΤϯδϯͰ؀ڥม਺ΛຒΊࠐΉ σϓϩΠ؀ڥࠩҟ΁ͷΞϓϩʔν ίϯςφΠϝʔδʹ؀ڥґଘͷ৘ใΛอ࣋ͤͣɺ؀ڥม਺ʹ੾Γग़͢
  9. 15 • ઃఆ৘ใΛఆٛ͠ɺPodʹ౉ͤΔϦιʔε • Spring΍Rails౳ɺઃఆϑΝΠϧͰ؀ڥม਺ΛಡΈࠐΊΔͳ ΒConfigMap͕ద͍ͯ͠Δ • Pod΍DeploymentଆͰ؀ڥม਺Λ௚઀ఆٛ͢Δ͜ͱ΋Մೳɻ ϢʔεέʔεʹԠͯ͡બ୒ ConfigMap

    / -2 : C/ " :1 C 2 2: 2 /1/ / :/ 2 C / 2 2 2 / C 2 / 2 2 : / :2 :/ 2 C / 2 / 2 C 2 2: :/ 2 C . / 2 : / 2 2 :/ 2 C / 2 2: : 2 C 2 :/ 2 C . / 2 : / 2 2 :/ 2 C / 2 2: : 2 C 2 / -2 : C :1 C : / 2 /1/ / :/ 2 C / 2 2: : 1/ / 2 C / 2 C / ConfigMap DeploymentϦιʔε͔Βͷࢀরํ๏
  10. 16 • envFromͰConfigMapΛ·ͱΊͯࢀর͢Δ͜ͱ΋Մೳ ConfigMap Ωʔ໊ʹ؀ڥม਺໊Ͱఆٛ͢Δ / -2 : :1 :

    / 2 /1/ / :/ 2 / 2 2: : 1/ / . / . / ConfigMap / -2 : / " :1 2 2: 2 /1/ / :/ 2 / 2 2 2 / 2 / 2 2 : / :2 :/ 2 / 2 / 2 2 2: : / 2 :/ 2 / 2 2: : DeploymentϦιʔε͔Βͷࢀরํ๏
  11. 19 • ؀ڥม਺ΛຒΊࠐΊΔܰྔςϯϓϨʔτΤϯδϯ envsubst test.properties.tmpl >< = #> 1 .

    > 1 Template Engine envsubst $ . test.properties • ؀ڥม਺͸Deployment΍ConfigMapʹఆٛ • ίϯςφΠϝʔδଆͰenvsubstͯ͠ઃఆϑΝΠϧʹ؀ڥม਺ΛຒΊࠐΉ $ $ .
  12. 22 Logging App Logger rotate ϥΠϒϥϦ΍ϑϨʔϜϫʔΫػೳΛར༻ ैདྷʢඇίϯςφʣ ίϯςφ ίϯςφΤϯδϯͷlogging driverΛར༻

    Docker logging driver App App stdout/err stdout/err somewhere (log store) ΞϓϦ͔Β͸ඪ४/Τϥʔग़ྗ΁ϩάΛग़ྗ ϋϯυϦϯά͸logging driverʹ೚ͤΔ
  13. 23 ୅දతͳυϥΠό ػೳ json-file JSONܗࣜͰग़ྗʢσϑΥϧτʣ ϩʔςʔγϣϯ΋Մೳɻίϯςφ࡟আʹΑͬͯ࡟আ͞ΕΔɻ ϗετଆʹϑΝΠϧग़ྗ͞ΕΔɻ /var/lib/docker/containers/[container-id]/[container-id]-json.log syslog syslog

    (※) ΁ग़ྗ journald journald (※) ΁ग़ྗ fluentd fluentd (※) ΁ग़ྗ (forward input) awslog Amazon CloudWatch Logs΁ग़ྗ gcplogs Google Cloud Platform Logging΁ग़ྗ Docker logging driver (※) ίϯςφ͔Β઀ଓՄೳͳσʔϞϯ͕ඞཁ
  14. 25 • ίϯςφ͚ͩͰ͸ͳ͘k8sίϯϙʔωϯτͷϩά΋ؚΊͯfluentdͰू໿ • DaemonSetͰfluentdΛશϊʔυʹඞͣ1Podͣͭ഑ஔ • ֤k8sϦιʔεͷlabelΛద੾ʹઃఆ͓ͯ͘͜͠ͱͰɺElasticsearchͰͷݕࡧੑ޲্ • ϚΠΫϩαʔϏεͷ৔߹ɺαʔϏεϝογϡͷར༻΋ݕ౼ʢޙड़ʣ ϩάू໿ˍՄࢹԽͷྫ

    (k8s) store visualize logging driver App stdout/err logging driver App stdout/err (※) ؆ུԽͷͨΊIngress, Service, Pod͸লུ k8s Node Docker Engine k8s Node Docker Engine k8s Node ৄࡉ͸fluentdެࣜDaemonSetΛࢀর https://github.com/fluent/fluentd-kubernetes-daemonset
  15. Ͳ͜ʹηογϣϯσʔλΛอ࣋͢Δ͔ client client session client session session app session app

    app app app app Stateful Stateless εέʔϧΞ΢τ࣌ʹηογϣϯετΞͷՄ༻ੑΛߟྀ͢Δඞཁ͋Γ ཁSticky Session ʢηογϣϯಉظΛͯ͠΋Πϯελϯε਺͕૿͑ͨ৔߹ͳͲݶք͕͋Δʣ ηογϣϯແޮԽͷ੍ޚͳͲɺηΩϡϦςΟ໘ͷ஫ҙ͸ඞཁ Cookie Store (JWT͸ηογϣϯσʔλอ࣋ʹ͸๬·͘͠ͳ͍) KVSͳͲ ՄೳͳݶΓΞϓϦ֎෦ʹอ࣋͠ɺΞϓϦΛεςʔτϨεʹ͓ͯ͘͠ͷ͕๬·͍͠ 28
  16. 29 • KubernetesͷSticky SessionػೳΛར༻ • Service (L4 Load Balancing) •

    service.spec.sessionAffinityΛClientIPʹ͢Δ • Ingress (L7 Load Balancing) • NGINX Ingress Controllerͷ৔߹ • metadata.annotationsΛࢦఆ StatefulͳطଘΞϓϦΛվमͤͣʹks8্Ͱಈ͔͍ͨ͠ annotations: nginx.ingress.kubernetes.io/affinity: "cookie" nginx.ingress.kubernetes.io/session-cookie-name: "route" nginx.ingress.kubernetes.io/session-cookie-hash: "sha1"
  17. 32 • ෳ਺ͷ෼ࢄαʔϏεؒͷ௨৴ • ϧʔςΟϯά • ϩʔυόϥϯγϯά • αʔϏεݸผͷϦϦʔε •

    Blue/GreenσϓϩΠϝϯτ • ΧφϦΞϦϦʔε ߟ͑Δ͜ͱ͸ଟ͍ • ো֐Өڹͷ࠷খԽ • αʔΩοτϒϨʔΧ • ϩά΍ϝτϦΫεͷऩूɾ؂ࢹ • ෼ࢄτϨʔγϯά
  18. 33 • Netflix OSS • Eureka + RibbonɿαʔϏεσΟεΧόϦ • HystrixɿαʔΩοτϒϨʔΧ

    • Spring CloudγϦʔζ ౳ʑɾɾɾ ϥΠϒϥϦ΍ϑϨʔϜϫʔΫͰͷରԠ͸ॏ͍ ࣮૷ݴޠʹґଘ ղܾ͍ͨ͜͠ͱʹ߹ΘͤͯݸผʹϥΠϒϥϦબఆʢֶशίετͷ૿େʣ ΞϓϦέʔγϣϯͷຊ࣭Ͱ͸ͳ͍෦෼ʹ։ൃऀͷوॏͳϦιʔε͕औΒΕΔ
  19. 34 • ౰ॳɿ෼ࢄγεςϜʹ͓͚ΔαʔϏεؒ௨৴ • ໢ͷ໨ (mesh) ͷΑ͏ʹෳࡶʹབྷΈ߹͏௨৴ܦ࿏ • ۙ೥ɿαʔϏεؒ௨৴Λ੍ޚɾ؅ཧ͢Δ࢓૊Έ •

    τϥϑΟοΫ؅ཧ • ରো֐ੑ • ςετ • ؂ࢹɺ౳ʑ αʔϏεϝογϡ https://www.slideshare.net/BruceWong3/the-case-for-chaos
  20. 35 • αʔϏεؒ௨৴Λ౷߹؅ཧ͢ΔιϦϡʔγϣϯ • Google/IBM/Lyftࣾ։ൃͷOSS • 2018/7 v1.0ϦϦʔε Istio ϚϧνϓϥοτϑΥʔϜରԠʢݱࡏKubernetesɺConsulʹରԠʣ

    ΞϓϦέʔγϣϯίʔυ΁ͷ૊ΈࠐΈෆཁ ΞϓϦέʔγϣϯ࣮૷ʹґଘ͠ͳ͍ ؂ࢹɺ෼ࢄτϨʔγϯάͳͲपลπʔϧͱͷ਌࿨ੑ͕ߴ͍
  21. 37 Istioػೳ • ϧʔςΟϯά • ϩʔυόϥϯγϯά • ϦτϥΠ/λΠϜΞ΢τ/αʔΩοτϒϨʔΧ τϥϑΟοΫ؅ཧ •

    ೝূɾೝՄ • αʔϏεؒ௨৴҉߸Խ ηΩϡϦςΟ • Logging • ෼ࢄτϨʔγϯά • ֤छϝτϦΫεऩू ςϨϝτϦ
  22. 38 IstioΞʔΩςΫνϟ • Proxyʹ͸֦ுͨ͠ Λ࢖༻ • C++࣮૷ͷߴ଎ͳL4/7ϓϩΩγ • Graduated CNCF

    Project • PodͷSidecarͱͯ͠഑ஔ͞ΕΔͨΊΞϓϦ ίʔυͷมߋෆཁ • PodϨϕϧͰDI͞ΕΔΠϝʔδ ProxyͷαʔϏε σΟεΧόϦ ςϨϝτϦ ೝূΩʔ౳ͷ ؅ཧ ֤PodͷSidecar ͱͯ͠഑ஔ Data Plane Control Plane Pod Pod https://istio.io/docs/concepts/what-is-istio/#architecture
  23. 39 Istio Demo client ؂ࢹ Node Kubernetes Mixer ෼ࢄτϨʔγϯά μογϡϘʔυ

    Pod Pod Pod frontend postgresql rest-api Proxy Proxy Proxy ςϨϝτϦ Demo؀ڥߏஙऀɿ αΠΦεςΫϊϩδʔ ख௩ ୓
  24. 42 kubectl logs • ίϯςφ୯Ґͷϩά֬ೝɻpod࠶࡞੒ͷ౓ʹίϚϯυଧͪ௚͢ඞཁ͕͋Δ stern • ϩάΛ֬ೝ͢ΔpodΛॊೈʹࢦఆՄೳ • ਖ਼نදݱ΍labelࢦఆɺnamespaceԣஅͰෳ਺PodͷϩάΛtail

    • pod࠶࡞੒Ͱid͕มΘͬͯ΋௥ਵͯ͘͠ΕΔ ޮ཰తͳϩά֬ೝ $ $ $ - - - https://github.com/wercker/stern Deployment ReplicaSet Pod 1 Pod 2 Container A Container B Container A Container B kubectl logs stern
  25. 43 • for Docker • Docker for Mac • https://docs.docker.com/docker-for-mac/#install-shell-completion

    • Windows PowerShell༻΋͋Γʢະݕূʣ (※) bash on WindowsͰDockerΛ࢖͏ख΋͋Γʢ޻෉͕ඞཁʣ • https://github.com/matt9ucci/DockerCompletion • for Kubernetes • kube-prompt • https://github.com/c-bata/kube-prompt ίϚϯυิ׬
  26. 46 • freshpod • ϩʔΧϧΠϝʔδͷߋ৽Λݕ஌ͯ͠PodΛ࠶σϓϩΠͯ͘͠ΕΔ • freshpodࣗମ΋podͱͯ͠ಈ࡞ • MinikubeɺKubernetes on

    Docker for Windows/MacͰར༻Մೳ k8s։ൃϫʔΫϩʔυͷޮ཰ԽʢΠϝʔδߋ৽ݕ஌ʣ freshpodʹΑΓࣗಈԽ͞ΕΔ෦෼ ߋ৽ ࣗಈݕ஌ https://github.com/GoogleCloudPlatform/freshpod imagePullPolicy͸IfNotPresentͱ͢Δඞཁ͋Γ Coding Build Deploy Test
  27. freshpodʹΑΓࣗಈԽ͞ΕΔ෦෼ ߋ৽ ࣗಈݕ஌ Coding Build Deploy Test 47 • entr

    (Event Notify Test Runner) • ίʔυमਖ਼Λݕ஌ͯ͠೚ҙͷίϚϯυ࣮ߦ͕ՄೳͳCLI • freshpodͱ૊Έ߹ΘͤΔ͜ͱͰίʔυमਖ਼ΛࣗಈతʹPod΁൓өՄೳ k8s։ൃϫʔΫϩʔυͷޮ཰Խʢίʔυमਖ਼ݕ஌ʣ entrʹΑΓࣗಈԽ͞ΕΔ෦෼ http://eradman.com/entrproject/ *.' .$- . ' * ' - $ ίʔυอଘͷ౓ʹPod࠶ىಈ͕ൃੜ͢ΔͨΊ࡞ۀ಺༰ʹΑͬͯదٓ࢖༻͢Δ ߋ৽ ࣗಈݕ஌
  28. Skaffold Pipeline 48 • Skaffold • σϓϩΠ·ͰͷύΠϓϥΠϯΛࣗಈԽ͢ΔGoogle੡CLIπʔϧ • ֤ϑΣʔζͷॲཧΛϓϥάΠϯతʹࠩସՄೳ •

    ϩʔΧϧ։ൃத͸Πϝʔδpushෆཁͱͨ͠ΓɺσϓϩΠʹHelmΛར༻ͨ͠Γɺ౳ʑ • Πϝʔδͷλά΋ࣗಈ෇༩ k8s։ൃϫʔΫϩʔυͷޮ཰ԽʢΑΓॊೈʹʣ Coding Build Deploy Test https://github.com/GoogleContainerTools/skaffold Push docker build docker push kubectl apply GA͍ͯ͠ͳ͍ͨΊɺޓ׵ੑͷͳ͍มߋʹ͸஫ҙ͕ඞཁ https://github.com/GoogleContainerTools/skaffold/blob/v0.19.0/deprecation-policy.md ߋ৽ ࣗಈݕ஌
  29. 51 • Twelve Factor App by Heroku • https://12factor.net/ja/ •

    Beyond the Twelve-Factor App by Pivotal • https://content.pivotal.io/blog/beyond-the-twelve-factor-app ʢࢀߟʣΞϓϦઃܭͷώϯτʹɾɾɾ
  30. 52 • “ΠϯϑϥΤϯδχΞ͕k8sͰΞϓϦΛ࡞ͬͯݟ͑ͨࠓޙͷΠϯϑϥ”, SlideShare • https://www.slideshare.net/susumu0108/k8s-91891197 • “Istioͷશ๴”, Think IT.

    • https://thinkit.co.jp/article/14640 • “IstioαʔϏεϝογϡೖ໳”, SlideShare • https://www.slideshare.net/yokawasa/istio-114360124 References