google-cloud-platform - Google Cloud Platform 使用 InstanceGroupManager 启动多个实例
问题描述
我正在尝试通过 Java API使用InstanceGroupManager从 Google Cloud Platform 启动多个实例。我遵循的顺序是
- 创建实例模板
- 使用给定的实例模板创建实例组管理器
据我了解,如果我需要创建多个实例,那么我需要创建相等数量的磁盘,其中每个磁盘都将分配给创建的每个实例。问题是我创建了相同数量的磁盘,然后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;
}
解决方案
我通过使用区域实例组解决了它。基本上,使用gcpClient.regionInstanceGroupManagers().insert("test-project", "us-west2", instanceGroupManager).execute();
解决了这个问题。
推荐阅读
- ansible - 将一个数组转换为另一个数组
- javascript - 如何映射(Laravel - ReactJS)中的每个数组对象
- spring-boot - 如何在我的 applicationContext 文件中的 Spring Boot 应用程序中配置 HikariCP?
- python - 美丽汤找不到元素时如何处理属性错误
- java - 如何修复 findbugs“方法访问具有常量索引的列表或数组”发现的错误
- spring-boot - 没有找到能够从 [java.lang.String] 类型转换为 [java.time.Instant] 类型的转换器
- java - 获取 java.nio.file.NoSuchFileException:
- spring-boot - Spring Cloud Vault - 存储 Secret-Id 和 Role-Id 的最佳位置
- c# - “File.ReadAllText(String)”:并非所有代码路径都返回值
- firebase - 使用多租户应用的 Firestore 安全规则验证用户权限