[AWS] [container] [ECS] container hands on experiment 201

Posted by PAZII on Fri, 19 Jun 2020 09:32:48 +0200

Before the experiment, we should first understand the overall ECS architecture of this experiment:

The experiments include:

1. Create and define ECS tasks

2. Create ECS Cluster

3. Deploy and apply to ECS Service

4. Update the application through Task Definition

5. Container extension

Preparation before experiment:

  1. AWS account
  2. Familiar with IAM role, EC2, Docker and other knowledge

Some key components of Amazon ECS:

  1. Container Instance: EC2 instance, including ECS agent and registered in ECs cluster.
  2. ECS Fargate: no need to manage and configure EC2 instance, fully Managed container arrangement service (not involved in this experiment)
  3. Cluster: a cluster of multiple container instances. In the cluster, you can define how each Task is placed.
  4. Task Definition: defines the Docker Container in ECS. For example, the image of Docker in the Container, CPU and memory quantity of each Container in each task (Fargate only needs to define the memory of the Container), Docker networking mode, IAM role, etc.
  5. Task: the smallest management unit of ECS, which can be understood as the Pod in kubernets. Contains one or more containers.
  6. Scheduler: service scheduler defines the number of task s to run and defines the service schedule policy:
    REPLICA - REPLICA scheduling policy: by default, task s are distributed among multiple availability zones. Placement policies can be specified.
    DAEMON -- DAEMON scheduling policy: no need to specify the number of tasks and placement policy. Only one task is deployed on a Container instance.
  7. Service: This is similar to the service definition in kubernets. It can be understood that it is composed of multiple tasks. You can define where a group of tasks run in a Container Instance. It can be accessed through ELB.

Task0: create Cluster

Task1: configure ecs sample image image image in Task Definition

Pull down the bottom and configure TASK through JSON (usually through the parameter selection of the console):

The following content is the TASK defined by JSON. Note here that containerPath is the path of the local Instance that I write. Usually, we replace it with the path of the docker hub or ECR.

{
  "family": "yourApp-demo",
  "containerDefinitions": [
    {
      "volumesFrom": [],
      "portMappings": [
        {
          "hostPort": 80,
          "containerPort": 80
        }
      ],
      "command": null,
      "environment": [],
      "essential": true,
      "entryPoint": null,
      "links": [],
      "mountPoints": [
        {
          "containerPath": "/usr/local/apache2/htdocs",
          "sourceVolume": "my-vol",
          "readOnly": null
        }
      ],
      "memory": 300,
      "name": "simple-app",
      "cpu": 10,
      "image": "httpd:2.4"
    },
    {
      "volumesFrom": [
        {
          "readOnly": null,
          "sourceContainer": "simple-app"
        }
      ],
      "portMappings": [],
      "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\""
      ],
      "environment": [],
      "essential": false,
      "entryPoint": [
        "sh",
        "-c"
      ],
      "links": [],
      "mountPoints": [],
      "memory": 200,
      "name": "busybox",
      "cpu": 10,
      "image": "busybox"
    }
  ],
  "volumes": [
    {
      "host": {
        "sourcePath": null
      },
      "name": "my-vol"
    }
  ]
}

Go back and see that most of the parameters have been defined,

Task 2: create a Service

Configure services:

Configure network:

Set Auto Scaling:

Test container service:

Task 3: publish a new application

Copy the following JSON to Configure via JSON,

{
  "family": "yourApp-demo",
  "containerDefinitions": [
    {
      "volumesFrom": [],
      "portMappings": [
        {
          "hostPort": 80,
          "containerPort": 80
        }
      ],
      "command": null,
      "environment": [],
      "essential": true,
      "entryPoint": null,
      "links": [],
      "mountPoints": [
        {
          "containerPath": "/usr/local/apache2/htdocs",
          "sourceVolume": "my-vol",
          "readOnly": null
        }
      ],
      "memory": 300,
      "name": "simple-app",
      "cpu": 10,
      "image": "httpd:2.4"
    },
    {
      "volumesFrom": [
        {
          "readOnly": null,
          "sourceContainer": "simple-app"
        }
      ],
      "portMappings": [],
      "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>Thank You!</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\""
      ],
      "environment": [],
      "essential": false,
      "entryPoint": [
        "sh",
        "-c"
      ],
      "links": [],
      "mountPoints": [],
      "memory": 200,
      "name": "busybox",
      "cpu": 10,
      "image": "busybox"
    }
  ],
  "volumes": [
    {
      "host": {
        "sourcePath": null
      },
      "name": "my-vol"
    }
  ]
}

Select the updated version 5, and keep the others default.

Task 4: container extension

Increase the number of tasks:

For detailed video reference of AWS Cloud Computing: https://edu.51cto.com/course/22206.html

Topics: Docker JSON AWS network