AWS ECS 容器未连接但在我的本地机器上运行良好
AWS ECS containers are not connecting but works perfectly in my local machine
我有一个应用程序(运行s 在 http://localhost:8080) that talks to a backend api which runs at http://localhost:8081. I have dockerized the frontend and the backend separately and running them through docker-compose locally works perfectly without any issues. But, when I run it in ECS, the frontend couldn't find http://localhost:8081(backend)。
我将 AutoScaling 组与 Elastic Load Balancer 一起使用,并且我在单个任务定义中定义了两个容器。另外,我将后端链接到前端。当我 ssh 进入我的 ECS 实例和 运行 docker ps -a
时,我可以看到我的两个容器都 运行ning 在正确的端口上,就像在我的本地机器上一样(Result of docker ps -a)和我可以成功地将它们中的每一个从一个容器 ping 到另一个容器。
任务定义
"CartTaskDefinition": {
"Type": "AWS::ECS::TaskDefinition",
"Properties": {
"ContainerDefinitions": [
{
"Name": "cs-cart",
"Image": "thishandp7/cs-cart",
"Memory": 400,
"PortMappings":[
{
"ContainerPort": "8080",
"HostPort": "8080"
}
],
"Links": [
"cs-server"
]
},
{
"Name": "cs-server",
"Image": "thishandp7/cs-server",
"Memory": 450,
"PortMappings":[
{
"ContainerPort": "8081",
"HostPort": "8081"
}
],
}
]
}
}
我的 ElasticLoadBalancer 中的监听器,
第一个监听器用于前端,第二个监听器用于后端
"Listeners" : [
{
"LoadBalancerPort": 80,
"InstancePort": 8080,
"Protocol": "http"
},
{
"LoadBalancerPort": 8081,
"InstancePort": 8081,
"Protocol": "tcp"
}
],
EC2 instacne 安全组入口规则:
"SecurityGroupIngress" : [
{
"IpProtocol" : "tcp",
"FromPort" : 8080,
"ToPort" : 8080,
"SourceSecurityGroupId" : { "Ref": "ElbSecurityGroup" }
},
{
"IpProtocol" : "tcp",
"FromPort" : 8081,
"ToPort" : 8081,
"SourceSecurityGroupId" : { "Ref": "ElbSecurityGroup" }
},
{
"IpProtocol" : "tcp",
"FromPort" : 22,
"ToPort" : 22,
"CidrIp" : "0.0.0.0/0"
}
],
Docker撰写
version: "3.5"
services:
cart:
build:
context: ..
dockerfile: docker/Dockerfile
args:
APP_LOCATION: /redux-saga-cart/
PORT: 8080
networks:
- server-cart
ports:
- 8080:8080
depends_on:
- server
server:
build:
context: ..
dockerfile: docker/Dockerfile
args:
APP_LOCATION: /redux-saga-shopping-cart-server/
PORT: 8081
ports:
- 8081:8081
networks:
- server-cart
networks:
server-cart:
快速更新:我已经尝试过使用带有应用程序负载均衡器的 awsvpc 网络模式。仍然无法正常工作
提前致谢。
您在 ECS 上使用哪种 Docker 网络模式 (Brdige/Host)?。我认为 localhost 无法在 ECS 容器上正常工作。我遇到了同样的问题,所以我使用 EC2 主机的私有 IP 或 DNS 名称进行通信作为临时测试目的。例如 - http://10.0.1.100:8081.
注意 - 请确保提供安全组规则以允许来自 EC2 内部的 8081 流量(编辑 EC2 安全组以允许来自与源相同的 sgid 的 8081)。
对于生产部署,我建议使用服务发现来识别后端服务(Hashicorp 的 Consul)或 ECS 上的 AWS 私有服务发现。
-- 更新--
由于您 运行 两个容器都在同一任务定义下(在同一 ECS 服务下),因此通常 ECS 会将两个 docker 容器置于同一主机上。执行如下操作。
- 默认情况下,ECS 在 Linux.
上使用桥接模式引入容器
- 您应该能够让每个容器使用 Docker 网关 IP - 172.17.0.1 在 Linux 上进行通信。因此,对于您的情况,请尝试配置 http://172.17.0.1:8081
我有一个应用程序(运行s 在 http://localhost:8080) that talks to a backend api which runs at http://localhost:8081. I have dockerized the frontend and the backend separately and running them through docker-compose locally works perfectly without any issues. But, when I run it in ECS, the frontend couldn't find http://localhost:8081(backend)。
我将 AutoScaling 组与 Elastic Load Balancer 一起使用,并且我在单个任务定义中定义了两个容器。另外,我将后端链接到前端。当我 ssh 进入我的 ECS 实例和 运行 docker ps -a
时,我可以看到我的两个容器都 运行ning 在正确的端口上,就像在我的本地机器上一样(Result of docker ps -a)和我可以成功地将它们中的每一个从一个容器 ping 到另一个容器。
任务定义
"CartTaskDefinition": {
"Type": "AWS::ECS::TaskDefinition",
"Properties": {
"ContainerDefinitions": [
{
"Name": "cs-cart",
"Image": "thishandp7/cs-cart",
"Memory": 400,
"PortMappings":[
{
"ContainerPort": "8080",
"HostPort": "8080"
}
],
"Links": [
"cs-server"
]
},
{
"Name": "cs-server",
"Image": "thishandp7/cs-server",
"Memory": 450,
"PortMappings":[
{
"ContainerPort": "8081",
"HostPort": "8081"
}
],
}
]
}
}
我的 ElasticLoadBalancer 中的监听器, 第一个监听器用于前端,第二个监听器用于后端
"Listeners" : [
{
"LoadBalancerPort": 80,
"InstancePort": 8080,
"Protocol": "http"
},
{
"LoadBalancerPort": 8081,
"InstancePort": 8081,
"Protocol": "tcp"
}
],
EC2 instacne 安全组入口规则:
"SecurityGroupIngress" : [
{
"IpProtocol" : "tcp",
"FromPort" : 8080,
"ToPort" : 8080,
"SourceSecurityGroupId" : { "Ref": "ElbSecurityGroup" }
},
{
"IpProtocol" : "tcp",
"FromPort" : 8081,
"ToPort" : 8081,
"SourceSecurityGroupId" : { "Ref": "ElbSecurityGroup" }
},
{
"IpProtocol" : "tcp",
"FromPort" : 22,
"ToPort" : 22,
"CidrIp" : "0.0.0.0/0"
}
],
Docker撰写
version: "3.5"
services:
cart:
build:
context: ..
dockerfile: docker/Dockerfile
args:
APP_LOCATION: /redux-saga-cart/
PORT: 8080
networks:
- server-cart
ports:
- 8080:8080
depends_on:
- server
server:
build:
context: ..
dockerfile: docker/Dockerfile
args:
APP_LOCATION: /redux-saga-shopping-cart-server/
PORT: 8081
ports:
- 8081:8081
networks:
- server-cart
networks:
server-cart:
快速更新:我已经尝试过使用带有应用程序负载均衡器的 awsvpc 网络模式。仍然无法正常工作
提前致谢。
您在 ECS 上使用哪种 Docker 网络模式 (Brdige/Host)?。我认为 localhost 无法在 ECS 容器上正常工作。我遇到了同样的问题,所以我使用 EC2 主机的私有 IP 或 DNS 名称进行通信作为临时测试目的。例如 - http://10.0.1.100:8081.
注意 - 请确保提供安全组规则以允许来自 EC2 内部的 8081 流量(编辑 EC2 安全组以允许来自与源相同的 sgid 的 8081)。
对于生产部署,我建议使用服务发现来识别后端服务(Hashicorp 的 Consul)或 ECS 上的 AWS 私有服务发现。
-- 更新--
由于您 运行 两个容器都在同一任务定义下(在同一 ECS 服务下),因此通常 ECS 会将两个 docker 容器置于同一主机上。执行如下操作。
- 默认情况下,ECS 在 Linux. 上使用桥接模式引入容器
- 您应该能够让每个容器使用 Docker 网关 IP - 172.17.0.1 在 Linux 上进行通信。因此,对于您的情况,请尝试配置 http://172.17.0.1:8081