【发布时间】:2019-06-26 18:04:36
【问题描述】:
我正在尝试通过 Java API 使用 InstanceGroupManager 从 Google Cloud Platform 启动多个实例。我遵循的顺序是
- 创建一个Instance Template
- 使用给定的实例模板创建实例组管理器
据我所知,如果我需要创建多个实例,那么我需要创建相同数量的磁盘,其中每个磁盘都将分配给创建的每个实例。问题是我创建了相同数量的磁盘,然后用它们创建了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-platform google-compute-engine