【问题标题】:Google Cloud Platform - Create instance with IPv4Google Cloud Platform - 使用 IPv4 创建实例
【发布时间】:2021-06-16 14:20:34
【问题描述】:

我使用的是 Google Cloud API,并且我创建了实例,但没有使用 IPv4。 我需要 IPv4 实例。

谁能帮我配置AccessConfig?

<?php
require_once 'vendor/autoload.php';

use Google\Cloud\Compute\V1\InstancesClient;
use Google\Cloud\Compute\V1\AttachedDisk;
use Google\Cloud\Compute\V1\AttachedDiskInitializeParams;
use Google\Cloud\Compute\V1\Instance;
use Google\Cloud\Compute\V1\NetworkInterface;
use Google\Cloud\Compute\V1\Operation;
use Google\Cloud\Compute\V1\ZoneOperationsClient;
use Google\Cloud\Compute\V1\AccessConfig;

putenv('GOOGLE_APPLICATION_CREDENTIALS=my.json');

$projectId = 'myProject';
$zoneName = 'europe-west3-c';

function create_instance(
    string $projectId,
    string $zone,
    string $instanceName,
    string $machineType = 'n1-standard-1',
    string $sourceImage = 'projects/debian-cloud/global/images/family/debian-10',
    string $networkName = 'global/networks/default'
) {
    // Set the machine type using the specified zone
    $machineTypeFullName = sprintf('zones/%s/machineTypes/%s', $zone, $machineType);

    // Set the boot disk
    $diskInitializeParams = (new AttachedDiskInitializeParams())
                            ->setSourceImage($sourceImage);
    $disk = (new AttachedDisk())
        ->setBoot(true)
        ->setInitializeParams($diskInitializeParams);

    // Set the network
    $network = (new NetworkInterface());

    // Create the Instance message
    $instance = (new Instance())
        ->setName($instanceName)
        ->setDisks([$disk])
        ->setMachineType($machineTypeFullName)
        ->setNetworkInterfaces([$network]);

    // Insert the new Compute Engine instance using the InstancesClient
    $instancesClient = new InstancesClient();
    $operation = $instancesClient->insert($instance, $projectId, $zone);

    if ($operation->getStatus() === Operation\Status::RUNNING) {
        // Wait until operation completes
        $operationClient = new ZoneOperationsClient();
        $operationClient->wait($operation->getName(), $projectId, $zone);
    }

    printf('Created instance %s' . PHP_EOL, $instanceName);
}

print_r(create_instance($projectId,$zoneName,"test-instance"));

我想我错过了这样的事情:

$accessConfig = (new AccessConfig())
    ->setNatIP('xx.xx.xx.xx')
    ->setType("ONE_TO_ONE_NAT")
    ->setName("External NAT");

$network = (new NetworkInterface())
    ->setNetworkIP('10.156.0.66')
    ->setAccessConfigs(array($accessConfig));

谁能帮帮我?

来源:https://github.com/googleapis/google-cloud-php/tree/master/Compute/src/V1 还有:https://github.com/GoogleCloudPlatform/php-docs-samples/blob/master/compute/cloud-client/instances/src/create_instance.php

谢谢

【问题讨论】:

    标签: php cloud config


    【解决方案1】:

    解决方案:

    $accessConfig = (new AccessConfig())
        ->setNatIP('xx.xx.xx.xx');
    
    $network = (new NetworkInterface())
        ->setAccessConfigs([$accessConfig]);
    

    【讨论】:

      【解决方案2】:

      通常,使用 gcloud,您会在创建实例时添加 ip:

      gcloud compute instances create INSTANCE_NAME \
          --zone [ZONE] \
          [--network-interface
              [network=NETWORK,subnet=SUBNET],
              [address=EXT_IP_NAME | no-address],
              [private-network-ip=INT_NETWORK_IP]
          ...]
      

      不熟悉 php API,但可能会查看类似参数的实例创建 API 调用。

      更多详情: https://cloud.google.com/vpc/docs/create-use-multiple-interfaces#gcloud

      【讨论】:

      • 这不是 PHP API,这是 GCLOUD 命令。
      猜你喜欢
      • 2017-05-23
      • 1970-01-01
      • 2021-07-15
      • 2021-01-15
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多