$30 off During Our Annual Pro Sale. View Details »

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

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

Hatena

May 31, 2022
Tweet

More Decks by Hatena

Other Decks in Technology

Transcript

  1. ,VCFSOFUFTהכ ˖ ؝ٝذشؔ٦؛أزٖ٦ءّٝך׋׭ךاؿزؐؑ، ˖ ؝ٝذشךرفٗ؎ծؔ٦زأ؛٦ؚٔٝ ˖ 暴䗙 ˖ 㹑鎉涸ז圓䧭盖椚 ˖

    إٕؿؼ٦ؚٔٝ ˖ ؟٦ؽأر؍أؕغٔ٦頾蚚ⴓ侔 ˖ 荈⹛⻉ׁ׸׋ٗ٦ٕ،ؐزٗ٦ٕغحؙ 
  2. 4FSWJDF ˖ 1PEח㼎׃גزٓؿ؍حؙ׾ٕ٦ذ؍ؚׅٝ׷ ➬穈׫ ˖ ؟٦ؽأر؍أؕغٔ ˖ ؙٓأةⰻך%/4״׶؟٦ؽأ׾䱱ֿׅה ָדֹ׷ ˖

    BDDPVOU؟٦ؽأכaccount.hatena- intern-2020.svc.cluster.localהְֲ 䕎䒭ד%/4"ٖ؝٦سחⶴ׶䔲ג׵׸׷ 
  3. ٔا٦أⵖꣲ ˖ 1PEח㼎׃גⶴ׶䔲ג׷$16װًٌٔךⵖꣲ׾遤ֲֿהָדֹ ׷ ˖ $16؝، W$16 ׾N NJMMJDPSFT ה׃ג䭷㹀

    ˖ 銲実הⵖꣲ ˖ ♴ꣲה♳ꣲ׾䭷㹀דֹ׷ ˖ ظ٦سח1PE׾ꂁ縧ׅ׷ꥷח罋䣁ׁ׸׷ 
  4. وصؿؑأز ˖ ٔا٦أ㹀纏ָ剅ַ׸׋:".- ˖ kubectl apply -f <manifest>ח״׏גLTؙٓأةח 黝欽 ˖

    ֿ׸ח״׶㹑鎉涸ז؝٦سח״׷盖椚ָ〳腉הז׷ ˖ *OGSBTUSVDUVSFBT$PEF 
  5. وصؿؑأزך圓䧭 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ך鏣㹀ؿ؋ ؎ٕ 
  6. 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 
  7. 饯⹛ % make up skaffold dev !"cleanup=false ˖ http:!"localhost:8080/ח،ؙإأ׃ג׫ת׃׳ֲ ˖

    services/blog/templates/index.html׾剅ֹ䳔ִגծ 㢌刿ָ⿾僥ׁ׸׷ֿה׾然钠׃ת׃׳ֲ 橆㞮ך⵴ꤐ % skaffold delete 
  8. LVCFDUM׾⢪ֲ彊⪒ CBTI[TIדך؝وٝس酡㸣ך鏣㹀 # bash source <(kubectl completion bash) # zsh

    source <(kubectl completion zsh) ֶׅׅ׭ ؒ؎ٔ،أך鏣㹀׾׃גֶֻ alias k=kubectl 
  9. 1PEָ饯⹛ׅ׷圫㶨׾鋅״ֲ ♧䏝橆㞮׾⵴ꤐ׃גծ饯⹛׃湫׃ת׃׳ֲ % skaffold delete % make up ⴽך畭劣ד㹋遤׃ג1PEך朐䡾׾鋅׷ %

    kubectl get pods -w % kubectl describe pods 1PEָ饯⹛׃׋ֿה׾然钠׃גمأز⩎ַ׵http:!"localhost:8080/ ח،ؙإأծـؚٗ׾⡲䧭׃ג׫ת׃׳ֲ 
  10. رغحؚ # 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 
  11. NFUSJDTTFSWFSך㼪Ⰵ ˖ IUUQTHJUIVCDPNLVCFSOFUFTTJHTNFUSJDTTFSWFS # docker desktopͷ৔߹ % wget -O k8s/system/metrics-server.yaml

    \ https:!"github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.7/components.yaml # metrics-serverͷҾ਺ʹ!#kubelet-insecure-tlsΛ༩͑ͯىಈ͢Δ % perl -i -pe 's/^(\s*)- !#secure-port=4443$/$&\n$1- \ !#kubelet-insecure-tls/' k8s/system/metrics-server.yaml % kubectl apply -f k8s/system/metrics-server.yaml # minikubeͷ৔߹ % minikube addons enable metrics-server 
  12. 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 
  13. ,VCFSOFUFT%BTICPBSEך㼪Ⰵ ˖ IUUQTHJUIVCDPNLVCFSOFUFTEBTICPBSE # docker desktopͷ৔߹ % kubectl apply -f

    https:!"raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml # αΠϯΠϯ͢ΔͨΊͷτʔΫϯ % kubectl apply -f k8s/system/sa.yaml % kubectl -n kubernetes-dashboard describe secret \ $(kubectl -n kubernetes-dashboard get secret | grep hatena-intern-2020-admin-user | awk '{print $1}') % kubectl proxy # http:!"localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/ ʹΞΫηε # minikubeͷ৔߹ % minikube dashboard 
  14. وصؿؑأزך鷄⸇ % 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ח縧ֹ䳔ִ 
  15. وصؿؑأزך箟꧊ TLBPMEZBNM apiVersion: skaffold/v2beta5 kind: Config metadata: name: hatena-intern-2020 build:

    artifacts: # (snip) - image: hatena-intern-2020-renderer-go context: services/renderer-go - image: hatena-intern-2020-renderer-ts # ! context: services/renderer-ts # ! local: # (snip) ˖ services/renderer-tsךEPDLFS؎ً٦آךؽٕسהLTؙٓأةפך⿾僥ָׁ׸׷״ֲחׅ׷ 
  16. وصؿؑأزך箟꧊ LTCMPHLVTUPNJ[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_2020_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 ח㢌ִ׷ 
  17. BC "QBDIF#FODI ח״׷頾蚚ذأز % kubectl exec deploy/blog-test !" ab -n

    1000 -c 10 http:!#blog:8080/ ˖ -n鷏⥋ׅ׷ؙٔؒأزך侧 ˖ -cず儗חؙٔؒأزׅ׷侧 ˖ CMPH؟٦ؽأךذأز㹋遤欽ך؝ٝذشַ׵BC׾㹋遤דֹ׷״ ֲח׃ג֮׷ 
  18. ⿫罋ؔ٦زأ؛٦ٕך鏣㹀׾׃גְגְזְ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 
  19. ؔ٦زأ؛٦ٕך鏣㹀 % kubectl autoscale deployment blog !"cpu-percent=50 !"min=1 !"max=10 ˖

    !"cpu-percent湡垥הז׷1PEⰋ⡤ך䎂㖱$16⢪欽桦 ˖ !"minؔ٦زأ؛٦ٕׅ׷ꥷך1PE侧ך♴ꣲ ˖ !"maxؔ٦زأ؛٦ٕׅ׷ꥷך1PE侧ך♳ꣲ 
  20. 然钠ׅ׷ֿה ˖ BCך穠卓ַ׵⡦ָ׻ַ׷ַ ˖ ؙٔؒأز֮׋׶ך䎂㖱儗꟦כ ˖ ,VCFSOFUFT%BTICPBSEַ׵ⴓַ׷ֿהכ ˖ CMPH1PEכְֻאתד㟓ִ׋ַ ˖

    kubectl get hpa blogדؔ٦زأ؛٦ٕך朐䡾׾然钠׃״ֲ ˖ ♧㹀劍꟦،ؙإأׇ׆ח佝縧׃גأ؛٦ٕتؐٝך圫㶨׮鋅ג׫׷