

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Amazon EKS Pod Identity と KEDA を使用して Amazon EKS でイベント駆動型自動スケーリングをセットアップする
<a name="event-driven-auto-scaling-with-eks-pod-identity-and-keda"></a>

*Amazon Web Services、Dipen Desai、Abhay Diwan、Kamal Joshi、Mahendra Revanasiddappa*

## 概要
<a name="event-driven-auto-scaling-with-eks-pod-identity-and-keda-summary"></a>

[Amazon Elastic Kubernetes Service (Amazon EKS)](https://docs.aws.amazon.com/eks/latest/userguide/what-is-eks.html) などのオーケストレーションプラットフォームは、コンテナベースのアプリケーションのライフサイクル管理を合理化してきました。これにより、組織はコンテナベースのアプリケーションの構築、保護、運用、保守に集中できます。イベント駆動型デプロイがより一般的になるにつれて、組織はさまざまなイベントソースに基づいて Kubernetes デプロイをより頻繁にスケーリングするようになっています。この方法と自動スケーリングを一緒に使用し、アプリケーションロジックに合わせたオンデマンドコンピューティングリソースと効率的なスケーリングを提供することで、大幅なコスト削減につながります。

[KEDA](https://keda.sh/) は、Kubernetes ベースのイベント駆動型オートスケーラーです。KEDA は、処理する必要があるイベントの数に基づいて Kubernetes のコンテナをスケールするのに役立ちます。軽量で、任意の Kubernetes クラスターと統合できます。また、[Horizontal Pod Autoscaling (HPA)](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/) などの標準 Kubernetes コンポーネントでも機能します。KEDA には、認証を委任するのに役立つ機能である [TriggerAuthentication](https://keda.sh/docs/2.14/concepts/authentication/#re-use-credentials-and-delegate-auth-with-triggerauthentication) も用意されています。これにより、ScaledObject やデプロイコンテナとは別の認証パラメータを記述できます。

AWS は AWS Identity and Access Management 、Amazon EKS、Amazon EKS Anywhere、(ROSA)、Amazon Elastic Compute Cloud (Amazon EC2) 上のセルフマネージド Kubernetes クラスターなど、さまざまな Kubernetes デプロイオプションをサポートする (IAM) ロールを提供します。 Red Hat OpenShift Service on AWS Amazon EC2 これらのロールは、OpenID Connect (OIDC) ID プロバイダーや IAM 信頼ポリシーなどの IAM コンストラクトを使用することにより、Amazon EKS サービスや API に直接依存することなく、さまざまな環境で機能します。詳細については、Amazon EKS ドキュメントの「[サービスアカウントの IAM ロール](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html)」を参照してください。

[Amazon EKS Pod Identity](https://docs.aws.amazon.com/eks/latest/userguide/pod-identities.html) を使用すると、Kubernetes サービスアカウントが OIDC プロバイダーなしで IAM ロールを引き受けるプロセスが簡素化されます。これにより、アプリケーションの認証情報を管理できます。 AWS 認証情報を作成してコンテナに配布したり、Amazon EC2 インスタンスのロールを使用する代わりに、IAM ロールを Kubernetes サービスアカウントに関連付け、サービスアカウントを使用するように Pod を設定します。これにより、複数のクラスターで IAM ロールを使用することができ、IAM ロール間でアクセス許可ポリシーを再利用することでポリシー管理を簡素化することができます。

KEDA と Amazon EKS Pod Identity を実装することで、企業は効率的なイベント駆動型の自動スケーリングとシンプルな認証情報管理を実現できます。アプリケーションは需要に基づいてスケールするため、リソース使用率を最適化し、コストを削減できます。

このパターンは、Amazon EKS Pod Identity を KEDA と統合するのに役立ちます。`keda-operator` サービスアカウントを使用し、`TriggerAuthentication` で認証を委任する方法を説明します。また、KEDA オペレータ用の IAM ロールとアプリケーション用の IAM ロールの間に信頼関係をセットアップする方法についても説明します。この信頼関係により、KEDA はイベントキュー内のメッセージをモニタリングし、送信先 Kubernetes オブジェクトのスケーリングを調整することができます。

## 前提条件と制限
<a name="event-driven-auto-scaling-with-eks-pod-identity-and-keda-prereqs"></a>

**前提条件**
+ AWS Command Line Interface (AWS CLI) バージョン 2.13.17 以降、[インストール](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)済み
+ Python バージョン 3.11.5 以降が[インストールされていること](https://www.python.org/downloads/)
+ AWS SDK for Python (Boto3) バージョン 1.34.135 以降、[インストール](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html)済み
+ Helm バージョン 3.12.3 以降が[インストールされていること](https://helm.sh/docs/intro/install/)
+ kubectl バージョン 1.25.1 以降が[インストールされていること](https://kubernetes.io/docs/tasks/tools/)
+ Docker Engine バージョン 26.1.1 以降が[インストールされていること](https://docs.docker.com/engine/install/)
+ Amazon EKS クラスターバージョン 1.24 以降が[作成されていること](https://docs.aws.amazon.com/eks/latest/userguide/create-cluster.html)
+ Amazon EKS Pod Identity エージェントを作成するための前提条件が[満たされていること](https://docs.aws.amazon.com/eks/latest/userguide/pod-id-agent-setup.html#pod-id-agent-add-on-create)

**制限事項**
+ `keda-operator` ロールと `keda-identity` ロールの間に信頼関係を確立する必要があります。手順については、このパターンの「[エピック](#event-driven-auto-scaling-with-eks-pod-identity-and-keda-epics)」セクションを参照してください。

## アーキテクチャ
<a name="event-driven-auto-scaling-with-eks-pod-identity-and-keda-architecture"></a>

このパターンでは、次の AWS リソースを作成します。
+ **Amazon Elastic Container Registry (Amazon ECR) リポジトリ** – このパターンでは、このリポジトリの名前は `keda-pod-identity-registry` です。このプライベートリポジトリは、サンプルアプリケーションの Docker イメージを保存するために使用されます。
+ **Amazon Simple Queue Service (Amazon SQS) キュー** – このパターンでは、このキューの名前は `event-messages-queue` です。キューは、受信メッセージを収集して保存するメッセージバッファとして機能します。KEDA は、メッセージ数やキューの長さなどのキューメトリクスをモニタリングし、これらのメトリクスに基づいてアプリケーションを自動的にスケーリングします。
+ **アプリケーションの IAM ロール** – このパターンでは、このロールの名前は `keda-identity` です。`keda-operator` ロールはこのロールを引き受けます。このロールにより、Amazon SQS キューへのアクセスが許可されます。
+ **KEDA オペレーターの IAM ロール** – このパターンでは、このロールの名前は `keda-operator` です。KEDA 演算子は、このロールを使用して必要な AWS API コールを行います。このロールには、`keda-identity` ロールを引き受けるためのアクセス権限が割り当てられています。`keda-operator` と `keda-identity` ロールの間には信頼関係があるため、`keda-operator` ロールにも Amazon SQS アクセス権限が割り当てられます。

`TriggerAuthentication` および `ScaledObject` Kubernetes カスタムリソースを通じて、オペレーターは `keda-identity` ロールで Amazon SQS キューに接続します。キューのサイズに基づいて、KEDA は自動的にアプリケーションのデプロイをスケーリングします。キュー内の 5 つの未読メッセージごとに 1 つのポッドを追加します。デフォルト設定では、Amazon SQS キューに未読メッセージがない場合、アプリケーションはポッド数 0 までスケールダウンします。KEDA オペレーターは、指定した間隔でキューをモニタリングします。

 

次の図に、Amazon EKS Pod Identity を使用して `keda-operator` ロールに Amazon SQS キューへの安全なアクセスを提供する方法を示します。

![KEDA と Amazon EKS Pod Identity を使用して、Kubernetes ベースのアプリケーションを自動的にスケーリングします。](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/images/pattern-img/56f7506d-e8d3-43e5-bec6-42267fedd0ae/images/05bdbd09-9eb8-4c0b-8c0d-efe38aecb683.png)


この図表は、次のワークフローを示しています:

1. Amazon EKS Pod Identity エージェントを Amazon EKS クラスターにインストールします。

1. Amazon EKS クラスターの KEDA 名前空間に KEDA オペレーターをデプロイします。

1. ターゲットに `keda-operator`および `keda-identity` IAM ロールを作成します AWS アカウント。

1. IAM ロールの間に信頼関係を確立します。

1. アプリケーションを `security` 名前空間にデプロイします。

1. KEDA オペレーターは、Amazon SQS キュー内のメッセージをポーリングします。

1. KEDA が HPA を開始し、キューのサイズに基づいてアプリケーションが自動的にスケーリングされます。

## ツール
<a name="event-driven-auto-scaling-with-eks-pod-identity-and-keda-tools"></a>

**AWS のサービス**
+ [Amazon Elastic Container Registry (Amazon ECR)](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html) は、セキュリティ、スケーラビリティ、信頼性を備えたマネージドコンテナイメージレジストリサービスです。
+ [Amazon Elastic Kubernetes Service (Amazon EKS)](https://docs.aws.amazon.com/eks/latest/userguide/getting-started.html) を使用すると、独自の Kubernetes コントロールプレーンやノードをインストールまたは維持 AWS することなく、 で Kubernetes を実行できます。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) は、誰を認証し、誰に使用する権限を付与するかを制御することで、 AWS リソースへのアクセスを安全に管理するのに役立ちます。
+ 「[Amazon Simple Queue Service (Amazon SQS)](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html)」は、分散したソフトウェアシステムとコンポーネントの統合と切り離しを支援し、セキュアで耐久性があり、利用可能なホスト型キューを提供します。

その他のツール
+ [KEDA](https://keda.sh/) は、Kubernetes ベースのイベント駆動型オートスケーラーです。

**コードリポジトリ**

このパターンのコードは、GitHub 内の「[Event-driven auto scaling using EKS Pod Identity and KEDA](https://github.com/aws-samples/event-driven-autoscaling-using-podidentity-and-keda/tree/main)」で入手できます。

## ベストプラクティス
<a name="event-driven-auto-scaling-with-eks-pod-identity-and-keda-best-practices"></a>

ACM のベストプラクティスに従うことをおすすめします。
+ [Amazon EKS ベストプラクティス](https://docs.aws.amazon.com/eks/latest/best-practices/introduction.html)
+ [IAM でのセキュリティのベストプラクティス](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)
+ [Amazon SQS のベストプラクティス](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-best-practices.html)

## エピック
<a name="event-driven-auto-scaling-with-eks-pod-identity-and-keda-epics"></a>

### AWS リソースの作成
<a name="create-aws-resources"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| KEDA オペレーターの IAM ロールを作成します。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | AWS 管理者 | 
| サンプルアプリケーションの IAM ロールを作成します。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | AWS 管理者 | 
| Amazon SQS キューを作成します。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | AWS 全般 | 
| Amazon ECR リポジトリを作成します。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | AWS 全般 | 

### Amazon EKS クラスターをセットアップする
<a name="set-up-the-eks-cluster"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| Amazon EKS Pod Identity エージェントをデプロイします。 | ターゲット Amazon EKS クラスターで、Amazon EKS Pod Identity エージェントをセットアップします。Amazon EKS ドキュメントの「[Amazon EKS Pod Identity エージェントのセットアップ](https://docs.aws.amazon.com/eks/latest/userguide/pod-id-agent-setup.html#pod-id-agent-add-on-create)」の手順に従います。 | AWS DevOps | 
| KEDA をデプロイします。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | DevOps エンジニア | 
| IAM ロールを Kubernetes サービスアカウントに割り当てます。 | Amazon EKS ドキュメントの「[IAM ロールを Kubernetes サービスアカウントに割り当てる](https://docs.aws.amazon.com/eks/latest/userguide/pod-id-association.html)」の手順に従ってください。以下の値を使用します。[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | AWS DevOps | 
|  名前空間を作成します。 | 次のコマンドを入力して、ターゲット Amazon EKS クラスターに `security` 名前空間を作成します。<pre>kubectl create ns security</pre> | DevOps エンジニア | 

### サンプルアプリケーションをデプロイする
<a name="deploy-the-sample-application"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| アプリケーションファイルのクローンを作成します。 | 次のコマンドを入力して、GitHub の「[Event-driven auto scaling using EKS Pod Identity and KEDA repository](https://github.com/aws-samples/event-driven-autoscaling-using-podidentity-and-keda/tree/main)」のクローンを作成します。<pre>git clone https://github.com/aws-samples/event-driven-autoscaling-using-podidentity-and-keda.git</pre> | DevOps エンジニア | 
| Docker イメージを作成します。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | DevOps エンジニア | 
| Amazon ECR にDocker イメージをプッシュします。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html)プッシュコマンドを見つけるには、Amazon ECR リポジトリページに移動し、**[プッシュコマンドを表示]** を選択します。 | DevOps エンジニア | 
| サンプルアプリケーションをデプロイします。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | DevOps エンジニア | 
| IAM ロールをアプリケーションサービスアカウントに割り当てます。 | `keda-identity` IAM ロールをサンプルアプリケーションのサービスアカウントに関連付けるには、次のいずれかを実行します。[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | DevOps エンジニア | 
| `ScaledObject` と `TriggerAuthentication` をデプロイします。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | DevOps エンジニア | 

### 自動スケーリングをテストする
<a name="test-auto-scaling"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| Amazon SQS キューにメッセージを送信します。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | DevOps エンジニア | 
| アプリケーションポッドをモニタリングします。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | DevOps エンジニア | 

## トラブルシューティング
<a name="event-driven-auto-scaling-with-eks-pod-identity-and-keda-troubleshooting"></a>


| 問題 | ソリューション | 
| --- | --- | 
| KEDA オペレーターがアプリケーションをスケールできません。 | 次のコマンドを入力して、`keda-operator` IAM ロールのログを確認します。<pre>kubectl logs -n keda -l app=keda-operator -c keda-operator</pre><br /> <br />`HTTP 403` レスポンスコードがある場合、アプリケーションと KEDA スケーラーには Amazon SQS キューにアクセスするための十分な権限がありません。以下のステップを実行します。[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html)<br />`Assume-Role` エラーが発生した場合、[Amazon EKS ノードの IAM ロール](https://docs.aws.amazon.com/eks/latest/userguide/create-node-role.html)は、`TriggerAuthentication` に定義されている IAM ロールを引き受けることができません。以下のステップを実行します。[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | 

## 関連リソース
<a name="event-driven-auto-scaling-with-eks-pod-identity-and-keda-resources"></a>
+ [Amazon EKS Pod Identity エージェントのセットアップ](https://docs.aws.amazon.com/eks/latest/userguide/pod-id-agent-setup.html) (Amazon EKS ドキュメント)
+ [Deploying KEDA](https://keda.sh/docs/2.14/deploy/) (KEDA ドキュメント)
+ [ScaledObject specification](https://keda.sh/docs/2.16/reference/scaledobject-spec/) (KEDA ドキュメント)
+ [Authentication with TriggerAuthentication](https://keda.sh/docs/2.14/concepts/authentication/) (KEDA ドキュメント)