【问题标题】:How to include firewall in VM instance creation by DeploymentManager in GCP如何在 GCP 中的 DeploymentManager 创建 VM 实例时包含防火墙
【发布时间】:2018-06-02 05:24:50
【问题描述】:

我的yaml模板如下,我想添加防火墙属性来允许http流量:

resources:

    - name: deployed-vm2222
      type: compute.v1.instance
      properties:
        zone: us-central1-f           
        machineType: https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/machineTypes/f1-micro
        disks:
        - deviceName: boot
          type: PERSISTENT
          boot: true
          autoDelete: true

【问题讨论】:

    标签: google-cloud-platform google-deployment-manager


    【解决方案1】:

    在防火墙中,我们使用:

    targetTags: ["http"]
    

    然后,在实例中,我们使用:

    tags:
        items: ["http"]
    

    完整的文件可以如图:

    resources:
    - name: default-allow-http
      type: compute.v1.firewall
      properties:
        targetTags: ["http"]
        sourceRanges: ["0.0.0.0/0"]
        allowed:
          - IPProtocol: TCP
            ports: ["80"]    
    - name: vm-test
      type: compute.v1.instance
      properties:
        zone: xxxx
        machineType: xxxx
        tags:
            items: ["http"]
        disks:
        - deviceName: boot
          type: PERSISTENT
          boot: true
          autoDelete: true
          initializeParams:
            diskName: xxxx
            sourceImage: xxxx
        networkInterfaces:
        - network: xxxx
          accessConfigs:
          - name: External NAT
            type: ONE_TO_ONE_NAT
    

    【讨论】:

    • 除了 Patrick 提到的 targetTags 之外,您还应该将 network 资源添加到属性中,否则使用默认网络。见:cloud.google.com/compute/docs/reference/rest/v1/…
    • 如何创建允许入站 HTTP 流量到标记实例。我尝试做类似 IPProtocol: HTTP 的事情。但这给我带来了这个错误 Invalid value for field 'resource.allowed[0].IPProtocol': 'HTTP"'
    【解决方案2】:

    执行此操作时需要注意几点,请确保正确标记实例以启用标签。例如,标记实例、http-server 或 https-server 可确保防火墙知道它正在处理公共流量。

    添加防火墙入口可以通过以下方式实现。

    resources:
      - name: instance
        type: xxxxxxxx
        properties:
          zone: us-east1-b
          tags:
            items: ["http-server", "tensorboard"]
      - name: default-allow-http
        type: compute.v1.firewall
        properties:
          network: https://www.googleapis.com/compute/v1/projects/myproject/global/networks/default
          targetTags: ["http-server"]
          sourceRanges: ["0.0.0.0/0"]
          allowed:
          - IPProtocol: TCP
            ports: ["80"]
      - name: default-allow-tensorboard
        type: compute.v1.firewall
        properties:
          network: https://www.googleapis.com/compute/v1/projects/myproject/global/networks/default
          targetTags: ["tensorboard"]
          sourceRanges: ["0.0.0.0/0"]
          allowed:
          - IPProtocol: TCP
            ports: ["6006"]
    

    【讨论】:

    • 使用tags: - http-server 时出现错误:message: '"/tags": domain: validation; keyword: type; message: instance does not match any allowed primitive type; allowed: ["object"]; found: "array"' 是什么意思?
    • 标签定义已更改 - 更新示例以反映项目/标签的正确用法。
    【解决方案3】:

    您可以在模板中添加防火墙规则,如下所示:

    - name: allow-http-fw
      type: compute.v1.firewall
      properties:
        allowed:
          - IPProtocol: TCP
            ports: 80
        sourceRanges: [ 0.0.0.0/0 ]
    

    您可以定义为firewall resource 列出的属性。

    【讨论】:

    • 理想情况下,您应该在防火墙规则中包含一个标签(而不是应用于所有)并将网络标签分配给实例(使用 tags.items[] 字段)以确保防火墙规则适用到实例
    • 除了 Patrick 提到的 targetTags 之外,您还应该将 network 资源添加到属性中,否则使用默认网络。见:cloud.google.com/compute/docs/reference/rest/v1/…
    【解决方案4】:

    @LundinCast 几乎完全正确 properties 下的network: 不见了。

    它与networkInterfaces:下的值相同

    【讨论】:

      猜你喜欢
      • 2017-02-05
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      • 2021-09-28
      • 2022-01-05
      • 1970-01-01
      • 2021-08-02
      相关资源
      最近更新 更多