2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ECSタスクでリバースプロキシを利用する

Last updated at Posted at 2024-05-31

ECSタスクでリバースプロキシを利用する方法

ECSタスクでリバースプロキシを利用し、後ろに控えるアプリケーションサーバーへトラフィックをルーティングします。

今回構成するのはDockerComposeで書くとこんな構成

version: '3.8'

services:
  nginx:
    image: public.ecr.aws/nginx/nginx:1.26
    ports:
      - 80:80
    volumes:
      - type: bind
        source: ./nginx
        target: /etc/nginx/conf.d
    depends_on:
      - app
  app:
    image: app-image:latest

参考ページ

1ー ECRにコンテナイメージを作成

  • nginx
    必要ない部分を削除し、ポートをアプリケーションサーバーに合わせる
nginx.conf
events {
  worker_connections 768;
}

http {
  # Nginx will handle gzip compression of responses from the app server
  gzip on;
  gzip_proxied any;
  gzip_types text/plain application/json;
  gzip_min_length 1000;

-  server{
-    listen 8080;
-    location /stub_status {
-        stub_status   on;
-    }
-  }

  server {
    listen 80;

    # Nginx will reject anything not matching /api
    location /api {
      # Reject requests with unsupported HTTP method
      if ($request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|DELETE)$) {
        return 405;
      }

      # Only requests matching the whitelist expectations will
      # get sent to the application server
-       proxy_pass http://app:3000;
+       proxy_pass http://app:80/;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_cache_bypass $http_upgrade;
    }
  }
}

nginxイメージをECRのパブリックギャラリーから取得するように変更

Dockerfile
- FROM nginx
+ FROM public.ecr.aws/nginx/nginx:1.26
COPY nginx.conf /etc/nginx/nginx.conf
  • アプリケーションサーバー
    任意のアプリケーションサーバーのコンテナイメージを構築

2ータスク定義作成

JSONだとこんな感じ

タスク定義
{
    "taskDefinitionArn": "{タスク定義のARN}",
    "containerDefinitions": [
        {
            "name": "nginx",
            "image": "{アカウントID}.dkr.ecr.{リージョン}.amazonaws.com/nginx:latest",
            "cpu": 0,
            "links": [
                "app"
            ],
            "portMappings": [
                {
                    "name": "nginx-80-tcp",
                    "containerPort": 80,
                    "hostPort": 0,
                    "protocol": "tcp"
                }
            ],
            "essential": true,
            "environment": [],
            "environmentFiles": [],
            "mountPoints": [],
            "volumesFrom": [],
            "ulimits": [],
            "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-create-group": "true",
                    "awslogs-group": "/ecs/{タスク定義名}",
                    "awslogs-region": "{リージョン}",
                    "awslogs-stream-prefix": "ecs"
                },
                "secretOptions": []
            },
            "systemControls": []
        },
        {
            "name": "app",
            "image": "{アカウントID}.dkr.ecr.{リージョン}.amazonaws.com/app:latest",
            "cpu": 0,
            "portMappings": [],
            "essential": false,
            "environment": [],
            "environmentFiles": [],
            "mountPoints": [],
            "volumesFrom": [],
            "systemControls": []
        }
    ],
    "family": "{タスク定義名}",
    "taskRoleArn": "{タスクロールのARN}",
    "executionRoleArn": "{タスク実行ロールのARN}",
    "networkMode": "bridge",
    "revision": 1,
    "volumes": [],
    "status": "ACTIVE",
    "requiresAttributes": [
        {
            "name": "com.amazonaws.ecs.capability.logging-driver.awslogs"
        },
        {
            "name": "ecs.capability.execution-role-awslogs"
        },
        {
            "name": "com.amazonaws.ecs.capability.ecr-auth"
        },
        {
            "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19"
        },
        {
            "name": "com.amazonaws.ecs.capability.task-iam-role"
        },
        {
            "name": "ecs.capability.execution-role-ecr-pull"
        },
        {
            "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18"
        },
        {
            "name": "com.amazonaws.ecs.capability.docker-remote-api.1.29"
        }
    ],
    "placementConstraints": [],
    "compatibilities": [
        "EC2"
    ],
    "requiresCompatibilities": [
        "EC2"
    ],
    "cpu": "1024",
    "memory": "3072",
    "runtimePlatform": {
        "cpuArchitecture": "X86_64",
        "operatingSystemFamily": "LINUX"
    },
    "registeredAt": "",
    "registeredBy": "",
    "tags": []
}
  • 重要点
    • ネットワークモードはbridge
      image.png
    • リンクにコンテナ名を選択
      image.png
      手動で作成する場合はコンテナ - 2を定義するとリンクにコンテナを選択できるようになります

3ークラスター(ECS)を作成し、タスクをデプロイ

クラスターを作成し、タスクをデプロイします。
ALBを作成してルーティングしてもOK

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?