部署至 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 與自訂規格衝突。如需詳細資訊,請參閱以下外部存放庫。

標記