首页 > 解决方案 > 如何使用 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,
                'autoDelete': True,
                '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。我已经试过了:

但没有任何帮助。

我怎么了?

标签: google-cloud-platform

解决方案


使用GCP 官方文档创建具有本地 SSD 的实例,并参考 Google 的 API,我们可以查看以下内容:

接口说明

除了上述信息之外,以下是一个示例请求有效负载,它创建了一个具有引导磁盘和本地 SSD 设备的实例:

示例请求有效负载

与使用 Python Google 的 API 客户端的有效负载请求相比,您似乎没有指定每个磁盘的“类型”。API 要求本地 SSD 类型为“ SCRATCH ”,而可启动磁盘需要类型为“ PERSISTENT ”。尝试这样做,看看它是否有任何影响。


推荐阅读