Slide 1

Slide 1 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. D E V D AY N O V 9 , 2 0 2 2

Slide 2

Slide 2 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon EKS上の 開発体験を最⼤化する プレビュー環境の作り⽅ ⼭﨑理弘(かたいなか) C - 2 SRE 株式会社リブセンス

Slide 3

Slide 3 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⾃⼰紹介

Slide 4

Slide 4 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⾃⼰紹介 • ⼭﨑理弘(かたいなか) • 株式会社リブセンス SRE • コンテナ関連の技術が好き • Twitter,GitHub: @katainaka0503

Slide 5

Slide 5 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. アジェンダ

Slide 6

Slide 6 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. アジェンダ • プレビュー環境とは • プレビュー環境の構成要素 • Amazon EKS上での ArgoCDとIstioを使った実装例 • プレビュー環境導⼊までのロードマップ • まとめ

Slide 7

Slide 7 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. プレビュー環境とは

Slide 8

Slide 8 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. マイクロサービス開発での悩み • 呼び出される側のマイクロサービスの動作確認が⼤変 § 開発環境にデプロイするまで 他マイクロサービスと結合して動作確認できない • 動作確認は後からになりPR Reviewの精度が低下 § ローカルで動かして動作確認することもできるが、 ⼤量のマイクロサービスをローカルで動かすのは⼤変 • バグ修正Pull Requestが頻発

Slide 9

Slide 9 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. プレビュー環境とは • Pull Request(以降PR)のブランチから作成されたコンテナイメージを 他のマイクロサービスと結合して動作確認できる環境 § 既存の環境を壊したり占有したりしない § PR作成などのタイミングで⾃動で環境が作成される § PRごとに専⽤の特別なURLから動作確認できる – 例: “pr-1.preview.example.com”

Slide 10

Slide 10 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. プレビュー環境の構成要素

Slide 11

Slide 11 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. プレビュー環境の構成要素 • アプリケーションをPRごとにデプロイ • クラスタの⼊り⼝でURLをヘッダに変換 • アプリケーションによるヘッダ伝播 • ルーティングの設定をPRごとにデプロイ

Slide 12

Slide 12 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 全体像

Slide 13

Slide 13 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. プレビュー環境の構成要素 • アプリケーションをPRごとにデプロイ • クラスタの⼊り⼝でURLをヘッダに変換 • アプリケーションによるヘッダ伝播 • ルーティングの設定をPRごとにデプロイ

Slide 14

Slide 14 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. アプリケーションをPRごとにデプロイ • PRからビルドしたイメージを、 開発環境と同様の設定でデプロイ • PRがマージされたら⾃動で削除

Slide 15

Slide 15 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. プレビュー環境の構成要素 • アプリケーションをPRごとにデプロイ • クラスタの⼊り⼝でURLをヘッダに変換 • アプリケーションによるヘッダ伝播 • ルーティングの設定をPRごとにデプロイ

Slide 16

Slide 16 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. クラスタの⼊り⼝でURLをヘッダに変換 • ヘッダ伝播で伝播させるため、 URLをヘッダに変換

Slide 17

Slide 17 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. プレビュー環境の構成要素 • アプリケーションをPRごとにデプロイ • クラスタの⼊り⼝でURLをヘッダに変換 • アプリケーションによるヘッダ伝播 • ルーティングの設定をPRごとにデプロイ

Slide 18

Slide 18 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. アプリケーションによるヘッダ伝播 • 上流から受け取った情報を 他のサービス呼び出し時に ヘッダで渡す • どのPRのプレビュー環境への アクセスかの情報を伝播

Slide 19

Slide 19 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. プレビュー環境の構成要素 • アプリケーションをPRごとにデプロイ • クラスタの⼊り⼝でURLをヘッダに変換 • アプリケーションによるヘッダ伝播 • ルーティングの設定をPRごとにデプロイ

Slide 20

Slide 20 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. ルーティングの設定をPRごとにデプロイ • ヘッダ伝播で使うヘッダを元に トラフィックを振り分ける設定を PRごとにデプロイ • PRがマージされたら削除 • プレビュー環境⽤の設定に マッチしなければ、 既存の環境に流す設定も適⽤

Slide 21

Slide 21 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. まとめると

Slide 22

Slide 22 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon EKS上での Argo CDとIstioを使った実装例

Slide 23

Slide 23 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 実装に使⽤する技術 • Argo CD • Istio • Open Telemetry

Slide 24

Slide 24 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Argo CDとは • KubernetesクラスタとGitHub等を同期するGitOpsを実現するツール § GitHub上のコードを変更したら変更がクラスタに同期 • Image Updaterによるイメージの更新も • Application SetsによるPRごとのデプロイ機能

Slide 25

Slide 25 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Istioとは • オープンソースのサービスメッシュの実装 § サービス間の通信をインターセプトして様々な便利機能を提供 • 可観測性の向上 § 分散トレーシング機能 • トラフィックの管理 § L7/L4情報によるルーティング • セキュリティの向上 § mTLSによる通信の暗号化 § ポリシーベースのアクセス制御

Slide 26

Slide 26 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Open Telemetryとは • 分散トレーシングのベンダに依存しない規格および実装 • 様々な⾔語の主要なライブラリをカバーしたSDKを公式で提供 • AWS X-RayやDatadog APM等に結果を送信できる • SDKがBaggageヘッダによるヘッダ伝播の仕組みを提供 § BaggageはW3Cによるコンテキスト伝播のための規格

Slide 27

Slide 27 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. プレビュー環境の構成要素(おさらい) • アプリケーションをPRごとにデプロイ • クラスタの⼊り⼝でURLをヘッダに変換 • アプリケーションによるヘッダ伝播 • ルーティングの設定をPRごとにデプロイ

Slide 28

Slide 28 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 全体像

Slide 29

Slide 29 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. プレビュー環境の構成要素 • アプリケーションをPRごとにデプロイ • クラスタの⼊り⼝でURLをヘッダに変換 • アプリケーションによるヘッダ伝播 • ルーティングの設定をPRごとにデプロイ

Slide 30

Slide 30 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. アプリケーションをPRごとにデプロイ • ArgoCDで右図を実現する

Slide 31

Slide 31 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. アプリケーションをPRごとにデプロイ • Pull Request Generatorで PRごとのデプロイを実現(①) • Image Updaterで ビルドされたイメージに 更新させる(②) apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: frontend-pr-env namespace: argocd spec: generators: - pullRequest: github: owner: katainaka0503 repo: grpc-pr-env-test-frontend template: metadata: name: '{{branch}}-frontend’ namespace: argocd finalizers: - resources-finalizer.argocd.argoproj.io annotations: argocd-image-updater.argoproj.io/image-list: app= argocd-image-updater.argoproj.io/app.update-strategy: latest argocd-image-updater.argoproj.io/app.allow-tags: regexp:{{branch}}-.* argocd-image-updater.argoproj.io/write-back-method: git spec: project: default source: repoURL: path: targetRevision: master kustomize: namePrefix: '{{branch}}-' ① ②

Slide 32

Slide 32 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. プレビュー環境の構成要素 • アプリケーションをPRごとにデプロイ • クラスタの⼊り⼝でURLをヘッダに変換 • アプリケーションによるヘッダ伝播 • ルーティングの設定をPRごとにデプロイ

Slide 33

Slide 33 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. クラスタの⼊り⼝でURLをヘッダに変換 • Nginx等のアプリケーションで URLをヘッダに変換 upstream frontend { server frontend.default.svc.cluster.local:80; } server { listen 80 http2; # 正規表現でbaggageヘッダに使う値を取得 server_name ~^(?.+)¥.preview¥.example¥.com$; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Real-IP $remote_addr; # baggageヘッダを設定 proxy_set_header baggage branch=${branch}; location / { proxy_pass http://frontend; } }

Slide 34

Slide 34 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. プレビュー環境の構成要素 • アプリケーションをPRごとにデプロイ • クラスタの⼊り⼝でURLをヘッダに変換 • アプリケーションによるヘッダ伝播 • ルーティングの設定をPRごとにデプロイ

Slide 35

Slide 35 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. アプリケーションによるヘッダ伝播 • OpenTelemetryにより baggageヘッダが伝播

Slide 36

Slide 36 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. アプリケーションによるヘッダ伝播 // Bagggageによる伝播を有効化 otel.SetTextMapPropagator(propagation.Baggage{}) // InterceptorでOpenTelemetryを仕込む s := grpc.NewServer(grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor()), grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor())) pb.RegiterGreeterServer(s, &server{}) サ ー バ 側 の 実 装 ( G R P C )

Slide 37

Slide 37 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. アプリケーションによるヘッダ伝播 // Baggageを有効化 otel.SetTextMapPropagator(propagation.Baggage{}) // InterceptorでOpenTelemetryを仕込む conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()), grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor())) ク ラ イ ア ン ト 側 の 実 装 ( G R P C )

Slide 38

Slide 38 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. プレビュー環境の構成要素 • アプリケーションをPRごとにデプロイ • クラスタの⼊り⼝でURLをヘッダに変換 • アプリケーションによるヘッダ伝播 • ルーティングの設定をPRごとにデプロイ

Slide 39

Slide 39 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. ルーティングの設定をPRごとにデプロイ • 右図をArgoCD + Istioで実現

Slide 40

Slide 40 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. ルーティングの設定をPRごとにデプロイ • Baggageヘッダを元に ルーティング • PRごとにデプロイした際に 優先順位で問題が⽣じない Gateway APIを使⽤ apiVersion: gateway.networking.k8s.io/v1alpha2 kind: HTTPRoute metadata: name: {{ .Release.Name }} spec: parentRefs: - kind: Mesh name: istio hostnames: ["{{ .Values.app }}.default.svc.cluster.local"] rules: - matches: - headers: - type: RegularExpression name: baggage value: ^(.*¥s*;)?branch¥s*=¥s*{{ .Values.branch }}(¥s*;.*)?$ backendRefs: - name: {{ .Values.branch }}-{{ .Values.app }} port: 80

Slide 41

Slide 41 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. ルーティングの設定をPRごとにデプロイ • Application Setで設定を複製 apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: backend-pr-env-routing namespace: argocd spec: generators: - pullRequest: github: owner: katainaka0503 repo: grpc-pr-env-test-backend template: metadata: name: '{{branch}}-backend-routing’ namespace: argocd finalizers: - resources-finalizer.argocd.argoproj.io spec: project: default source: repoURL: path: targetRevision: master helm: releaseName: backend-{{ branch }}-pr parameters: - name: branch value: '{{ branch }}’ - name: app value:

Slide 42

Slide 42 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. まとめると

Slide 43

Slide 43 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. デモ

Slide 44

Slide 44 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 実際の環境に適⽤するには § 実装上の制限 – Image Updaterが作成するファイルを定期的に消す § コスト⾯ – Preview環境のPodのCPU・メモリを最⼩限に – GitOpsの⼒を借りて必要な部分だけPRごとにデプロイ § セキュリティ⾯ – レビューが通っていない差分のSQLが実⾏されるのでDBの権限を絞る – プレビュー環境のURLで画像等が取得できるようCORSの設定変更

Slide 45

Slide 45 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. プレビュー環境導入までの ロードマップ

Slide 46

Slide 46 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 現実 • ここまで紹介したような仕組みをいますぐ⼊れようと頑張るのが、 すべての組織にとってベストというわけではない § 構成要素が多いため プレビュー環境のためだけに導⼊するのは⼤変 § 構成要素が新しいのでキャッチアップコストがかかる

Slide 47

Slide 47 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⼀⽅で • プレビュー環境の実装に使えるツールは プレビュー環境が関係ない⽂脈でも便利 § Argo CD § Istio § Open Telemetry

Slide 48

Slide 48 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 提案 • まずは運⽤負荷・導⼊コストが低いよりライトな仕組みを実現する • 分散トレーシングやサービスメッシュをサービスの成⻑に合わせて 順番に導⼊する § 技術選定の基準にPreview環境の実現可能性も加える • 仕組みが整い、開発⼈数も増え、ビジネスも成⻑したタイミングで プレビュー環境を導⼊する

Slide 49

Slide 49 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. よりライトな仕組みの例 • PRマージ時のステージングデプロイ • ブランチを指定してステージングにデプロイ • フロントエンドだけプレビュー環境を⽤意

Slide 50

Slide 50 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. PRマージ時のステージングデプロイ • ⼀般的なCI/CD • 開発初期ではこれで充分な場合も多そう • モノリスをごく少⼈数で開発しているケース等、 ローカルで動作確認できるならこれで⼗分な場合も

Slide 51

Slide 51 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. ブランチを指定してステージングにデプロイ • mainブランチではないブランチも指定して デプロイできるようにする • ステージングを⼀時的に占有して動作確認 • ステージングを占有するので開発⼈数が増えると待ちが発⽣する

Slide 52

Slide 52 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. フロントエンドにだけプレビュー環境を⽤意 • フロントエンドだけならヘッダ伝播のしくみは不要 § モノリスの場合も同様の理由で不要な場合あり • 重要なロジックが各マイクロサービスに移譲されるに従って カバーできるケースが減る • Amplify Hosting等既存サービスが使える場合も

Slide 53

Slide 53 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. ロードマップのスケッチ例 • サービスが軌道に乗るまで § 最低限のCI/CD • 運⽤改善に⼿を回せるようになったら § Open Telemetryで分散トレーシングを⼊れる § フロントだけのプレビュー環境を⽤意する • 開発⼈員を増やして更に開発本格化するときには § マイクロサービス化 or BFFの導⼊に合わせて ブランチデプロイを導⼊ • さらに開発⼈員やマイクロサービスの数が増えてきたら § Istioでプレビュー環境を⽤意

Slide 54

Slide 54 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. まとめ

Slide 55

Slide 55 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. まとめ • プレビュー環境の構成要素 § アプリケーションのPRごとのデプロイ § URLをヘッダに変換 § アプリケーションによるヘッダ伝播 § ルーティングの設定のPRごとにデプロイ • プレビュー環境を無理に急いで作ろうとせず、 サービスの成⻑に合わせて計画的に

Slide 56

Slide 56 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Thank you! © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.