Slide 1

Slide 1 text

How to develop “Container/Kubernetes Ready” Go Web Application 2019/10/28 Go Conference Autumn ෋Ԭকޗ @tomiokasyogo

Slide 2

Slide 2 text

͸͡Ίʹ..

Slide 3

Slide 3 text

૝ఆௌऀ • GoΛ࢖ͬͯΞϓϦέʔγϣϯΛ։ൃͨ͜͠ͱ͕͋Δ • ίϯςφΛͳΜͱͳ͘஌ͬͯΔ or ࢖ͬͯΔ • Kubernetesʹڵຯ͕͋Δ

Slide 4

Slide 4 text

Container

Slide 5

Slide 5 text

ίϯςφͱ͸

Slide 6

Slide 6 text

https://www.docker.com/resources/what-container ίϯςφ͸ ϗετOS্ͷಠཱ࣮ͨ͠ߦ؀ڥ

Slide 7

Slide 7 text

ͦͷ࣮ମ͸

Slide 8

Slide 8 text

ίϯςφϥϯλΠϜʹΑͬͯ ϗετOSͷϦιʔεΛִ཭ɾ੍ݶ ͞Εͨϓϩηε

Slide 9

Slide 9 text

ίϯςφؔ࿈ͷϓϩμΫτͷଟ͘ Ͱ͸Goݴޠ͕࠾༻͞Ε͍ͯΔ

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Docker and Go • Dockerͷ։ൃʹͳͥGo͕બ͹Εͨͷ͔?(2013) https://www.slideshare.net/jpetazzo/docker-and-go-why-did-we-decide-to-write-docker-in-go 1. Static compilation 2. Neutral 3. Multi-arch build 4. Full development environment etc…

Slide 12

Slide 12 text

• ґଘؔ܎ΛҰͭͷόΠφϦʹͰ͖Δ • γϯάϧόΠφϦͰಈ࡞Մೳ • ޙड़) γεςϜίʔϧΛ؆୯ʹѻ͑Δ • etc… Go͕ίϯςφͱ૬ੑͷྑ͍ཧ༝

Slide 13

Slide 13 text

DockerΛ࢖ͬͯίϯςφԽ
 ͢Δࡍʹ͸DockerfileΛهड़͢Δ

Slide 14

Slide 14 text

جຊతͳDockerfile ➀ ➂ ➁ ➀ BaseʹͳΔΠϝʔδͷࢦఆ ➁ Ϗϧυͷ࣮ߦ ➂ ΞϓϦέʔγϣϯͷ࣮ߦ

Slide 15

Slide 15 text

Multi-stage build ➀ ➁ ➂ ➀ BaseʹͳΔΠϝʔδͷࢦఆ ➁ Ϗϧυͷ࣮ߦ ➂ Alpine΍DebianͳͲͷΑΓܰྔͳΠϝʔδ ➃Ϗϧυͨ͠όΠφϦΛίϐʔ ➃

Slide 16

Slide 16 text

Dockerͷbest practiceʹؔ͢Δهࣄ https://qiita.com/inductor/items/25d5accb80ea606d5c51 https://www.docker.com/blog/intro-guide-to-dockerfile-best-practices/

Slide 17

Slide 17 text

,VCFSOFUFTš

Slide 18

Slide 18 text

Kubernetes • ίϯςφ؀ڥͷΦʔέετϨʔγϣϯπʔϧ • ίϯςφͷσϓϩΠɺϝϯςφϯεɺεέʔϦϯάΛϚωδϝ ϯτͯ͘͠ΕΔ • Linux FoundationͷαϒϓϩδΣΫτͰ͋ΔCNCFʹϗετ͞ Ε͍ͯΔ

Slide 19

Slide 19 text

https://www.cncf.io/projects/

Slide 20

Slide 20 text

Kubernetesʹొ৔͢Δ֓೦ • Pod • ReplicaSetɺDeployment • Service ຊ౰͸΋ͬͱͨ͘͞Μ͋Δ͕ɺ্هΛཧղ͢Δ͜ͱ͕࠷ॏཁ ೉͍͜͠ͱ͸Kubernetes͕΍ͬͯ͘ΕΔ

Slide 21

Slide 21 text

Pod • Pod͸ҰͭҎ্ͷίϯςφٴͼετϨʔδͰߏ੒ • IPΞυϨεͱϙʔτۭؒΛڞ༗ɺಉ͡Pod಺ͳΒ”localhost” Ͱૄ௨Մೳ https://kubernetes.io/docs/tutorials/kubernetes-basics/explore/explore-intro/

Slide 22

Slide 22 text

➀ ➂ ➁ ➀ Podͷ໊લ ➁ ίϯςφ1 ➂ ίϯςφ2

Slide 23

Slide 23 text

ReplicaSet • “PodTemplate”ͱݺ͹ΕΔPodͷςϯϓϨʔτΛݩʹPodΛࢦఆ͞ Εͨ਺ʹௐ੔ɾ؅ཧ͢Δ 3FQMJDB4FU 1PE 1PE 1PE Node

Slide 24

Slide 24 text

Deployment • ReplicaSetΛ؅ཧ͠ɺRolling-Update΍RollBackΛ࣮ݱ • imageͷมߋ͕͋ͬͨ࣌ʹ͸৽ͨͳReplicaSetΛੜ੒ɻݹ͍PodΛ৽ ͍͠PodʹೖΕସ͑Δ

Slide 25

Slide 25 text

3FQMJDB4FU 1PE 1PE Podͷߋ৽ 3FQMJDB4FU %FQMPZNFOU 1PE 1PE v1 Podͷߋ৽

Slide 26

Slide 26 text

3FQMJDB4FU 1PE 1PE v1 3FQMJDB4FU 1PE 1PE v2 Podͷߋ৽ ✖ %FQMPZNFOU Node ৽͍͠ReplicaSet͕࡞ΒΕΔ

Slide 27

Slide 27 text

Service • τϥϑΟοΫΛPod΁ϧʔςΟϯά͢Δ • Podͷ૿ݮ͕ΞϓϦέʔγϣϯʹӨڹΛ༩͑ͳ͍Α͏ʹந৅Խ ͯ͘͠Ε͍ͯΔ 4FSWJDF 1PE 1PE 1PE ϦΫΤετ

Slide 28

Slide 28 text

KubernetesΛ࢖ͬͯ։ൃ͢Δ ࡍɺ͍͔ͭ͘஫ҙ఺͕ଘࡏ͢Δ

Slide 29

Slide 29 text

Graceful Start & Shutdown

Slide 30

Slide 30 text

Podͷγϟοτμ΢ϯαΠΫϧ https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods

Slide 31

Slide 31 text

ཁ఺ͷΈநग़ 1. PreStop hook͕ઃఆ͞Ε͍ͯΔ৔߹ɺ࠷ॳʹ࣮ߦ͞ΕΔ 2. SIGTERM͕ίϯςφʹૹΒΕΔ(ෳ਺ͷ৔߹,ॱෆಉ) 3. Service΍ReplicaSetͷPodϦετ͔Β࡟আ 4. Grace periodΛ௒͑ͨ৔߹ɺSIGKILL͕ૹΒΕΔ Default grace period = 30s (มߋՄ)

Slide 32

Slide 32 text

1. PreStop hook͕ઃఆ͞Ε͍ͯΔ৔߹ɺ࠷ॳʹ࣮ߦ͞ΕΔ 2. SIGTERM͕ίϯςφʹૹΒΕΔ(ෳ਺ͷ৔߹,ॱෆಉ) 3. Service΍ReplicaSetͷPodϦετ͔Β࡟আ 4. Grace periodΛ௒͑ͨ৔߹ɺSIGKILL͕ૹΒΕΔ Default grace period = 30s (มߋՄ)

Slide 33

Slide 33 text

SIGTERMΛϋϯυϦϯά͠ αʔόʔͷऴྃॲཧΛߦ͏

Slide 34

Slide 34 text

➀ αʔόʔͷىಈ ➁ γάφϧΛड͚औΔνϟωϧΛ࡞੒ ➂ γάφϧΛड͚औΔ·ͰϒϩοΫɺ ड͚औͬͨΒγϟοτμ΢ϯॲཧΛ։࢝ ➀ ➂ ➁

Slide 35

Slide 35 text

Dockerར༻࣌ʹ΋ඞཁͳཧ༝ • “docker stop” => SIGTERM • “docker kill “ => SIGKILL ͕ૹ৴͞Ε͍ͯΔͨΊɺDockerར༻࣌ʹ΋ϋϯυϦϯάͰ͖Δ Α͏ʹ͓͍ͯͨ͠ํ͕ྑ͍

Slide 36

Slide 36 text

ཁ఺ͷΈநग़ 1. PreStop hook͕ઃఆ͞Ε͍ͯΔ৔߹ɺ࠷ॳʹ࣮ߦ͞ΕΔ 2. SIGTERM͕ίϯςφʹૹΒΕΔ(ෳ਺ͷ৔߹,ॱෆಉ) 3. Service΍ReplicaSetͷPodϦετ͔Β࡟আ 4. Grace periodΛ௒͑ͨ৔߹ɺSIGKILL͕ૹΒΕΔ Default grace period = 30s (มߋՄ) ͜ΕΒͷॲཧ͸ඇಉظ

Slide 37

Slide 37 text

Podͷ࡟আ͕PodͷService͔Βͷ ࡟আΑΓૣ͍ͱɺ֘౰Pod΁ͷϦ ΫΤετ͕ࣦഊ͢ΔՄೳੑ͕͋Δ

Slide 38

Slide 38 text

࠶ܝ 4FSWJDF 1PE 1PE 1PE ϦΫΤετ

Slide 39

Slide 39 text

͋ΔPodΛ࡟আ͍ͨ͠

Slide 40

Slide 40 text

ཧ૝ 4FSWJDF 1PE 1PE 1PE ϦΫΤετ ϧʔςΟϯάઌ͔Β࡟আ

Slide 41

Slide 41 text

ཧ૝ 4FSWJDF 1PE 1PE ϦΫΤετ Podͷ࡟আ

Slide 42

Slide 42 text

΋͠ઌʹPod͕ऴྃ͢Δͱ… 4FSWJDF 1PE 1PE ϦΫΤετ Podͷ࡟আ

Slide 43

Slide 43 text

΋͠ઌʹPod͕ऴྃ͢Δͱ… 4FSWJDF 1PE 1PE ϦΫΤετ PodͷIP΁ͷϦΫΤετʹ ࣦഊ͢Δ

Slide 44

Slide 44 text

Ͳ͏΍ͬͯղܾ͢Δ͔

Slide 45

Slide 45 text

preStopHookΛར༻

Slide 46

Slide 46 text

preStop hookॲཧΛTERMγάφϧ͕ૹΒΕΔલʹ࣮ߦՄೳ

Slide 47

Slide 47 text

• preStop hookͰsleepͤ͞Δ • ͜ͷsleepͷؒʹɺγϟοτμ΢ϯ࣌ʹ͖͍ͯΔ ϦΫΤετ΋׬ྃͤ͞Δ

Slide 48

Slide 48 text

ΑΓৄ͍͠಺༰ Handling Client Requests Properly with Kubernetes https://srcco.de/posts/kubernetes-liveness-probes-are-dangerous.html

Slide 49

Slide 49 text

Health-checkͷछྨͱ࣮૷

Slide 50

Slide 50 text

Health-check Kubernetes͕αϙʔτ͢ΔPodͷHealth-checkʹ͸ɺ 1. ExecAction: ೚ҙͷίϚϯυͷ࣮ߦ 2. TCPSocketAction: ࢦఆͨ͠port΁ͷTCPνΣοΫ 3. HTTPGetAction: ೚ҙͷΤϯυϙΠϯτʹGetϦΫΤετ ͕࢖༻Մೳ

Slide 51

Slide 51 text

Health-checkͷछྨ 1. Readiness Probe: ɹτϥϑΟοΫ͕ड͚ೖΕΒΕΔঢ়ଶ͔ΛνΣοΫ͢Δ ɹࣦഊ͢ΔͱServiceͷϧʔςΟϯάઌ͔Βআ֎͞ΕΔ 2. Liveness Probe: ɹਖ਼͘͠ىಈͰ͖͍ͯΔ͔ΛνΣοΫ͢Δ ɹࣦഊͨ͠৔߹͸ίϯςφ͸࠶ߏங͞ΕΔ

Slide 52

Slide 52 text

LivenessProbeͱReadinessProbe͕͋Δ͕ɺ ·ͣ͸ReadinessProbeͷઃఆΛΦεεϝ͠·͢ https://srcco.de/posts/kubernetes-liveness-probes-are-dangerous.html

Slide 53

Slide 53 text

➀ ➂ ➁ ➀ ΤϯυϙΠϯτͱportͷࢦఆ ➁ ࠷ॳʹϦΫΤετΛૹΔ·Ͱͷ࣌ؒ ➂ ϦΫΤετΛૹΔִؒ (default: 10sec)

Slide 54

Slide 54 text

• DB΁ͷૄ௨֬ೝ΍ɺΩϟογϡͷϦϩʔυ΋
 Health-checkͷΤϯυϙΠϯτͰ֬ೝ • ͙͢ʹαʔόʔ͕Ԡ౴Ͱ͖Δঢ়ଶ͔ΛνΣοΫͰ͖Δ

Slide 55

Slide 55 text

ઃఆͷѻ͍

Slide 56

Slide 56 text

ίϯςφΛ࢖͏ͱ֤؀ڥ(dev؀ڥɺprd؀ڥʣ Ͱಉ͡ΞϓϦέʔγϣϯΛಈ࡞ͤ͞ΒΕΔ

Slide 57

Slide 57 text

ͨͩ͠ɺઃఆʹؔͯ͠͸dev΍prdͳͲͷ؀ڥ ͝ͱʹҟͳΔ஋͕࢖༻͞ΕΔ΋ͷ΋͋Δ ྫ: • DBͷ઀ଓઌ • ೝূ৘ใͳͲ

Slide 58

Slide 58 text

ނʹઃఆ͸ίʔυͱ෼཭͢Δඞཁ͕͋Δ ➡؀ڥม਺ͱͯ֨͠ೲ͢Δ ͨͩ͠ɺΞϓϦέʔγϣϯ಺෦ͷઃఆ͸ ؚ·ͳ͍

Slide 59

Slide 59 text

؀ڥม਺Λѻ͏ϥΠϒϥϦ • https://github.com/kelseyhightower/envconfig • https://github.com/caarlos0/env • https://github.com/spf13/viper ؀ڥม਺Λߏ଄ମ΁Ϛοϐϯάͯ͘͠ΕΔ

Slide 60

Slide 60 text

ϩΪϯά • ϩά͸σʔλͰ͸ͳ͘ΠϕϯτετϦʔϜͱͯ͠ѻ͏ • ΞϓϦέʔγϣϯ͸ඪ४ग़ྗʹϩάΛग़͚ͩ͢ • FluentdͳͲͷϩάίϨΫλʔ͕ͦΕΒͷϩάΛճऩ͢Δ https://kubernetes.io/docs/concepts/cluster-administration/logging/

Slide 61

Slide 61 text

ϩΪϯά • ϩά͸ߏ଄Խ(jsonͳͲʣ͓ͯ͘͜͠ͱΛΦεεϝ͠·͢(ݕࡧ͕͠ ΍͘͢ͳΔͨΊʣ • StackDriverLoggingͳͲΛར༻͢Δ৔߹ɺϩάͷॏཁ౓ϨϕϧͱҰ கͤ͞Δ

Slide 62

Slide 62 text

uber-go/zap https://github.com/uber-go/zap • ϩΪϯάͷσϑΝΫτతͳϥΠϒϥϦ • ߏ଄Խϩά΍ϩάϨϕϧ΋؆୯ʹઃఆͰ͖ɺύϑΥʔϚϯε΋ଞͷ ϥΠϒϥϦͱൺֱͯ͠ྑ͍໛༷ • ͜Μͳײ͡ͷϩά͕ग़ͤΔ

Slide 63

Slide 63 text

Twelve-Factor App • Ϟμϯͳ։ൃ؀ڥͰ஫ҙ͢΂͖఺Λ·ͱΊͨ΋ͷ • Herokuͷ։ൃऀɺઃཱऀͷAdam WigginsʹΑͬͯఏএ͞Εͨ https://12factor.net/ja/ ʢ೔ຊޠ༁ʣ Adam Wiggins

Slide 64

Slide 64 text

Twelve-Factor App • ґଘؔ܎ͷ໌ࣔԽ • ؀ڥ͝ͱͷઃఆͷ੾Γସ͑ʹ؀ڥม਺Λ࢖͏ • ϩάͷΠϕϯτετϦʔϛϯάԽ • ϏϧυɾϦϦʔεͷ෼཭ ͳͲ ͦͷ໊ͷ௨Γ12ͷݪଇ͕ड़΂ΒΕ͍ͯΔ

Slide 65

Slide 65 text

Beyond The Twelve-Factor App • PivotalࣾͷKevin Hoffman͔Βఏএ͞Εͨ • Ϋϥ΢υωΠςΟϒΞϓϦ޲͚ͷTwelve-Factor App • Concurrency΍Telemetry͕௥Ճ͞Ε͍ͯΔ https://content.pivotal.io/ebooks/beyond-the-12-factor-app PDF͕ແྉͰఏڙ͞Ε͍ͯΔ

Slide 66

Slide 66 text

ࢀߟهࣄ • https://github.com/tommy-sho/gocon-k8s-server <- ࠓճͷൃද༻ͷαϯϓϧϦϙδτϦ • https://www.docker.com/resources/what-container • https://www.slideshare.net/jpetazzo/docker-and-go-why-did-we-decide-to-write-docker-in-go • https://www.docker.com/blog/intro-guide-to-dockerfile-best-practices/ • https://qiita.com/inductor/items/25d5accb80ea606d5c51 • https://kubernetes.io/docs/concepts/workloads/pods/pod/ • https://srcco.de/posts/kubernetes-liveness-probes-are-dangerous.html • https://12factor.net/ja/ • https://freecontent.manning.com/handling-client-requests-properly-with-kubernetes/ ࢖༻ͨ͠Goper܅ͷΠϝʔδ • https://github.com/MariaLetta/free-gophers-pack

Slide 67

Slide 67 text

ྑ͍ίϯςφϥΠϑΛ!!