
もくじ
ECS FargateのNginxのエラーログ
CloudWatch Insightsで検索
fields @timestamp, @message | filter @message like "open" | sort @timestamp desc | limit 10000
結果
・・・
{"log":"2022/09/10 12:43:46 [alert] 35#35: *487874 socket() failed (24: Too many open files) while connecting to upstream, client: 172.xxx.xxx.xxx, server: (略)
JMeterで負荷テストかけたらエラーメッセージ発生していた
現状の暗黙のデフォルト値を確認
// ECS execがECS Serviceで有効であることが前提
タスク定義
aws ecs execute-command --profile {AWS Cli Profile名} \
--region ap-northeast-1 \
--cluster {Cluster ARN} \
--task {Task id} \
--container {コンテナ名} \
--interactive \
--command "cat /proc/sys/fs/file-max"
The Session Manager plugin was installed successfully. Use the AWS CLI to start a session.
Starting session with SessionId: ecs-execute-command-0cb31a663f90fbd49
781509
781509であることがわかった。
90%を割り振ることにする 781509 * 0.9 = 703358
タスク定義
{
"ipcMode": null,
"executionRoleArn": "arn:aws:iam::xxxxxxx:role/ecs-service-role-develop",
"containerDefinitions": [
{
・・・
"ulimits": [
{
"name": "nofile",
"softLimit": 703358,
"hardLimit": 703358
}
],
・・・
- softLimit … 閾値に達したらコンテナを落とすかもしれない値
- hardLimit … 閾値に達したらコンテナを必ず落とす値
nginx.conf
user www-data;
worker_processes auto;
error_log /dev/stderr;
pid /var/run/nginx.pid;
# cat /proc/sys/fs/file-maxの結果 781509
# 90%を割り振ることにする 781509 * 0.9 = 703358
# 703358 / 2(CPU) = 351679
worker_rlimit_nofile 351679;
events {
# worker_connections * 2 < worker_rlimit_nofile
# worker_rlimit_nofile / 2.1 = 167466.190476 ... 167466
worker_connections 167466;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
types_hash_max_size 2048;
gzip on;
server_tokens off;
# ALB Timeout対策
fastcgi_connect_timeout 120;
fastcgi_read_timeout 120;
fastcgi_send_timeout 120;
keepalive_timeout 120;
keepalive_requests 100;
client_header_timeout 60s;
client_body_timeout 60s;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
}
ワーカーは1回の処理で2つを消費するので
- 通信用のエフェメラルポート用
- ファイル
worker_rlimit_nofileを2で割ったものがworker_connectionsとなる。
worker_connections * 2 < worker_rlimit_nofileは厳守しなければならない。
ぎりぎりは怖いので2.1で割って設定
負荷をかけても「Too many open files」エラーがでなくなった🐱
やったね!
![[Solved] Nginx 空のファイルがダウンロードされる nginx empty file download](https://www.yuulinux.tokyo/contents/wp-content/uploads/2022/04/Nginx_logo.svg_-150x150.png)
![[Resolved] ResourceInitializationError: unable to pull secrets or registry auth: execution resource retrieval failed: unable to retrieve ecr registry auth: service call has been retried 3 time(s): RequestError: send request failed caused by: Post “https://api.ecr.ap-northeast-1.amazonaws.com/”: dial tcp 52.119.218.165:443: i/o timeout](https://www.yuulinux.tokyo/contents/wp-content/uploads/2017/04/aws_logo_smile_1200x630-150x150.png)

![【Soved】Nginx [crit] 20655#20655: *56 open() “/var/log/nginx/xxxx.access.log” failed (13: Permission denied) while logging request](https://www.yuulinux.tokyo/contents/wp-content/uploads/2020/06/linux_2021_2-150x150.jpg)