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

はてなリモートインターン2021 Kubernetes 講義資料

Hatena
October 08, 2021

はてなリモートインターン2021 Kubernetes 講義資料

Hatena

October 08, 2021
Tweet

More Decks by Hatena

Other Decks in Technology

Transcript

  1. ؕ٤تع٭ٜ ˝ %PDLFS %PDLFSGPS.BD ˝ CSFXJOTUBMM % brew update %

    brew install kubectl kustomize minikube mysql skaffold 
  2. ,VCFSOFUFTכע ˝ LVCFSOFUFT LT ˝ ؤ٤طػ؛٭آتعٝ٭ب٘٤סג״סخنعؘؗؓ ˝ ؤ٤طػסظوٞؕյ؛٭عتآ٭ٛ٤ءםלס畘杼؅舅Ⳃ⴫׌׾ג ״סوٚشعنؚ٭ّ ˝

    舅⯥ס擻杼ُب٤┕מوٚشعنؚ٭ّ؅啶疣׌׾׆כ׵⺎耆 ˝ قهٛشؠؠٚؗغך׵وٚشعنؚ٭ֿّ䬠❠׈׿יַ׾ ˝ "84&,4 ($1(,& "[VSF",4 
  3. $MVTUFS ˝ ُتذ٭ؿ٭غכ٠٭؜٭ؿ٭غך啶䡗׈׿׾ ˝ ُتذ٭ؿ٭غ ˝ ؤ٤عٞ٭ٜوٝ٭٤ ˝ ؠٚتذ؅畘杼؅䥵䎎 ˝

    قهٛشؠؠٚؗغךעُؾ٭ةغמ䬠❠׈׿׾ ˝ ٠٭؜٭ؿ٭غ ˝ ظ٭ذوٝ٭٤ ˝ ؤ٤طػ؅㲔车׌׾ג״סؤ٤مٖ٭طؔ٤ءٛ خ٭تס꥗ױ׽ ˝ 邾丗סOPEF؅玮ה┕ׅ׾׆כך냕⺎榫䓪؅䐂׾׆ כֿך׀׾ 
  4. /PEF ˝ 擻杼PS♞䘶סئ٭ف٭ ˝ 邾丗ס1PE ؤ٤طػ ؅㲔车׌׾ג״סؤ٤ مٖ٭طؔ٤ءٛخ٭ت ˝ LVCFMFUֿ颯Ⳃ׊יַיُتذ٭ؿ٭غס

    "1*כ׷׽כ׽؅׌׾ ˝ /PEFע邾丗⺏ך颯Ⳃ׈׿ؠٚؗغ梪㗞ךע تآ٭ٜ׌׾☼磝ײ؅㵵⪌׊יַ׾㕙⺬ֿ 㛡ַ 
  5. LVCFSOFUFTסٛخ٭تכ؜طإٛ ˝ 8PSLMPBET ؤ٤طػס㲔车מ꞊׌׾ٛخ٭ت ˝ 4FSWJDF ؤ٤طػ؅㛙鼧⪜ꝧ׌׾׻ֹםؙ٤غَؕ٤ع؅䬠❠׌׾ٛخ٭ت ˝ $POH4UPSBHF 錃㲊٬嚀㳡䗯㕔٬姧禈⴫ٍٖٛ٭ّםלמ꞊׌׾ٛخ٭ت

    ˝ $MVTUFS ج؞ٖٛطؔ׷ؠؚ٭ذםלמ꞊׌׾ٛخ٭ت ˝ .FUBEBUB ؠٚتذ⫂ס☽סٛخ٭ت؅䳩⛼׌׾ג״סٛخ٭ت /BNFTQBDFכַֹ♞䘶氳םؠٚتذס⮆ꦕ嚀耆 
  6. 8PSLMPBET؜طإٛ ˝ 1PE ח♓┕סؤ٤طػ־׼啶䡗׈׿יַ׾ 1PE⫂מ⻠ױ׿׾ؤ٤طػע⺱׋ؾشع٠٭ ؠ⫂מ㰆㏇׊⺱┉*1؅䧏ח ˝ 3FQMJDBTFU 1PEסٝوٛ؜؅⛼䡗׊י䧗㲊׊ג丗ס1PE ؅禴䧏׌׾

    جٜنك٭ٛ٤ء ⺱׋ؓوٛآ٭ب٘٤ֿⳂ⛼׊יַ׾邾丗ס 1PEֿ姡䇖מ㷣ꝧ׈׿׾ ˝ %FQMPZNFOU 邾丗ס3FQMJDBTFU؅畘杼׊יٞ٭ٛ٤ءؓش وظ٭ع׷ٞ٭ٜفشؠםל؅⺎耆מ׌׾ 
  7. *OHSFTT ˝ *OHSFTT ˝ -סٞ٭غفٚ٤ئ ˝ 㲔逷ע喋չ "84"-# ($1($-# ؠٚتذ⫂/HJOY

    ˝ )5514ס磙珷 ˝ *OHSFTT$POUSPMMFS ˝ *OHSFTTٛخ٭تֿ氧ꎰ׈׿׾כ┕鋗㲔逷ס㲔⛮؅⛼䡗׌׾ ˝ "84-PBE#BMBODFS$POUSPMMFS ˝ (,&*OHSFTT$POUSPMMFS ˝ /HJOY*OHSFTT$POUSPMMFS 
  8. .BOJGFTU ˝ ٛخ٭ت㲊紶ֿ傴־׿ג:".- ˝ kubectl apply -f <manifest>מ׻זיLTؠٚتذמ 鸵榫 ˝

    ׆׿מ׻׽㲚銧氳םؤ٭غמ׻׾畘杼ֿ⺎耆כם׾ ˝ *OGSBTUSVDUVSFBT$PEF 
  9. ٛخ٭ت⯆꡾ ˝ 1PEמ㵚׊יⰟ׽䎎י׾$16׷ْٓٛס⯆꡾؅车ֹ׆כֿך׀ ׾ ˝ $16ؤؓ W$16 ؅N NJMMJDPSFT כ׊י䧗㲊

    ˝ 釐姲כ⯆꡾ ˝ ┖꡾כ┕꡾؅䧗㲊ך׀׾ ˝ ؿ٭غמ1PE؅鿥糋׌׾꤀מ縒䜡׈׿׾ 
  10. 䦡䍖䓪 ˝ 姡䇖؛٭عتآ٭ٜ ˝ )PSJ[POUBM1PE"VUPTDBMFS )1" ˝ $16璡סْعٛؠت؅⹆拨׊י1PEסٝوٛ؜؅舅Ⳃך鴑ⱶ׌׾ ˝ 㑈泡؛٭عتآ٭ٜ

    ˝ 7FSUJDBM1PE"VUPTDBMFS 71" ˝ $16 ْٓٛס✳榫ꄈ؅⮆冪׊יٛؠؙتع⡁؅䬠勅׊ג׽舅Ⳃ氳מ催 二׊ג׽׌׾ 
  11. ُؼنؘتعס啶䡗 k8s ├── account │ ├── app.yaml │ ├── config

    │ │ └── schema.sql │ ├── db.yaml │ ├── kustomization.yaml │ ├── secret │ │ └── ecdsa-private.pem │ └── test.yaml ├── blog │ ├── app.yaml │ ├── config │ │ └── schema.sql │ ├── db.yaml │ ├── kustomization.yaml │ ├── secret │ │ └── account-ecdsa-public.pem │ └── test.yaml ├── kustomization.yaml ├── namespace.yaml ├── renderer-go │ ├── app.yaml │ └── kustomization.yaml └── system └── sa.yaml ˝ k8sظؔٝؠعֿُٛؼنؘتع糋׀㕙 ˝ BDDPVOUCMPHSFOEFSFSHPכُؕؠٞئ٭لتׇכ מظؔٝؠعٛ؅⮆ׄ׾ ˝ kustomization.yamlֿLVTUPNJ[Fס錃㲊نٜؒؕ 
  12. apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources: - app.yaml - db.yaml -

    test.yaml secretGenerator: - name: blog-app-secret files: - secret/account-ecdsa-public.pem configMapGenerator: - name: blog-app-env-vars literals: # (snip) - name: blog-db-schema-config files: - config/schema.sql 
  13. 颯Ⳃ # Minikube Λىಈ % minikube start !"kubernetes-version v1.19.8 !"driver

    docker % eval $(minikube docker-env) # context Λઃఆ % kubectl config set-context hatena-intern-2021 !"cluster=minikube !"user=minikube !"namespace=hatena-intern-2021 % kubectl config use-context hatena-intern-2021 # skaffold Λىಈ % make up skaffold dev !"cleanup=false 
  14. LVCFDUM؅✳ֹ嶖⤓ CBTI[TIךסؤُ٤غ遨㱭ס錃㲊 # bash source <(kubectl completion bash) # zsh

    source <(kubectl completion zsh) ֽ׌׌״ ؙؕٛؓتס錃㲊؅׊יֽׂ alias k=kubectl 
  15. 1PEֿ颯Ⳃ׌׾喋㯸؅釤׻ֹ ┉䈱梪㗞؅⯡ꢜ׊יյ颯Ⳃ׊泡׊ױ׊׺ֹ % skaffold delete % make up ⮯ס珷儕ך㲔车׊י1PEס敯䛜؅釤׾ %

    kubectl get pods -w % kubectl describe pods 1PEֿ颯Ⳃ׊ג׆כ؅澬鏀׊י % minikube -n hatena-intern-2021 service blog ؅㲔车յهٞء؅⛼䡗׊יײױ׊׺ֹ 
  16. ظفشء # Podͷৄࡉ৘ใͷදࣔ % kubectl describe pod blog # ωʔϜεϖʔε্ʹ͋ΔϦιʔεͷ৘ใΛදࣔ

    % kubectl get all # ىಈ͍ͯ͠ΔPodͰγΣϧΛىಈ͢Δ % kubectl exec -it svc/account !" /bin/sh ˝ 颯Ⳃ׊יַ׾وٞجتע ! ps ˝ CMPHئ٭لتמؓؠجت ! wget -q -O - blog:8080 ˝ ! nslookup blog 
  17. OPEFյQPEסْعٛؠت؅⹦䐂 ⹦䐂ך׀׾ױךמ侇ꝴֿ־־׾סך㵼׊䏨זי־׼㲔车׌׾ % kubectl top nodes NAME CPU(cores) CPU% MEMORY(bytes)

    MEMORY% docker-desktop 469m 5% 1873Mi 32% % kubectl top pods NAME CPU(cores) MEMORY(bytes) account-86649c57b4-jq26g 2m 3Mi account-db-55579cb588-mmfv6 15m 345Mi account-test-59c689cdd4-q89rh 0m 1Mi blog-db-6f7f4c8797-rxtnz 15m 406Mi blog-test-86cff5d98c-ttp2z 0m 0Mi renderer-go-7d8d7fdf64-qhn95 5m 13Mi 
  18. ُؼنؘتعס鴑ⱶ % cp -R k8s/renderer-go k8s/renderer-ts % perl -i -pe

    's/renderer-go/renderer-ts/g' k8s/renderer-ts/*.yaml ˝ k8s/renderer-goظؔٝؠعٛ؅ؤم٭׊יk8s/ renderer-ts؅⛼䡗 ˝ ُؼنؘتع⫂סrenderer-go؅renderer-tsמ糋׀䬵ֻ 
  19. ُؼنؘتعס稴꥗ TLBPMEZBNM apiVersion: skaffold/v2beta5 kind: Config metadata: name: hatena-intern-2021 build:

    artifacts: # (snip) - image: hatena-intern-2021-renderer-go context: services/renderer-go - image: hatena-intern-2021-renderer-ts # ! context: services/renderer-ts # ! local: # (snip) ˝ services/renderer-tsסEPDLFSْؕ٭ةסلٜغכLTؠٚتذ׫ס⹚何ֿ׈׿׾׻ֹמ׌׾ 
  20. ُؼنؘتعס稴꥗ LTCMPHLVTUPNJ[BUJPOZBNM apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization # (snip) configMapGenerator: -

    name: blog-app-env-vars literals: - MODE=development - DATABASE_DSN=root@(blog-db:3306)/intern_2021_blog?time_zone=UTC&parseTime=true&loc=UTC - ACCOUNT_ADDR=account:50051 - RENDERER_ADDR=renderer-ts:50051 # ! - name: blog-test-env-vars # (snip) ˝ CMPHئ٭لتס鋗嫎㚺䬵ئ٭لت׫ס⺸׀⩝؅SFOEFSFSHP־׼SFOEFSFSUTמ㚺 ֻ׾ 
  21. 㲔꤀מSFOEFSFSUTֿⳂׂ׆כ؅澬鏀׌׾ ˝ kubectl get pods׻׽׌׬יס1PEֿ塜䅻מ颯Ⳃ׊יַ׾׆ כ؅澬鏀 ˝ minikube -n hatena-intern-2021

    service blog ؅㲔车׊יهٚؗا־׼ئؕ٤ؓشو؅车ֹ ˝ 鋗◄؅⛼䡗׊renderer-tsئ٭لتֿⳂׂ׆כ؅澬鏀 
  22. BC "QBDIF#FODI מ׻׾靷蕔طتع % kubectl exec deploy/blog-test !" ab -n

    1000 -c 10 http:!#blog:8080/ ˝ -n鴜⟓׌׾ٛؠؙتعס丗 ˝ -c⺱侇מٛؠؙتع׌׾丗 ˝ CMPHئ٭لتסطتع㲔车榫סؤ٤طػ־׼BC؅㲔车ך׀׾׻ ֹמ׊יֵ׾ 
  23. ⹆縒؛٭عتآ٭ٜס錃㲊؅׊יַיַםַ1PEסٝوٛ؜丗ֿס敯䛜 % ab -n 1000 -c 10 http:!"localhost:8080/ (snip) Server

    Software: Server Hostname: localhost Server Port: 8080 Document Path: / Document Length: 831 bytes Concurrency Level: 10 Time taken for tests: 97.873 seconds Complete requests: 1000 Failed requests: 42 (Connect: 0, Receive: 0, Length: 42, Exceptions: 0) Total transferred: 1099784 bytes HTML transferred: 796098 bytes Requests per second: 10.22 [#/sec] (mean) Time per request: 978.730 [ms] (mean) Time per request: 97.873 [ms] (mean, across all concurrent requests) Transfer rate: 10.97 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.1 0 1 Processing: 1 975 605.9 897 3696 Waiting: 0 910 579.2 798 3696 Total: 1 975 605.9 898 3696 Percentage of the requests served within a certain time (ms) 50% 898 66% 1099 75% 1299 80% 1399 90% 1798 95% 2098 98% 2498 99% 2798 100% 3696 (longest request) ˝ 靷蕔طتعמ־־זג侇ꝴע T ˝ ٛؠؙتعֵג׽ס䇖㏯侇ꝴע NT 
  24. ؛٭عتآ٭ٜס錃㲊 % kubectl autoscale deployment blog !"cpu-percent=50 !"min=1 !"max=10 ˝

    !"cpu-percent泘嘅כם׾1PE⪒⛮ס䇖㏯$16✳榫曍 ˝ !"min؛٭عتآ٭ٜ׌׾꤀ס1PE丗ס┖꡾ ˝ !"max؛٭عتآ٭ٜ׌׾꤀ס1PE丗ס┕꡾ 
  25. 澬鏀׌׾׆כ ˝ BCס磵冽־׼⛰ֿ؂־׾־ ˝ ٛؠؙتعֵג׽ס䇖㏯侇ꝴע ˝ ,VCFSOFUFT%BTICPBSE־׼⮆־׾׆כע ˝ CMPH1PEעַׂחױך㘃ֻג־ ˝

    kubectl get hpa blogך؛٭عتآ٭ٜס敯䛜؅澬鏀׊׻ֹ ˝ ┉㲊僿ꝴؓؠجت׎׍מ䷑糋׊יتآ٭ٜرؗ٤ס喋㯸׵釤יײ׾ 
  26. ױכ״ ˝ LVCFSOFUFTס哭鈝כ㓹儖氳ם嚀耆؅磆☭׊ג ˝ ؤ٤طػ؛٭آتعٝ٭ب٘٤׌׾ג״ס☼磝ײ؅⤓ֻגوٚشعنؚ٭ّ ˝ 㲚銧氳מؕ٤نٚ؅畘杼ך׀յ냕⺎榫ך䦡䍖䓪סֵ׾㓹泅؅啶疣ך׀׾ ˝ قهٛشؠؠٚؗغך׵䬠❠׈׿יַ׾ؤ٤طػ؛٭آتعٝ٭ب٘٤סظنؒؠعتذ٤ ر٭غ

    ˝ ـ٤ث؛٤؅鵟׊יLVCFSOFUFT؅⛮꽦׊ג ˝ LVCFSOFUFT梪㗞؅啶疣׊יئ٭لت؅Ⳃ־׌׆כע哧׊ַ ˝ .BOJGFTU־׼䣆鬭מتآ٭ٜ⺎耆ך䦡䍖䓪ס냕ַئ٭لت؅⛼׾׆כֿך׀׾ ˝ 䣆鬭׈ס遊מע邾ꥭםؕ٤نٚ׷ؾشع٠٭ؠֿ㰆㏇׊יַ׾ 