繁体   English   中英

如何访问在谷歌云平台上创建的虚拟机

[英]How to access VM created on google cloud platform

我正在尝试通过 java 代码在 Google 云计算上创建 VM。 虚拟机正在 GCP 上创建,但我不知道如何访问它们。

创建虚拟机时如何创建用户名和密码? 是否有任何构建器或方法来添加用户名和密码。? 还有为操作系统和版本(项目和系列)定义的任何方式或常量吗? 因为我们必须从最终用户那里得到这个,用户可能不知道项目和家庭。

提前致谢

下面是我的代码:


private void createVMInstance(String projectId, String sourceImage, String zone, String diskType, String vmName, String vmSize)
            throws IOException, InterruptedException, ExecutionException, TimeoutException {
        String machineType = String.format("zones/%s/machineTypes/n1-standard-1", zone);
        long diskSizeGb = Long.parseLong(vmSize);
        String networkName = "default";
        InstancesSettings instancesSettings = InstancesSettings.newBuilder()
                .setCredentialsProvider(FixedCredentialsProvider.create(googleCredential)).build();
        try (InstancesClient instancesClient = InstancesClient.create(instancesSettings)) {
            // Instance creation requires at least one persistent disk and one network interface.
            AttachedDisk disk = AttachedDisk.newBuilder().setBoot(true).setAutoDelete(true).setType(AttachedDisk.Type.PERSISTENT.toString())
                    .setDeviceName("disk-1").setInitializeParams(AttachedDiskInitializeParams.newBuilder()
                            .setSourceImage(sourceImage).setDiskSizeGb(diskSizeGb).setDiskType(diskType).build()).build();
            NetworkInterface networkInterface = NetworkInterface.newBuilder()
                    .setName(networkName).build();
            // Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
            Instance instanceResource = Instance.newBuilder().setName(vmName).setMachineType(machineType)
                    .addDisks(disk).addNetworkInterfaces(networkInterface).build();
            // Insert the instance in the specified project and zone.
            InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder().setProject(projectId)
                    .setZone(zone).setInstanceResource(instanceResource).build();
            OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
                    insertInstanceRequest);
            // Wait for the operation to complete.
            Operation response = operation.get(3, TimeUnit.MINUTES);
            if (response.hasError()) {
                throw new GCPConnectorException("GCP VM : Failed to create VM in GCP." + response);
            }
        }
    }```

虚拟机正在 GCP 上创建,但我不知道如何访问它们。

您可能想要访问多种方式。

如果从其他一些内部 GCP 资源(如另一个 VM)访问,您可以使用私有内部 IP 地址。

如果完全从 GCP 外部访问,例如从您的计算机 ssh 到 VM,那么您将需要配置一些东西以使其工作 - 创建 GCP 防火墙,为其分配一个标签,将该标签添加到您的 VM。 从那里,您将能够像这样将 ssh 到该 VM 实例(将“us-central1-a”替换为任何区域):

gcloud compute ssh \
--zone "us-central1-a" \
--project "project-name-here" \
"vm-instance-name-goes-here"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM