--Packing nginx + express into fargate --Connect using unix socket --express launches 4 processes --Because the pattern that node cluster is not used, socket conflicts. --Set api1.sock on common volume --api4.sock and upstream from nginx
api
VOLUME /usr/src/tmp
main.js
const sock = process.env.SOCK_PATH;
app.listen(sock, () => {
fs.chmodSync(sock, '666');
console.log(`Server running on ${sock} with pid ` + process.pid);
});
nginx
api1.sock --upstream to api4.sock
nginx.conf
upstream api {
server unix:///usr/src/tmp/api_1.sock;
server unix:///usr/src/tmp/api_2.sock;
server unix:///usr/src/tmp/api_3.sock;
server unix:///usr/src/tmp/api_4.sock;
}
server {
listen 80 default;
listen [::]:80;
location / {
proxy_pass http://api;
break;
}
}
Connect with ECS task definition
task-define.json
{
"containerDefinitions": [
{
"name": "nginx",
"portMappings": [
{ "hostPort": 80, "protocol": "tcp", "containerPort": 80 }
],
"mountPoints": [
{
"readOnly": true,
"containerPath": "/usr/src/tmp",
"sourceVolume": "etc"
}
]
},
{
"name": "api1",
"mountPoints": [
{ "containerPath": "/usr/src/tmp", "sourceVolume": "etc" }
],
"environment": [
{ "name": "SOCK_PATH", "value": "/usr/src/tmp/api_1.sock" }
]
},
{
"name": "api2",
"mountPoints": [
{ "containerPath": "/usr/src/tmp", "sourceVolume": "etc" }
],
"environment": [
{ "name": "SOCK_PATH", "value": "/usr/src/tmp/api_2.sock" }
]
},
{
"name": "api3",
"mountPoints": [
{ "containerPath": "/usr/src/tmp", "sourceVolume": "etc" }
],
"environment": [
{ "name": "SOCK_PATH", "value": "/usr/src/tmp/api_3.sock" }
]
},
{
"name": "api4",
"mountPoints": [
{ "containerPath": "/usr/src/tmp", "sourceVolume": "etc" }
],
"environment": [
{ "name": "SOCK_PATH", "value": "/usr/src/tmp/api_4.sock" }
]
}
],
"volumes": [{ "name": "etc" }],
}
Recommended Posts