【问题标题】:Configure a Firewall and a Startup Script with Deployment Manager使用 Deployment Manager 配置防火墙和启动脚本
【发布时间】:2020-08-31 05:47:10
【问题描述】:

我正在执行 GCP 平台“使用部署管理器配置防火墙和启动脚本”的实验,我为此代码更改了 qwicklabs.jinja:

 resources:
- name: default-allow-http
  type: compute.v1.firewall
  properties:
    targetTags: ["http"]
    sourceRanges: ["0.0.0.0/0"]
    allowed:
      - IPProtocol: TCP
        ports: ["80"]
- type: compute.v1.instance
  name: vm-test
  properties:
    zone: {{ properties["zone"] }}
    machineType: https://www.googleapis.com/compute/v1/projects/{{ env["project"] }}/zones/{{ properties["zone"] }}/machineTypes/f1-micro
    # For examples on how to use startup scripts on an instance, see:
    #   https://cloud.google.com/compute/docs/startupscript
    tags:
        items: ["http"]
    metadata:
      items:
      - key: startup-script
        value: "apt-get update \n apt-get install -y apache2"
    disks:
    - deviceName: boot
      type: PERSISTENT
      boot: true
      autoDelete: true
      initializeParams:
        diskName: disk-{{ env["deployment"] }}
        sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-9
    networkInterfaces:
    - network: https://www.googleapis.com/compute/v1/projects/{{ env["project"] }}/global/networks/default
      # Access Config required to give the instance a public IP address
      accessConfigs:
      - name: External NAT
        type: ONE_TO_ONE_NAT

虚拟机和磁盘已成功制作,但我无法完成最后一个任务“检查部署管理器是否包含启动脚本和防火墙资源”,因为我在制作防火墙规则时遇到问题:

ERROR: (gcloud.deployment-manager.deployments.create) Error in Operation [operation-1598852175371-5a
e25c7f61bda-1c55c951-22ca1242]: errors:
- code: RESOURCE_ERROR
  location: /deployments/deployment-templates/resources/http-firewall-rule
  message: '{"ResourceType":"compute.v1.firewall","ResourceErrorCode":"400","ResourceErrorMessage":{
"code":400,"message":"Request
    contains an invalid argument.","status":"INVALID_ARGUMENT","statusMessage":"Bad
    Request","requestPath":"https://compute.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-888e7
df2843f/global/firewalls","httpMethod":"POST"}}'

有人可以帮我吗?我必须完成这个实验!

【问题讨论】:

  • 当您运行 gcloud deployment-manager deployments list 时,您的部署是否显示在列表中?即使没有防火墙规则,它也应该存在。
  • 是的,没错,VM和磁盘创建正确,但防火墙规则没有

标签: google-cloud-platform lab


【解决方案1】:

由于某种原因,您的文件给了我“格式无效”错误,因此我创建了一个新的部署管理器配置文件;拿了VM template from here,添加了你的外部IP配置和防火墙规则部分(没有任何改变)。

我的 yaml 文件看起来像这样(虽然我没有使用任何变量)。

resources:
- name: vm-created-by-deployment-manager
  type: compute.v1.instance
  properties:
    zone: us-central1-a
    machineType: zones/us-central1-a/machineTypes/n1-standard-1
    tags:
        items: ["http"]
    metadata:
      items:
      - key: startup-script
        value: "apt-get update \n apt-get install -y apache2"
    disks:
    - deviceName: boot
      type: PERSISTENT
      boot: true
      autoDelete: true
      initializeParams:
        sourceImage: projects/debian-cloud/global/images/family/debian-9
    networkInterfaces:
    - network: global/networks/default
      accessConfigs:
      - name: External NAT
        type: ONE_TO_ONE_NAT
- name: default-allow-http3
  type: compute.v1.firewall
  properties:
    targetTags: ["http"]
    sourceRanges: ["0.0.0.0/0"]
    allowed:
      - IPProtocol: TCP
        ports: ["80"]

当我运行文件时,一切都按预期工作:

wbogacz@cloudshell:~/fire (wojtek)$ gcloud deployment-manager deployments create test1 --config dm1.yaml
The fingerprint of the deployment is b'n63E-AtErTCKtWOvktfUsA=='
Waiting for create [operation-1599036146720-5ae5-----99-2a45880e-addbce89]...done.
Create operation operation-1599036146720-5ae-----99-2a45880e-addbce89 completed successfully.
NAME                              TYPE                 STATE      ERRORS  INTENT
default-allow-http3               compute.v1.firewall  COMPLETED  []
vm-created-by-deployment-manager  compute.v1.instance  COMPLETED  []

最后我通过 SSH 登录到 VM 并验证启动脚本已执行 - 再次成功。

【讨论】:

  • 感谢您的帮助,我将使用您的代码而不是我的代码并告诉您结果,但是,我希望我知道我的 jinja 文件做错了什么:/
  • 顺便说一句,很抱歉这个愚蠢的问题,但你是否省略了第一行?我的意思是“资源:”行和您创建实例时的“名称”行?
  • 我添加了两行缺失的行 - 感谢您发现这一点。我怀疑只有您文件中的变量可能是罪魁祸首,但我有时间尝试一下。如果您接受我的回答和/或如果它对您有用,我将不胜感激:)
  • 。是的,谢谢。我已经完成了这个实验。真正的问题是在 .yaml 文件中已经有一个防火墙规则脚本,所以我创建了 2 个防火墙规则,其中一个有错误的标签,所以部署引发了错误
  • > "真正的问题是在 .yaml 文件中已经有一个防火墙规则脚本"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-31
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多