Cloud Assembly 中部署到 vSphere 时,可以分配一个静态 IP 地址,但必须注意不要在 cloudConfig 初始化命令和自定义规范之间引入冲突。

设计示例

以下设计可安全地应用静态 IP 地址,而不会在云模板初始化命令和自定义规范之间产生任何冲突。所有设计均包含 assignment: static 网络设置。

设计 云模板代码示例

将静态 IP 地址分配给没有 cloud-init 代码的 Linux 计算机

resources:
  wpnet:
    type: Cloud.Network
    properties:
      name: wpnet
      networkType: public
      constraints:
        - tag: sqa
  DBTier:
    type: Cloud.vSphere.Machine
    properties:
      flavor: small
      image: linux-template
      networks:
        - name: '${wpnet.name}'
          assignment: static
          network: '${resource.wpnet.id}'

将静态 IP 地址分配给具有 cloud-init 代码但代码不含网络分配命令的 Linux 计算机。

注意:无论是将 customizeGuestOs 自定义规范设置为 true 还是省略 customizeGuestOs 属性,都会应用 vSphere 自定义规范。

Ubuntu 示例

resources:
  wpnet:
    type: Cloud.Network
    properties:
      name: wpnet
      networkType: public
      constraints:
        - tag: sqa
  DBTier:
    type: Cloud.vSphere.Machine
    properties:
      flavor: small
      image: ubuntu-template
      customizeGuestOs: true
      cloudConfig: |
        #cloud-config
        ssh_pwauth: yes
        chpasswd:
          list: |
            root:Pa$$w0rd
          expire: false
        write_files:
          - path: /tmpFile.txt
            content: |
              ${resource.wpnet.dns}
        runcmd:
          - hostnamectl set-hostname --pretty ${self.resourceName}
          - touch /etc/cloud/cloud-init.disabled
      networks:
        - name: '${wpnet.name}'
          assignment: static
          network: '${resource.wpnet.id}'

CentOS 示例

resources:
  wpnet:
    type: Cloud.Network
    properties:
      name: wpnet
      networkType: public
      constraints:
        - tag: sqa
  DBTier:
    type: Cloud.vSphere.Machine
    properties:
      flavor: small
      image: centos-template
      customizeGuestOs: true
      cloudConfig: |
        #cloud-config
        write_files:
          - path: /test.txt
            content: |
              deploying in power off.
              then rebooting.
      networks:
        - name: '${wpnet.name}'
          assignment: static
          network: '${resource.wpnet.id}'

将静态 IP 地址分配给具有 cloud-init 代码同时代码包含网络分配命令的 Linux 计算机。

customizeGuestOs 属性必须为 false。

Ubuntu 示例

resources:
  wpnet:
    type: Cloud.Network
    properties:
      name: wpnet
      networkType: public
      constraints:
        - tag: sqa
  DBTier:
    type: Cloud.vSphere.Machine
    properties:
      flavor: small
      image: ubuntu-template
      customizeGuestOs: false
      cloudConfig: |
        #cloud-config
        write_files:
          - path: /etc/netplan/99-installer-config.yaml
            content: |
              network:
                version: 2
                renderer: networkd
                ethernets:
                  ens160:
                    addresses:
                      - ${resource.DBTier.networks[0].address}/${resource.wpnet.prefixLength}
                    gateway4: ${resource.wpnet.gateway}
                    nameservers:
                      search: ${resource.wpnet.dnsSearchDomains}
                      addresses: ${resource.wpnet.dns}
        runcmd:
          - netplan apply
          - hostnamectl set-hostname --pretty ${self.resourceName}
          - touch /etc/cloud/cloud-init.disabled
      networks:
        - name: '${wpnet.name}'
          assignment: static
          network: '${resource.wpnet.id}'

CentOS 示例

resources:
  wpnet:
    type: Cloud.Network
    properties:
      name: wpnet
      networkType: public
      constraints:
        - tag: sqa
  DBTier:
    type: Cloud.vSphere.Machine
    properties:
      flavor: small
      image: centos-template
      customizeGuestOs: false
      cloudConfig: |
        #cloud-config
        ssh_pwauth: yes
        chpasswd:
          list: |
            root:VMware1!
          expire: false
        runcmd:
          - nmcli con add type ethernet con-name 'custom ens192' ifname ens192 ip4 ${self.networks[0].address}/${resource.wpnet.prefixLength} gw4 ${resource.wpnet.gateway}
          - nmcli con mod 'custom ens192' ipv4.dns "${join(resource.wpnet.dns,' ')}"
          - nmcli con mod 'custom ens192' ipv4.dns-search "${join(resource.wpnet.dnsSearchDomains,',')}"
          - nmcli con down 'System ens192' ; nmcli con up 'custom ens192'
          - nmcli con del 'System ens192'
          - hostnamectl set-hostname --static `dig -x ${self.networks[0].address} +short | cut -d "." -f 1`
          - hostnamectl set-hostname --pretty ${self.resourceName}
          - touch /etc/cloud/cloud-init.disabled
      networks:
        - name: '${wpnet.name}'
          assignment: static
          network: '${resource.wpnet.id}'

基于引用映像部署时,将静态 IP 地址分配给具有 cloud-init 代码同时代码包含网络分配命令的 Linux 计算机。

customizeGuestOs 属性必须为 false。

此外,云模板不得包含 ovfProperties 属性,该属性会阻止自定义。

resources:
  wpnet:
    type: Cloud.Network
    properties:
      name: wpnet
      networkType: public
      constraints:
        - tag: sqa
  DBTier:
    type: Cloud.vSphere.Machine
    properties:
      flavor: small
      imageRef: 'https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64.ova'
      customizeGuestOs: false
      cloudConfig: |
        #cloud-config
        ssh_pwauth: yes
        chpasswd:
          list: |
            root:Pa$$w0rd
            ubuntu:Pa$$w0rd
          expire: false
        write_files:
          - path: /etc/netplan/99-netcfg-vrac.yaml
            content: |
              network:
                version: 2
                renderer: networkd
                ethernets:
                  ens192:
                    dhcp4: no
                    dhcp6: no
                    addresses:
                      - ${resource.DBTier.networks[0].address}/${resource.wpnet.prefixLength}
                    gateway4: ${resource.wpnet.gateway}
                    nameservers:
                      search: ${resource.wpnet.dnsSearchDomains}
                      addresses: ${resource.wpnet.dns}
        runcmd:
          - netplan apply
          - hostnamectl set-hostname --pretty ${self.resourceName}
          - touch /etc/cloud/cloud-init.disabled
      networks:
        - name: '${wpnet.name}'
          assignment: static
          network: '${resource.wpnet.id}'

不起作用或可能会产生不想要的结果的设计

  • cloud-init 代码不包含网络分配命令,且 customizeGuestOs 属性为 false。

    既没有初始化命令也没有自定义规范用于配置网络设置。

  • cloud-init 代码不包含网络分配命令,并设置了 ovfProperties 属性。

    初始化命令不存在,但 ovfProperties 阻止了自定义规范。

  • cloud-init 代码包含网络分配命令,但 customizeGuestOs 属性缺失或设置为 true。

    应用自定义规范与初始化命令冲突。

有关 cloud-init 和自定义规范的其他解决办法

部署到 vSphere 时,还可以自定义映像,以解决 cloud-init 和自定义规范的冲突。有关详细信息,请参见以下外部存储库。

标记