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

Open Policy AgentとSpinnakerで実現するマイクロサービスの安全な継続的...

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for Keke Keke
June 13, 2020

Open Policy AgentとSpinnakerで実現するマイクロサービスの安全な継続的デリバリー

KubeFest Tokyo 2020 Track1 15:35~16:10

Avatar for Keke

Keke

June 13, 2020
Tweet

More Decks by Keke

Other Decks in Technology

Transcript

  1. 2 Keisuke Yamashita(@_k_e_k_e) Merpay SRE ・Microservices Platform CI/CD Team Like

    Kubernetes, Hashicorp Products(Terraform, Vault) Spinnaker , Open Policy Agent, CircleCI, GitHub Actions, Istio, Envoy, etc...
  2. 5 Agenda 01 • Mercari’s Microservices Architecture 02 03 •

    Mercari’s CI/CD • Problems with Microservices Architecture CI/CD 04 • Open Policy AgentとSpinnaker 05 • Wrap up
  3. 11 Nodes Mercari’s Microservice Architecture Microservice B Namespace B Microservice

    B Microservice B Microservice B Microservice C Namespace C Microservice C Microservice C Microservice A Namespace A Service A Team Service B Team Service C Team RBAC RBAC RBAC
  4. 12 Mercari’s Microservice Architecture Project C Project A Service A

    Team Service B Team Service C Team IAM IAM IAM Project B
  5. 15 Application CI Service A Team Service B Team Service

    C Team Service A Repository Service B Repository Service C Repository PR & Review PR & Review PR & Review Circle CI Cloud Build Container Registry Project A Cloud Build Container Registry Project B Cloud Build Container Registry Project C
  6. 16 Kubernetes Resource Repository PR & Review Service B Team

    Service A Team Service C Team PR & Review PR & Review Kubernetes Manifest CI
  7. 18 Mercari’s CD Container Registry GKE Cluster Pull Deploy CDプラットフォームであるSpinnakerを使って、Google

    Container RegistryにあるDocker ImageとGitHubにあ るKubernetes Manifestを参照し、GKEクラスタにデプロイをしている 
 
 
 Kubernetes Resource Repository Pull
  8. 19 Mercari’s CD Service A Team Service B Team Service

    C Team Service C Application Service A Application Service B Application Trigger Trigger Trigger Microservice A Namespace A Microservice B Namespace B Microservice C Namespace C Deploy Deploy Deploy
  9. 22 Problems with Microservices Architecture CI/CD Microservice A DB Team

    A マイクロサービスアーキテクチャは各マイクロサービスが 自律的に開発サイクルを回していく という理念に根 ざしている。
 
 
 +

  10. 23 Problems with Microservices Architecture CI/CD すべてのマイクロサービスが同じレベルで、全てのことを 同じようにできるわけではなく サービス全体の質の担保が難しくなる 。

    例1: Kubernetes Manifestの設定 ・Labelはマイクロサービスの名前と一致しているか ・Memory Limitが10GB以下に設定されているのか などのルールが守られていることを保証することは難しい。 
 
 

  11. 24 Platform Team Kubernetes Resource Repository PR & Review Service

    B Team Service A Team Service C Team PR & Review PR & Review Problems with Microservices Architecture CI/CD Pull Review ✅ ✅
  12. 25 Problems with Microservices Architecture CI/CD CPU limitsが 高い latestタグを

    使っている 記述的に間違っているわけではないので、このままでもApplyはできる 
 Namespaceが サービスの名前に 合っていない
  13. 26 Problems with Microservices Architecture CI/CD すべてのマイクロサービスが同じレベルで、全てのことを同じようにできるわけではなく サービス全体の質の担保が難しくなる 。 例1:

    Kubernetes Manifestの設定 ・Labelはマイクロサービスの名前と一致しているか ・Memory Limitが10GB以下に設定されているのか などのルールが守られていることを保証することは難しい。 例2: デプロイ ・手法(カナリア、Blue-Green、ハイランダーなど)差異 ・デプロイ先の設定(Namespace、クラスタなど)
 
 

  14. 27 Problems with Microservices Architecture CI/CD Item Item Listing DB

    Team C User DB Team E Item DB Team B Shipping DB Team D Timeline DB Team A ✅ カナリアデプロイ ✅ Blue-Greenデプロイ ✅ Blue-Greenデプロイ ❌ リリースフローに 沿わないデプロイ ❌ 雑なデプロイ
  15. 28 Problems with Microservices Architecture CI/CD Item Item Listing DB

    Team C User DB Team E Item DB Team B Shipping DB Team D Timeline DB Team A ✅ カナリアデプロイ ✅ Blue-Greenデプロイ ✅ Blue-Greenデプロイ ❌ 雑なデプロイ それぞれのマイクロサービスにデプロイ手法が委ねられていると ”デプロイの質” にも差が生じ、サービス全体の質へ影響する ❌ リリースフローに 沿わないデプロイ
  16. 33 Problem & Solution マイクロサービスが、ルール違反するリソースをApplyする可能性があり、サービス 全体の質が落ちてしまっている 解決策 • Developerが行うCI/CDの中で検証し、Applyを防ぐ仕組み •

    CircleCIやSpinnakerがApplyするリソースに限らず、クラスタの上の全ての リソースがルール違反していないことを保証する仕組み • ルールを定義して、運用をしている仕組み
  17. 34 Problem & Solution マイクロサービスが、ルール違反するリソースをApplyする可能性があり、サービス 全体の質が落ちてしまっている 解決策 • Developerが行うCI/CDの中で検証し、Applyを防ぐ仕組み •

    CircleCIやSpinnakerがApplyするリソースに限らず、クラスタの上の全ての リソースがルール違反していないことを保証する仕組み • ルールを定義して、運用をしている仕組み Open Policy Agent(OPA)の導入
  18. 37 Policy as Codeとは、組織のルール(Policy)をコードで管理をすること。 Policyをもとに、Kubernetes Manifestなどの入力データを検証する。 メリット • Policyを明文化できる •

    Policyをコードベースで運用・開発ができる • 誰が見ても理解できる・検証できる など Policy as Code OPA YAML 検証 Policy
  19. 38 Policies in CI/CD Service A Team Service B Team

    Service C Team Service A Application Trigger Deploy(kubectl) Deploy YAML YAML Deploy(kubectl)
  20. 39 Policies in CI/CD Service A Team Service B Team

    Service C Team Service A Application Trigger Deploy(kubectl) Deploy YAML YAML Deploy(kubectl) Conftest Gatekeeper
  21. 41 Kubernetes Resource Repository PR & Review Service B Team

    Service A Team Service C Team PR & Review PR & Review Conftest in CI Trigger Circle CI Policies(.Rego) Platform Team Maintain ✅ ✅ Conftest validation
  22. 42 Kubernetes Resource Repository PR & Review Service B Team

    Service A Team Service C Team PR & Review PR & Review Conftest in CI Trigger Circle CI Policies(.Rego) Platform Team Maintain ✗ Conftest validation
  23. 43 Kubernetes Resource Repository PR & Review Service B Team

    Service A Team Service C Team PR & Review PR & Review Conftest in CI Trigger Circle CI Policies(.Rego) Platform Team Maintain ✗ Conftest Warning ✅ Best Pracitise is … Please ...
  24. 44 Kubernetes Resource Repository PR & Review Service B Team

    Service A Team Service C Team PR & Review PR & Review Conftest in CI Trigger Circle CI Policies(.Rego) Platform Team Maintain ✅ ✅ Conftest validation ローカル環境でも検証できるので素早く フィードバックを得ることが出来る
  25. 45 Gatekeeper OPAをKubernetesのAdmission Controllerとして使えるようにしたもの。現在はv3である。 
 
 
 Developers kubectl apply

    Kubernetes API HTTP ハンドラ Authn / Authz Mutating admission スキーマ バリデー ション Validating admission etcdへ 格納 Gatekeeper 主な機能 ・kubectl apply入力と既存リソースを同時に参照しながら の検証(Sync機能) ・定期的な既存リソースの検証 (Audit機能)
  26. 49 Gatekeeper SpinnakerやCircleCIに限らず、すべてのApplyを検証できる 
 
 
 必要なラベルがある Manifest Kubernetes Client

    必要なラベルがない Manifest Gatekeeper Deploymentを生成 ✅ Cluster内部からの Policy違反するApply 必要なラベルがある Manifest ✅
  27. 50 Policies for Kubernetes Deployment Service A Team Service B

    Team Service C Team Service A Application Trigger Deploy(kubectl) Deploy YAML YAML Deploy(kubectl) Conftest Gatekeeper
  28. 51 Policies for Kubernetes Deployment Service A Team Service B

    Team Service C Team Service A Application Trigger Deploy(kubectl) Deploy YAML YAML Feedbacks (Area) Bad(CD) Deploy(kubectl) Gatekeeper
  29. 52 Policies for Kubernetes Deployment Service A Team Service B

    Team Service C Team Service A Application Trigger Deploy(kubectl) Deploy YAML YAML Feedbacks (Area) Good (CI) Deploy(kubectl) Conftest
  30. 53 Policies for Kubernetes Deployment Service A Team Service B

    Team Service C Team Service A Application Trigger Deploy(kubectl) Deploy YAML YAML Feedbacks (Area) Good (CI) Bad(CD) Deploy(kubectl) Conftest Gatekeeper
  31. 54 Conftest v.s. Gatekeeper GatekeeperとConftestはどちらもPolicyをもとに検証を行うツールであるものの、 立ち位置が大きく異なるため両者は排他的なものではない。 Conftest Gatekeeper 検証 Client-side検証

    Server-side検証 ツールの形態 CLIツール Kubernetes Admission Controller Policyの定義方法 Policy専用ファイル(.rego) Kubernetes CRD 検証のロケーション ローカル環境・CI CD後のKubernetesクラスタ Feedbackの速さ 早い 遅い 役割 ・Apply前にフィードバックを得るための素 早い検証 ・Best Practiceをキャッチするための検証 クラスタにApplyされるリソースに対して Policyを確実に課したいとき 表 ConftestとGatekeeperの比較
  32. 55 Conftest v.s. Gatekeeper GatekeeperとConftestはどちらもPolicyをもとに検証を行うツールであるものの、 立ち位置が大きく異なるため両者は排他的なものではない。 Conftest Gatekeeper 検証 Client-side検証

    Server-side検証 ツールの形態 CLIツール Kubernetes Admission Controller Policyの定義方法 Policy専用ファイル(.rego) Kubernetes CRD 検証のロケーション ローカル環境・CI CD後のKubernetesクラスタ Feedbackの速さ 早い 遅い 検証の役割 ・Apply前にフィードバックを得るための素 早い検証 ・Best Practiceをキャッチするための検証 クラスタにApplyされるリソースに対して Policyを確実に課したいとき 表 ConftestとGatekeeperの比較
  33. 56 Conftest v.s. Gatekeeper GatekeeperとConftestはどちらもPolicyをもとに検証を行うツールであるものの、 立ち位置が大きく異なるため両者は排他的なものではない。 Conftest Gatekeeper 検証 Client-side検証

    Server-side検証 ツールの形態 CLIツール Kubernetes Admission Controller Policyの定義方法 Policy専用ファイル(.rego) Kubernetes CRD 検証のロケーション ローカル環境・CI CD後のKubernetesクラスタ Feedbackの速さ 早い 遅い 検証の役割 ・Apply前にフィードバックを得るための素 早い検証 ・Best Practiceをキャッチするための検証 クラスタにApplyされるリソースに対して Policyを確実に課したいとき 表 ConftestとGatekeeperの比較
  34. 57 Overview of CI/CD Service Push Pull Conftest validation Kubernetes

    Repository Circle CI Spinnaker Deploy Gatekeeper Kubernetes Gatekeeper validation 組織的なルールをPolicyとして定義し、Gatekeeperと Conftestを使うことによって、Kubernetesへデプロイされる 全てのマイクロサービスへPolicyを課すことができるように なった。

  35. 59 CI/CD of Policies Trigger Kubernetes Repository Deploy CRD Policy自体のCI/CDも行っている。ConftestはUnit

    testする仕組みがあるためPolicyもテストできる。 ConftestのPolicy(Rego)とGatekeeperのContstraintTemplate(CRD)に互換性がないため、変換して CRD を生成し、デプロイ。 Security Team Platform Team SRE Team PR & Review PR & Review PR & Review Circle CI Policy(.Rego) Policy(.Rego) Policy(.Rego) Generate Constraints(CRD)
  36. 60 CI/CD of Policies Trigger Kubernetes Repository Deploy CRD Policy自体のCI/CDも行っている。ConftestはUnit

    testする仕組みがあるためPolicyもテストできる。 ConftestのPolicy(Rego)とGatekeeperのContstraintTemplate(CRD)に互換性がないため、変換して CRD を生成し、デプロイ。ルールがConftestとGatekeeperで統一されている。 Security Team Platform Team SRE Team PR & Review PR & Review PR & Review Circle CI Policy(.Rego) Policy(.Rego) Policy(.Rego) Generate Constraints(CRD)
  37. 62 Problem & Solution Spinnaker Pipelineは、DeveloperがGUI上で設定するので設定ミスをしやす く、デプロイ手法もサービスによって大きく質が異なる。 解決策 • DeveloperがPipelineを簡単な設定で作れる仕組み

    • Pipelineを作成すると、あとから変更することができないImmutableな仕組 み • Platform Teamが各マイクロサービスのデプロイ手法を管理し、デプロイ手 法の標準化・Best Practiceを提供しつづけられる仕組み
  38. 63 Problem & Solution Spinnaker Pipelineは、DeveloperがGUI上で設定するので設定ミスをしやす く、デプロイ手法もサービスによって大きく質が異なる。 解決策 • DeveloperがPipelineを簡単な設定で作れる仕組み

    • Pipelineを作成すると、あとから変更することができないImmutableな仕組 み • Platform Teamが各マイクロサービスのデプロイ手法を管理し、デプロイ手 法の標準化・Best Practiceを提供しつづけられる仕組み Spinnaker Managed Pipelineの導入
  39. 66 Spinnaker Managed Pipeline Managed Pipelineとは、雛形になるManaged Pipeline Template(MPT)に設定情報であるPipeline Configurationsを渡してできるPipelineのこと。すべてコードで定義される。 Managed

    Pipeline Template(JSON) JSON Pipeline Configurations(JSON) JSON Managed Pipeline = + ・変数を定義 ・どのMPTを使うか指定 ・変数を値を定義 Platform Team
  40. 67 Spinnaker Managed Pipeline 主要な機能 ・Sync機能: MPTが更新すると、そのMPTから生成された全てのPipelineも同時に変更できる。 想定ユースケース : Platform

    Teamが各マイクロサービスのデプロイ手法を更新したい ・Immutable機能: Managed Pipelineは直接、変更をすることができない。 ・Configuration機能: 必要な部分だけMPTの一部を変数化をして、Managed Pipelineを生成時に Pipeline Configurationsとして設定できる。 (変数はImmutableではなく、いつでも変更できる ) Managed Pipelineを使えば、Managed Pipeline Templateを管理するだけで横断的に 全マイクロサービスのPipelineを管理することができる。
  41. 68 Service A Team Service B Team Service C Team

    Configure Configure Configure Managed Pipeline Application A Application B Sync機能 Managed Pipeline Template Pipeline Configurations Pipeline Configurations Pipeline Configurations Spinnaker Application C Managed Pipeline Managed Pipeline
  42. 69 Service A Team Service B Team Service C Team

    Configure Configure Configure Managed Pipeline Application A Application B Sync機能 Managed Pipeline Template Pipeline Configurations Pipeline Configurations Pipeline Configurations Spinnaker Application C Managed Pipeline Managed Pipeline V2 Platform Team デプロイ先を変更
  43. 70 Service A Team Service B Team Service C Team

    Configure Configure Configure Managed Pipeline Application A Application B Sync機能 Managed Pipeline Template Pipeline Configurations Pipeline Configurations Pipeline Configurations Spinnaker Application C Managed Pipeline Managed Pipeline V2 V2 V2 V2 Platform Team デプロイ先を変更
  44. 71 Service A Team Service B Team Service C Team

    Configure Configure Configure Managed Pipeline Application A Application B Sync機能 Managed Pipeline Template Pipeline Configurations Pipeline Configurations Pipeline Configurations Spinnaker Application C Managed Pipeline Managed Pipeline
  45. 72 Service A Team Service B Team Service C Team

    Configure Configure Configure Managed Pipeline Application A Application B Sync機能 Managed Pipeline Template Pipeline Configurations Pipeline Configurations Pipeline Configurations Spinnaker Application C Managed Pipeline Managed Pipeline Platform Team ベストプラクティスを追加 ✅
  46. 73 Service A Team Service B Team Service C Team

    Configure Configure Configure Managed Pipeline Application A Application B Sync機能 Managed Pipeline Template Pipeline Configurations Pipeline Configurations Pipeline Configurations Spinnaker Application C Managed Pipeline Managed Pipeline Platform Team ベストプラクティスを追加 ✅ ✅ ✅ ✅
  47. 75 Service Team Configure Application Configuration機能 Pipeline Configurations Spinnaker Managed

    Pipeline containerPort: 8000 Managed Pipeline Template { “variables”: { “containerPort”: 8000 } } 各マイクロサービスに依存する設定値だけは Pipeline生成時に設定をすることができる。
  48. 77 Spinnaker Managed Pipeline 主要な機能 ・Sync機能 Platform TeamはMPTを管理するだけど全マイクロサービスのデプロイを管理することできる ・Immutable機能 PipelineをDeveloperに変更されて、壊されるリスクもない

    ・Configuration機能 必要な部分だけDeveloperに設定してもらえる マイクロサービスに必要な最小限の設定だけを公開して、あとから変更されることもなく、全マイクロサービス のデプロイの標準化・管理することができる
  49. 78 Platform TeamがMPTを保守・開発することによって、全マイクロサービスの Pipelineを管理することがで き、サービス全体でデプロイの質を担保しつづけることができる。 例: Best PracticeをMPTに実装することによって、全マイクロサービスの PipelineにそのBest Practiceが自動的に実装される

    クラスタMigrationやマイクロサービスのPipelineの変更を要するときにも、ひとつの MPTを変更するだけで 済み、Developerは何もする必要がない。 
 Pipeline as Code Trigger Kubernetes Repository Deploy MPT Platform Team SRE Team PR & Review PR & Review Circle CI Managed Pipeline Template