啟用 Auto Scaling 群組指標集合 - Amazon EKS

協助改善此頁面

想要為此使用者指南做出貢獻嗎? 捲動至此頁面底部,然後選取 [編輯此頁面於] GitHub。您的貢獻將有助於使我們的用戶指南更適合所有人。

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

啟用 Auto Scaling 群組指標集合

本主題說明如何使用 AWS LambdaAWS CloudTrail 啟用 Auto Scaling 群組指標集合。Amazon EKS 不會自動針對為受管節點建立的 Auto Scaling 群組啟用群組指標集合。

您可以使用 Auto Scaling 群組指標來追蹤 Auto Scaling 群組的變更,並針對閾值設定警示。Auto Scaling 群組指標可在 Auto Scaling 主控台或 Amazon 主 CloudWatch控台中使用。啟用後,Auto Scaling 群組會 CloudWatch 每分鐘將取樣資料傳送到 Amazon。啟用這些指標不會產生額外費用。

透過啟用 Auto Scaling 群組指標集合,您將能夠監控受管節點群組的擴缩。Auto Scaling 群組指標報告 Auto Scaling 群組最小、最大和所需規模。如果節點群組中的節點數量低於最小規模,這代表節點群組運作狀態不佳,您可以建立警示。追蹤節點群組規模在調整最大計數時也很有用,讓資料平面不會耗盡容量。

建立受管節點群組時,AWS CloudTrail會將CreateNodegroup事件傳送至 Amazon EventBridge。透過建立符合CreateNodegroup事件的 Amazon EventBridge 規則,您可以觸發 Lambda 函數,為與受管節點群組關聯的 Auto Scaling 群組啟用群組指標收集。

顯示受管理節點群組和 EventBridge 元件的圖表 CloudTrail
啟用 Auto Scaling 群組指標集合
  1. 建立適用於 Lambda 的 IAM 角色。

    LAMBDA_ROLE=$(aws iam create-role \ --role-name lambda-asg-enable-metrics \ --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}' \ --output text \ --query 'Role.Arn') echo $LAMBDA_ROLE
  2. 建立允許描述 Amazon EKS 節點群組並啟用 Auto Scaling 群組指標集合的政策。

    cat > /tmp/lambda-policy.json <<EOF { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "eks:DescribeNodegroup", "autoscaling:EnableMetricsCollection" ], "Resource": [ "*" ] } ] } EOF LAMBDA_POLICY_ARN=$(aws iam create-policy \ --policy-name lambda-asg-enable-metrics-policy \ --policy-document file:///tmp/lambda-policy.json \ --output text \ --query 'Policy.Arn') echo $LAMBDA_POLICY_ARN
  3. 將政策連接至 Lambda 的 IAM 角色。

    aws iam attach-role-policy \ --policy-arn $LAMBDA_POLICY_ARN \ --role-name lambda-asg-enable-metrics
  4. 新增受AWSLambdaBasicExecutionRole管理的策略,該策略具有將日誌寫入日誌所需的 CloudWatch 權限。

    aws iam attach-role-policy \ --role-name lambda-asg-enable-metrics \ --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
  5. 建立 Lambda 代碼。

    cat > /tmp/lambda-handler.py <<EOF import json import boto3 import time import logging eks = boto3.client('eks') autoscaling = boto3.client('autoscaling') logger = logging.getLogger() logger.setLevel(logging.INFO) def lambda_handler(event, context): ASG_METRICS_COLLLECTION_TAG_NAME = "ASG_METRICS_COLLLECTION_ENABLED" initial_retry_delay = 10 attempts = 0 #print(event) if not event["detail"]["eventName"] == "CreateNodegroup": print("invalid event.") return -1 clusterName = event["detail"]["requestParameters"]["name"] nodegroupName = event["detail"]["requestParameters"]["nodegroupName"] try: metricsCollectionEnabled = event["detail"]["requestParameters"]["tags"][ASG_METRICS_COLLLECTION_TAG_NAME] except KeyError: print(ASG_METRICS_COLLLECTION_TAG_NAME, "tag not found.") return # Check if metrics collection is enabled in tags if metricsCollectionEnabled.lower() != "true": print("Metrics collection is not enabled in nodegroup tags.") return # Get the name of the associated autoscaling group print("Getting the autoscaling group name for nodegroup=", nodegroupName, ", cluster=", clusterName ) for i in range(0,10): try: autoScalingGroup = eks.describe_nodegroup(clusterName=clusterName,nodegroupName=nodegroupName)["nodegroup"]["resources"]["autoScalingGroups"][0]["name"] except: attempts += 1 print("Failed to obtain the associated autoscaling group for nodegroup", nodegroupName, "Retrying in", initial_retry_delay*attempts, "seconds.") time.sleep(initial_retry_delay*attempts) else: break print("Enabling metrics collection on autoscaling group ", autoScalingGroup) # Enable metrics collection in the autoscaling group try: enableMetricsCollection = autoscaling.enable_metrics_collection(AutoScalingGroupName=autoScalingGroup,Granularity="1Minute") except: print("Unable to enable metrics collection on nodegroup=",nodegroup) print("Enabled metrics collection on nodegroup", nodegroupName) EOF
  6. 建立部署套件。

    cd /tmp zip function.zip lambda-handler.py
  7. 建立 Lambda 函數。

    LAMBDA_ARN=$(aws lambda create-function --function-name asg-enable-metrics-collection \ --zip-file fileb://function.zip --handler lambda-handler.lambda_handler \ --runtime python3.9 \ --timeout 600 \ --role $LAMBDA_ROLE \ --output text \ --query 'FunctionArn') echo $LAMBDA_ARN
  8. 建立 EventBridge 規則。

    RULE_ARN=$(aws events put-rule --name CreateNodegroupRuleToLambda \ --event-pattern "{\"source\":[\"aws.eks\"],\"detail-type\":[\"AWS API Call via CloudTrail\"],\"detail\":{\"eventName\":[\"CreateNodegroup\"],\"eventSource\":[\"eks.amazonaws.com\"]}}" \ --output text \ --query 'RuleArn') echo $RULE_ARN
  9. 新增 Lambda 函數作為目標。

    aws events put-targets --rule CreateNodegroupRuleToLambda \ --targets "Id"="1","Arn"="$LAMBDA_ARN"
  10. 新增允許 EventBridge 叫用 Lambda 函數的政策。

    aws lambda add-permission \ --function-name asg-enable-metrics-collection \ --statement-id CreateNodegroupRuleToLambda \ --action 'lambda:InvokeFunction' \ --principal events.amazonaws.com \ --source-arn $RULE_ARN

Lambda 函數會為您透過將 ASG_METRICS_COLLLECTION_ENABLED 設定 TRUE 來標記之任何受管節點群組啟用 Auto Scaling 群組指標集合。若要確認 Auto Scaling 群組指標集合已啟用,請導航至 Amazon EC2 主控台中相關聯的 Auto Scaling 群組。在 Monitoring (監控) 索引標籤中,您應該會看到 Enable (啟用) 核取方塊已啟用。