如何确保Google Cloud Compute实例已启动并正在运行 [英] How to ensure Google Cloud Compute instance is up and running

查看:67
本文介绍了如何确保Google Cloud Compute实例已启动并正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从shell脚本创建Google Cloud Compute实例,然后通过ssh在该实例上启动几个命令.

I create Google Cloud Compute instance from the shell script, then launch several commands on that instance via ssh.

如何确保实例中的操作系统已启动并正在运行?

How to ensure that operating system in the instance is up and running?

例如:

<代码>gcloud计算实例创建"$ my_name" \--tags"http-server" \--image容器-vm \--metadata-from-file google-container-manifest ="container.yml" \--zone"$ my_zone" \-机械型g1-小

然后我要运行gcloud计算ssh \"$ my_name" --zone"$ my_zone" \--command'sudo docker stop $(sudo docker ps -q -a)'

gcloud计算副本文件\some.conf root @"$ my_name":/existing_dir/\--zone"$ my_zone"

据我了解,如果实例未启动,第二个命令可能会由于连接拒绝而失败.

As far as I understand, the second command may fail with connection refuse if the instance is not up.

如何确保实例已启动并准备接受ssh连接?

How to ensure that instance is up and ready to accept ssh connection?

推荐答案

在发送命令之前,只需检查SSH端口是否已打开.直到实例操作系统启动,SSH服务器才启动:

Just check that SSH port is open before sending the command. SSH server won't be up until the instance OS is up:

IP=$(gcloud compute instances list | awk '/'$my_name'/ {print $5}')
if nc -w 1 -z $IP 22; then
    echo "OK! Ready for heavy metal"
    : Do your heavy metal work
else
    echo "Maybe later?"
fi

说明:

  1. 获取实例$ my_name的IP
  2. 检查端口22是否接受传入连接.

-w:连接超时(1秒应该足够)

-w: Connection timeout (1 second should be more that enough)

-z:仅检查端口是否打开并立即退出

-z: Check only if port is open and exit inmediately

这篇关于如何确保Google Cloud Compute实例已启动并正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆