As verificações de integridade de escalonamento automático falham no modelo ECS CloudFormation fornecido pela Amazon.

3

Estou tentando criar um novo cluster do ECS usando o modelo AWS do CloudFormation ECS Service fornecido aqui como um guia. Minhas instâncias do ECS são inicializadas no grupo AutoScaling, mas falham na verificação de integridade e são sempre finalizadas.

A saída realmente não me diz muito sobre o que as verificações estão falhando ou por quê.

O código do CloudFormation que estou usando é basicamente o código de ações fornecido nos documentos da AWS. Eu adicionei um grupo de segurança com permissões mais amplas (para que eu pudesse fazer o SSH durante a iteração) e atualizei a AMI para a versão mais recente do Amazon Linux otimizado para ECS no us-east-1.

Modelo atual:

{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Description": "Deploys PoC ECS infrastructure.",  

  "Parameters" : {
    "KeyName": {
      "Description": "Name of an existing EC2 KeyPair to enable SSH access to the Elastic Beanstalk and Bastion hosts",
      "Type": "String",
      "MinLength": "1",
      "MaxLength": "255",
      "AllowedPattern": "[\x20-\x7E]*",
      "ConstraintDescription": "can contain only ASCII characters.",
      "Default": "smx-test-key"
    },
    "SubnetID": {
      "Type": "List<AWS::EC2::Subnet::Id>",
      "Description": "Select a default subnet ID."
    },    
    "DesiredCapacity": {
      "Type": "Number",
      "Default" : "1",
      "Description": "Number of instances to launch in your ECS cluster."
    },    
    "MaxSize": {
      "Type": "Number",
      "Default" : "1",
      "Description": "Maximum number of instances that can be launched in your ECS cluster."
    },
    "ECSInstanceType": {
      "Description": "The type of instance to use for ECS app servers",
      "Type": "String",
      "Default": "t2.micro",
      "AllowedValues": ["t2.micro", "t2.small", "t2.medium", "t2.large", "m3.medium", "m3.large", "m3.xlarge" ]
    },
    "SSHLocation" : {
      "Description" : " The IP address range that can be used to SSH to the EC2 instances.",
      "Type": "String",
      "MinLength": "9",
      "MaxLength": "18",
      "Default": "0.0.0.0/0",
      "AllowedPattern": "(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})",
      "ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
    }
  },

  "Mappings" : {
    "AWSRegionToAMI" : {
      "us-east-1"      : { "AMIID" : "ami-5d1b984a" }
    }
  },

  "Resources" : {

    "ECSCluster": {
      "Type": "AWS::ECS::Cluster"
    },

    "taskdefinition": {
      "Type": "AWS::ECS::TaskDefinition",
      "Properties" : {
        "ContainerDefinitions" : [
          {
            "Name": "simple-app",
            "Cpu": "10",
            "Essential": "true",
            "Image":"httpd:2.4",
            "Memory":"300",
            "MountPoints": [{
              "ContainerPath": "/usr/local/apache2/htdocs",
              "SourceVolume": "my-vol"
            }],
            "PortMappings": [
              { "HostPort": 80, "ContainerPort": 80 }
            ]
          },
          {
            "Name": "busybox",
            "Cpu": 10,
            "Command": [
              "/bin/sh -c \"while true; do echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p>' > top; /bin/date > date ; echo '</div></body></html>' > bottom; cat top date bottom > /usr/local/apache2/htdocs/index.html ; sleep 1; done\""
            ],      
            "EntryPoint": [ "sh", "-c"],
            "Essential": false,
            "Image": "busybox",
            "Memory": 200,
            "VolumesFrom": [
              {
                "SourceContainer": "simple-app"
              }
            ]
          }
        ],
        "Volumes": [
          { "Name": "my-vol" }
        ]
      }
    },    

    "EcsElasticLoadBalancer" : {
      "Type" : "AWS::ElasticLoadBalancing::LoadBalancer",
      "Properties" : {
        "Subnets" : { "Ref" : "SubnetID" },
        "Listeners" : [ {
          "LoadBalancerPort" : "80",
          "InstancePort" : "80",
          "Protocol" : "HTTP"
        } ],
        "HealthCheck" : {
          "Target" : "HTTP:80/",
          "HealthyThreshold" : "2",
          "UnhealthyThreshold" : "10",
          "Interval" : "30",
          "Timeout" : "5"
        }
      }
    }, 

    "ECSAutoScalingGroup" : {
      "Type" : "AWS::AutoScaling::AutoScalingGroup",
      "Properties" : {
        "VPCZoneIdentifier" : { "Ref" : "SubnetID" },
        "LaunchConfigurationName" : { "Ref" : "ContainerInstances" },
        "MinSize" : "1",
        "MaxSize" : { "Ref" : "MaxSize" },
        "DesiredCapacity" : { "Ref" : "DesiredCapacity" }
      },
      "CreationPolicy" : {
        "ResourceSignal" : {
          "Timeout" : "PT60M"
        }
      },
      "UpdatePolicy": {
        "AutoScalingRollingUpdate": {
          "MinInstancesInService": "1",
          "MaxBatchSize": "1",
          "PauseTime" : "PT60M",
          "WaitOnResourceSignals": "true"
        }
      }
    },   

    "ContainerInstances": {
      "Type": "AWS::AutoScaling::LaunchConfiguration",
      "Metadata" : {
        "AWS::CloudFormation::Init" : {
          "config" : {
            "commands" : {
              "01_add_instance_to_cluster" : {
                "command" : { "Fn::Join": [ "", [ "#!/bin/bash\n", "echo ECS_CLUSTER=", { "Ref": "ECSCluster" }, " >> /etc/ecs/ecs.config" ] ] }
              }
            },
            "files" : {
              "/etc/cfn/cfn-hup.conf" : {
                "content" : { "Fn::Join" : ["", [
                  "[main]\n",
                  "stack=", { "Ref" : "AWS::StackId" }, "\n",
                  "region=", { "Ref" : "AWS::Region" }, "\n"
                ]]},
                "mode"    : "000400",
                "owner"   : "root",
                "group"   : "root"
              },
              "/etc/cfn/hooks.d/cfn-auto-reloader.conf" : {
                "content": { "Fn::Join" : ["", [
                  "[cfn-auto-reloader-hook]\n",
                  "triggers=post.update\n",
                  "path=Resources.ContainerInstances.Metadata.AWS::CloudFormation::Init\n",
                  "action=/opt/aws/bin/cfn-init -v ",
                  "         --stack ", { "Ref" : "AWS::StackName" },
                  "         --resource ContainerInstances ",
                  "         --region ", { "Ref" : "AWS::Region" }, "\n",
                  "runas=root\n"
                ]]}
              }
            },
            "services" : {
              "sysvinit" : {
                "cfn-hup" : { "enabled" : "true", "ensureRunning" : "true", "files" : ["/etc/cfn/cfn-hup.conf", "/etc/cfn/hooks.d/cfn-auto-reloader.conf"] }
              }
            }
          }
        }
      },
      "Properties": {
        "ImageId" : { "Fn::FindInMap" : [ "AWSRegionToAMI", { "Ref" : "AWS::Region" }, "AMIID" ] },
        "InstanceType"   : { "Ref" : "ECSInstanceType" },
        "IamInstanceProfile": { "Ref": "EC2InstanceProfile" },
        "KeyName"        : { "Ref" : "KeyName" },
        "SecurityGroups": { "Ref" : "ECSSecurityGroup" },
        "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
             "#!/bin/bash -xe\n",
             "yum install -y aws-cfn-bootstrap\n",

             "/opt/aws/bin/cfn-init -v ",
             "         --stack ", { "Ref" : "AWS::StackName" },
             "         --resource ContainerInstances ",
             "         --region ", { "Ref" : "AWS::Region" }, "\n",

             "/opt/aws/bin/cfn-signal -e $? ",
             "         --stack ", { "Ref" : "AWS::StackName" },
             "         --resource ECSAutoScalingGroup ",
             "         --region ", { "Ref" : "AWS::Region" }, "\n"
        ]]}},
        "Tags" : [ {"Key" : "Name", "Value" : "ECS autoscaling instance"} ]
      }
    },

    "service": {
      "Type": "AWS::ECS::Service",
      "DependsOn": ["ECSAutoScalingGroup"],
      "Properties" : {
        "Cluster": {"Ref": "ECSCluster"},
        "DesiredCount": "1",
        "LoadBalancers": [
          {
            "ContainerName": "simple-app",
            "ContainerPort": "80",
            "LoadBalancerName" : { "Ref" : "EcsElasticLoadBalancer" }
          }
        ],
        "Role" : {"Ref":"ECSServiceRole"},
        "TaskDefinition" : {"Ref":"taskdefinition"}
      }
    },

    "ECSServiceRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "ecs.amazonaws.com"
                ]
              },
              "Action": [
                "sts:AssumeRole"
              ]
            }
          ]
        },
        "Path": "/",
        "Policies": [
          {
            "PolicyName": "ecs-service",
            "PolicyDocument": {
              "Statement": [
                {
                  "Effect": "Allow",
                  "Action": [
                    "elasticloadbalancing:Describe*",
                    "elasticloadbalancing:DeregisterInstancesFromLoadBalancer",
                    "elasticloadbalancing:RegisterInstancesWithLoadBalancer",
                    "ec2:Describe*",
                    "ec2:AuthorizeSecurityGroupIngress"
                  ],
                  "Resource": "*"
                }
              ]
            }
          }
        ]
      }
    },

    "EC2Role": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "ec2.amazonaws.com"
                ]
              },
              "Action": [
                "sts:AssumeRole"
              ]
            }
          ]
        },
        "Path": "/",
        "Policies": [
          {
            "PolicyName": "ecs-service",
            "PolicyDocument": {
              "Statement": [
                {
                  "Effect": "Allow",
                  "Action": [
                    "ecs:CreateCluster",
                    "ecs:DeregisterContainerInstance",
                    "ecs:DiscoverPollEndpoint",
                    "ecs:Poll",
                    "ecs:RegisterContainerInstance",
                    "ecs:StartTelemetrySession",
                    "ecs:Submit*",
                    "logs:CreateLogStream",
                    "logs:PutLogEvents"
                  ],
                  "Resource": "*"
                }
              ]
            }
          }
        ]
      }
    },

    "EC2InstanceProfile": {
      "Type": "AWS::IAM::InstanceProfile",
      "Properties": {
        "Path": "/",
        "Roles": [ { "Ref": "EC2Role" } ]
      }
    }

    "ECSSecurityGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "GroupDescription" : "Fortigate recommended settings.  See marketplace for docs.",
        "VpcId" : { "Ref" : "VPC" },
        "SecurityGroupIngress" : [
           { "IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : "0.0.0.0/0" },
           { "IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "CidrIp" : "0.0.0.0/0" },
           { "IpProtocol" : "icmp", "FromPort" : "-1",  "ToPort" : "-1",  "CidrIp" : {"Fn::GetAtt" : [ "VPC" , "CidrBlock" ]}}
        ],
        "Tags" : [ {"Key" : "Name", "Value" : "ECS Security Group"} ]
      }
    }
  },

  "Outputs" : {
    "ecsservice" : {
      "Value" : { "Ref" : "service" }
    },
    "ecscluster" : {
      "Value" : { "Ref" : "ECSCluster" }
    },
    "taskdef" : {
      "Value" : { "Ref" : "taskdefinition" }
    }
  }
}

Quando eu crio a pilha, tudo até o grupo AutoScaling é concluído. O grupo AS é criado e a instância é inicializada. Mas, em seguida, a verificação de integridade falha, a instância é finalizada e a pilha é revertida. O CloudFormation mostra que a criação do grupo de escalonamento automático falhou com recebido sinal (s) de SUCESSO 0 de 1. Incapaz de satisfazer o requisito 100% MinSuccessfulInstancesPercent .

Etapas tomadas para solucionar problemas até agora:

  • Rolou os mapeamentos de AMI de volta para as versões listadas em seus resultados de amostra nas mesmas verificações de falha. Então não é isso.
  • Defina a pilha para um tempo limite maior (60m) para SSH e comece a explorar a instância.
  • Parece que o CloudFormation Init não está sendo executado no recurso AWS :: AutoScaling :: LaunchConfiguration. /etc/ecs/ecs.config não existe, outros arquivos estão ausentes ou faltam a configuração. Mas os recursos LaunchConfiguration parecem concluir nos logs de eventos do console do CloudFormation.

Perguntas do casal:

  • Antes de descobrirmos o caminho a seguir, acho que devo perguntar se existe um modelo de trabalho mais atualizado e verificado publicado em algum lugar?
  • O código AWS :: CloudFormation :: Init do recurso "ContainerInstances" parece correto?
  • O próprio grupo AutoScaling parece bem?
  • Como posso solucionar meu problema com esses problemas de configuração?
por invict_us 14.07.2016 / 21:51

0 respostas