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

Kubernetes上でアプリケーションを 運用するまでの道のり/The way to run applications with Kubernetes

Kubernetes上でアプリケーションを 運用するまでの道のり/The way to run applications with Kubernetes

Shunya Murata

January 31, 2019
Tweet

More Decks by Shunya Murata

Other Decks in Programming

Transcript

  1. ▶ Kuberneteに向いたアプリケーションとは ▶ Pod(コンテナ)は⼀時的なオブジェクトとして扱えるようにすべき + Pod はいつでも終了して別のNodeへ移動できるように作るべき ▶ 特定のノードに依存しない +

    IP アドレスなどのノード固有の情報に依存しない ▶ アプリケーション例 + The Twelve Factor App: https://12factor.net/ja/ 5 https://cdn.chrisshort.net/The-Illustrated-Childrens-Guide-to-Kubernetes.pdf
  2. ▶ 宣⾔的なアプローチでデプロイしよう ▶ kubectl + 命令的なコマンド : kubectl run, kubectl

    create deployment + 命令的なオブジェクトの操作: kubectl create -f, kubectl replace -f + 宣⾔的なオブジェクトの操作: kubectl apply -f 9 https://kubernetes.io/docs/concepts/overview/object-management-kubectl/declarative-config/
  3. ▶ 宣⾔的なアプローチのメリット ▶ This declarative approach is critical to the

    system’s self-healing, autonomic capabilities, and application updates. + セルフヒーリングや⾃⽴制御、更新しやすい ▶ Version control facilitates reproducibility, reversibility, and an audit trail. + 再現性、可逆性、監査 ▶ Version control enables the use of familiar tools and processes for change control, review, and conflict resolution. + 変更管理、レビュー、コンフリクトの解消が容易 12
  4. ▶ YAMLでデプロイしよう! ▶ 構成がシンプルになる ▶ Kubernetes API の変更に強い ▶ 既存のツールとの連携が容易

    ▶ 抽象化されたDSLやGUIの使い⽅を覚えなくて良い + まだスタンダートとなるプロダクトがない + Kuberentes の標準オブジェクトの API はほぼ仕様が固まってきている + バージョン管理も明確に⾏われている ▶ YAMLにはKubernetesの考え⽅や仕組みが詰まっている + YAMLを覚えれば Kubernetes の理解が進む + 結局問題が起きると理解する必要が出てくる 13
  5. apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nginx

    name: nginx spec: replicas: 1 selector: matchLabels: app: nginx strategy: {} template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx name: nginx resources: {} status: {} ▶ kubectl create deployment nginx --image=nginx -- dry-run -o yaml ▶ kubectl explain ▶ マニフェストを作成する 14
  6. ▶ マニフェストの管理 ▶ 環境別の設定 + kustomize: https://github.com/kubernetes-sigs/kustomize + helm: https://helm.sh/

    + ksonnet: https://ksonnet.io/ ▶ SCM(バージョン管理システム)で管理 + GitOps: https://www.weave.works/blog/gitops-operations-by-pull-request + CI/CD パイプラインを作成して git の操作だけでオペレーションする 15
  7. ▶ kubectl apply -f ▶ 差分を検知して変更点を反映する + 差分の反映は単純な上書きではないので注意が必要 + applyで作成したオブジェクトはapply以外では変更しない

    ▶ 詳しくは 「Kubernetes: kubectl apply の動作」 @tkusumi + https://qiita.com/tkusumi/items/0bf5417c865ef716b221 16 https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands
  8. apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nginx

    name: nginx spec: replicas: 1 selector: matchLabels: app: nginx strategy: {} template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx name: nginx resources: {} status: {} ▶ 本番環境で動作させるには設定が⾜りていない 18
  9. ▶ Pod が要求するリソース量を設定する必要がある ▶ Pod.spec.containers[].resource.requests + コンテナが最低限利⽤するリソース量 + 指定しないとリソース量が0でも構わないという意味になる +

    スケジューラはこの値をもとにPodをスケジュールする ▶ Pod.spec.containers[].resource.limis + コンテナが最⼤利⽤できるリソース量 + 指定しないと無制限に利⽤しても構わないという意味になる 21 https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/
  10. ▶ アプリケーションのハングアップから⾃動復旧する ▶ アプリケーションは様々な要因でハングアップする + リソースリーク、デッドロック ▶ Pod.spec.containers[].livenessProbe + コンテナの状態を監視する

    + httpGet, tcpSocket, exec + 監視の結果ダウンしていると判断したらコンテナを再起動 22 https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/
  11. ▶ GracefulShutdown ▶ アプリケーション⾃体をグレースフルにシャットダウンできるようにする + SIGTERMシグナルまたはpreStopフックを受けてから短時間 (TerminationGracePeriodSeconds)に終了する必要がある ▶ Pod.spec.containers[]. lifecycle.preStop

    + Podの終了時に実⾏したいコマンドを実⾏することができる + SIGTERMでは終了処理を実現できない場合などに利⽤できる 24 https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/
  12. ▶ ReadinessProbe ▶ Pod.spec.containers[].ReadinessProbe + リクエストを受け付けられる(Readiness)状態であるかを監視する + httpGet, tcpSocket, exec

    + Readiness な状態と判断したら Service からリクエストが送られるようになる 25 https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/
  13. ▶ クラスタのメンテナンスに対応する ▶ Kuberentesクラスタのメンテナンス + 全体を⽌めないように少しずつ停⽌して更新していく + kubectl drain node

    + 指定のNodeを unschedulable にして Pod を退去(evict)する ▶ 特定の Node に Pod が偏っていると意図しないレプリカ数に減る可能性がある + PodDisruptionBudget + 指定した条件を満たすように削除を待つ + 例: 最低1Pod動いていることを条件にする 29 https://kubernetes.io/docs/tasks/run-application/configure-pdb/
  14. Node b ▶ PodDisruptionBudget設定済み 34 Node a Pod(Running) Kubernetes Pod(Terminating)

    PDBͷઃఆʹج͍ͮͯୀڈΛ଴ͭ Pod(Terminated) PDB minAvailable: 1
  15. ▶ まとめ ▶ Kubernetes でアプリケーションを運⽤するまでの道のり + マニフェストの管理 + 宣⾔的にシステムの状態をファイルで定義 +

    ファイルをバージョン管理 + 本番環境に必要な設定 + resource.request + ReadinessProbe/LivenessProbe + GracefulShutdown/preStop + PodDisruptionBudget 37