Skip to content

Network mode bridge - #2

Open
akmhmgc wants to merge 3 commits into
improvementfrom
network-mode-bridge
Open

akmhmgc wants to merge 3 commits into
improvementfrom
network-mode-bridge

Conversation

@akmhmgc

@akmhmgc akmhmgc commented Aug 27, 2024 •

Copy link
Copy Markdown
Owner

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

Comment thread ec2.tf
Comment on lines +43 to +44
from_port = 32768
to_port = 60999

Copy link
Copy Markdown
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

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

Comment thread ecs.tf
{
containerPort = 80
hostPort = 80
hostPort = 0

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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

Comment thread alb.tf
@@ -33,7 +33,7 @@ resource "aws_lb_target_group" "nginx_tg" {
port = 80

@akmhmgc akmhmgc Aug 28, 2024 •

Copy link
Copy Markdown
Owner Author

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/

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

Comment thread alb.tf
vpc_id = aws_vpc.main.id
target_type = "ip"
target_type = "instance"
}

Copy link
Copy Markdown
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.

1 participant