Tìm hiểu về aws ecs

  • September 14, 2022
  • 562

AWS ECS là gì?

Amazon Elastic Container Service(ECS) là một service quản lý container có tính mở rộng cao và nhanh. Dễ dàng run, stop, hay quản lý docker container ở trong một cluster. Bạn có thể host một serverless infrastructure bằng cách chạy service hay task sử dụng Fragate launch type.

Các thành phần của ECS

  • ECR: Elastic Container Registry tương tự như docker hub, là nơi chứa các docker image. Các image sẽ được build sau đó push lên ECR
  • Task definition: Tương tự như 1 file docker-compose.yml. Là nơi định nghĩa các service container sẽ hoạt động trong hệ thống ví dụ: nginx, php-fpm,...
  • Clusters: Là nơi để chạy các service
  • Service: Là nơi để chạy các task definition, config networking, load balancer, thiết lập số lượng các instance sẽ được khởi tạo
Một cluster có thể chạy nhiều service, mỗi một service có thể run nhiều task, các task được tạo ra dựa trên task definition(nơi khai báo xem 1 task có bao nhiêu container). Khi task được chạy các container được tạo ra dựa và task defination.

Tạo 1 ECS chạy Laravel project

Tạo 1 ECS chạy laravel project, để làm được việc này bạn cần đọc thêm docker compose và cài đặt AWS CLI. Để tạo 1 ecs chạy 1 project laravel chúng t sẽ cần làm lần lượt các bước sau

1. Push image to ECR

Để đẩy được image lên ECR chúng ta cần cài đặt AWS CLI. Sau đó đăng nhập ECR với AWS CLI


# Login AWS ECR sử dụng profile
aws ecr get-login-password --region [region] --profile [profile] | docker login --username AWS --password-stdin [account-id].dkr.ecr.[region].amazonaws.com

# Login AWS ECR không sử dụng profile
aws ecr get-login-password --region [region] | docker login --username AWS --password-stdin [account-id].dkr.ecr.[region].amazonaws.com

# Login success
➜  ~ aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 056663857946.dkr.ecr.us-east-1.amazonaws.com

Login Succeeded

Tạo ECR bằng AWL CLI sử dụng command sau.


// Create image laravel fpm
aws ecr create-repository --repository-name laravel-fpm --region us-east-1

{ "repository": { "repositoryArn": "arn:aws:ecr:us-east-1:056663857946:repository/laravel-fpm", "registryId": "056663857946", "repositoryName": "laravel-fpm", "repositoryUri": "056663857946.dkr.ecr.us-east-1.amazonaws.com/laravel-fpm", "createdAt": "2022-09-15T21:32:46+07:00", "imageTagMutability": "MUTABLE", "imageScanningConfiguration": { "scanOnPush": false }, "encryptionConfiguration": { "encryptionType": "AES256" } } } // Create image nginx aws ecr create-repository --repository-name laravel-nginx --region us-east-1

{ "repository": { "repositoryArn": "arn:aws:ecr:us-east-1:056663857946:repository/laravel-nginx", "registryId": "056663857946", "repositoryName": "laravel-nginx", "repositoryUri": "056663857946.dkr.ecr.us-east-1.amazonaws.com/laravel-nginx", "createdAt": "2022-09-15T21:34:16+07:00", "imageTagMutability": "MUTABLE", "imageScanningConfiguration": { "scanOnPush": false }, "encryptionConfiguration": { "encryptionType": "AES256" } } }

Hoặc tạo ECR bằng cách đăng nhập AWS console.
Create ECR
Kiểm tra ECR được tạo bằng cách đăng nhập aws console
Create ecr success
Chú ý: URI của image được dùng để push image lên ECR repo, khai báo image trong task definitions

Build docker image để đẩy lên ecr, khai báo image:[repositoryUri] vào trong file docker-compose.yml


version: '3.4'

networks:
  backend:
    driver: bridge

services:
  nginx:
    image: 056663857946.dkr.ecr.us-east-1.amazonaws.com/laravel-nginx
    build:
      context: .
      dockerfile: docker/app/Dockerfile.nginx
      args:
          PHP_FPM_SERVER: app
          PHP_FPM_PORT: 9000
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./docker/nginx/conf.d:/etc/nginx/conf.d
      - .:/var/www/
    links:
      - app
    depends_on:
      - app
    networks:
      - backend

  app:
    image: 056663857946.dkr.ecr.us-east-1.amazonaws.com/laravel-fpm
    build:
      context: .
      dockerfile: docker/app/Dockerfile
    volumes:
      - .:/var/www/
    networks:
      - backend
volumes:
  db-data:

Build image với docker-compose


# Build Laravel-fpm
docker-compose build app

# Push image lên ECR
ocker-compose push app

 # Build nginx
docker-compose build nginx

# Push image lên ECR
ocker-compose push nginx

# Build, push image with docker
docker build -t laravel-nginx -f docker/app/Dockerfile.nginx .
docker tag laravel-nginx:latest [account-id].dkr.ecr.[region].amazonaws.com/laravel-nginx:latest
docker push [account-id].dkr.ecr.[region].amazonaws.com/laravel-nginx:latest

Như vậy là chúng ta đã tạo và push được image lên ECR.

2. Create Task Definitions

Tạo Task Definitions khai báo các container sẽ sử dụng, mình sẽ thiết lập 2 container là laravel-fpm và laravel-nginx.
Tạo task definitions, ở ví dụ demo này mình chọn launch type compatibility là EC2.
Task definition type
Create aws task definitions
Thêm laravel-fpm vào task definition
Add laravel container to task
Định nghĩa tên container, image là repo uri trong ECR, thiết lập memory, mapping post giữa host và container, dùng php-fpm nên post là 9000.

Thêm laravel-nginx vào task definition
Add nginx container to task
Thiết lập container name, image, memory, mapping post cho nginx là 80, setting thêm networking giữa laravel-nginx container và laravel-fpm(container:service_name).
Setting networking
Thiết lập CloudWatch Logs
ECS cloudWatch log

Như vậy là đã thiết lập xong task definition, ấn create để tạo.
Create task definition success

3. Create Clusters

Tiếp theo cần create clusters
Create cluster
Chọn cluster type là EC2
EC2 cluster
Thiết lập cluster, chọn instance type là t2.micro, bỏ qua load balancer, auto scale, để giảm chi phí
Define cluster
Create cluster success, các service aws trong hình bên dưới cũng được thiết lập.

Create cluster success

4. Create service

Tạo service chạy task definition
Create service
Thiết lập service
Setting service ecs
Kiểm tra service có chạy thành công không? Khi service chạy thành công tất cả các aws service liên quan sẽ được tạo.
Run service ecs success
Kiểm tra instance được tạo. Nếu bạn muốn access vào ec2 check các container run, cần thêm security group cho phép access qua post 22 từ bên ngoài, tham khảo bài viết về Aws ec2
Check ec2 instance
Kiêm tra laravel project chạy thành công
Deploy ecs success
Như vậy là chúng ta đang deploy 1 project laravel thành công sử dụng ecs.
Để xoá ECS vừa tạo, chúng ta cần xoá bỏ service, cluster, task definition. Khi ecs được xoá, tất cả các aws service liên quan cũng được xoá.
Remove cluster
Remove cluster
 Remove task definition
Remove task definition
Như vậy chúng ta đã deploy được project laravel sử dụng aws ecs. Ngoài ra để tìm hiểu thêm về aws ecs chúng ta sẽ đọc document của aws ecs trên aws.