首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google平台-使用IPv4创建实例

Google平台-使用IPv4创建实例
EN

Stack Overflow用户
提问于 2021-06-16 14:20:34
回答 2查看 165关注 0票数 0

我使用了,并创建了实例,但没有IPv4。我需要使用IPv4的实例。

有人能帮我配置AccessConfig吗?

代码语言:javascript
复制
<?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"));

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

代码语言:javascript
复制
$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

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-17 15:22:51

解决办法:

代码语言:javascript
复制
$accessConfig = (new AccessConfig())
    ->setNatIP('xx.xx.xx.xx');

$network = (new NetworkInterface())
    ->setAccessConfigs([$accessConfig]);
票数 0
EN

Stack Overflow用户

发布于 2021-06-17 14:16:16

通常,在使用gcloud时,可以在实例的创建时添加ip:

代码语言:javascript
复制
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调用。

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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68004550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档