Python gcp:磁盘*完成*装载后,我无法ssh到我的实例
在GoogleCloudPlatform(GCP)中,磁盘装载完成后,我无法ssh到我的实例 详情: 通过windows 7 PC上的google cloud sdk shell cmd windows,我在Python gcp:磁盘*完成*装载后,我无法ssh到我的实例,python,linux,google-cloud-platform,google-cloud-storage,gcp,Python,Linux,Google Cloud Platform,Google Cloud Storage,Gcp,在GoogleCloudPlatform(GCP)中,磁盘装载完成后,我无法ssh到我的实例 详情: 通过windows 7 PC上的google cloud sdk shell cmd windows,我在C:\>上运行以下命令: python "C:\Users\user's name\path\to\py\create_instance_working.py" --name inst-test2 --zone us-central1-a direct-to pic-1234 cc-test
C:\>
上运行以下命令:
python "C:\Users\user's name\path\to\py\create_instance_working.py" --name inst-test2 --zone us-central1-a direct-to
pic-1234 cc-test1
它运行create\u instance\u working.py
,如下所示:
import argparse
import os
import time
import googleapiclient.discovery
from six.moves import input
# [START list_instances]
def list_instances(compute, project, zone):
result = compute.instances().list(project=project, zone=zone).execute()
return result['items']
# [END list_instances]
# [START create_instance]
def create_instance(compute, project, zone, name, bucket):
image_response = compute.images().getFromFamily(
project='direct-topic-1234', family='theFam').execute()
source_disk_image = image_response['selfLink']
machine_type = "projects/direct-topic-1234/zones/us-central1-a/machineTypes/n1-standard-4"
startup_script = open(
os.path.join(
os.path.dirname(__file__), 'startup-script_working.sh'), 'r').read()
print(machine_type)
config = {
'name': name,
'machineType': machine_type,
'disks': [
{
'boot': True,
'autoDelete': True,
'initializeParams': {
'sourceImage': source_disk_image,
'diskSizeGb': '15',
}
}, {
"deviceName": "disk-2",
"index": 1,
"interface": "SCSI",
"kind": "compute#attachedDisk",
"mode": "READ_WRITE",
"source": "projects/direct-topic-1234/zones/us-central1-a/disks/disk-2",
"type": "PERSISTENT"
}
],
'networkInterfaces': [{
'network': 'global/networks/default',
'accessConfigs': [
{'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'}
]
}],
"serviceAccounts": [
{
"email": "123456789-compute@developer.gserviceaccount.com",
"scopes": [
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring.write",
"https://www.googleapis.com/auth/servicecontrol",
"https://www.googleapis.com/auth/service.management.readonly",
"https://www.googleapis.com/auth/trace.append"
]
}
],
'metadata': {
'items': [{
'key': 'startup-script',
'value': startup_script
}, {
'key': 'bucket',
'value': bucket
}]
}
}
return compute.instances().insert(
project=project,
zone=zone,
body=config).execute()
# [END create_instance]
# [START delete_instance]
def delete_instance(compute, project, zone, name):
return compute.instances().delete(
project=project,
zone=zone,
instance=name).execute()
# [END delete_instance]
# [START wait_for_operation]
def wait_for_operation(compute, project, zone, operation):
print('Waiting for operation to finish...')
while True:
result = compute.zoneOperations().get(
project=project,
zone=zone,
operation=operation).execute()
if result['status'] == 'DONE':
print("done.")
if 'error' in result:
raise Exception(result['error'])
return result
time.sleep(1)
# [END wait_for_operation]
# [START run]
def main(project, bucket, zone, instance_name, wait=True):
compute = googleapiclient.discovery.build('compute', 'v1')
print('Creating instance.')
operation = create_instance(compute, project, zone, instance_name, bucket)
wait_for_operation(compute, project, zone, operation['name'])
instances = list_instances(compute, project, zone)
print('Instances in project %s and zone %s:' % (project, zone))
for instance in instances:
print(' - ' + instance['name'])
print("""
Instance created.
It will take a minute or two for the instance to complete work.
Check this URL: http://storage.googleapis.com/{}/output.png
Once the image is uploaded press enter to delete the instance.
""".format(bucket))
# if wait:
# input()
#
# print('Deleting instance.')
#
# operation = delete_instance(compute, project, zone, instance_name)
# wait_for_operation(compute, project, zone, operation['name'])
print('all done with instance.')
if __name__ == '__main__':
print('in here 3')
main('direct-topic-1234', 'cc-test1', 'us-central1-a', 'inst-test1')
print('in here 4')
# [END run]
sudo mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/sdb
sudo mount -o discard,defaults /dev/sdb /var
sudo chmod a+w /var
sudo cp /etc/fstab /etc/fstab.backup
echo UUID=`sudo blkid -s UUID -o value /dev/sdb` /var ext4 discard,defaults,nofail 0 2 | sudo tee -a /etc/fstab
它调用的启动脚本(startup-script\u working.sh
)如下所示:
import argparse
import os
import time
import googleapiclient.discovery
from six.moves import input
# [START list_instances]
def list_instances(compute, project, zone):
result = compute.instances().list(project=project, zone=zone).execute()
return result['items']
# [END list_instances]
# [START create_instance]
def create_instance(compute, project, zone, name, bucket):
image_response = compute.images().getFromFamily(
project='direct-topic-1234', family='theFam').execute()
source_disk_image = image_response['selfLink']
machine_type = "projects/direct-topic-1234/zones/us-central1-a/machineTypes/n1-standard-4"
startup_script = open(
os.path.join(
os.path.dirname(__file__), 'startup-script_working.sh'), 'r').read()
print(machine_type)
config = {
'name': name,
'machineType': machine_type,
'disks': [
{
'boot': True,
'autoDelete': True,
'initializeParams': {
'sourceImage': source_disk_image,
'diskSizeGb': '15',
}
}, {
"deviceName": "disk-2",
"index": 1,
"interface": "SCSI",
"kind": "compute#attachedDisk",
"mode": "READ_WRITE",
"source": "projects/direct-topic-1234/zones/us-central1-a/disks/disk-2",
"type": "PERSISTENT"
}
],
'networkInterfaces': [{
'network': 'global/networks/default',
'accessConfigs': [
{'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'}
]
}],
"serviceAccounts": [
{
"email": "123456789-compute@developer.gserviceaccount.com",
"scopes": [
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring.write",
"https://www.googleapis.com/auth/servicecontrol",
"https://www.googleapis.com/auth/service.management.readonly",
"https://www.googleapis.com/auth/trace.append"
]
}
],
'metadata': {
'items': [{
'key': 'startup-script',
'value': startup_script
}, {
'key': 'bucket',
'value': bucket
}]
}
}
return compute.instances().insert(
project=project,
zone=zone,
body=config).execute()
# [END create_instance]
# [START delete_instance]
def delete_instance(compute, project, zone, name):
return compute.instances().delete(
project=project,
zone=zone,
instance=name).execute()
# [END delete_instance]
# [START wait_for_operation]
def wait_for_operation(compute, project, zone, operation):
print('Waiting for operation to finish...')
while True:
result = compute.zoneOperations().get(
project=project,
zone=zone,
operation=operation).execute()
if result['status'] == 'DONE':
print("done.")
if 'error' in result:
raise Exception(result['error'])
return result
time.sleep(1)
# [END wait_for_operation]
# [START run]
def main(project, bucket, zone, instance_name, wait=True):
compute = googleapiclient.discovery.build('compute', 'v1')
print('Creating instance.')
operation = create_instance(compute, project, zone, instance_name, bucket)
wait_for_operation(compute, project, zone, operation['name'])
instances = list_instances(compute, project, zone)
print('Instances in project %s and zone %s:' % (project, zone))
for instance in instances:
print(' - ' + instance['name'])
print("""
Instance created.
It will take a minute or two for the instance to complete work.
Check this URL: http://storage.googleapis.com/{}/output.png
Once the image is uploaded press enter to delete the instance.
""".format(bucket))
# if wait:
# input()
#
# print('Deleting instance.')
#
# operation = delete_instance(compute, project, zone, instance_name)
# wait_for_operation(compute, project, zone, operation['name'])
print('all done with instance.')
if __name__ == '__main__':
print('in here 3')
main('direct-topic-1234', 'cc-test1', 'us-central1-a', 'inst-test1')
print('in here 4')
# [END run]
sudo mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/sdb
sudo mount -o discard,defaults /dev/sdb /var
sudo chmod a+w /var
sudo cp /etc/fstab /etc/fstab.backup
echo UUID=`sudo blkid -s UUID -o value /dev/sdb` /var ext4 discard,defaults,nofail 0 2 | sudo tee -a /etc/fstab
二者均改编自:
在GCP控制台中,当我看到实例绿灯时,我立即单击实例ssh
按钮并成功连接到实例。但是,如果我一直打开到实例的新ssh连接,它们都会工作,直到我在/var
上的装载完成。我通过:df-h
在运行的ssh连接(通常是第一个)中查找挂载,看到挂载完成。我可以看到/dev/sdb 197G 60M 197G 1%/var
。在尝试失败之前,装载未显示。但是在它出现之后,没有任何东西会ssh到它。已尝试在控制台中执行gcloud compute ssh[实例名称]
的按钮(shell)。尝试使用[user name]@[external IP]
填充腻子
我试着只等待5分钟,以ssh方式连接到实例(挂载将在那时完成),这也不起作用
重要提示:
如果我注释掉所有可以无限期连接的启动脚本行,就没有SSH
问题。我试着创建一个新的磁盘并将其连接起来
因此,似乎是磁盘的装载导致了ssh问题
早期的ssh连接继续正常工作,即使我不能创建一个新的连接
当ssh连接失败时,我会在ssh窗口中看到:
“连接失败
与SSH服务器通信时出错。请检查服务器和网络配置。“
你知道这是什么原因吗
实例是linux发行版SUSE 12
我的安装说明来自这里:
如果有一个很好的方法可以避免这种情况(请提供),但我真的很想知道我做错了什么
我不熟悉GCP、云、python、ssh和Linux。(这个问题中的所有内容都是全新的!)
如果我注释掉启动脚本行,按照描述运行所有内容,ssh到保险,手动运行启动脚本命令,我没有得到任何错误,我仍然需要测试我是否创建了另一个ssh连接。我们这样做并报告。装载在/var
上,其中包含与ssh
相关的数据(以及其他内容),使得到达/var
的系统可以看到一个空白磁盘。数据必须保存在别处(cp-ar
),装载到var,然后将数据移回
我先前的回答是错误的