簡體   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