如何创建带有gcp附加的本地ssd的实例

问题描述

我尝试使用python googleapiclient创建具有启动磁盘和本地ssd磁盘的实例。 这是用于构建系统的gcp映像,我想要更好的性能

def create_instance(compute,image_name):
    image_response = compute.images().get(project=GCP_PROJECT_ID,image=GCP_IMAGE_NAME).execute()
    source_disk_image = image_response['selfLink']
    machine_type = f"zones/{GCP_ZONE}/machineTypes/n2-standard-4"
    config = {
        'name': image_name,'machineType': machine_type,# Specify the boot disk and the image to use as a source.
        'disks': [
            {
                'boot': True,'autoDelete': True,'initializeParams': {
                    'sourceImage': source_disk_image,}
            },{
                'boot': False,'initializeParams': {
                    'disk_type': 'local-ssd'
                }
            }
        ],# Specify a network interface with NAT to access the public
        # internet.
        'networkInterfaces': [{
            'network': 'global/networks/default','accessConfigs': [
                {'type': 'ONE_TO_ONE_NAT','name': 'External NAT'}
            ]
        }],# Allow the instance to access cloud storage and logging.
        'serviceAccounts': [{
            'email': 'default','scopes': [
                "https://www.googleapis.com/auth/cloud-platform"
            ]
        }]
    }

    operation = compute.instances().insert(
        project=GCP_PROJECT_ID,zone=GCP_ZONE,body=config).execute()
    return operation

很难尝试提供不同的initializeParams diskTypes,它总是为我创建标准的持久性500GB磁盘,而不是本地ssd。我已经尝试过了:

但没有任何帮助。

我怎么了?

解决方法

使用official GCP documentation创建带有本地SSD的实例并引用Google的API,我们可以查看以下内容:

Description of API

除了上面的信息外,下面是一个示例请求有效负载,该示例使用启动磁盘和本地SSD设备创建实例:

sample request payload

与使用Python Google的API客户端进行的有效负载请求相比,您似乎未指定每个磁盘的“类型”。该API要求本地SSD的类型为“ SCRATCH ”,其中可引导磁盘的类型必须为“ PERSISTENT ”。尝试这样做,看看是否有影响。

,

作为文档[1,本地SSD位于运行虚拟机实例的物理计算机上,因此只能在实例创建过程中创建它们。本地SSD不能用作引导设备。

在创建本地SSD之后,我们必须先格式化并装入设备[2],然后再使用它。

[1] https://cloud.google.com/compute/docs/disks/local-ssd#create_local_ssd

[2] https://cloud.google.com/compute/docs/disks/local-ssd#format_and_mount_a_local_ssd_device