terraform-ecs.已注册的容器实例显示为0 [英] terraform-ecs. Registered container instance is showing 0

查看:76
本文介绍了terraform-ecs.已注册的容器实例显示为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行terraform apply时,它正在创建集群,服务,ec2实例.但是已注册的容器实例为0,正在运行的任务计数为0.

On running terraform apply it is creating a cluster, service, ec2 instance. But Registered container instances is 0, running tasks count is 0.

我尝试将ecs.amazonaws.com更改为ec2.amazonaws.com,但是它抛出错误:

I tried changing ecs.amazonaws.com to ec2.amazonaws.com but it is throwing an error:

aws_ecs_service.nginx:InvalidParameterException:无法担当角色并无法验证在负载均衡器上配置的侦听器.请确认所传递的ECS服务角色具有适当的权限.

aws_ecs_service.nginx: InvalidParameterException: Unable to assume role and validate the listeners configured on your load balancer. Please verify that the ECS service role being passed has the proper permissions.

    provider "aws" {
        region = "us-east-1"
    }

    resource "aws_ecs_cluster" "demo" {
      name = "demo"
    }

    resource "aws_iam_role" "ecs_elb" {
        name = "ecs-elb"
        assume_role_policy = <<EOF
    {
      "Version": "2008-10-17",
      "Statement": [
        {
          "Sid": "",
          "Effect": "Allow",
          "Principal": {
            "Service": "ecs.amazonaws.com"
          },
          "Action": "sts:AssumeRole"
        }
      ]
    }
    EOF
    }

    resource "aws_iam_policy_attachment" "ecs_elb" {
        name = "ecs_elb"
        roles = ["${aws_iam_role.ecs_elb.id}"]
        policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceRole"
    }

    resource "aws_launch_configuration" "ecs_instance"{
        name_prefix = "ecs-instance-"
        instance_type = "t2.micro"
        image_id = "ami-4fffc834"
    }

    resource "aws_autoscaling_group" "ecs_cluster_instances"{
        availability_zones = ["us-east-1a"]
        name = "ecs-cluster-instances"
        min_size = 1
        max_size = 1
        launch_configuration = "${aws_launch_configuration.ecs_instance.name}"
    }

    resource "aws_ecs_task_definition" "nginx" {
      family = "nginx"
      container_definitions = <<EOF
      [{
        "name": "nginx",
        "image": "nginx",
        "cpu": 1024,
        "memory": 768,
        "essential": true,
        "portMappings": [{"containerPort":80, "hostPort":80}]
      }]
      EOF
    }

    resource "aws_ecs_service" "nginx" {
        name = "nginx"
        cluster = "${aws_ecs_cluster.demo.id}"
        task_definition = "${aws_ecs_task_definition.nginx.arn}"
        desired_count = 1
        iam_role = "${aws_iam_role.ecs_elb.arn}"
        load_balancer {
            elb_name = "${aws_elb.nginx.id}"
            container_name = "nginx"
            container_port = 80
        }
    }
    resource "aws_elb" "nginx" {
        availability_zones = ["us-east-1a"]
        name = "nginx"
        listener {
            lb_port = 80
            lb_protocol = "http"
            instance_port = 80
            instance_protocol = "http"
        }
    }

推荐答案

这里有一些建议可以登录 AWS Console :

Here are few suggestions to check in AWS Console:

基本上,这些实例一旦以root登录,它们应该具有start ecs命令.

Basically these instances, once you login as root, they should have start ecs command.

Terraform示例:

Terraform example:

data "aws_ami" "ecs_ami" {
  most_recent = true
  owners      = ["amazon"]

  filter {
    name   = "name"
    values = ["amzn-ami-*-amazon-ecs-optimized"]
  }
}

  • 检查EC2是否已启动.

  • Check whether EC2 are spinned up.

    检查ECS代理是否正在EC2实例上运行.

    Check whether ECS agent is running on the EC2 instances.

    1. root身份登录到EC2实例.
    2. 运行docker ps并检查ecs-agent容器是否正在运行.
    3. 否则,由start ecsrestart ecs手动启动.
    1. Login to EC2 instance as root.
    2. Run docker ps and check for whether ecs-agent container is running.
    3. Otherwise start manually by start ecs or restart ecs.

    注意:如果您没有dockerstartrestart命令,则说明您未使用ECS优化的AMI.

    Note: If you don't have docker, start or restart command, you're not using ECS-optimized AMI.

    实例终止时.

    • Verify that ECS agent is still running (check above).
    • When using Launch Configurations, check your user data script for errors. Also, that it adds the right cluster to /etc/ecs/ecs.config ECS config file. And it starts ECS agent (start ecs).
    • Check system logs of terminated instances by navigating to EC2 Running Instances Dashboard, selecting terminated instance, Get System Log in Instance Settings (menu), then scroll down to the bottom to see any obvious issues. The logs are kept for a while after instance is terminated.
    • Check the ECS logs (tail -f /var/log/ecs/*).
    • See: Why is my Amazon ECS agent listed as disconnected?.
    • Check: How do I find the cause of an EC2 autoscaling group "health check" failure? (no load balancer involved)

    一旦实例运行了 ECS代理,请确保已将它们分配到正确的集群中.例如

    Once instances have ECS agent running, make sure you assigned them into the right cluster. E.g.

    root# cat /etc/ecs/ecs.config
    ECS_CLUSTER=demo
    

  • 请注意正在运行的EC2实例的 IAM角色,然后确保

  • Note the IAM role of the running EC2 instance, then make sure that AmazonEC2ContainerServiceforEC2Role policy is attached to that role.

    在该群集角色的 信任关系 选项卡中,确保将对EC2提供程序的访问权限授予该角色.示例角色信任策略:

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "",
          "Effect": "Allow",
          "Principal": {
            "Service": "ec2.amazonaws.com"
          },
          "Action": "sts:AssumeRole"
        }
      ]
    }
    

    Terraform示例:

    Terraform example:

    data "aws_iam_policy_document" "instance" {
      provider = "aws.auto-scale-group"
    
      statement {
        effect  = "Allow"
        actions = ["sts:AssumeRole"]
    
        principals {
          type        = "Service"
          identifiers = ["ec2.amazonaws.com"]
        }
      }
    }
    

    请参阅: IAM中AssumeRolePolicyDocument的用途是什么?.

    您还需要aws_iam_instance_profileaws_iam_role,例如

    resource "aws_iam_instance_profile" "instance" {
      provider = "aws.auto-scale-group"
      name     = "myproject-profile-instance"
      role     = "${aws_iam_role.instance.name}"
    
      lifecycle {
        create_before_destroy = true
      }
    }
    
    resource "aws_iam_role" "instance" {
      provider           = "aws.auto-scale-group"
      name               = "myproject-role"
      path               = "/"
      assume_role_policy = "${data.aws_iam_policy_document.instance.json}"
    
      lifecycle {
        create_before_destroy = true
      }
    }
    

  • 现在,您的集群应该已准备就绪.

  • Now, your cluster should be ready to go.

    相关:

    这篇关于terraform-ecs.已注册的容器实例显示为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆