【问题标题】:How do you create an interface with a public IP address with Google compute node.js api?如何使用 Google 计算 node.js api 创建具有公共 IP 地址的接口?
【发布时间】:2021-01-17 04:56:56
【问题描述】:

我用的是谷歌官方api:https://www.npmjs.com/package/@google-cloud/compute

有示例,但没有显示如何使用公共 IP 创建接口。

我不能使用http:true,因为我需要打开一个特定的 TCP 端口。

【问题讨论】:

    标签: node.js api google-cloud-platform google-compute-engine


    【解决方案1】:

    创建防火墙规则以允许连接到 VM 的公共 IP 上的端口。

    参考来自:https://googleapis.dev/nodejs/compute/latest/Network.html#createFirewall

    const Compute = require('@google-cloud/compute');
    const compute = new Compute();
    const network = compute.network('network-name');
    
    const config = {
      protocols: {
        tcp: [3000],
        udp: [] // An empty array means all ports are allowed.
      },
    
      ranges: ['0.0.0.0/0']
    };
    
    function callback(err, firewall, operation, apiResponse) {
      // `firewall` is a Firewall object.
    
      // `operation` is an Operation object that can be used to check the status
      // of the request.
    }
    
    network.createFirewall('new-firewall-name', config, callback);
    
    //-
    // If the callback is omitted, we'll return a Promise.
    //-
    network.createFirewall('new-firewall-name', config).then(function(data) {
      const firewall = data[0];
      const operation = data[1];
      const apiResponse = data[2];
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 2011-09-11
      • 1970-01-01
      • 2019-12-04
      • 2014-12-16
      相关资源
      最近更新 更多