简体   繁体   English

AWS ECS Fargate-任务未运行

[英]AWS ECS Fargate - Task Not Running

I followed this AWS Tutorial on setting up a task on AWS ECS Fargate https://docs.aws.amazon.com/AmazonECS/latest/userguide/ECS_AWSCLI_Fargate.html 我按照此AWS教程上的说明在AWS ECS Fargate上设置任务https://docs.aws.amazon.com/AmazonECS/latest/userguide/ECS_AWSCLI_Fargate.html

I have an image in ECR, I have setup the Cluster and the Service, as well as the task definition and yet nothing, is running. 我在ECR中有一个映像,已经设置了集群和服务以及任务定义,但是没有任何运行。 I have to manually run the task in the aws console to get it to start and when I update the service with a new task definition the running task does not get updated even with a force deployment. 我必须在aws控制台中手动运行该任务才能启动它,并且当我使用新的任务定义更新服务时,即使强制部署也无法更新正在运行的任务。

I wanted a very simple setup so I have no ELB or AutoScaling policy and the following setup for the service: 我想要一个非常简单的设置,因此我没有ELB或AutoScaling策略以及该服务的以下设置:

Number of tasks 1 任务数1

Minimum healthy percent 100 最低健康百分比100

Maximum percent 200 最大百分比200

Deployment Type: Rolling Update 部署类型: 滚动更新

I feel like I am missing something, my task are not starting automatically and not updating upon service update. 我感觉好像丢失了一些东西,我的任务没有自动启动,也没有在服务更新时更新。

I have attached below the deploy code I am using in my bitbucket pipeline if that helps at all: 如果有帮助,我会在我的位桶管道中附加正在使用的部署代码下面:

    #!/bin/bash

    set -e

    # possible -b (base / app name) -i (image version), -e (deploy env) and -s (service id)
    while getopts b:i:e:s:r: option
    do
    case "${option}"
    in
    b) BASE_NAME=${OPTARG};;
    i) IMG_VERSION=${OPTARG};;
    e) DEPLOY_ENV=${OPTARG};;
    s) SERVICE_ID=${OPTARG};;
    r) EXECUTION_ROLE=${OPTARG};;
    esac
    done

    echo "BASE_NAME: " $BASE_NAME
    echo "IMG_VERSION: " $IMG_VERSION
    echo "DEPLOY_ENV: " $DEPLOY_ENV
    echo "SERVICE_ID: " $SERVICE_ID
    echo "EXECUTION_ROLE: " $EXECUTION_ROLE

    if [ -z "$BASE_NAME" ]; then
        echo "exit: No BASE_NAME specified"
        exit;
    fi

    if [ -z "$SERVICE_ID" ]; then
        echo "exit: No SERVICE_ID specified"
        exit;
    fi

    if [ -z "$DEPLOY_ENV" ]; then
        echo "exit: No DEPLOY_ENV specified"
        exit;
    fi

    if [ -z "$IMG_VERSION" ]; then
        echo "exit: No IMG_VERSION specified"
        exit;
    fi

    if [ -z "$EXECUTION_ROLE" ]; then
        echo "exit: No EXECUTION_ROLE specified"
        exit;
    fi

    # Define variables
    TASK_FAMILY=${BASE_NAME}-${DEPLOY_ENV}-${SERVICE_ID}
    SERVICE_NAME=${BASE_NAME}-${DEPLOY_ENV}-${SERVICE_ID}-service
    CLUSTER_NAME=${BASE_NAME}-${DEPLOY_ENV}-cluster

    IMAGE_PACEHOLDER="<IMAGE_VERSION>"

    CONTAINER_DEFINITION_FILE=$(cat ${BASE_NAME}-$SERVICE_ID.container-definition.json)
    CONTAINER_DEFINITION="${CONTAINER_DEFINITION_FILE//$IMAGE_PACEHOLDER/$IMG_VERSION}"


    export TASK_VERSION=$(aws ecs register-task-definition --family ${TASK_FAMILY} --container-definitions "$CONTAINER_DEFINITION" --requires-compatibilities '["FARGATE"]' --cpu "512" --memory "1024" --network-mode "awsvpc" --execution-role-arn ${EXECUTION_ROLE} | jq --raw-output '.taskDefinition.revision')
    echo "Registered ECS Task Definition: " $TASK_VERSION



    if [ -n "$TASK_VERSION" ]; then
        echo "Update ECS Cluster: " $CLUSTER_NAME
        echo "Service: " $SERVICE_NAME
        echo "Task Definition: " $TASK_FAMILY:$TASK_VERSION


        #Update ECS Service
        DEPLOYED_SERVICE=$(aws ecs update-service --cluster $CLUSTER_NAME --service $SERVICE_NAME --task-definition $TASK_FAMILY:$TASK_VERSION --force-new-deployment | jq --raw-output '.service.serviceName')

        echo "Deployment of $DEPLOYED_SERVICE complete"


    else
        echo "exit: No task definition"
        exit;
    fi

EDIT: 编辑:

Here is my task definition: 这是我的任务定义:

    {
      "ipcMode": null,
      "executionRoleArn": "arn:aws:iam::<Account-id>:role/:arn:aws:iam::<Account-id>:role/ecsTaskExecutionRole",
      "containerDefinitions": [
        {
          "dnsSearchDomains": null,
          "logConfiguration": null,
          "entryPoint": [],
          "portMappings": [
            {
              "hostPort": 80,
              "protocol": "tcp",
              "containerPort": 80
            },
            {
              "hostPort": 443,
              "protocol": "tcp",
              "containerPort": 443
            }
          ],
          "command": [],
          "linuxParameters": null,
          "cpu": 0,
          "environment": [],
          "resourceRequirements": null,
          "ulimits": null,
          "dnsServers": null,
          "mountPoints": [],
          "workingDirectory": "/usr/share/nginx/html/",
          "secrets": null,
          "dockerSecurityOptions": null,
          "memory": null,
          "memoryReservation": null,
          "volumesFrom": [],
          "stopTimeout": null,
          "image": "<Account-id>.dkr.ecr.us-east-1.amazonaws.com/<my-ecr-image>:latest",
          "startTimeout": null,
          "dependsOn": null,
          "disableNetworking": null,
          "interactive": null,
          "healthCheck": null,
          "essential": true,
          "links": null,
          "hostname": null,
          "extraHosts": null,
          "pseudoTerminal": null,
          "user": null,
          "readonlyRootFilesystem": null,
          "dockerLabels": null,
          "systemControls": null,
          "privileged": null,
          "name": "dig-website"
        }
      ],
      "placementConstraints": [],
      "memory": "1024",
      "taskRoleArn": null,
      "compatibilities": [
        "EC2",
        "FARGATE"
      ],
      "taskDefinitionArn": "arn:aws:ecs:us-east-1:<Account-id>:task-definition/myapp-production-website:11",
      "family": "myapp-production-website",
      "requiresAttributes": [
        {
          "targetId": null,
          "targetType": null,
          "value": null,
          "name": "com.amazonaws.ecs.capability.docker-remote-api.1.17"
        },
        {
          "targetId": null,
          "targetType": null,
          "value": null,
          "name": "ecs.capability.execution-role-ecr-pull"
        },
        {
          "targetId": null,
          "targetType": null,
          "value": null,
          "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18"
        },
        {
          "targetId": null,
          "targetType": null,
          "value": null,
          "name": "ecs.capability.task-eni"
        },
        {
          "targetId": null,
          "targetType": null,
          "value": null,
          "name": "com.amazonaws.ecs.capability.ecr-auth"
        }
      ],
      "pidMode": null,
      "requiresCompatibilities": [
        "FARGATE"
      ],
      "networkMode": "awsvpc",
      "cpu": "512",
      "revision": 11,
      "status": "ACTIVE",
      "proxyConfiguration": null,
      "volumes": []
    }

Solved my issue. 解决了我的问题。 The error originated from an argument I passed to my bitbucket pipeline. 该错误源自我传递给我的Bitbucket管道的参数。

I have an env variable in my pipeline to populate the execution role, what I did not know is that all I needed to pass to the aws cli for ecs register-task was the name of the role and not the full ARN like shown below: 我在管道中有一个env变量来填充执行角色,我不知道我为传递给ecs register-task的aws cli所需要的只是角色的名称,而不是完整的ARN,如下所示:

"executionRoleArn": "arn:aws:iam::<Account-id>:role/:arn:aws:iam::<Account-id>:role/ecsTaskExecutionRole"

which should be instead: 应该改为:

 "executionRoleArn": "arn:aws:iam::<Account-id>:role/ecsTaskExecutionRole"

because it can't resolve the arn it will throw an error saying the role does not have the right permissions. 因为它无法解析arn,所以将引发错误消息,指出角色没有正确的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM