

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Amazon EKS Pod Identity 和 KEDA 在 Amazon EKS 中設定事件驅動的自動擴展
<a name="event-driven-auto-scaling-with-eks-pod-identity-and-keda"></a>

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

## 總結
<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 叢集整合。它也適用於標準 Kubernetes 元件，例如 [Horizontal Pod Autoscaling (HPA)](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/)。KEDA 也提供 [TriggerAuthentication](https://keda.sh/docs/2.14/concepts/authentication/#re-use-credentials-and-delegate-auth-with-triggerauthentication)，這項功能可協助您委派身分驗證。它可讓您描述與 ScaledObject 和部署容器分開的身分驗證參數。

AWS 提供支援各種 Kubernetes 部署選項的 AWS Identity and Access Management (IAM) 角色，包括 Amazon EKS、Amazon EKS Anywhere、 Red Hat OpenShift Service on AWS (ROSA) 和 Amazon Elastic Compute Cloud (Amazon EC2) 上的自我管理 Kubernetes 叢集。這些角色使用 OpenID Connect (OIDC) 身分提供者和 IAM 信任政策等 IAM 建構，在不同的環境中操作，而無需直接依賴 Amazon EKS 服務或 APIs。如需詳細資訊，請參閱 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 服務帳戶擔任 IAM 角色的程序，而無需 OIDC 供應商。它可讓您管理應用程式的登入資料。您可以將 IAM 角色與 Kubernetes 服務帳戶建立關聯，並將 Pod 設定為使用服務帳戶，而不是建立 AWS 登入資料並將其分發至容器或使用 Amazon EC2 執行個體的角色。這可協助您跨多個叢集使用 IAM 角色，並透過啟用跨 IAM 角色重複使用許可政策來簡化政策管理。

透過使用 Amazon EKS Pod Identity 實作 KEDA，企業可以實現高效的事件驅動型自動擴展和簡化的憑證管理。應用程式會根據需求擴展，以最佳化資源使用率並降低成本。

此模式可協助您將 Amazon EKS Pod 身分與 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/)
+ 適用於 Python (Boto3) 的 AWS SDK 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 引擎 26.1.1 版或更新版本，[已安裝](https://docs.docker.com/engine/install/)
+ [已建立](https://docs.aws.amazon.com/eks/latest/userguide/create-cluster.html) Amazon EKS 叢集 1.24 版或更新版本
+ [符合](https://docs.aws.amazon.com/eks/latest/userguide/pod-id-agent-setup.html#pod-id-agent-add-on-create)建立 Amazon EKS Pod Identity 代理程式的先決條件

**限制**
+ 您必須在`keda-operator`角色與`keda-identity`角色之間建立信任關係。此模式的 [Epics](#event-driven-auto-scaling-with-eks-pod-identity-and-keda-epics) 區段提供說明。

## Architecture
<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 個 Pod。在預設組態中，如果 Amazon SQS 佇列中沒有未讀取的訊息，應用程式會縮減至 0 個 Pod。KEDA 運算子會以您指定的間隔監控佇列。

 

下圖顯示如何使用 Amazon EKS Pod Identity 為`keda-operator`角色提供 Amazon SQS 佇列的安全存取權。

![\[使用 KEDA 和 Amazon EKS Pod Identity 自動擴展以 Kubernetes 為基礎的應用程式。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/56f7506d-e8d3-43e5-bec6-42267fedd0ae/images/05bdbd09-9eb8-4c0b-8c0d-efe38aecb683.png)


該圖顯示以下工作流程：

1. 您在 Amazon EKS 叢集中安裝 Amazon EKS Pod Identity 代理程式。

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 [事件驅動的自動擴展中使用 EKS Pod Identity 和 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>

建議您遵循下列最佳實務：
+ [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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 KEDA 運算子的 IAM 角色。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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/zh_tw/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/zh_tw/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/zh_tw/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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署 Amazon EKS Pod Identity 代理程式。 | 針對目標 Amazon EKS 叢集，設定 Amazon EKS Pod Identity 代理程式。請遵循 [Amazon EKS 文件中的設定 Amazon EKS Pod 身分代理](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/zh_tw/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/zh_tw/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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 複製應用程式檔案。 | 輸入下列命令，從 GitHub [使用 EKS Pod Identity 和 KEDA 儲存庫複製事件驅動的自動擴展](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/zh_tw/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | DevOps 工程師 | 
| 將 Docker 映像推送至 Amazon ECR。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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/zh_tw/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/zh_tw/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/zh_tw/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | DevOps 工程師 | 

### 測試自動擴展
<a name="test-auto-scaling"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 傳送訊息至 Amazon SQS 佇列。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html) | DevOps 工程師 | 
| 監控應用程式 Pod。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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 運算子無法擴展應用程式。 | 輸入下列命令來檢查 IAM `keda-operator` 角色的日誌：<pre>kubectl logs -n keda -l app=keda-operator -c keda-operator</pre> 如果有`HTTP 403`回應代碼，則應用程式和 KEDA 擴展器沒有足夠的許可來存取 Amazon SQS 佇列。請完成下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/event-driven-auto-scaling-with-eks-pod-identity-and-keda.html)如果發生錯誤`Assume-Role`，則 [Amazon EKS 節點 IAM 角色](https://docs.aws.amazon.com/eks/latest/userguide/create-node-role.html)無法擔任為 定義的 IAM 角色`TriggerAuthentication`。請完成下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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 Agent](https://docs.aws.amazon.com/eks/latest/userguide/pod-id-agent-setup.html) (Amazon EKS 文件）
+ [部署 KEDA](https://keda.sh/docs/2.14/deploy/) (KEDA 文件）
+ [ScaledObject 規格](https://keda.sh/docs/2.16/reference/scaledobject-spec/) (KEDA 文件）
+ [使用 TriggerAuthentication 進行身分驗證](https://keda.sh/docs/2.14/concepts/authentication/) (KEDA 文件）