【问题标题】:Create Instance with HTTP enable from Java Google Cloud从 Java Google Cloud 创建启用 HTTP 的实例
【发布时间】:2016-09-22 03:31:20
【问题描述】:

我使用以下代码使用 Google Cloud java(https://github.com/GoogleCloudPlatform/google-cloud-java) 从 Java 创建实例

public class CreateInstance {

  public static void main(String... args) throws InterruptedException, TimeoutException {
    Compute compute = ComputeOptions.defaultInstance().service();
    ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329");
    NetworkId networkId = NetworkId.of("default");
    AttachedDisk attachedDisk = AttachedDisk.of(AttachedDisk.CreateDiskConfiguration.of(imageId));
    NetworkInterface networkInterface = NetworkInterface.of(networkId);
    InstanceId instanceId = InstanceId.of("us-central1-a", "instance-name");
    MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1");
    Operation operation =
        compute.create(InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface));
    operation = operation.waitFor();
    if (operation.errors() == null) {
      // use instance
      Instance instance = compute.getInstance(instanceId);
    }
  }
}

Evrything 工作正常,但所有创建的实例默认都禁用 HTTP 和 HTTPS,我如何创建默认启用 HTTP 的实例。

我已经有这种方法了:

Firewall firewall = new Firewall();
    firewall.setName(name);
    Firewall.Allowed allowedTCP8080 = new Firewall.Allowed();
    allowedTCP8080.setIPProtocol("tcp");
    List<String> ports = new ArrayList<>();
    ports.add("80");
    ports.add("8080");
    ports.add("8280");
    ports.add("8180");
    ports.add("9610");
    allowedTCP8080.setPorts(ports);
    List<Firewall.Allowed> allowedPorts = new ArrayList<>();
    allowedPorts.add(allowedTCP8080);
    firewall.setAllowed(allowedPorts);
    firewall.setDescription("httpports");
    firewall.setNetwork(googleCloudUtils.networkId.network());

但我不知道如何将防火墙附加到网络配置和/或实例。

【问题讨论】:

  • 您使用的是什么版本的 API?当我查看源代码时,我在任何地方都没有看到 Firewall 类。
  • @WillHayworth 0.3 版:com.google.cloudgoogle-cloud-compute0.3.0 就是在这个包包com.google.api.services.compute.model;

标签: java http google-cloud-platform


【解决方案1】:

我只是想办法解决这个问题。

  1. 在创建实例时将实例标签设置为“http-server”

  2. 创建一个完全命名为“default-allow-http”的防火墙规则,我尝试了其他名称,但未能应用于实例。

  3. 将 targettage 设置为“http-server”。

记住你只需要用名字设置一次防火墙,否则你会得到一个“名字存在错误”。

如果你不明白这里发生了什么,只需输入

gcloud compute --project "projectname" firewall-rules create "default-allow-http" --allow tcp:80 --network "default" --source-ranges "0.0.0.0/0" --target-tags "http-server"

在终端,然后去控制台看看网络防火墙页面发生了什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-16
    • 2019-07-18
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多