首页 > 解决方案 > Google Cloud Platform 使用 InstanceGroupManager 启动多个实例

问题描述

我正在尝试通过 Java API使用InstanceGroupManager从 Google Cloud Platform 启动多个实例。我遵循的顺序是

  1. 创建实例模板
  2. 使用给定的实例模板创建实例组管理器

据我了解,如果我需要创建多个实例,那么我需要创建相等数量的磁盘,其中每个磁盘都将分配给创建的每个实例。问题是我创建了相同数量的磁盘,然后InstanceTemplate用它们创建。但是,当我使用 InstanceGroupManager 创建实例时,第二个实例总是由于The disk resource XXX is already being used by instance-ABC. 所以,如果我创建两个磁盘(一个是引导磁盘,另一个不是),我看到两个磁盘连接到第一个实例,这样第二个实例就不会得到任何磁盘。

我试图简化我在这里运行的 java 代码。您可以看到我正在创建和执行它的InstanceTemplate方式InstanceGroupManager

    private void launchInstances(String instanceData, int instanceCount) GeneralSecurityException, IOException {
        Compute gcpClient = createGCPClient();
        String image = "projects/ubuntu-os-cloud/global/images/family/ubuntu-1804-lts";
        String securityGroup = "global/networks/default";
        String instanceType = "n1-highcpu-4";

        // create instance template
        InstanceTemplate instanceTemplate = new InstanceTemplate();
        String templateUUID = UUID.randomUUID().toString();
        instanceTemplate.setName("test-template-" + templateUUID);
        instanceTemplate.setProperties(buildInstanceProperties(instanceData, cloudRegionEntity, count));
        Operation createIntanceTemplateOp = gcpClient.instanceTemplates().insert("test-project", instanceTemplate).execute();

        // create instance group manager
        InstanceGroupManager instanceGroupManager = new InstanceGroupManager();
        InstanceGroupManagerVersion version = new InstanceGroupManagerVersion();
        version.setInstanceTemplate("global/instanceTemplates/" + instanceTemplate.getName());
        instanceGroupManager.setVersions(Collections.singletonList(version));
        instanceGroupManager.setTargetSize(count);
        instanceGroupManager.setName("intance-group-manager" + templateUUID);

        String zone = "us-west2-a"
        Operation launchInsGrOp = zone; gcpClient.instanceGroupManagers().insert("test-project", zone, instanceGroupManager).execute();
        launchInsGrOp = gcpClient.zoneOperations().get("test-project", zone, operation.getName()).execute();

    }

    private InstanceProperties buildInstanceProperties(String instanceData, String image, int instanceCount) {
        InstanceProperties instanceProperties = new InstanceProperties();

        // set disk properties
        instanceProperties.setDisks(buildAttachedDisk(image, instanceCount));

        // set metadata properties
        Metadata metadata = new Metadata();
        Metadata.Items item = new Metadata.Items();
        item.setKey("startup-script");
        item.setValue(instanceData);
        metadata.setItems(Collections.singletonList(item));
        instanceProperties.setMetadata(metadata);

        // set machine-type property
        instanceProperties.setMachineType("n1-highcpu-4");

        // set network-interface property
        instanceProperties.setNetworkInterfaces(Collections.singletonList(buildNetworkInterface("global/networks/default")));

        // build service account property
        instanceProperties.setServiceAccounts(Collections.singletonList(buildServiceAccount()));
        return instanceProperties;
    }

    private List<AttachedDisk> buildAttachedDisk(String machineType, int count) {
        List<AttachedDisk> attachedDisks = new ArrayList<>();
        for (int ind = 0; ind < count; ind++) {
            // Add attached Persistent Disk to be used by VM Instance.
            AttachedDisk attachedDisk = new AttachedDisk();
            attachedDisk.setAutoDelete(true);
            if (ind == 0) {
                attachedDisk.setBoot(true);
            } else {
                attachedDisk.setBoot(false);
            }
            attachedDisk.setType("PERSISTENT");
            AttachedDiskInitializeParams params = new AttachedDiskInitializeParams();
            params.setDiskName("test-disk-" + UUID.randomUUID().toString());

            // Specify the source operating system machine image to be used by the VM Instance.
            params.setSourceImage(machineType);
            attachedDisk.setInitializeParams(params);

            attachedDisks.add(attachedDisk);
        }

        return attachedDisks;
    }

标签: google-cloud-platformgoogle-compute-engine

解决方案


我通过使用区域实例组解决了它。基本上,使用gcpClient.regionInstanceGroupManagers().insert("test-project", "us-west2", instanceGroupManager).execute();解决了这个问题。


推荐阅读