GCE 如何在启动时将外部 IP 添加到现有实例
GCE how to add external IP to existing instance at boot
我正在使用 Gcloud-java 来管理一些 VM 实例。创建新实例的代码很清楚,如下:
Address externalIp = compute.getAddress(addressId);
InstanceId instanceId = InstanceId.of("us-central1-a", "test-instance");
NetworkId networkId = NetworkId.of("default");
PersistentDiskConfiguration attachConfiguration =
PersistentDiskConfiguration.builder(diskId).boot(true).build();
AttachedDisk attachedDisk = AttachedDisk.of("dev0", attachConfiguration);
NetworkInterface networkInterface = NetworkInterface.builder(networkId)
.accessConfigurations(AccessConfig.of(externalIp.address()))
.build();
MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1");
InstanceInfo instance =
InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface);
Operation operation = compute.create(instance);
// Wait for operation to complete
operation = operation.waitFor();
if (operation.errors() == null) {
System.out.println("Instance " + instanceId + " was successfully created");
} else {
// inspect operation.errors()
throw new RuntimeException("Instance creation failed");
}
但是如果我有一个现有的实例想要启动并且我想附加一个外部IP,我该怎么办?
我已经尝试过这种方式:首先我创建了一个 RegionAddressId 并获得了一个用于创建网络接口的地址。
RegionAddressId addressId = RegionAddressId.of("europe-west1", "test-address");
Operation operationAdd = compute.create(AddressInfo.of(addressId));
operationAdd = operationAdd.waitFor();
Address externalIp = compute.getAddress(addressId);
NetworkId networkId = NetworkId.of("default");
NetworkInterface networkInterface = NetworkInterface.builder(networkId)
.accessConfigurations(NetworkInterface.AccessConfig.of(externalIp.address()))
.build();
我得到我的实例并添加 accessConfig
InstanceId instanceId = InstanceId.of("my-server", "europe-west1-b","my-instance");
Instance instance = compute.getInstance(instanceId);
instance.addAccessConfig("default", NetworkInterface.AccessConfig.of(externalIp.address()));
Operation operation = instance.start();
结果是我的实例使用另一个我不知道如何获取的外部 IP 启动。
正确的程序是什么?
谢谢
我自己找到了解决方案。
Compute compute = ComputeOptions.defaultInstance().service();
InstanceId instanceId = InstanceId.of("my-server", "europe-west1-b","my-instance");
Operation operation = compute.start(instanceId);
Operation completedOperation = operation.waitFor();
if (completedOperation == null) {
// operation no longer exists
} else if (completedOperation.errors() != null) {
// operation failed, handle error
}
Instance instance = compute.getInstance(instanceId);
String publicIp =
instance.networkInterfaces().get(0).accessConfigurations().get(0).natIp();
我使用Compute的start方法启动实例,然后(运行完成后)我得到实例
我正在使用 Gcloud-java 来管理一些 VM 实例。创建新实例的代码很清楚,如下:
Address externalIp = compute.getAddress(addressId);
InstanceId instanceId = InstanceId.of("us-central1-a", "test-instance");
NetworkId networkId = NetworkId.of("default");
PersistentDiskConfiguration attachConfiguration =
PersistentDiskConfiguration.builder(diskId).boot(true).build();
AttachedDisk attachedDisk = AttachedDisk.of("dev0", attachConfiguration);
NetworkInterface networkInterface = NetworkInterface.builder(networkId)
.accessConfigurations(AccessConfig.of(externalIp.address()))
.build();
MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1");
InstanceInfo instance =
InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface);
Operation operation = compute.create(instance);
// Wait for operation to complete
operation = operation.waitFor();
if (operation.errors() == null) {
System.out.println("Instance " + instanceId + " was successfully created");
} else {
// inspect operation.errors()
throw new RuntimeException("Instance creation failed");
}
但是如果我有一个现有的实例想要启动并且我想附加一个外部IP,我该怎么办?
我已经尝试过这种方式:首先我创建了一个 RegionAddressId 并获得了一个用于创建网络接口的地址。
RegionAddressId addressId = RegionAddressId.of("europe-west1", "test-address");
Operation operationAdd = compute.create(AddressInfo.of(addressId));
operationAdd = operationAdd.waitFor();
Address externalIp = compute.getAddress(addressId);
NetworkId networkId = NetworkId.of("default");
NetworkInterface networkInterface = NetworkInterface.builder(networkId)
.accessConfigurations(NetworkInterface.AccessConfig.of(externalIp.address()))
.build();
我得到我的实例并添加 accessConfig
InstanceId instanceId = InstanceId.of("my-server", "europe-west1-b","my-instance");
Instance instance = compute.getInstance(instanceId);
instance.addAccessConfig("default", NetworkInterface.AccessConfig.of(externalIp.address()));
Operation operation = instance.start();
结果是我的实例使用另一个我不知道如何获取的外部 IP 启动。 正确的程序是什么? 谢谢
我自己找到了解决方案。
Compute compute = ComputeOptions.defaultInstance().service();
InstanceId instanceId = InstanceId.of("my-server", "europe-west1-b","my-instance");
Operation operation = compute.start(instanceId);
Operation completedOperation = operation.waitFor();
if (completedOperation == null) {
// operation no longer exists
} else if (completedOperation.errors() != null) {
// operation failed, handle error
}
Instance instance = compute.getInstance(instanceId);
String publicIp =
instance.networkInterfaces().get(0).accessConfigurations().get(0).natIp();
我使用Compute的start方法启动实例,然后(运行完成后)我得到实例