Slide 1

Slide 1 text

Docker Composeར༻ऀ ͔ΒݟͨKubernetes ։ൃ؀ڥߏஙೖ໳ WEBΤϯδχΞษڧձ #09 Koichi Nagaoka

Slide 2

Slide 2 text

ࣗݾ঺հ ௕Ԭ ߒҰ (@kkoudev) גࣜձࣾϛΫγΟͰΤϯδχΞ΍ͬͯ·͢ɻ ޷͖ͳΤσΟλ͸VSCode

Slide 3

Slide 3 text

ࠓճͷςʔϚͰ͋Δ Kubernetesͷલʹ… Dockerʹ͍ͭͯ

Slide 4

Slide 4 text

DockerΛ࢖͍࢝ΊͯɺDocker ComposeʹΑΔӡ༻Λ͋Δఔ౓ ߦ͍ͬͯΔͱɺ͍͔ͭ͘໰୊఺ ͕ݟ͖͑ͯ·͢ɻ

Slide 5

Slide 5 text

Docker ComposeͷΈͷӡ༻ʹΑΔ ओͳ໰୊఺ ϗετΛ৑௕Խ͢Δʹ͋ͨͬͯ͸ͦ͜ ·Ͱ೉͘͠͸ͳ͍͕ɺϗετ͝ͱʹΞ ϓϦέʔγϣϯΛσϓϩΠͨ͠Γɺσ ϓϩΠ࣌ʹϩʔυόϥϯαʔ͔Βͷ੾ Γ཭͠Λߦ͏ͳͲΛࣗ࡞͢Δඞཁ͕ग़ ͯ͘Δɻ 1. ϗετͷ৑௕Խ ྫ͑͹σϓϩΠʹࣦഊͯ͠ଈ੾Γ໭͠ Λ͍ͨ͠ͱ͍ͬͨέʔεʹ͓͍ͯ͸ɺ Docker Compose୯ମͰಛผͳػೳ͕ ఏڙ͞Ε͍ͯΔΘ͚Ͱ͸ͳ͍ͷͰࣗ෼ Ͱͦͷ࢓૊Λ࡞Δඞཁ͕͋Δɻ
 (Ϗϧυͨ͠Πϝʔδͷλά؅ཧɺλά Λࢦఆͯ͠ͷσϓϩΠͳͲ) 2. ίϯςφͷόʔδϣϯ؅ཧ ίϯςφͷࢮ׆؂ࢹ΍ো֐࣌ͷ෮چʹ ͍ͭͯ͸खಈͩͬͨΓࣗ࡞͢Δ෦෼͕ ଟ͍ɻ 3. ࢮ׆؂ࢹ΍ো֐͔Βͷ෮چ

Slide 6

Slide 6 text

Docker ComposeΛ࢖͏͜ͱͰϗε τͷҧ͍ʹΑΔ؀ڥࠩҟ΍໘౗ͳη οτΞοϓΛ΄΅ແ͘͢͜ͱ͕ग़དྷ ΔΑ͏ʹ͸ͳ͕ͬͨɺϗετؒͷ࿈ ܞ΍৑௕ߏ੒࣌ͷӡ༻ͳͲɺΠϯϑ ϥཁૉ͕ؔ࿈͢Δ෦෼ʹ͍ͭͯ͸ࣗ ࡞෦෼͕ଟͯ͘ख͕͔͔ؒΔ

Slide 7

Slide 7 text

ͦ͜ͰKubernetes

Slide 8

Slide 8 text

Kubernetesͱ͸ DockerίϯςφͷΫϥελ؅ཧΛ࢝Ίͱ͠ ͨΦʔέετϨʔγϣϯΛߦ͏αʔϏεͰ ͢ɻϗετؒͷ࿈ܞ΍σϓϩΠʹ͍ͭͯ΋૯ ׅతʹ؅ཧͰ͖ΔΑ͏ʹͳΓ·͢ɻ (໊শ͕௕͍ͷͰ k8s ͱུ͞Ε·͢) ※DockerެࣜͷΦʔέετϨʔγϣϯπʔϧͱͯ͠ Docker Swarmͱ͍͏΋ͷ΋͋Γ·͕͢ɺk8s͕ੈͷதతʹ͸ ΄΅σϑΝΫτελϯμʔυͱͳ͍ͬͯ·͢

Slide 9

Slide 9 text

ࠓ೔͓࿩͢Δ͜ͱ • Kubernetesͷ֓೦ͱ֤Ϧιʔεͷ໾ׂʹ͍ͭͯ • KubernetesΛ࢖ͬͨ։ൃ؀ڥͷγεςϜߏஙखॱ • Docker Compose͔ΒͷҠߦํ๏ • Kubernetesͷྑ͍ϙΠϯτͱগ͠ਏ͍ϙΠϯτ

Slide 10

Slide 10 text

Kubernetesͷ֓೦ͱ Ϧιʔεʹ͍ͭͯ

Slide 11

Slide 11 text

KubernetesͷϦιʔεͱ͸ Kubernetes͸ෳ਺ͷϦιʔεͱݺ͹ΕΔ୯Ґ͕ڠ ௐ͠߹͏͜ͱͰߏ੒͞Ε͍ͯ·͢ɻ࢓૊ΈΛཧղ ͢Δ্Ͱ΋·ͣ͸Ϧιʔεʹ͍ͭͯཧղ͢Δඞཁ ͕͋Γ·͢ɻ

Slide 12

Slide 12 text

Nodeʹ͍ͭͯ • Nodeͱ͸DockerϗετΛࢦ͢Ϧιʔε • ཁ͢Δʹίϯςφ͕ಈ࡞͢Δαʔόͷ͜ͱΛࢦ͢ɻϩʔΧϧ؀ڥ Ͱ͋Ε͹ɺNode͸ৗʹ1ͭͱͳΔ • Nodeʹ͸KubernetesΫϥελͷશNodeΛ؅ཧ͢ΔMaster (Master Node)ͱɺͦΕҎ֎ͷ֤ϦιʔεΛಈ͔͢Node (Worker Node)ʹ෼͔ΕΔ

Slide 13

Slide 13 text

NodeͷΠϝʔδ

Slide 14

Slide 14 text

Podʹ͍ͭͯ • Podͱ͸Node಺ʹ഑ஔ͢ΔίϯςφΛಈ͔ͨ͢ΊͷϦιʔεɻؔ ࿈ੑͷ͋ΔDockerίϯςφͷू·ΓΛ1ͭͱͨ͠୯ҐͱͳΔ • ྫ͑͹NginxΛϑϩϯταʔόͱͨ͠RubyΞϓϦέʔγϣϯ͕͋ Δ৔߹ɺNginxͷίϯςφͱRubyΞϓϦέʔγϣϯͷίϯςφΛ1 ͭͷ·ͱ·Γͱ͍ͨ͠৔߹͕͋ΔɻͦͷΑ͏ͳ৔߹ɺ1ͭͷPodͱ ͯ͠ߏ੒͢Δ

Slide 15

Slide 15 text

PodͷΠϝʔδ

Slide 16

Slide 16 text

PodͱReplicaSetʹ͍ͭͯ • Pod͸ReplicaSetͱ͍͏ϦιʔεͰର৅PodͷΫϥελશମʹ͓͚ Δෳ੡਺Λఆ͓ٛͯ͘͜͠ͱ͕Ͱ͖Δ • Podͷෳ੡਺Λࢦఆ͓ͯ͘͜͠ͱͰɺPod͕ԿΒ͔ͷཧ༝Ͱڧ੍ऴ ྃͨ͠৔߹Ͱ΋ෳ੡਺Λҡ࣋͢ΔͨΊʹηϧϑώʔϦϯάΛߦ͏ ͜ͱ͕ՄೳʹͳΔ

Slide 17

Slide 17 text

PodͱReplicaSetͷΠϝʔδ

Slide 18

Slide 18 text

ReplicaSetͱDeploymentʹ͍ͭͯ • ReplicaSet͸Deploymentͱ͍͏ϦιʔεͰ؅ཧ͞ΕΔ • ReplicaSet͸Podͷෳ੡ɾҡ࣋ͷͨΊͷ؅ཧΛ୲͍ͬͯΔ͕ɺ Deployment͸ReplicaSetͷ࡞੒ɾҡ࣋ͷͨΊͷ؅ཧΛ୲͍ͬͯΔ • Deployment͸σϓϩΠ࣌ʹ৽͍͠࢓༷ͷReplicaSetΛ࡞੒͠ɺچ ReplicaSet͕؅ཧ͍ͯ͠ΔچPodͱͷ਺Λௐ੔͠ͳ͕Β৽PodΛ૿ ΍͍͖ͯ͠ɺ࠷ऴతʹReplicaSet͝ͱ৽͍͠࢓༷ʹஔ͖׵͑ΔΑ ͏ʹͳ͍ͬͯΔɻ·ͨɺϦϏδϣϯ؅ཧ΋͓ͯ͠Γɺ৽όʔδϣ ϯʹ໰୊͕͋ͬͨࡍʹچόʔδϣϯ΁໭͢͜ͱ΋ՄೳͰ͋Δ

Slide 19

Slide 19 text

ReplicaSetͱDeploymentͷΠϝʔδ

Slide 20

Slide 20 text

Serviceʹ͍ͭͯ • Serviceͱ͸Pod΁ͷΞΫηεܦ࿏Λఏڙ͢ΔϦιʔεͰ͋Δ • ओʹPodʹ͚ͭͨϥϕϧΛݩʹɺͲͷϥϕϧ͕͍ͭͨPod΁ΞΫη ε͢Δ͔Λܾఆ͢Δ • Ϋϥελ಺෦ͷΈͰར༻Ͱ͖ΔService(ClusterIP)΍ɺΫϥελ֎ ෦͔ΒΞΫηεՄೳͳService(NodePort)ͳͲͷServiceΛ࡞੒͢Δ ͜ͱ͕ՄೳͰ͋Δ • AWSΛ࢖͍ͬͯΔ৔߹͸ɺLoad Balancerͱͯ͠CLBɾNLBΛׂΓ ౰ͯΔ͜ͱ΋Մೳ

Slide 21

Slide 21 text

ServiceͷΠϝʔδ

Slide 22

Slide 22 text

ͦͷଞϦιʔεʹ͍ͭͯ • ֓೦Λઆ໌͢Δ্Ͱ୅දతͳϦιʔεʹ͍ͭͯ͸঺հ͠·ͨ͠ ͕ɺͦͷଞΑ͘࢖͏Ϧιʔεʹ͍ͭͯ΋঺հ͍ͯ͠·͢ɻ

Slide 23

Slide 23 text

Ingress Serviceͷ্ҐϦιʔεɻService͸OSIࢀরϞσϧͰ͍͏ͱ͜ΖͷL4૚·Ͱͷ ੍ޚ͔͠ग़དྷͳ͍(※)͕ɺIngressΛ࢖͏͜ͱͰL7૚ϨϕϧͷύεϕʔεͷৼΓ෼͚΍ ϗετ໊ʹΑΔৼΓ෼͚ͳͲ͕ग़དྷΔΑ͏ʹͳΔɻ AWSͰ͍͏ͱ͜ΖͷALBͱࢥͬͯ΋Β͑Ε͹Α͍͔ͱࢥ͍·͢ɻ (※কདྷతʹ͸ L7૚ ·ͰServiceͰ΋ѻ͑ΔΑ͏ʹ͢Δ༧ఆ͕͋ΔΒ͍͠)

Slide 24

Slide 24 text

ConfigMap ؀ڥม਺ͷΑ͏ͳઃఆ஋ɺ·ͨઃఆϑΝΠϧ৘ใͦͷ΋ͷΛ ؅ཧ͢ΔͨΊͷϦιʔεɻKey-ValueܗࣜͰఆٛ͞ΕΔɻ Secret ઃఆ஋ͷதͰ΋ύεϫʔυͷΑ͏ͳൿಗ৘ใΛѻ͏ࡍʹར༻͢Δɻ ઃఆϑΝΠϧ্͸Base64ܗࣜͱͯ͠஋Λอ࣋͢Δͱ͍͏఺Ҏ֎͸ ConfigMapͱ΄΅มΘΒͳ͍ɻ

Slide 25

Slide 25 text

PersistentVolume ϘϦϡʔϜྖҬΛఆٛ͢ΔϦιʔεɻ EBS΍NFSͷΑ͏ͳ֎෦ετϨʔδΛఆٛ͢Δ͜ͱ΋Մೳ PersistentVolumeClaim ར༻͢ΔϘϦϡʔϜྖҬͷཁٻΛఆٛ͢ΔϦιʔεɻ PersistentVolumeͱPodΛඥ෇͚ΔͨΊʹར༻͢Δɻ

Slide 26

Slide 26 text

Docker Composeར༻࣌͸ಈ࡞ͤ͞Δίϯς φΛҙࣝ͢Δ͚ͩͰຆͲࣄ଍Γ͍ͯ·͕ͨ͠ɺ KubernetesͰ͸ͦΕʹՃ͑ͯಈ࡞ͤ͞Δϗε τ(Node)΍ίϯςφͷάϧʔϓԽ(Pod)ɺͦͷ ෳ੡(ReplicaSet)ͱެ։(ServiceɺIngress)ͱ ͍ͬͨΠϯϑϥϨϕϧͰҙ͍ࣝͯͨ͜͠ͱ΋શ ͯKubernetesͷઃఆͷ̍ͭͱͯ͠؅ཧग़དྷΔ Α͏ʹͳΓ·͢ɻ “

Slide 27

Slide 27 text

KubernetesΛ࢖ͬͨ ։ൃ؀ڥͷߏங

Slide 28

Slide 28 text

ͦΕͰ͸ɺ࣮ࡍʹ։ൃ؀ڥΛߏங ͍͖ͯ͠·͢

Slide 29

Slide 29 text

ࠓճߏங͢ΔγεςϜͷΠϝʔδ ※WEBΤϯδχΞษڧձ #05 Ͱ Dockerʹ͍ͭͯൃදͨ͠ͱ͖ͱશ͘ಉ͡ߏ੒

Slide 30

Slide 30 text

ࠓճߏங͢ΔγεςϜͷ֓ཁ • NginxΛϑϩϯτΤϯυͱͨ͠DjangoΞϓϦέʔγϣϯ • RedisΛΩϟογϡαʔόͱͯ͠ར༻ • ఆظతʹεέδϡʔϦϯά͞Εͨόον͕ಈ࡞͢Δ • DBʹ͸MySQLΛ࢖༻

Slide 31

Slide 31 text

KubernetesͰ؀ڥΛߏங͢Δʹ͸ • KubernetesͰ͸Docker Composeಉ༷ʹYAMLܗࣜͷϑΝΠϧΛѻ ֤ͬͯϦιʔεͷઃఆΛهड़͢Δ͜ͱͰ؀ڥΛߏங͍͖ͯ͠· ͢ɻઃఆ಺༰͸͓͓ͬ͟ͺʹ͸࣍ͷΑ͏ͳϑΥʔϚοτʹͳͬͯ ͍·͢ • ϩʔΧϧ؀ڥͷߏஙʹ͍ͭͯ͸Ұੲલ͸ minikube Λ࢖ͬͯߏங͠ ͍ͯ·͕ͨ͠ɺݱࡏ͸ Docker for Mac/Win Ͱ΋ਖ਼ࣜαϙʔτ͞Ε ͍ͯ·͢ɻ(σϑΥϧτͰ༗ޮʹͳ͍ͬͯͳ͍ͷͰ༗ޮԽ͢Δඞཁ ͕͋Γ·͢)

Slide 32

Slide 32 text

ओͳKubernetesͷઃఆϑΝΠϧ߲໨ apiVersion: ϦιʔεͰར༻͢ΔAPIͷόʔδϣϯΛهࡌɻϦιʔε͝ͱʹҟͳΔ kind: ϦιʔεͷछผΛهड़ɻ (ex: Deployment, Service) metadata: Ϧιʔε΁෇༩Մೳͳϝλσʔλɻओʹ໊শ΍ϥϕϧΛ෇༩͢Δͷʹ࢖༻ spec: Ϧιʔεݻ༗ͷઃఆΛهड़͢Δ data: ConfigMap΍SecretΛ࢝Ίͱͨ͠ઃఆσʔλΛهड़͢ΔϦιʔεͰར༻͞ΕΔ

Slide 33

Slide 33 text

1. DeploymentͷઃఆΛ࡞੒͢Δ

Slide 34

Slide 34 text

Deploymentͷઃఆ • NginxΛϑϩϯτͱͨ͠DjangoΞϓϦέʔγϣϯ ↑·ͣ͸͜ͷ෦෼͔Β࡞੒͠·͢

Slide 35

Slide 35 text

ConfigMapͷ४උ Docker Composeͷઃఆͱൺ΂ͯେ͖͘ҟͳΔͷ͕ nginx.conf ͷΑ ͏ͳઃఆϑΝΠϧͷѻ͍Ͱ͢ɻDocker Composeͷ৔߹͸ϗετଆͷ ಛఆͷσΟϨΫτϦʹઃఆϑΝΠϧΛ഑ஔ͠ɺͦΕΛίϯςφ಺΁ Ϛ΢ϯτͯ͠ѻ͏ྫ͕ଟ͔͔ͬͨͱࢥ͍·͕͢ɺKubernetesͰ͸ ClusterશମͰѻ͑ΔΑ͏ʹɺConfigMapͷϦιʔεͱͯ͠YAMLϑΝ Πϧ಺ʹ௚઀ઃఆϑΝΠϧͷ಺༰Λهड़͢Δํ๏ΛऔΓ·͢ɻ

Slide 36

Slide 36 text

1. ਤͷΑ͏ͳNginxͷઃఆϑΝΠϧΛ഑ஔ ͨ͠σΟϨΫτϦΛ༻ҙ͓ͯ͘͠ 2. nginxσΟϨΫτϦͷ1্ͭʹҠಈͯ͠Ҏ ԼͷίϚϯυͰConfigMapΛ࡞੒͢Δ kubectl create configmap nginx-config-common -—from-file=nginx/etc/common ConfigMapͷ࡞੒ྫ 3. ͜ΕΛඞཁͳ෼͚ͩߦ͏ɻ※αϒσΟϨΫτϦ·Ͱ͸ ݟͯ͘Εͳ͍ͷͰσΟϨΫτϦߏ੒ʹ͸஫ҙ

Slide 37

Slide 37 text

࡞੒ͨ͠ConfigMapͷ಺༰ΛkubectlͰ֬ೝ͢ΔͱҎԼͷΑ͏ʹͳ͍ͬͯΔ $ kubectl get configmap nginx-config-common -o yaml ——————————————————————— apiVersion: v1 data: nginx.conf: | user daemon daemon; daemon off; error_log /var/opt/nginx/log/error.log; pid /var/run/nginx.pid; worker_processes auto; worker_rlimit_nofile 100000; events { worker_connections 4000; use epoll; multi_accept on; } ɾɾɾɾ(௕͍ͷͰলུ) kind: ConfigMap metadata: creationTimestamp: 2018-09-12T12:47:51Z name: nginx-config-common namespace: default resourceVersion: "121247" selfLink: /api/v1/namespaces/default/configmaps/nginx-config-common uid: 0fae62dd-b68a-11e8-bbb2-025000000001

Slide 38

Slide 38 text

DeploymentͷઃఆΛهड़ ࡞੒ͨ͠ConfigMapΛNginxίϯςφͰಡΈࠐΉΑ͏ʹઃఆͯ͠Έ· ͢ɻ

Slide 39

Slide 39 text

࡞੒ͨ͠ConfigMapͷ಺༰ΛಡΈࠐΜͰ͍ΔDeploymentઃఆྫ apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: library/nginx:latest ports: - containerPort: 8080 - containerPort: 8443 volumeMounts: - name: v-nginx-config mountPath: /etc/nginx volumes: - name: v-nginx-config-common configMap: name: nginx-config-common volumesʹ࡞੒ͨ͠ConfigMap໊Λࢦఆͯ͠ɺ ίϯςφͷvolumeMounts ͷઃఆͰ Ϛ΢ϯτ͍ͨ͠ύεΛهड़͢Δ

Slide 40

Slide 40 text

Deploymentͷઃఆͷهड़·ͱΊ Nginxͷઃఆྫ·Ͱ঺հ͠·͕ͨ͠ɺಉ༷ʹDjangoΞϓϦέʔγϣ ϯͷઃఆʹ͍ͭͯ΋ίϯςφઃఆΛDeployment΁௥Ճ͢Ε͹OKͰ ͢ɻ͜͜Ͱ͸DjangoΞϓϦέʔγϣϯͷઃఆʹ͍ͭͯ·Ͱ͸ࡉ͔͘ ৮Ε·ͤΜ͕ɺConfigMapͷઃఆͱDeployment (Pod) ΁ͷඥ෇͚ํ ๏ʹ͍ͭͯ͸͜ΕͰΠϝʔδ͕͍͔ͭͨͱࢥ͍·͢ɻ

Slide 41

Slide 41 text

Deploymentͷ࡞੒ ྫͱͯ͠ɺ࡞੒ͨ͠DeploymentͷઃఆΛ nginx-django- deployment.yaml ͱ͍͏ϑΝΠϧ໊Ͱอଘ͠·͢ɻ ͦͷޙɺҎԼͷίϚϯυͰ Deployment ͷ࡞੒Λߦ͍·͢ɻ kubectl apply -f nginx-django-deployment.yaml kubectl create Ͱ΋࡞੒͸ՄೳͰ͕͢ɺapply͸࡞੒͓Αͼߋ৽ͷ྆ ํʹରԠ͍ͯ͠·͢ɻ(create͸طʹ࡞੒͞Ε͍ͯΔͱΤϥʔʹͳΔ) ͦͷͨΊɺσϓϩΠ࣌ͷมߋ൓өͰ͸͜ͷίϚϯυΛར༻͢Δέʔ ε͕ଟ͍Ͱ͢ɻ

Slide 42

Slide 42 text

2. ServiceͱIngressͷ ઃఆΛ࡞੒͢Δ

Slide 43

Slide 43 text

Serviceͷઃఆ Pod΁ΞΫηεՄೳʹ͢ΔͨΊʹServiceΛఆٛͯ͠ɺެ։ϙʔτΛ هड़͠·͢ɻ

Slide 44

Slide 44 text

Serviceͷઃఆྫ kind: Service apiVersion: v1 metadata: name: nginx-service spec: type: NodePort selector: app: nginx ports: - name: http protocol: TCP port: 8080 targetPort: 8080 - name: https protocol: TCP port: 8443 targetPort: 8443 selectorʹର৅Podͷϥϕϧʹఆٛͨ͠஋Λࢦ ఆͯ͠Serviceͱඥ෇͚Δ

Slide 45

Slide 45 text

IngressͷઃఆͱΠϯετʔϧ IngressΛ࢖͑͹AWSͰ͋Ε͹ALBΛׂΓ౰ͯΔ͜ͱ΋ՄೳͳͷͰ͢ ͕ɺएׯઃఆ͕໘౗ͳͷͰࠓճ͸ Nginx Ingress Controller Λར༻͠· ͢ɻ(ALBͷׂΓ౰ͯʹ͍ͭͯ΋ؾʹͳΔํ͸ alb-ingress-controller, kube-aws-ingress-controller ͋ͨΓΛௐ΂ͯΈ͍ͯͩ͘͞) Nginx Ingress Controllerʹ͍ͭͯ͸ helm ͱ͍͏Kubernetesͷύοέ ʔδϚωʔδϟʔΛ࢖ͬͯҎԼͷΑ͏ʹΠϯετʔϧ͠·͢ɻ helm install stable/nginx-ingress

Slide 46

Slide 46 text

Ingress (Nginx Ingress Controller)ͷઃఆྫ apiVersion: extensions/v1beta1 kind: Ingress metadata: annotations: kubernetes.io/ingress.class: nginx name: example-lb spec: rules: - host: www.example.com http: paths: - backend: serviceName: nginx-service servicePort: 8080 path: / tls: - hosts: - www.example.com secretName: example-tls selectorʹର৅Podͷϥϕϧʹఆٛͨ͠஋Λࢦ ఆͯ͠Serviceͱඥ෇͚ΔɻservicePortʹ͸ serviceͰࢦఆ͞Εͨ port ·ͨ͸ targetPort Λ ࢦఆ͢ΔɻHTTPSͷઃఆΛߦ͏৔߹͸ tls ͱ͍ ͏߲໨ʹূ໌ॻͷઃఆ͕ඞཁʹͳΓɺSecret ʹূ໌ॻΛొ࿥ͯͦ͠ΕΛࢦఆ͢Δඞཁ͕͋Δɻ

Slide 47

Slide 47 text

ServiceͱIngressͷઃఆ·ͱΊ Serviceʹ͸AWSͰ͸CLB΍NLBΛׂΓ౰ͯΔ͜ͱ͕ՄೳͰ͕͢ɺ L7૚Ϩϕϧͷ੍ޚ΋͍ͨ͠ˍHTTP2ରԠͨ͠HTTPαʔόΛ࢖͍͍ͨ ͱ͍͏έʔεʹ͓͍ͯ͸IngressΛ࢖͏͜ͱ͕͓͢͢ΊͰ͢ɻ ͳ͓ɺIngressͷIPΛRoute53ͳͲͷDNS΁ొ࿥͢ΔͨΊʹ͸ external-dns ΛΠϯετʔϧˍઃఆ͢Δ͜ͱͰࣗಈԽՄೳͰ͢ɻ(ઃ ఆʹ͍ͭͯ͸͜͜Ͱ͸঺հ͠·ͤΜͷͰɺڵຯͷ͋Δํ͸ௐ΂ͯΈͯ ͍ͩ͘͞)

Slide 48

Slide 48 text

3. όονΛ࡞੒͢Δ

Slide 49

Slide 49 text

Kubernetesʹ͓͚Δόον KubernetesͰ͸ Job ͱ͍͏ϦιʔεΛ࢖ͬͯɺ୯ҰίϚϯυͷ࣮ߦΛ ߦ͏PodΛఆٛ͢Δ͜ͱ͕ՄೳͰ͢ɻྫ͑͹DBͷϚΠάϨʔγϣϯͷ Α͏ͳॲཧΛఆٛ͢Δͷʹ޲͍͍ͯ·͢ɻ ·ͨɺఆظతʹॲཧΛ܁Γฦ࣮͠ߦ͍ͨ͠৔߹͸ CronJob Ϧιʔε Λར༻͠·͢ɻ

Slide 50

Slide 50 text

Jobͷઃఆྫ apiVersion: batch/v1 kind: Job metadata: name: db-migrate spec: backoffLimit: 1 parallelism: 1 completions: 1 template: spec: containers: - name: job-sleep image: debian:stretch-slim command: ["migrate", "up"] restartPolicy: Never backoffLimitͷ਺Λมߋ͢Δ͜ͱͰࣦഊ࣌ͷ࠶ ࢼߦճ਺ͳͲ΋ఆٛ͢Δ͜ͱ΋Ͱ͖Δɻ restartPolicy͕Neverʹઃఆ͞Εͨδϣϒ͕Ұ౓ ਖ਼ৗ׬ྃ͢Δͱɺdelete͞ΕΔ·Ͱ࠶࣮ߦ͞Ε Δ͜ͱ͸ͳ͘ͳΔɻ

Slide 51

Slide 51 text

CronJobͷઃఆྫ apiVersion: batch/v1beta1 kind: CronJob metadata: name: cron-job spec: schedule: "*/1 * * * *" jobTemplate: spec: template: spec: containers: - name: job-sleep image: debian:stretch-slim imagePullPolicy: Always command: ["echo", "test"] restartPolicy: Never scheduleʹcronܗࣜͰ࣮ߦ࣌ؒΛࢦఆ͢Δ͜ͱ ͕ग़དྷΔ

Slide 52

Slide 52 text

4. RedisͱDBΛ࡞੒͢Δ

Slide 53

Slide 53 text

Kubernetesʹ͓͚ΔΩϟογϡ ͱDBͷѻ͍ ϩʔΧϧ΍։ൃ؀ڥͰ͸Redis/MySQLίϯςφΛಈ͔ͯ͠΋Α͍ͷͰ ͕͢ɺAWSΛར༻͍ͯ͠Δέʔεͩͱຊ൪؀ڥͰ͸ElastiCache΍ RDSΛ࢖͍ͨ͘ͳΔ͔ͱࢥ͍·͢ɻ͜ͷ৔߹ɺKubernetes୯ମ (kubectl)Ͱ͸؆୯ʹ੍ޚ͕Ͱ͖ͳ͍ͨΊɺଞͷϓϩϏδϣχϯάπʔ ϧͰ͋Δ kops ΍ Terraform Λ૊Έ߹Θͤͯߏங͢Δ͜ͱʹͳΓ· ͢ɻ·ͨɺKubernetes on AWSͱͯ͠͸ EKS ͱ͍͏Ϛωʔδυαʔ Ϗε΋͋Γɺ2018೥9݄ݱࡏ͸౦ژϦʔδϣϯͰ͸·ͩ࢖͑·ͤΜ ͕ɺ౦ژϦʔδϣϯʹରԠͨ͠ࠒʹ͸ͪ͜ΒΛར༻͢Δͷ΋ྑ͍͔ͱ ࢥ͍·͢ɻ

Slide 54

Slide 54 text

Ҏ্Ͱߏங͢Δ্Ͱͷ͍͍ͩͨͷϙΠϯτΛ ঺հ͍͖ͤͯͨͩ͞·ͨ͠ɻ Kubernetes͸ΦʔέετϨʔγϣϯπʔϧͱ ͍͏ಛੑ্ɺߟྀ͢΂͖ϙΠϯτ΍ઃఆ͕ଟ͍ ͨΊʹDocker Composeʹ׳ΕͨํͰ΋࠷ॳ ͸ͱʹ͔͘ϋϚΔ͜ͱ͕ଟ͍Ͱ͕͢ɺ࢖͍͜ͳ ͤΔΑ͏ʹͳΔͱແఀࢭσϓϩΠ΍ো֐ൃੜ࣌ ͷΦʔτώʔϦϯά͕ൺֱత؆୯ʹߦ͑ΔΑ͏ ʹͳΔͨΊɺੋඇಋೖͯ͠ΈΔ͜ͱΛ͓͢͢Ί ͠·͢ɻ “

Slide 55

Slide 55 text

Docker Compose ͔ΒͷҠߦํ๏

Slide 56

Slide 56 text

Docker Compose͔ΒҠߦ͍ͨ͠ kompose ͱ͍͏πʔϧΛ࢖͏͜ͱͰɺdocker-compose.yaml ͔Β Kubernetes ͷϦιʔε΁ͱࣗಈม׵ͯ͘͠Ε·͢ɻͨͩ͠Kubernetes ͱDocker ComposeͰ͸ϘϦϡʔϜͷѻ͍ํ͕ҟͳΔͨΊɺͦͷ఺ʹ ͍ͭͯ͸গ͠ॻ͖௚͢ඞཁ͕͋Γ·͢ɻͦͷͨΊɺθϩ͔Βهड़͢Δ ΑΓ͸Ϛγ͘Β͍ͷೝࣝͰ࢖͏෼ʹ͸ɺ݁ߏख͕ؒল͚ͯศརͳͷͰ ͓͢͢ΊͰ͢ɻ

Slide 57

Slide 57 text

kompose ࣮ߦྫ 1. HomebrewͰΠϯετʔϧ brew install kompose 2. ݩͱͳΔ docker-compose.yaml Λࢦఆ͢Δ kompose convert -f docker-compose.yaml -o (ग़ྗઌσΟϨΫτϦ) volumeʹ͍ͭͯ͸σϑΥϧτͩͱPersistentVolumeClaim΁ม׵͞Ε·͕͢ɺ ͜Εʹ͍ͭͯ͸ --volumes Φϓγϣϯʹͯ hostPath ͱͯ͠ίϯόʔτ͢Δ͜ͱ΋Մ ೳͰ͢ɻDocker ComposeͰ͸ϗετྖҬΛϚ΢ϯτͯ͠ར༻͍ͯ͠Δέʔε͕ଟ͍ ͱࢥ͏ͷͰɺ࠷ॳ͸ hostPath ࢦఆͰίϯόʔτ͢Δํ͕ྑ͍͔΋஌Ε·ͤΜɻ (΋ͪΖΜޙ͔Βॻ͖௚͢લఏʹ͸ͳΓ·͢)

Slide 58

Slide 58 text

Kubernetesͷྑ͍ͱ ͜Ζͱগ͠ਏ͍ͱ͜ Ζͷ·ͱΊ

Slide 59

Slide 59 text

Kubernetesͷྑ͍ͱ͜Ζ͸ͳΜͱ ͳ͘Θ͔͖͔ͬͯͨͱࢥ͍·͢ɻ࠷ ޙʹɺྑ͍ͱࢥͬͨϙΠϯτͱɺ· ͩ·ͩগʑਏ͍ϙΠϯτΛ·ͱΊͯ Έ·ͨ͠ͷͰ঺հ͠·͢ɻ

Slide 60

Slide 60 text

Kubernetesͷྑ͍ϙΠϯτ

Slide 61

Slide 61 text

1. σϓϩΠ͕ൺֱత؆୯ kubectl apply ίϚϯυ͚ͩͰࠩ෼ݕ஌ͯ͠چ࢓༷ͷPodͱ৽࢓༷ͷPodͷೖΕସ͑Λ ߦ͑Δͱ͍͏ͷ͸ඇৗʹ؆୯Ͱ͢ɻ(΋ͪΖΜͦΕ͚ͩͰ͸ग़དྷͳ͍έʔε΋͋Γ· ͕͢) Podͷ਺΍RollingUpdateͷઃఆΛҙࣝͯ͠ઃఆ͢Δ͜ͱͰແఀࢭσϓϩΠ͕؆ ୯ʹ࣮ݱͰ͖Δͷ͸΍͸Γڧ͘ɺKubernetesಋೖΛܾఆ෇͚ΔϙΠϯτͷ̍ͭʹͳ Δ͔ͱࢥ͍·͢ɻ

Slide 62

Slide 62 text

2. ΫϥελߏஙͷͨΊͷपลπʔϧ͕๛෋ kops ΍ kube-aws ͳͲɺΫϥελߏஙΛ਺ίϚϯυ࣮ߦ͢Δ͚ͩͰ؆୯ʹ༻ҙͯ͠ ͘ΕΔपลπʔϧ͕͋ΔͨΊɺҰ͔ΒVPC΍αϒωοτͷઃܭΛࡉ͔͘Terraformͳ ͲͰ࡞͍ͬͯ͘ඞཁ΋গͳ͍Ͱ͢ɻ·ͨɺࡉ੍͔͍ޚΛ͍ͨ͠৔߹Ͱ΋ kops ͕ TerraformͷtfϑΝΠϧΛग़ྗͰ͖ΔͷͰͦͷΑ͏ͳέʔεʹ΋ରԠ͕ग़དྷ·͢ɻ

Slide 63

Slide 63 text

Kubernetesͷਏ͍ϙΠϯτ

Slide 64

Slide 64 text

1. ൿಗ৘ใΛϦϙδτϦʹͦͷ··ίϛοτ͠ਏ͍ KubernetesͰ͸SecretϦιʔεΛ࢖͏͜ͱͰൿಗ৘ใΛѻ͍΍͘͢ͳΓ·͕͢ɺઃ ఆϑΝΠϧ্͸஋ΛBase64Τϯίʔυͯ͠ه࿥͍ͯ͠Δ͚ͩʹա͗ͳ͍ͨΊɺ؆୯ ʹσίʔυͰ͖·͢ɻͦͷͨΊɺ͜ΕΛͦͷ·· git ϦϙδτϦ΁ίϛοτ͢Δͷ͸ ጨΒΕͯ͠·͍·͢ɻྫ͑ΔͳΒ Rails 5.2ͷCredentialsʹ૬౰͢Δػೳ͕ݸਓతʹ ͸͋Ε͹͍͍ͳͱࢥ͍ͬͯ·͕͢ɺͦͷΑ͏ͳػೳ͸ඪ४Ͱ͸ଘࡏ͠ͳ͍໛༷Ͱ ͢ɻͰ͕͢ɺ͜Εʹ͍ͭͯ͸ bitnami-labs/sealed-secrets ΛΠϯετʔϧͯ͠ SealedSecretϦιʔεΛ௥Ճ͢Δ͜ͱͰ࣮ݱՄೳͷΑ͏Ͱ͢ɻ ʻࢀߟαΠτʼ
 https://engineering.bitnami.com/articles/sealed-secrets.html

Slide 65

Slide 65 text

2. ؀ڥม਺ΛઃఆϑΝΠϧ಺ͰࢀরͰ͖ͳ͍ Docker ComposeͷΑ͏ʹίϚϯυ࣮ߦ࣌ʹ؀ڥม਺Λ༩͑ɺͦΕΛઃఆϑΝΠϧ Ͱࢀর͢Δͱ͍ͬͨػೳ͸ݱঢ়αϙʔτ͞Ε͍ͯͳ͍Α͏Ͱ͢ɻDocker Composeͷ ͱ͖͸͜ΕͰϏϧυͨ͠λάͷόʔδϣϯΛ੾Γସ͍͑ͯͨͨΊɺগʑࠔΓ·͠ ͨɻ( $(pwd) ͷΑ͏ʹΧϨϯτσΟϨΫτϦΛࢦఆ͢Δ͜ͱ΋ग़དྷ·ͤΜ) ͨͩɺ͜Εʹ͍ͭͯ΋ճආࣗମ͸ՄೳͰɺͲ͏ͯ͠΋ͦͷΑ͏ͳ࢖͍ํΛ͍ͨ͠৔ ߹͸ envsubst ίϚϯυΛ࢖͑͹࣮ݱ͸ՄೳʹͳΓ·͢ɻ

Slide 66

Slide 66 text

·ͱΊ

Slide 67

Slide 67 text

Kubernetes͸Docker for Mac/Win͕ωΠςΟ ϒͰαϙʔτ͢ΔΑ͏ʹͳͬͨӨڹ΍AWSͰ ΋EKS͕ొ৔ͨ͠ྲྀΕ΋͋Γɺࠓޙ΋࢖༻ࣄྫ ͕૿͍͔͑ͯ͘ͱࢥ͍·͢ɻ ͦͷͨΊ͜Ε͔Β৽͍͠αʔϏεΛߏங͠Α͏ ͱ͍ͯ͠Δํ΍ɺDockerΛ࢖͍ͬͯΔ΋ͷͷ Πϯϑϥ΍σϓϩΠ·ΘΓͷߏ੒؅ཧʹ೰ΜͰ ͍Δํ͸ɺੋඇಋೖΛݕ౼ͯ͠ΈΔ͜ͱΛ͓͢ ͢Ί͍ͨ͠·͢ɻ

Slide 68

Slide 68 text

͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠