首页 > 解决方案 > 将谷歌云中的外部 IP 地址设置为我保留的静态地址

问题描述

我最近开始使用谷歌云并构建了一个节点应用程序,以根据我设置的图像自动部署实例。

该脚本运行良好,我遇到的唯一问题是我似乎无法分配我使用配置保留的静态 IP 地址,我是节点和谷歌云的新手,并且不确定我做错了什么。

任何帮助或指导将不胜感激

我的配置看起来像:

const config = {
        machineType: 'e2-micro',
        http:true,
        https:true,
        networkInterfaces: [
            {
            accessConfigs: [
                {
                  type: 'ONE_TO_ONE_NAT',
                  name: 'External NAT',
                  natIP: '0.0.0.0',// this is an ip address that is passed into this as a variable in that format(it is not a scope issue)
                  setPublicPtr: false,
                  networkTier: 'PREMIUM', //ip address is reserved using this network tier as well
                }
              ],
              
 
      kind: 'compute#networkInterface'

      }],
        
        
        disks: [
          {
            boot: true,
            initializeParams: {
              sourceImage:
                'https://www.googleapis.com/compute/v1/projects' +
                '/myproject/global/images/devimage'
            }
          }
        ],
        metadata: {
           
            items: [
              {
                key: 'startup-script',
                value: 'sudo node index.js'
              }
            ],
            kind: 'compute#metadata'
          },
        
        };

标签: node.jsgoogle-cloud-platformgoogle-apis-explorer

解决方案


请注意,到目前为止,还没有办法通过Google API将静态 IP 分配给现有的Compute Engine 实例。

为了对现有的 Compute Engine 实例执行此操作,您需要使用Google Cloud 控制台或 Cloud SDK

但是,要将静态 IP 分配给新的 Compute Engine 实例,您确实可以通过 API 通过类似于以下的请求来执行此操作:

{
"name": "INSTANCE_NAME",
  "machineType": "zones/ZONE/machineTypes/MACHINE_TYPE",
  "networkInterfaces": [{
    "accessConfigs": [{
      "type": "ONE_TO_ONE_NAT",
      "name": "External NAT",
      "natIP": "IP_ADDRESS"
     }],
    "network": "global/networks/default"
  }],
  "disks": [{
     "autoDelete": "true",
     "boot": "true",
     "type": "PERSISTENT",
     "initializeParams": {
        "sourceImage": "projects/debian-cloud/global/images/v20150818"
     }
   }]
 }

在这种情况下,在创建新实例并分配 IP 地址时,可能未提供实例名称。


推荐阅读