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

Introduction to Kubernetes Using GKE

Introduction to Kubernetes Using GKE

lanocci

June 19, 2018
Tweet

More Decks by lanocci

Other Decks in Technology

Transcript

  1. ໌೔͔Β࢖͑Δ Kubernetes ೖ໳ ~جૅ͔ΒॳΊͯͷσϓϩΠ~ Supporterz CoLab ษڧձ Jun. 19, 2018

    Ӝ໺ ߛҰ / URANO, Koichi (@lanocci) SRE at Adtech Studio, CyberAgent, Inc.
  2. ໨࣍ 1. ࣗݾ঺հ 2. ຊ೔ͷ͓඼ॻ͖ 3. લఏ஌ࣝͷ͓͞Β͍ 4. Kubernetesͷதͷ֓೦ʢຊ୊ʣ 5.

    ࣄྫ঺հʢ෭୊ᶃʣ 6. ίϚϯυϥΠϯπʔϧͱ͔ͷ঺հʢ෭୊ᶄʣ
  3. ࣗݾ঺հ • Ӝ໺ɹߛҰ (@lanocci) • גࣜձࣾαΠόʔΤʔδΣϯτ • Πϯλʔωοτ޿ࠂͷΠϯϑϥ΍ͬͯ·͢ • Kubernetesྺ:

    GKEͰ໿1೥ • ࡢ೥7݄·Ͱ͸SIerͰέʔϒϧςϨϏاۀͷγεςϜ։ൃɾӡ༻ • CKA (Certified Kubernetes Administrator) ϗϧμʔ(#366) Koichi Urano April 1, 2018 CKA-1800-0366-0100 1 / 1
  4. The Goal of This Talk • ʮKubernetesͬͯͳʹʁΑ͘Θ͔ΒΜʯͱ͍͏ਓ ͕ɺKubernetesͰΞϓϦΛಈ͔͢Πϝʔδ͕ͳΜͱ ͳ͘Ͱ͖ΔΑ͏ʹͳΔ͜ͱ •

    ʮKubernetes৮ͬͯΈ͚ͨͲ࠳ંͨ͠ʯͱ͍͏ਓ ͕΋͏Ұ౓ϋνϚΩΛకΊ௚͖͔͚ͬ͢ͱͳΔ͜ͱ • ໌೔͔ΒGKEͰ༡ΜͰΈ͍ͯͩ͘͞Ͷʂʂ
  5. What’s Docker Engine? • ʮίϯςφʯͷ࣮ߦ؀ڥΛఏڙ • ϑϨʔϜϫʔΫͷόʔδϣϯͳͲɺ͍ΖΜͳґଘؔ܎Λʮίϯςφʯ ͷதʹด͡ࠐΊΔ͜ͱͰɺҠ২ੑͷߴ͍ΞϓϦέʔγϣϯΛ࡞Δ͜ͱ ͕Ͱ͖Δɹɹ(Build Once,

    Run Anywhere) ⁶ PaaS • ίϯςφͷ࡞੒ɺ࡟আ͸ߴ଎ͳͷͰෛՙঢ়گʹΑͬͯॊೈʹϦιʔε Λ࢖͍෼͚ΒΕΔͱ͍͏ϝϦοτ͕͋Δ • Docker୯ମͰ͸ಉҰϗετ಺ͷίϯςφ͔͠؅ཧͰ͖ͳ͍ ɹɹɹ→ͪΌΜͱ࢖͏ͳΒΦʔέετϨʔγϣϯγεςϜ͕ඞཁ
  6. What’s Kubernetes? • ίϯςφΛ࢖ͬͨΞϓϦέʔγϣϯͷσϓϩΠɺεέʔϧɺ؅ཧΛࣗಈԽ͢ ΔΦʔϓϯιʔεγεςϜ • Kubernetes is an open-source

    system for automating deployment, scaling, and management of containerized applications. (https:// kubernetes.io/) • DockerͰ΋ެࣜαϙʔτ͞ΕͨΓʢDocker for Mac ʹඪ४౥ࡌʣɺઌ೔ AWSͰ΋KubernetesͷαʔϏε͕GA͞ΕͨΓͰɺΦʔέετϨʔγϣϯ πʔϧͷ΄΅σϑΝΫτελϯμʔυͱݴ͑Δײ͡ • Docker Engine౳Λ࢖ͬͯ࡞੒ͨ͠ΞϓϦέʔγϣϯΛෳ਺ͷαʔόʹσϓ ϩΠͯ͠ӡ༻͢ΔͨΊʹඞཁͳػೳΛఏڙͯ͘͠ΕΔ • ΞϓϦέʔγϣϯಉ࢜Λ࿈ܞͤͨ͞ΓɺϚγϯϦιʔεΛ؅ཧͨ͠Γɺ֎෦͔ΒͷΞΫηεํ๏Λఏڙͨ͠ΓɺΞϓϦέʔγϣϯͷΞοϓσʔτख ஈΛఏڙͨ͠Γ • Docker͚ͩ͡Όͳͯ͘rktͱ͔΋࢖͑ΔΒ͍͠
  7. What’s GKE? • Google Kubernetes Engineͷུ • ʢࡢ೥຤ࠒ·Ͱ͸Google Container Engineʣ

    • Google Cloud Platform ͷͳ͔ͰKubernetes Λ࢖ͬͨίϯςφج൫Λఏڙ͢ΔαʔϏε
  8. Why GKE? • PCϩʔΧϧ΍ɺGCEͳͲͷVMʹKubernetesΛߏங͢Δ͜ͱ΋Ͱ͖Δ͕ɺ ͦΕΛ͢Δʹ͸Kubernetes಺෦ͷΞʔΩςΫνϟʹର͢Δཧղ͕ඞཁ • ಺෦ωοτϫʔΫͷઃఆ΋ผ్ඞཁͩͬͨΓͯ݁͠ߏͦ͜Ͱ٧·ͬͨΓ͢ Δ • खͬऔΓૣ͘KubernetesΛཧղ͢ΔͨΊʹ͸·ͣɺϚωʔδυαʔϏεΛ

    ࢖ͬͯΈΔͷ͕ۙಓͩͱࢥ͍·͢ • ͔͠΋ɺGKE͸ແྉ࿮Ͱ݁ߏ৭ʑ͍͡ΕΔͷͰΦεεϝ • $300෼ͷແྉ࿮͕1೥ؒ࢖͑Δ • minikubeͰ΋͍͍͚Ͳtype: loadBalancerͱ͔Ͱ֎෦ެ։Ͱ͖Δͷ͸ศར
  9. جຊ֓೦ᶅ - Node • ίϯςφ͕഑ஔ͞ΕΔϚγϯ(෺ཧϚγϯ / VM) • ͜ͷNodeʹҰͭҰͭͷPod(ޙड़)ׂ͕Γ౰ͯΒ ΕΔΠϝʔδ

    • GKEͰ͸ClusterΛ࡞੒͢ΔͱࣗಈͰNodeʹ૬ ౰͢ΔGCEΠϯελϯε͕࡞੒͞ΕΔ
  10. DEMO • Ϋϥελͷ࡞੒ • • image-type: NodeͷOSλΠϓ • machine-type: Node

    ͷαΠζ(CPU/Mem) • num-nodes: Nodeͷ਺ • preemptible: ҆͘͢ΔͨΊͷ͓·͡ͳ͍ʢpreemptive nodeʣ $ gcloud container clusters create test-cluster \ --image-type=cos \ —-machine-type=n1-standard-1 \ -—preemptible \ -—num-nodes=2
  11. Deployment • Podͷ࡞੒ɺߋ৽Λߦ͏୯ҐΛఆٛ • DeploymentΛ࡞੒͢Δͱ͜Μͳ͜ͱ͕Ͱ͖Δ • Podࣗମͷ࡞੒ • Pod਺ͷઃఆɾมߋ •

    Deploymentʹଐ͢ΔPodͷΞϓϦέʔγϣϯΛࢭΊͣʹΞοϓ σʔτʢϩʔϦϯάΞοϓσʔτʣ • Serviceʢޙड़ʣ΁ͷΞλον ※ ࡉ͔͍͜ͱΛݴ͏ͱDeployment͸Replica SetΛ؅ཧ͍ͯ͠Δ͕ɺઆ໌͸ׂѪ
  12. Deployment Sample(nginx) apiVersion: extensions/v1beta1 kind: Deployment metadata: name: test-deployment spec:

    replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest resources: limits: cpu: 400m memory: 200Mi requests: cpu: 200m memory: 200Mi ports: - containerPort: 80 σϓϩΠϝϯτ໊ ࡞੒͢Δ1PE਺ ͜ͷϥϕϧ͕͍͍ͭͯΔ1PEΛσϓϩΠϝϯτ͕؅ཧ͢Δ 1PEʹ͚ͭΔϥϕϧ ίϯςφͷ൪ϙʔτΛ։͚Δ ʢࢦఆ͠ͳͯ͘΋։͘ʣ ࢖༻͢ΔEPDLFSΠϝʔδ ׂΓ౰ͯϦιʔε 1PEͷ ఆٛ
  13. • Deploymentͷద༻ • yamlϑΝΠϧ͔ΒDeploymentΛద༻͢Δ • ͪΌΜͱͰ͖ͯΔ͔֬ೝ $ kubectl apply -f

    path/to/the/deployment-manifest.yaml DEMO $ kubectl port-forward test-nginx 8080:80
  14. Service.yml Sample(LoadBalancer) apiVersion: v1 kind: Service metadata: name: test-service spec:

    ports: - port: 80 targetPort: 80 protocol: TCP loadBalancerIP: xx.xx.xx.xx type: LoadBalancer selector: app: nginx ֎෦͔ΒͷΞΫηεΛड͚෇͚Δ*1ΞυϨε (,&Ͱ͸ࢦఆ͠ͳ͚Ε͹ࣗಈͰׂΓ౰ͯ ΞΫηεΛసૹ͢Δઌͷ 1PEଆͷϙʔτ൪߸ ΞΫηεΛड͚෇͚Δ 4FSWJDFଆͷϙʔτ൪߸ ΞΫηεΛసૹ͢Δઌͷ 1PEΛϥϕϧͰࢦఆ
  15. • Serviceͷద༻ • yamlϑΝΠϧ͔ΒServiceΛద༻͢Δ • ͜Μͳ΍Γํ΋͋Δ $ kubectl apply -f

    path/to/the/service-manifest.yaml DEMO $ kubectl expose deployment test-deployment —-port=80 —-target-port=80
  16. Service.yml Sample(NodePort) apiVersion: v1 kind: Service metadata: name: test-service spec:

    ports: - port: 80 targetPort: 80 nodePort: 30080 protocol: TCP type: NodePort selector: app: nginx ΞΫηεΛసૹ͢Δઌͷ 1PEΛࢦఆ /PEFͷ͜ͷϙʔτ΁ͷΞΫηεΛ͜ͷαʔϏεʹసૹ ࢦఆ͠ͳ͚Ε͹ࣗಈͰׂΓ౰ͯ
  17. ࢖ͬͯΈΔͱศརͳίϯϙʔωϯτ • Config Map • Secret • Ingress • Daemon

    Set • Cronjob • ʢ͜ͷล͸ຊ൪؀ڥ࡞ΔͳΒඞਢ͔΋͚ͩͲɺݸਓͰ༡Ϳ෼ʹ͸ͳͯ͘΋ͳΜͱ͔ͳΔʣ
  18. Config Map Sample apiVersion: v1 kind: ConfigMap metadata: name: dev-common-env

    data: TEST_HOST: “www.testdomain.com" TEST_PORT: "8081" TEST_ID: “test-project" EBUBϑΟʔϧυͷԼʹ ΩʔόϦϡʔΛॻ͘
  19. Deployment͔Βͷݺͼग़͠ … envFrom: - configMapRef: name: dev-common-env … … env:

    - name: DB_ACCESS_POINT valueFrom: configMapKeyRef: name: dev-common—env key: TEST_ID … DPOpHNBQΛؙ͝ͱಡΈग़͠ ͯ؀ڥม਺ʹద༻ DPOpHNBQ͔Β஋ΛҰͭ ಡΈग़ͯ͠؀ڥม਺ʹద༻
  20. Secret Sample --- apiVersion: v1 data: tls.crt: <tlsূ໌ॻ> tls.key: <tlsൿີ伴>

    kind: Secret metadata: name: domain-certs type: Opaque EBUBϑΟʔϧυͷԼʹ ΩʔόϦϡʔΛॻ͘
  21. Deployment͔Βͷݺͼग़͠ … env: - name: AWS_ACCESS_KEY_ID valueFrom: secretKeyRef: name: aws-credential

    key: aws.access_key_id … TFDSFU͔Β஋ΛҰͭ ಡΈग़ͯ͠؀ڥม਺ʹద༻
  22. Ingress Sample --- apiVersion: extensions/v1beta1 kind: Ingress metadata: name: test-ingress

    annotations: kubernetes.io/ingress.global-static-ip-name: dev-console-ingress kubernetes.io/ingress.class: "gce" spec: tls: - secretName: domain-certs backend: serviceName: test-service servicePort: 80 rules: - host: www.test.com http: paths: - path: /* backend: serviceName: test-service servicePort: 80 (,&ͩͱɺOHJOYΛ࢖͏͔ɺ ($1ͷ)551-#Λ࢖͏͔બ΂Δ σϑΥϧτͷόοΫΤϯυͷαʔϏεΛࢦఆ ΞΫηεϧʔϧΛࢦఆͯ͠ όʔνϟϧϗετͷΑ͏ʹ࢖͏͜ͱ΋Ͱ͖Δ ʢ͜ͷCBDLFOEͷࢦఆ͕TQFDCBDLFOEΑΓɹ ɹ༏ઌ͞ΕΔʣ ※ Ingress͕ར༻͢ΔHealth Check͸σϑΥϧτͰbackendͷ/΁ͷΞΫηε ※ มߋ͍ͨ͠৔߹͸PodͷఆٛͰ`readinessProbe`Λઃఆ͢Δඞཁ͕͋Δ 5-4ͷऴ୺΋*OHSFTTͰͰ͖Δ
  23. `gcloud container clusters` • ओʹΫϥελͷ࡞੒΍ϩάΠϯઌͷΫϥελͷ ੾Γସ͑ʹ࢖͏ • ଞʹ΋ɺΫϥελΛϦαΠζ͢ΔίϚϯυͱ͔͋Δ͚Ͳࠓ೔͸লུ e.g. (Լઢ෦͸ࣗ͝਎ͷ؀ڥʹஔ͖׵͑ͯ࢖͍ͬͯͩ͘͞)

    # Ϋϥελͷ࡞੒ ## CPU 0.5core/ Memory 1.7GB ͰcoreosϕʔεͷϚγϯ3୆ΛnodeΫϥελͱͯ͠૊Ή $ gcloud container clusters create test-cluster \ —-project=your-project —-zone=asia-northeast1-a —image-type=cos \ —-machine-type=g1-small --num-nodes=3 # Ϋϥελ΁ͷϩάΠϯ ## ্ͷίϚϯυͰ࡞੒ͨ͠Ϋϥελ΁ϩάΠϯ͠ɺkubectlΛ࢖͑ΔΑ͏ʹ͢Δ $ gcloud container clusters get-credentials test_cluster \ —-project=your-project ——zone=asia-northeast1-a # ࡞ͬͨΫϥελΛ࡟আ $ gcloud container clusters delete test-cluster —-zone=asia-northeast1-a
  24. Α͘࢖͏ίϚϯυ # ϑΝΠϧ͔ΒίϯϙʔωϯτΛ࡞੒͢Δ # ಉ໊ͷίϯϙʔωϯτΛߋ৽͢Δͱ͖΋ಉ͡ίϚϯυ $ kubectl apply -f deployment.yml

    # ཁૉҰཡΛऔಘ (pods /ɹdeployments / services / ingress / configmap …) $ kubectl get pods # লུ΋Ͱ͖ΔΑ pods -> po ͱ͔ services -> svcͱ͔ # ৄࡉΈ͍ͨ৔߹͸ͬͪ͜ $ kubectl describe deployment test-deployment # ίϯςφΠϝʔδΛߋ৽ $ kubectl set image deployment/test-deployment nginx=nginx:latest # ίϯςφͷதʹೖͬͯΈΔ $ kubectl exec -it test-deployment-xxxxxxx —- /bin/bash
  25. Others • kubectx <https://github.com/ahmetb/kubectx> Ϋϥελͷ੾Γସ͑ʢίϯςΩετͷ੾Γସ͑ʣ • stern <https://github.com/wercker/stern> ࢦఆͨ͠จࣈྻΛؚΉPodͷϩάΛ tail

    ͯ͘͠ΕΔ • ckube <https://github.com/devonmoss/ckube> kubectl ͰͷϫʔΫϑϩʔΛ؆୯ʹͯ͘͠ΕΔπʔϧ | ࢀߟ: <https://speakerdeck.com/showmurai/kubernetestecliwokuai-shi-woshi-itai>
  26. ༻ྫ # ίϯςΩετҰཡ $ kubectx # ίϯςΩετ੾Γସ͑ $ kubectx test-cluster

    # ͋ΔσϓϩΠϝϯτͷlogΛ·ͱΊͯtail $ stern test-deployment # ΠϯλϥΫςΟϒʹ֤ίϯϙʔωϯτͷ৘ใʹΞΫηε $ ckube ls
  27. Further Reading • ࠔͬͨΒͱΓ͋͑ͣެࣜʢӳޠͷΈʣ ͜Ε: <https://kubernetes.io/> • ίϯςφج൫ͷσβΠϯύλʔϯ͕Google͔Βެ։͞Ε͍ͯΔͷͰɺڵຯ͕͋ͬͨ ΒͦΕΛࢀߟʹͯ͠ຏ͖Λ͔͚͍ͯ͘ͱྑ͍͔΋ ͜Ε:

    <https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/45406.pdf> • ͔ͬ͜Α͘σϓϩΠ͍ͨ͠ਓ޲͚ Helm(ύοέʔδ؅ཧ): <https://helm.sh/> : Kubernetes ϚχϑΣετΛύοέʔδͷΑ͏ʹར༻ Spinnaker(CDπʔϧ): <https://www.spinnaker.io/> : σϓϩΠύΠϓϥΠϯΛߏங Istio (Service Mesh): <https://istio.io/> : Canary Release Ͱ Canary ൛΁ͷτϥϑΟοΫίϯτϩʔϧͱ͔
  28. We Are Hiring • ͝ڵຯͷ͋Δํ͸͓ؾܰʹ͓੠͔͚͍ͩ͘͞ • ΦϑΟεݟֶ͚ͩͰ΋େ׻ܴ • ຊ൪؀ڥ Ͱ

    Kubernetes ࢖͏ϓϩμΫτ΋૿͖͑ͯ·ͨ͠ • ࣾ಺ʹεΩϧΞοϓθϛͱ͍͏੍౓͕͋ͬͯɺKubernetesͷθϛ΋͋Γ·͢ גࣜձࣾαΠόʔΤʔδΣϯτ ΞυςΫελδΦ / ΞυςΫຊ෦ <https://adtech.cyberagent.io>