连接挂起并且不能通过端口9200在ec2实例之间连接; 快速从AWS以外

我正在尝试从Web服务器EC2实例连接到ElasticSearch服务器ec2实例。 从EC2连接时连接速度很慢,但从普通计算机(不在AWS内部)连接时速度非常快。

如果我从笔记本电脑发出请求,速度很快:

laptop:~ jordan$ time curl -vvv search.example.org:9200 * About to connect() to search.example.org port 9200 (#0) * Trying 1.2.3.4... connected * Connected to search.example.org (1.2.3.4) port 9200 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8y zlib/1.2.3 > Host: search.example.org:9200 > Accept: */* > < HTTP/1.1 200 OK < Content-Type: application/json; charset=UTF-8 < Content-Length: 294 < … snip … * Connection #0 to host search.example.org left intact * Closing connection #0 real 0m0.071s user 0m0.004s sys 0m0.005s laptop:~ jordan$ 

从EC2实例中,请求首先从负载平衡器中尝试一个实例:

 [jordan@ip-5-6-7-8 ~]$ time curl -vvv search.example.org:9200 * Rebuilt URL to: search.example.org:9200/ * Hostname was NOT found in DNS cache * Trying 1.2.3.4... 

然后它尝试另一个实例:

 * connect to 1.2.3.4 port 9200 failed: Connection timed out * Trying 9.10.11.12... 

在完全放弃之前:

 * connect to 9.10.11.12 port 9200 failed: Connection timed out * Failed to connect to search.example.org port 9200: Connection timed out * Closing connection 0 curl: (7) Failed to connect to search.example.org port 9200: Connection timed out 

当我查看我的ELB的统计信息时,会显示一些“后端连接错误”。

请注意, search.example.org是指向ELB的网域。 但是 ,如果我请求指向的实例,它仍然遇到连接超时:

 [jordan@ip-5-6-7-8 ~]$ time curl -vvv ec2-40-41-42-43.compute-1.amazonaws.com:9200 * Rebuilt URL to: ec2-40-41-42-43.compute-1.amazonaws.com:9200/ * Hostname was NOT found in DNS cache * Trying 40.41.42.43... 

而且从非EC2位置仍然很快:

 laptop:~ jordan$ time curl -vvv ec2-40-41-42-43.compute-1.amazonaws.com:9200 * About to connect() to ec2-40-41-42-43.compute-1.amazonaws.com port 9200 (#0) * Trying 40.41.42.43... connected * Connected to ec2-40-41-42-43.compute-1.amazonaws.com (40.41.42.43) port 9200 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8y zlib/1.2.3 > Host: ec2-40-41-42-43.compute-1.amazonaws.com:9200 > Accept: */* > < HTTP/1.1 200 OK < Content-Type: application/json; charset=UTF-8 < Content-Length: 294 < … snip … * Connection #0 to host ec2-54-85-45-128.compute-1.amazonaws.com left intact * Closing connection #0 real 0m0.864s user 0m0.006s sys 0m0.011s laptop:~ jordan$ 

我有nginxsearch服务器上运行,并且从任何地方访问它,包括另一个EC2实例,同样很快。 所以它似乎是专门的,如果我试图访问端口9200.请注意,所有提到的服务器都在一个共享的安全组,其中包括端口9200的入站访问。

如果我使用私有IP地址,服务器似乎连接就好了。 但是,如果我能以其他方式解决这个问题,我宁愿不创build一个内部负载平衡器。

AWS优化连接出去而不是从里到外。 如果您使用的是公共IP,则可能是您的连接正在离开AWS(或至less进入边缘路由器),然后重新路由到AWS。

如果您有两个内部实例,请使用专用IP。 然后连接在本地交换机之间(这有点过于简化,因为2台服务器仍然可能很远)。

如果你仍然看到不好的performance,检查你的实例大小…小和微有很差的延迟。 最后,你可以做一个VPC。 这就像云中的小云。 你可以使用自己的虚拟路由器,他们会尝试为你整合服务器,所以基本上它们是非常靠近的(如果不在同一个机架上的话)。