告知何时可以连接新的 ec2 主机的过程
我一直在使用 Fabric 和 boto 启动新的 ec2 主机进行一些临时处理,但我总是无法知道何时可以连接到主机。问题是我可以询问 ec2 何时准备好,但它从未真正准备好。
这是我注意到效果最好的过程(尽管它仍然很糟糕):
- 轮询 ec2 直到它说主机“活动”
- 轮询 ec2 直到它有一个
public_dns_name
- 尝试连接到新的主机循环直到它接受连接
但有时它似乎在知道我与之关联的 ssh 密钥对之前就接受了连接,然后要求输入密码。
有没有更好的方法来决定我的 ec2 主机启动后何时可以开始连接?有没有人编写过一个可以很好且高效地完成此任务的库?
I've been using fabric and boto to start up new ec2 hosts for some temporary processing but I've always had trouble knowing when I can connect to the host. The problem is that I can ask ec2 when something is ready but it's never really ready.
This is the process that I've noticed works best (though it still sucks):
- Poll ec2 until it says that the host it "active"
- Poll ec2 until it has a
public_dns_name
- Try to connect to the new host in a loop until it accepts the connection
But sometimes it accepts the connection seemingly before it knows about the ssh key pair that I've associated it with and then asks for a password.
Is there a better way to decide when I can start connecting to my ec2 hosts after they've started up? Has anyone written a library that does this nicely and efficiently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 #1 和 #2 执行了相同的操作,但对于 #3,我有一个代码循环,尝试与 ssh 端口 (22) 建立简单的 TCP 连接,并设置短超时并重试。当它最终成功时,它会再等待五秒钟,然后运行 ssh 命令。
启动 sshd 以及将公共 ssh 密钥添加到 .ssh/authorized_keys 的时间和顺序可能会有所不同,具体取决于您运行的 AMI。
注意:我强烈建议直接使用公共 IP 地址而不是 DNS 名称。 IP 地址编码在 DNS 名称中,因此在进程中添加 DNS 查找没有任何好处。
I do the same for #1 and #2, but for #3 I have a code loop that attempts to make a simple TCP connection to the ssh port (22) with short timeouts and retry. When it finally succeeds, it waits five more seconds an then run the ssh command.
The timing and order in which sshd is started and the public ssh key is added to .ssh/authorized_keys may vary depending on the AMI you are running.
Note: I mildly recommend using the public IP address directly instead of the DNS name. The IP address is encoded in the DNS name, so there's no benefit to adding DNS lookups into the process.
EC2 本身没有任何方式知道您的实例何时准备好接受 SSH 连接;它的运作水平比这低得多。
执行此操作的最佳方法是更新您的 AMI 以拥有某种运行状况 Servlet。它可以非常简单 - 只需几行 web.py 脚本 - 在启动的后期阶段运行,并且它只向任何 HTTP 请求返回状态代码 200。当 servlet 响应请求时,其他一切都应该已启动,因此您可以在该 URL 上使用指数退避来检查您的实例。
如果您曾经将实例放在负载均衡器后面(这有其自身的好处),则无论如何都需要此运行状况 servlet,并且它还有一个额外的好处,即在实例因任何原因发生故障时通知负载均衡器。这只是 EC2 上的一般最佳实践。
EC2 itself doesn't have any way of knowing when your instance is ready to accept SSH connections; it operates on a much lower level than that.
The best way to do this is to update your AMI to have some sort of health servlet. It can be very simple -- just a few lines of web.py script -- that runs at the later stages of startup, and which just returns status code 200 to any HTTP request. By the time that servlet is responding to requests, everything else should be up too, so you can check your instance with exponential backoff on that URL.
If you ever put your instances behind a load balancer (which has its own benefits), this health servlet is required anyway, and has the added benefit of telling the load balancer when an instance has gone down, for any reason. It's just a general best-practice on EC2.