Skip to content

Conversation

@akmhmgc
Copy link
Owner

@akmhmgc akmhmgc commented Aug 27, 2024

Amazon ECS タスク定義のネットワークモードをbridgeにした

Comment on lines +43 to +44
from_port = 32768
to_port = 60999
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ロードバランサーからコンテナインスタンスへのインバウンドトラフィックを受け入れるルールを追加します。セキュリティグループとネットワークアクセスコントロールリスト (ネットワーク ACL) では、ロードバランサーからインスタンスへのトラフィックをエフェメラルポート範囲で受け入れる必要があります。
https://repost.aws/ja/knowledge-center/dynamic-port-mapping-ecs

上記のように、受け入れる必要があるのはエフェメラルポートだけで十分

多くの Linux カーネル (Amazon Linux カーネルを含む) は、ポート 32768~61000 を使用します。
https://docs.aws.amazon.com/ja_jp/vpc/latest/userguide/nacl-ephemeral-ports.html

とあるが、

sysctl net.ipv4.ip_local_port_range

でエフェメラルポートは確認可能

{
containerPort = 80
hostPort = 80
hostPort = 0
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

動的ポートマッピングによってホスト側のエフェメラルポートを動的に割り当ててくれる


resource "aws_lb_target_group" "nginx_tg" {
name = "nginx-tg"
port = 80
Copy link
Owner Author

@akmhmgc akmhmgc Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

動的ポートマッピングの場合ecs-service-schedulerというユーザーが動的にターゲットグループ割り当て時にポートを決定しているのでここのポート指定は結局上書きされるので、ターゲットグループ作成時のポートは実はなんでも良い

image image

ref: https://dev.classmethod.jp/articles/dynamic-portmapping-elb-ecs/

デフォルトでは、ロードバランサーはターゲットグループの作成時に指定したプロトコルとポート番号を使用して、リクエストをターゲットにルーティングします。または、ターゲットグループへの登録時にターゲットへのトラフィックのルーティングに使用されるポートを上書きすることもできます。
ルーティング設定

vpc_id = aws_vpc.main.id
target_type = "ip"
target_type = "instance"
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bridge modeの場合インスタンスとポート番号さえわかれば良い
一方で、awsvpcの場合タスクごとでENIが異なるのでインスタンスとポート番号だけだとリクエストの送り先が定まらない

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants