Nodejs 应用程序未连接到 amazon ecs 任务定义中的 redis。 getaddrinfo ENOTFOUND redis redis:6379
Nodejs app not connecting to redis in amazon ecs task definition. getaddrinfo ENOTFOUND redis redis:6379
我正在尝试为连接到 redis 的 nodejs 应用程序创建任务定义。我已经尝试了几种不同的任务定义,但是当我 运行 它们在我的集群中时,它们都失败了,因为我的应用程序无法连接到 redis。我的任务定义、日志输出、docker-compose.yml 以及我的 api docker 文件如下所列。
任务定义
{
"ipcMode": null,
"executionRoleArn": "arn:aws:iam::210939474461:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"dnsSearchDomains": null,
"logConfiguration": {
"logDriver": "awslogs",
"secretOptions": null,
"options": {
"awslogs-group": "/ecs/rest-api-explorer",
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "ecs"
}
},
"entryPoint": null,
"portMappings": [
{
"hostPort": 0,
"protocol": "tcp",
"containerPort": 3000
},
{
"hostPort": 0,
"protocol": "tcp",
"containerPort": 6379
}
],
"command": null,
"linuxParameters": null,
"cpu": 0,
"environment": [
{
"name": "redis_server_addr",
"value": "redis"
}
],
"resourceRequirements": null,
"ulimits": null,
"dnsServers": null,
"mountPoints": [],
"workingDirectory": null,
"secrets": null,
"dockerSecurityOptions": null,
"memory": null,
"memoryReservation": 128,
"volumesFrom": [],
"stopTimeout": null,
"image": "210939474461.dkr.ecr.us-west-2.amazonaws.com/rest-api-explorer",
"startTimeout": null,
"dependsOn": [
{
"containerName": "redis",
"condition": "START"
}
],
"disableNetworking": null,
"interactive": null,
"healthCheck": null,
"essential": true,
"links": null,
"hostname": null,
"extraHosts": null,
"pseudoTerminal": null,
"user": null,
"readonlyRootFilesystem": null,
"dockerLabels": null,
"systemControls": null,
"privileged": null,
"name": "api-rest-explorer"
},
{
"dnsSearchDomains": null,
"logConfiguration": {
"logDriver": "awslogs",
"secretOptions": null,
"options": {
"awslogs-group": "/ecs/rest-api-explorer",
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "ecs"
}
},
"entryPoint": null,
"portMappings": [
{
"hostPort": 0,
"protocol": "tcp",
"containerPort": 6379
}
],
"command": null,
"linuxParameters": null,
"cpu": 0,
"environment": [],
"resourceRequirements": null,
"ulimits": null,
"dnsServers": null,
"mountPoints": [],
"workingDirectory": null,
"secrets": null,
"dockerSecurityOptions": null,
"memory": null,
"memoryReservation": null,
"volumesFrom": [],
"stopTimeout": null,
"image": "redis",
"startTimeout": null,
"dependsOn": null,
"disableNetworking": null,
"interactive": null,
"healthCheck": null,
"essential": true,
"links": null,
"hostname": null,
"extraHosts": null,
"pseudoTerminal": null,
"user": null,
"readonlyRootFilesystem": null,
"dockerLabels": null,
"systemControls": null,
"privileged": null,
"name": "redis"
}
],
"placementConstraints": [],
"memory": "1024",
"taskRoleArn": "arn:aws:iam::210939474461:role/ecsTaskExecutionRole",
"compatibilities": [
"EC2"
],
"taskDefinitionArn": "arn:aws:ecs:us-west-2:210939474461:task-definition/rest-api-explorer:8",
"family": "rest-api-explorer",
"requiresAttributes": [
{
"targetId": null,
"targetType": null,
"value": null,
"name": "ecs.capability.container-ordering"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "ecs.capability.execution-role-ecr-pull"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.ecr-auth"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.task-iam-role"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "ecs.capability.execution-role-awslogs"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.logging-driver.awslogs"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.21"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.19"
}
],
"pidMode": null,
"requiresCompatibilities": [
"EC2"
],
"networkMode": "bridge",
"cpu": "128",
"revision": 8,
"status": "ACTIVE",
"proxyConfiguration": null,
"volumes": []
}
应用容器日志
2019-06-12 16:01:06 events.js:183
2019-06-12 16:01:06 throw er; // Unhandled 'error' event
2019-06-12 16:01:06 ^
2019-06-12 16:01:06 Error: Redis connection to redis:6379 failed - getaddrinfo ENOTFOUND redis redis:6379
2019-06-12 16:01:06 at RedisClient.on_error (/var/api-console/node_modules/redis/index.js:148:24)
2019-06-12 16:01:06 at Socket.<anonymous> (/var/api-console/node_modules/redis/index.js:83:14)
2019-06-12 16:01:06 at emitOne (events.js:116:13)
2019-06-12 16:01:06 at Socket.emit (events.js:211:7)
2019-06-12 16:01:06 at emitErrorNT (internal/streams/destroy.js:66:8)
2019-06-12 16:01:06 at _combinedTickCallback (internal/process/next_tick.js:139:11)
2019-06-12 16:01:06 at process._tickCallback (internal/process/next_tick.js:181:9)
redis容器日志
2019-06-12 16:01:071:M 12 Jun 2019 23:01:07.216 * DB saved on disk
2019-06-12 16:01:071:M 12 Jun 2019 23:01:07.216 # Redis is now ready to exit, bye bye...
2019-06-12 16:01:071:M 12 Jun 2019 23:01:07.213 # User requested shutdown...
2019-06-12 16:01:071:M 12 Jun 2019 23:01:07.213 * Saving the final RDB snapshot before exiting.
2019-06-12 16:01:071:signal-handler (1560380467) Received SIGTERM scheduling shutdown...
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 * Running mode=standalone, port=6379.
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 # Server initialized
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 * Ready to accept connections
docker-compose.yml
version: '3'
services:
redis:
image: redis
expose:
- "6379"
command:
redis-server
networks:
- webnet
app:
build: ./
image: rest_api_console
ports:
- 3000:3000
depends_on:
- redis
environment:
redis_server_addr: redis
networks:
- webnet
networks:
webnet:
nodejs docker文件
# Use an official Python runtime as a parent image
FROM node:8-jessie
# Set the working directory to /var/api-console
WORKDIR /var/api-console
# Copy api-consol.tar, unpack, and install dependencies
COPY api-console.tar ./
RUN tar -xvf api-console.tar && \
npm install && \
apt-get update && \
apt-get install python
# Modify config.js to replace host:'localhost' with host:'redis'
RUN sed -i -r 's/localhost/redis/g' config.json
# Make port 3000 available to the world outside this container
EXPOSE 3000
# Run app.js when the container launches
CMD ["node", "app.js"]
AWS 支持 link 桥接网络模式,但您的任务定义似乎 link 为空。
"links": null,
您的 redis 和 nodejs 服务器应该在同一个任务定义中。
首先,在同一个任务定义中创建redis容器
然后在nodejs服务器中定义linking
{
"ipcMode": null,
"executionRoleArn": null,
"containerDefinitions": [
{
"dnsSearchDomains": null,
"logConfiguration": null,
"entryPoint": null,
"portMappings": [
{
"hostPort": 8700,
"protocol": "tcp",
"containerPort": 8700
}
],
"command": null,
"linuxParameters": null,
"cpu": 0,
"environment": [],
"resourceRequirements": null,
"ulimits": null,
"dnsServers": null,
"workingDirectory": null,
"secrets": null,
"dockerSecurityOptions": null,
"memory": 1000,
"memoryReservation": 300,
"volumesFrom": [],
"stopTimeout": null,
"image": "****.dkr.ecr.us-west-2.amazonaws.com/stage/nodejs:latest",
"startTimeout": null,
"dependsOn": null,
"disableNetworking": null,
"interactive": null,
"healthCheck": null,
"essential": true,
"links": [
"redis"
],
"hostname": null,
"extraHosts": null,
"pseudoTerminal": null,
"user": null,
"readonlyRootFilesystem": null,
"dockerLabels": {
"Name": "nodejs"
},
"systemControls": null,
"privileged": null,
"name": "nodejs"
},
{
"dnsSearchDomains": null,
"logConfiguration": null,
"entryPoint": null,
"portMappings": [
{
"hostPort": 6379,
"protocol": "tcp",
"containerPort": 6379
}
],
"command": null,
"linuxParameters": null,
"cpu": 0,
"environment": [],
"resourceRequirements": null,
"ulimits": null,
"dnsServers": null,
"mountPoints": [],
"workingDirectory": null,
"secrets": null,
"dockerSecurityOptions": null,
"memory": 600,
"memoryReservation": 200,
"volumesFrom": [],
"stopTimeout": null,
"image": "****.dkr.ecr.us-west-2.amazonaws.com/redis:latest",
"startTimeout": null,
"dependsOn": null,
"disableNetworking": null,
"interactive": null,
"healthCheck": null,
"essential": true,
"links": null,
"hostname": null,
"extraHosts": null,
"pseudoTerminal": null,
"user": null,
"readonlyRootFilesystem": null,
"dockerLabels": null,
"systemControls": null,
"privileged": null,
"name": "redis"
}
],
"memory": null,
"family": "staging-nodejs-redis",
"pidMode": null,
"requiresCompatibilities": [],
"networkMode": null,
"cpu": null,
"proxyConfiguration": null,
"placementConstraints": []
}
我正在尝试为连接到 redis 的 nodejs 应用程序创建任务定义。我已经尝试了几种不同的任务定义,但是当我 运行 它们在我的集群中时,它们都失败了,因为我的应用程序无法连接到 redis。我的任务定义、日志输出、docker-compose.yml 以及我的 api docker 文件如下所列。
任务定义
{
"ipcMode": null,
"executionRoleArn": "arn:aws:iam::210939474461:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"dnsSearchDomains": null,
"logConfiguration": {
"logDriver": "awslogs",
"secretOptions": null,
"options": {
"awslogs-group": "/ecs/rest-api-explorer",
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "ecs"
}
},
"entryPoint": null,
"portMappings": [
{
"hostPort": 0,
"protocol": "tcp",
"containerPort": 3000
},
{
"hostPort": 0,
"protocol": "tcp",
"containerPort": 6379
}
],
"command": null,
"linuxParameters": null,
"cpu": 0,
"environment": [
{
"name": "redis_server_addr",
"value": "redis"
}
],
"resourceRequirements": null,
"ulimits": null,
"dnsServers": null,
"mountPoints": [],
"workingDirectory": null,
"secrets": null,
"dockerSecurityOptions": null,
"memory": null,
"memoryReservation": 128,
"volumesFrom": [],
"stopTimeout": null,
"image": "210939474461.dkr.ecr.us-west-2.amazonaws.com/rest-api-explorer",
"startTimeout": null,
"dependsOn": [
{
"containerName": "redis",
"condition": "START"
}
],
"disableNetworking": null,
"interactive": null,
"healthCheck": null,
"essential": true,
"links": null,
"hostname": null,
"extraHosts": null,
"pseudoTerminal": null,
"user": null,
"readonlyRootFilesystem": null,
"dockerLabels": null,
"systemControls": null,
"privileged": null,
"name": "api-rest-explorer"
},
{
"dnsSearchDomains": null,
"logConfiguration": {
"logDriver": "awslogs",
"secretOptions": null,
"options": {
"awslogs-group": "/ecs/rest-api-explorer",
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "ecs"
}
},
"entryPoint": null,
"portMappings": [
{
"hostPort": 0,
"protocol": "tcp",
"containerPort": 6379
}
],
"command": null,
"linuxParameters": null,
"cpu": 0,
"environment": [],
"resourceRequirements": null,
"ulimits": null,
"dnsServers": null,
"mountPoints": [],
"workingDirectory": null,
"secrets": null,
"dockerSecurityOptions": null,
"memory": null,
"memoryReservation": null,
"volumesFrom": [],
"stopTimeout": null,
"image": "redis",
"startTimeout": null,
"dependsOn": null,
"disableNetworking": null,
"interactive": null,
"healthCheck": null,
"essential": true,
"links": null,
"hostname": null,
"extraHosts": null,
"pseudoTerminal": null,
"user": null,
"readonlyRootFilesystem": null,
"dockerLabels": null,
"systemControls": null,
"privileged": null,
"name": "redis"
}
],
"placementConstraints": [],
"memory": "1024",
"taskRoleArn": "arn:aws:iam::210939474461:role/ecsTaskExecutionRole",
"compatibilities": [
"EC2"
],
"taskDefinitionArn": "arn:aws:ecs:us-west-2:210939474461:task-definition/rest-api-explorer:8",
"family": "rest-api-explorer",
"requiresAttributes": [
{
"targetId": null,
"targetType": null,
"value": null,
"name": "ecs.capability.container-ordering"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "ecs.capability.execution-role-ecr-pull"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.ecr-auth"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.task-iam-role"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "ecs.capability.execution-role-awslogs"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.logging-driver.awslogs"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.21"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.19"
}
],
"pidMode": null,
"requiresCompatibilities": [
"EC2"
],
"networkMode": "bridge",
"cpu": "128",
"revision": 8,
"status": "ACTIVE",
"proxyConfiguration": null,
"volumes": []
}
应用容器日志
2019-06-12 16:01:06 events.js:183
2019-06-12 16:01:06 throw er; // Unhandled 'error' event
2019-06-12 16:01:06 ^
2019-06-12 16:01:06 Error: Redis connection to redis:6379 failed - getaddrinfo ENOTFOUND redis redis:6379
2019-06-12 16:01:06 at RedisClient.on_error (/var/api-console/node_modules/redis/index.js:148:24)
2019-06-12 16:01:06 at Socket.<anonymous> (/var/api-console/node_modules/redis/index.js:83:14)
2019-06-12 16:01:06 at emitOne (events.js:116:13)
2019-06-12 16:01:06 at Socket.emit (events.js:211:7)
2019-06-12 16:01:06 at emitErrorNT (internal/streams/destroy.js:66:8)
2019-06-12 16:01:06 at _combinedTickCallback (internal/process/next_tick.js:139:11)
2019-06-12 16:01:06 at process._tickCallback (internal/process/next_tick.js:181:9)
redis容器日志
2019-06-12 16:01:071:M 12 Jun 2019 23:01:07.216 * DB saved on disk
2019-06-12 16:01:071:M 12 Jun 2019 23:01:07.216 # Redis is now ready to exit, bye bye...
2019-06-12 16:01:071:M 12 Jun 2019 23:01:07.213 # User requested shutdown...
2019-06-12 16:01:071:M 12 Jun 2019 23:01:07.213 * Saving the final RDB snapshot before exiting.
2019-06-12 16:01:071:signal-handler (1560380467) Received SIGTERM scheduling shutdown...
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 * Running mode=standalone, port=6379.
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 # Server initialized
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 * Ready to accept connections
docker-compose.yml
version: '3'
services:
redis:
image: redis
expose:
- "6379"
command:
redis-server
networks:
- webnet
app:
build: ./
image: rest_api_console
ports:
- 3000:3000
depends_on:
- redis
environment:
redis_server_addr: redis
networks:
- webnet
networks:
webnet:
nodejs docker文件
# Use an official Python runtime as a parent image
FROM node:8-jessie
# Set the working directory to /var/api-console
WORKDIR /var/api-console
# Copy api-consol.tar, unpack, and install dependencies
COPY api-console.tar ./
RUN tar -xvf api-console.tar && \
npm install && \
apt-get update && \
apt-get install python
# Modify config.js to replace host:'localhost' with host:'redis'
RUN sed -i -r 's/localhost/redis/g' config.json
# Make port 3000 available to the world outside this container
EXPOSE 3000
# Run app.js when the container launches
CMD ["node", "app.js"]
AWS 支持 link 桥接网络模式,但您的任务定义似乎 link 为空。
"links": null,
您的 redis 和 nodejs 服务器应该在同一个任务定义中。
首先,在同一个任务定义中创建redis容器
然后在nodejs服务器中定义linking
{
"ipcMode": null,
"executionRoleArn": null,
"containerDefinitions": [
{
"dnsSearchDomains": null,
"logConfiguration": null,
"entryPoint": null,
"portMappings": [
{
"hostPort": 8700,
"protocol": "tcp",
"containerPort": 8700
}
],
"command": null,
"linuxParameters": null,
"cpu": 0,
"environment": [],
"resourceRequirements": null,
"ulimits": null,
"dnsServers": null,
"workingDirectory": null,
"secrets": null,
"dockerSecurityOptions": null,
"memory": 1000,
"memoryReservation": 300,
"volumesFrom": [],
"stopTimeout": null,
"image": "****.dkr.ecr.us-west-2.amazonaws.com/stage/nodejs:latest",
"startTimeout": null,
"dependsOn": null,
"disableNetworking": null,
"interactive": null,
"healthCheck": null,
"essential": true,
"links": [
"redis"
],
"hostname": null,
"extraHosts": null,
"pseudoTerminal": null,
"user": null,
"readonlyRootFilesystem": null,
"dockerLabels": {
"Name": "nodejs"
},
"systemControls": null,
"privileged": null,
"name": "nodejs"
},
{
"dnsSearchDomains": null,
"logConfiguration": null,
"entryPoint": null,
"portMappings": [
{
"hostPort": 6379,
"protocol": "tcp",
"containerPort": 6379
}
],
"command": null,
"linuxParameters": null,
"cpu": 0,
"environment": [],
"resourceRequirements": null,
"ulimits": null,
"dnsServers": null,
"mountPoints": [],
"workingDirectory": null,
"secrets": null,
"dockerSecurityOptions": null,
"memory": 600,
"memoryReservation": 200,
"volumesFrom": [],
"stopTimeout": null,
"image": "****.dkr.ecr.us-west-2.amazonaws.com/redis:latest",
"startTimeout": null,
"dependsOn": null,
"disableNetworking": null,
"interactive": null,
"healthCheck": null,
"essential": true,
"links": null,
"hostname": null,
"extraHosts": null,
"pseudoTerminal": null,
"user": null,
"readonlyRootFilesystem": null,
"dockerLabels": null,
"systemControls": null,
"privileged": null,
"name": "redis"
}
],
"memory": null,
"family": "staging-nodejs-redis",
"pidMode": null,
"requiresCompatibilities": [],
"networkMode": null,
"cpu": null,
"proxyConfiguration": null,
"placementConstraints": []
}