Как запустить несколько служб на экземпляре контейнера EC2 и иметь целевые показатели работоспособности для проверок работоспособности? - PullRequest
0 голосов
/ 29 января 2020

У меня необычная проблема (возможно, простое решение, но я несколько раз безуспешно пытался).

У меня есть 2 задачи, выполняющиеся на экземпляре контейнера EC2 в кластере ECS. Task 1 is a DAEMON exposing port X Task 2 is an application exposing port Y Они оба бегут. Когда я проверяю статус проверки работоспособности для экземпляра EC2, он показывает 2 записи для него. Entry 1 is Healthy for the DAEMON health check port at port X Entry 2 is Draining for the Application health check port at port Y

Я бы хотел одну запись (for the application health check port Y) для экземпляра EC2 и состояние работоспособности для него.

Как я могу выполнить это sh, запустив на нем 2 задачи. Разве я не должен выставлять порт Health Check для задачи DAEMON?

РЕДАКТИРОВАТЬ: Конфигурация балансировщика нагрузки и целевой группы -

resource "aws_alb" "alb" {
  name            = "${var.alb_name}"
  subnets         = flatten(["${var.public_subnet_ids}"])
  security_groups = ["${aws_security_group.alb.id}"]

  access_logs {
    bucket  = "${aws_s3_bucket.alb-logs.bucket}"
    prefix  = "load-balancer-logs"
    enabled = true
  }

  tags = {
    Environment = "${var.environment}"
  }
}

resource "aws_alb_target_group" "lb_target_group_app" {
  name                 = "${var.alb_name}-default"
  /* port                 = 5555 */  /* DAMEON health check port */
  port                 = 5000 .   /* Application health check port */
  protocol             = "HTTP"
  vpc_id               = "${var.vpc_id}"
  deregistration_delay = "${var.deregistration_delay}"

  health_check {
    path     = "${var.health_check_path}"
    port = "5000"        /* Is this needed here ?  */
    interval = "300"
    timeout = "120"
  }

  lifecycle {
    create_before_destroy = true
  }

  depends_on = ["aws_alb.alb"]
}

resource "aws_alb_listener" "https_listener" {
  load_balancer_arn = "${aws_alb.alb.id}"
  port              = "443"
  protocol          = "HTTPS"
  ssl_policy        = "ELBSecurityPolicy-2016-08"
  certificate_arn = "${aws_acm_certificate.ssl_cert.arn}"

  default_action {
    target_group_arn = "${aws_alb_target_group.lb_target_group_app.arn}"
    type             = "forward"
  }
}

Container definition for dameon - 

  container_definitions = <<EOF
[
  {
    "name": "${var.environment}-${var.datadog-identifier}",
    "image": "datadog/agent:latest",
    "portMappings": [
      {
        "containerPort": 8126,
        "hostPort": 8126,
        "protocol": "TCP"
      },
      {
        "containerPort": 5555,
        "hostPort": 5555,
        "protocol": "TCP"
      },
      {
        "containerPort": 8125,
        "hostPort": 8125,
        "protocol": "UDP"
      }
    ],
    "environment": [
      { "name" : "DD_API_KEY", "value" : "xxxxxxxxx" },
      { "name" : "DD_APM_NON_LOCAL_TRAFFIC", "value" : "true" },
      { "name" : "DD_DOGSTATSD_NON_LOCAL_TRAFFIC", "value" : "true" },
      { "name": "DD_LOG_LEVEL", "value": "trace" },
      { "name": "DD_SITE", "value": "datadoghq.com" },
      { "name": "DD_HEALTH_PORT", "value": "5555" },
      { "name" : "DD_PROCESS_AGENT_ENABLED", "value" : "true" },
      { "name" : "DD_LOGS_ENABLED", "value" : "true" },
      { "name" : "DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL", "value" : "true" },
      { "name" : "DD_APM_ENABLED", "value" : "true" }
    ],

Container definition for app - 

    container_definitions = <<DEFINITION
[
  {
    "name": "app",
    "image": "quay.io/xxxxxxxx",
    "essential": true,
    "logConfiguration": {
      "logDriver": "awslogs",
      "options": {
        "awslogs-group": "/ec2/service/${var.cluster}",
        "awslogs-region": "us-west-2",
        "awslogs-stream-prefix": "ec2"
      }
    },
    "environment": [
      {
        "name": "APP_ENV",
        "value": "localhost"
      },
      {
        "name": "AUTH_JWT_SECRET",
        "value": "xxxxxxxx"
      }
    ],
    "portMappings": [
      {
        "containerPort": 5000,
        "hostPort": 5000,
        "protocol": "tcp"
      }
    ],

Service definition for dameon task - 
resource "aws_ecs_service" "datadog" {
  name            = "${var.environment}-${var.datadog-identifier}-datadog-ecs-service"
  cluster         = "${var.cluster}"
  task_definition = "${aws_ecs_task_definition.datadog.arn}"


  load_balancer {
    target_group_arn = "${element(var.default_alb_target_group,0)}"
    container_name   = "${var.environment}-${var.datadog-identifier}"
    container_port   = 5555 .   /* datadog health check port */
  }
  # This allows running one for every instance
  scheduling_strategy = "DAEMON"
}



Service definition for app task - 
resource "aws_ecs_service" "app-service" {
        name            = "app-service"
        cluster         = "${var.cluster}"
        task_definition = "${aws_ecs_task_definition.app.arn}"
        desired_count   = 2
        launch_type    = "EC2"

        load_balancer {
          target_group_arn  = "${element(var.default_alb_target_group,0)}"
          container_port    = 5000  /* app port exposed here */
          container_name    = "pared-somm"
        }



Воспроизвести это можно следующим образом -

1. Create ALB as below
2. Create 1 Target Group as below with a port to listen on. Add health check block to the Target Group (is port needed here or should I use Traffic port instead ?)
3. Create 2 tasks and service definitions (container definitions below) both pointing to the load balancer above. So when the tasks start running, they both run on the same targets in the above target group. 
4. Check the Targets and their health and you should see 2 entries per target. 
...