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

前任者が残したコンテナサービスを突然任された!はじめてのコンテナ運用~Amazon ECS の運用ベストプラクティス~

前任者が残したコンテナサービスを突然任された!はじめてのコンテナ運用~Amazon ECS の運用ベストプラクティス~

More Decks by kashinoki38 - Yasuhiro Horiuchi

Other Decks in Technology

Transcript

  1. © 2022, Amazon Web Services, Inc. or its affiliates. ©

    2022, Amazon Web Services, Inc. or its affiliates. 前任者が残したコンテナサービスを突然任された! はじめてのコンテナ運用 ~Amazon ECS の運用ベストプラクティス~ 堀内 保大 アマゾン ウェブ サービス ジャパン 合同会社 Solutions Architect Digital Native Business
  2. © 2022, Amazon Web Services, Inc. or its affiliates. 自己紹介

    2 堀内 保大 (Yasuhiro Horiuchi) • AWS Japan G.K. ソリューションアーキテクト • 主に Digital 関連企業様を担当 バックグラウンド • 前職:SIerで多岐にわたるシステムへの性能関連の技術支援 (性能試験、監視、チューニング、非機能設計 etc) 好きなAWSサービス コンテナ関連、特に • Amazon Elastic Kubernetes Service (EKS) • AWS Fargate • Amazon Managed Service for Prometheus (AMP) 趣味 • スノーボード、旅行 @ka_shino_ki [email protected]
  3. © 2022, Amazon Web Services, Inc. or its affiliates. 本日話すこと

    • 話すこと ▪ Amazon ECS の運用で対応すべき課題 ▪ ベストプラクティスによる ECS 運用改善 • 話さないこと ▪ AWS のコンテナサービスのそれぞれの説明 ▪ ECS の仕組みや細かいコンポーネントの説明 – こちらを見てください • AWS Black Belt コンテナシリーズのあるきかた 2021年まとめ | Amazon Web Services ブログ 3
  4. © 2022, Amazon Web Services, Inc. or its affiliates. ©

    2022, Amazon Web Services, Inc. or its affiliates. 前任者が残したコンテナ サービスを突然任された! 4
  5. © 2022, Amazon Web Services, Inc. or its affiliates. 想像してください、、

    • Amazon ECS on Fargate を中心としたWebサービス • 前任の管理者が離職、あなたが引継ぎ先に • とにかく動けば良い、と作られたサービスで不安 ▪ セキュリティ上のリスクがどこにあるのか不明 ▪ 監視やログもどこをそもそも出ているのか不明 • 引き継いだからには上手く運用したい 5 ECS service ECS cluster
  6. © 2022, Amazon Web Services, Inc. or its affiliates. 想像してください、、

    • Amazon ECSを中心としたWebサービス • 前任の管理者が離職、あなたが引継ぎ先に • とにかく動けば良い、と作られたサービスで不安 ▪ セキュリティ上のリスクがどこにあるのか不明 ▪ 監視やログもどこをそもそも出ているのか不明 • 引き継いだからには上手く運用したい • 以下、観点で改善していきます ▪ セキュリティ強化 ▪ Observabilityの確保 6 ECS service ECS cluster
  7. © 2022, Amazon Web Services, Inc. or its affiliates. Agenda

    • 前任者が残したコンテナサービスを突然任された! ▪ 何はともあれセキュリティ ▪ 観測せよ、Observability • まとめ 7
  8. © 2022, Amazon Web Services, Inc. or its affiliates. ©

    2022, Amazon Web Services, Inc. or its affiliates. 何はともあれ、セキュリティ 8
  9. © 2022, Amazon Web Services, Inc. or its affiliates. ここから確認していきます

    • 観点 ▪ ネットワーク → 外部公開する箇所を局所的にする ▪ 機密情報の管理 → ハードコードしない ▪ IAM → 最小権限を割り当てる ▪ コンテナイメージ → イメージ内の脆弱性検知 ▪ ログ・モニタリング → AWS APIのログ 9
  10. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    1: セキュリティグループ ECS service 11
  11. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    1: セキュリティグループ ECS service sg-ALB sg-ECSTask sg-AuroraDB Allowed type and port HTTPS: 443 HTTP: 80 MySQL/Aurora: 3306 Allowed source 0.0.0.0/0 (Public access) 10.0.0.0/16 (From within the VPC) 10.0.0.0/16 (From within the VPC) 12
  12. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    1: セキュリティグループ ECS service sg-ALB sg-ECSTask sg-AuroraDB Allowed type and port HTTPS: 443 HTTP: 80 MySQL/Aurora: 3306 Allowed source 0.0.0.0/0 (Public access) sg-ALB (Only from the ALB) sg-ECSTask (Only from the ECS tasks) 13
  13. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    2: 機密情報の管理 ECS service { ... snip ... "containerDefinitions":[{ "name":"my-web-app", "image":"my-web-app:v1", ... snip ... "environment": [{ "name": "DB_USER", "value": "myappdbuser" },{ "name": "DB_PASSWD", "value": "mysupersecretpasswd" },{ "name": "DB_HOST", "value": "my-db...rds.amazonaws.com" },{ "name": "DB_PORT", "value": "3306" }], ... snip ... }], ... snip ... } ECS task definition 14
  14. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    2: 機密情報の管理 ECS service { ... snip ... "containerDefinitions":[{ "name":"my-app", "image":"my-web-app:v1", ... snip ... "secrets": [{ "name": "DB_USER", "valueFrom": ”...secretsmanager:...:my-db-secret:username::" },{ "name": "DB_PASSWD", "valueFrom": ”...secretsmanager:...:my-db-secret:password::" }], "environment": [{ "name": "DB_HOST", "value": ”my-db...rds.amazonaws.com" },{ "name": "DB_PORT", "value": ”3306" }] ... snip ... } ECS task definition AWS Secrets Manager Secrets injected by ECS at task runtime 15
  15. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    3: ECS タスクロール ECS service { ... snip ... "taskRoleArn": “AdministratorAccessRole", "executionRoleArn": "", "containerDefinitions":[{ ... snip ... }], ... snip ... } ECS task definition 18
  16. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    3: ECS タスクロール ECS service { ... snip ... "taskRoleArn": “AdministratorAccessRole", "executionRoleArn": "", "containerDefinitions":[{ ... snip ... }], ... snip ... } ECS task definition 19 許可されてしまう (意図通り?)
  17. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    3: ECS タスクロール • できるだけ最小権限にする ▪ サービスとアクションの限定 ▪ 対象リソースの限定 { ... snip ... "taskRoleArn": ”TaskRole-ARN-here", "executionRoleArn": "", "containerDefinitions":[{ ... snip ... }], ... snip ... } ECS task definition { "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Action": ["s3:ListBucket"], "Resource": ["arn:aws:s3:::your-bucket"] },{ "Effect": "Allow", "Action": [ "s3:PutObject", "s3:GetObject", "s3:DeleteObject" ], "Resource": ["arn:aws:s3:::your-bucket/*"] }] } S3 write-only policy { "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Action": "sqs:SendMessage", "Resource": "arn:aws:sqs:...:your-queue-name" }] } SQS write-only policy { "AttachedPolicies": [{ "PolicyName": "S3WriteOnlyPolicy", "PolicyArn": "S3WriteOnlyPolicy-ARN-here" },{ "PolicyName": "SQSWriteOnlyPolicy", "PolicyArn": "SQSWriteOnlyPolicy-ARN-here" }] } ECS task role 20
  18. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    3: ECS タスクロール 🚫 拒否 IAM role 21 ECS service
  19. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    3: “タスク実行ロール”? タスク実行ロール EC2 instance Amazon ECS container agent Container runtime ECS task Amazon ECR registry Logs Amazon CloudWatch Logs AWS Secrets Manager EC2 instance Amazon ECS container agent Container runtime ECS task タスクロール 22
  20. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    4: ECSクラスターへのアクセス制御 • Allow Access to specific Resources Amazon ECS ECS users API calls { "Version": "2012-10-17", "Statement": [{ "Sid": ”ReadOnlyAccess", "Effect": "Allow", "Action": [ "ecs:DescribeCluster” ], "Resource": "arn:aws:ecs:region:account-id:cluster/our-cluster" }] } IAM policy
  21. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    4: ECSクラスターへのアクセス制御 • Using “Condition” Amazon ECS ECS users API calls { "Version": "2012-10-17", "Statement": [{ "Sid": ”ReadOnlyClusterResources", "Effect": "Allow", "Action": [ "ecs:Describe*", "ecs:List*" ], "Condition": { "ArnEquals": { "ecs:cluster": "arn:aws:ecs:region:account-id:cluster/our-cluster" } }, "Resource": "*" }] } IAM policy
  22. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    5: ECR の機能活用 1.タグのイミュータビリティ • 通常イメージタグはプッシュで上書き可能 • タグのイミュータビリティを有効して、意図したコンテナイ メージを利用できるようにする 2. イメージスキャン • 基本スキャン: • プッシュする度にイメージスキャン(OS パッケージの脆弱性のみ) • 拡張スキャン: • OS とプログラミングライブラリ両方の脆弱性を検出 • push 時のスキャン / 継続的なスキャン 25
  23. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    6: AWS API のログ • AWS CloudTrail は AWS API 呼び出しを記録 • レコード ▪ eventTime ▪ userIdentity ▪ eventSource (represents an AWS service) ▪ eventName (represents an API action name) ▪ and more! • AWS CloudTrail の活用 参照: https://docs.aws.amazon.com/awscloudtrail/latest/userguide/ cloudtrail-event-reference-record-contents.html 26
  24. © 2022, Amazon Web Services, Inc. or its affiliates. Step

    6: AWS API のログ AWS CloudTrail S3 bucket Amazon Athena Ad hoc auditing Regular analysis and auditing 27
  25. © 2022, Amazon Web Services, Inc. or its affiliates. 更なるセキュリティ強化のために

    • 観点 ▪ ネットワーク → 外部公開する箇所を局所的にする ▪ 機密情報の管理 → ハードコードしない ▪ IAM → 最小権限を割り当てる ▪ コンテナイメージ → イメージ内の脆弱性検知 ▪ ログ・モニタリング → AWS APIのログ 28
  26. © 2022, Amazon Web Services, Inc. or its affiliates. 更なるセキュリティ強化のために

    • 観点 ▪ ネットワーク → 外部公開する箇所を局所的にする – Private Subnetの検討 ▪ 機密情報の管理 → ハードコードしない ▪ IAM → 最小権限を割り当てる – 継続的な棚卸と最小権限化 ▪ コンテナイメージ → イメージ内の脆弱性検知 – セキュアなコンテナイメージ開発、、Dockerfileの実装 ▪ ログ・モニタリング → AWS APIのログ – アプリケーションのログ(次のセクション)、設定のモニタリング(AWS Config) 29 参考: Best practices for writing Dockerfiles | Docker Documentation ベストプラクティス-セキュリティ - Amazon Elastic Container Service
  27. © 2022, Amazon Web Services, Inc. or its affiliates. ©

    2022, Amazon Web Services, Inc. or its affiliates. 観測せよ、Observability 30
  28. © 2022, Amazon Web Services, Inc. or its affiliates. 想像してください、、

    ECS service ECS cluster Resp. 5.0 sec. Req. POST /something 31
  29. © 2022, Amazon Web Services, Inc. or its affiliates. あのとき、どこで、何が起きていた?

    ECS service ECS cluster Resp. 5.0 sec. Req. POST /something ① ② ③ ④ 32
  30. © 2022, Amazon Web Services, Inc. or its affiliates. システムを観測可能にする「3

    つの柱」 • ある時点のなんらかのシステム状態を表現する 数値情報 • 一定間隔ごとの時系列データとして記録される • 1 つ以上のディメンション/ラベルをメタデータとして持つ • 例) CPU 使用率、リクエストレート、ストレージ残容量、など • システム内で発生した イベント情報 • 各イベントが独立したレコードとして記録される • メトリクスよりも多くの情報を含むケースが多い • 例) アクセスログ、 エラー情報、など • 1 つのトランザクションを複数システムで構成する フロー情報 • トランザクションごとにユニークな識別子をもって記録される • システム間のやりとりに関するメタ情報付与も • 例) とある HTTP リクエストの受け取りからレスポンスまで
  31. © 2022, Amazon Web Services, Inc. or its affiliates. システムの内部を観測する

    - メトリクス ECS service ECS cluster Resp. 5.0 sec. Req. POST /something タスクA CPU Util. 25 % Mem Util. 40 % ... 34 TargetResponseTime 4.8秒 サービスでの平均 CPU Util. 50 % Mem Util. 40 % ... タスクB CPU Util. 99 % Mem Util. 40 % ... タスクC CPU Util. 25 % Mem Util. 40 % ...
  32. © 2022, Amazon Web Services, Inc. or its affiliates. システムの内部を観測する

    - メトリクス ECS service ECS cluster Resp. 5.0 sec. Req. POST /something タスクA CPU Util. 25 % Mem Util. 40 % ... 35 TargetResponseTime 4.8秒 サービスでの平均 CPU Util. 50 % Mem Util. 40 % ... タスクB CPU Util. 99 % Mem Util. 40 % ... タスクC CPU Util. 25 % Mem Util. 40 % ...
  33. © 2022, Amazon Web Services, Inc. or its affiliates. Amazon

    CloudWatch Container Insights • コンテナアプリケーションの メトリクス 収集、集計 • タスクレベル でのメトリクスの収集が可能 • CPU やメモリ、ディスク、ネットワークなど、多数のリソースのメトリクス • 自動的に作成されるダッシュボードにメトリクスは集約 36 ECS サービス タスク A タスク B タスク C CPU使用率: 25% CPU使用率: 99% CPU使用率: 25%
  34. © 2022, Amazon Web Services, Inc. or its affiliates. システムの内部を観測する

    - ログ ECS service ECS cluster Resp. 5.0 sec. Req. POST /something . . [11/05:09:00:13] target: Aurora: xxx, Table name: yyy [11/05:09:00:14] ”POST /something" 200 4750ms . . . 37
  35. © 2022, Amazon Web Services, Inc. or its affiliates. システムの内部を観測する

    - ログ ECS service ECS cluster Resp. 5.0 sec. Req. POST /something . . [11/05:09:00:13] target: Aurora: xxx, Table name: yyy [11/05:09:00:14] ”POST /something" 200 4750ms . . . 38
  36. © 2022, Amazon Web Services, Inc. or its affiliates. awslogs

    ログドライバー • コンテナ化されたアプリケーションログを CloudWatch Logs に送信 ➢ STDOUT および STDERR をログイベントとして取得 • タスク定義に logConfiguration パラメータを追加するだけで有効化が可能 ➢ 追加のエージェントなどのインストールは不要 ➢ ECS on EC2, ECS on Fargate のいずれでも利用可能 コンテナ Amazon CloudWatch Logs Container Runtime STDOUT / STDERR ログイベント送信 39
  37. © 2022, Amazon Web Services, Inc. or its affiliates. Amazon

    ECS 対応 FireLens Fluentd / Fluent Bit を利用した柔軟なログルーティング Fluentd/Fluent Bit コンテナ アプリケーション コンテナ コンテナランタイム etc. タスクロール ECS タスク AWS CloudWatch Logs S3 外部サービス データプレーン ENI etc. Datadog Splunk https://docs.aws.amazon.com/AmazonECS/latest/developerguide/using_firelens.html 40
  38. © 2022, Amazon Web Services, Inc. or its affiliates. システムの内部を観測する

    - トレース ECS service ECS cluster Resp. 5.0 sec. Req. POST /something ALB 4,800 ms アプリケーション on ECS 4,750 ms time SQL 100 ms 41
  39. © 2022, Amazon Web Services, Inc. or its affiliates. AWS

    X-Ray 分散アプリケーションの分析と調査のための分散トレーシングサービス 総処理時間 最も時間を要した処理 呼び出し順 42
  40. © 2022, Amazon Web Services, Inc. or its affiliates. AWS

    X-Ray の概要 トレースデータのサービスマップやセグメントの処理時間などを分析可能 43
  41. © 2022, Amazon Web Services, Inc. or its affiliates. ECS

    での AWS X-Ray セットアップ 1. X-Ray SDK でアプリケーションをインストルメント (計装) する 2. X-Ray daemon をサイドカーコンテナとしてタスク定義に追加 3. タスク IAM ロールに必要な権限を追加 ECS タスク アプリケーション タスク IAM ロール X-Ray daemon X-Ray API X-Ray SDK 44
  42. © 2022, Amazon Web Services, Inc. or its affiliates. ECS

    における Observability 3本柱 まとめ ECS cluster Resp. 5.0 sec. Req. POST /something End to end のHTTPレスポンス アプリケーション on ECS time SQL to Aurora ECS service . [11/05:09:00:13] target: Aurora: xxx, Table name: yyy [11/05:09:00:14] ”POST /something" 200 2950ms . . CPU Util. 30 % Mem Util. 40 % ... CloudWatch Logs 45
  43. © 2022, Amazon Web Services, Inc. or its affiliates. ©

    2022, Amazon Web Services, Inc. or its affiliates. まとめ 46
  44. © 2022, Amazon Web Services, Inc. or its affiliates. まとめ

    • コンテナサービスを任されたら、まずは! ▪ 何はともあれセキュリティ – コンテナのライフサイクル各ポイントで強化 • ネットワーク、機密情報の管理、コンテナイメージ、ログ・モニタリング ▪ 把握せよ、Observability – 3本柱 • メトリクス(CloudWatch Container Insights)、ログ(awslogs, Firelens)、トレース(AWS X-Ray) 47
  45. © 2022, Amazon Web Services, Inc. or its affiliates. ECS

    service ECS cluster 48 #これがこう
  46. © 2022, Amazon Web Services, Inc. or its affiliates. #これがこう

    ECS service ECS cluster CloudWatch Logs タスク ロール セキュリティグループ AWS Secrets Manager ECR セキュリティグループ イメージスキャン イミュータブルタグ AWS CloudTrail 49
  47. © 2022, Amazon Web Services, Inc. or its affiliates. 継続的改善

    ぜひ、ご自身のECSを振り返ってみましょう! Amazon ECS のご利用で、 発生したお悩みや課題や要望はぜひお近くの Solutions Architect まで!! 50