Azure 中適用於虛擬機器的 cloud-init 支援cloud-init support for virtual machines in Azure

此文章說明針對 cloud-init (英文) 存在的支援,以便在 Azure 中於佈建階段設定虛擬機器 (VM) 或虛擬機器擴展集。This article explains the support that exists for cloud-init to configure a virtual machine (VM) or virtual machine scale sets at provisioning time in Azure. 一旦 Azure 佈建資源,這些 cloud-init 設定就會在初次開機時執行。These cloud-init configurations are run on first boot once the resources have been provisioned by Azure.

VM 佈建是 Azure 向下傳遞您的 VM Creater 參數值 (例如主機名稱、使用者名稱、密碼等),並在 VM 開機時為其加以提供的程序。VM Provisioning is the process where the Azure will pass down your VM Create parameter values, such as hostname, username, password etc., and make them available to the VM as it boots up. 「佈建代理程式」將會使用那些值,設定 VM,然後在完成時回報。A 'provisioning agent' will consume those values, configure the VM, and report back when completed.

Azure 支援兩種佈建代理程式,cloud-init (英文) 和 Azure Linux 代理程式 (WALA) (部分機器翻譯)。Azure supports two provisioning agents cloud-init, and the Azure Linux Agent (WALA).

cloud-init 概觀cloud-init overview

cloud-init (英文) 時常用來對初次開機的 Linux VM 進行自訂。cloud-init is a widely used approach to customize a Linux VM as it boots for the first time. 您可以使用 cloud-init 來安裝封裝和寫入檔案,或者設定使用者和安全性。You can use cloud-init to install packages and write files, or to configure users and security. 因為在初次開機程序期間時會呼叫 Cloud-init,因此不需要使用任何額外的步驟或必要的代理程式,就可以套用您的設定。Because cloud-init is called during the initial boot process, there are no additional steps or required agents to apply your configuration. 如需如何正確地設定 #cloud-config 檔案或其他輸入之格式的詳細資訊,請參閱 cloud-init 文件網站 (英文)。For more information on how to properly format your #cloud-config files or other inputs, see the cloud-init documentation site. #cloud-config 檔案是以 base64 編碼的文字檔。#cloud-config files are text files encoded in base64.

cloud-init 也可在不同的發行版本上運作。cloud-init also works across distributions. 例如,您不使用 apt-get installyum install 來安裝套件。For example, you don't use apt-get install or yum install to install a package. 您可以改為定義要安裝的套件清單。Instead you can define a list of packages to install. cloud-init 會針對您選取的發行版本,自動使用原生的套件管理工具。cloud-init automatically uses the native package management tool for the distro you select.

我們正積極與我們背書的 Linux 發行版本合作夥伴合作,在 Azure Marketplace 中提供已啟用雲端初始化的映射。We are actively working with our endorsed Linux distro partners in order to have cloud-init enabled images available in the Azure Marketplace. 這些映像會讓您的 Cloud-init 部署和設定順暢地與 VM 和虛擬機器擴展集運作。These images will make your cloud-init deployments and configurations work seamlessly with VMs and virtual machine scale sets. 一開始,我們會與經過背書的 Linux 發行版本合作夥伴及上游合作,以確保 cloud-init 能在 Azure 上與 OS 搭配運作,然後便會更新套件,並在發行版本套件存放庫中公開提供。Initially we collaborate with the endorsed Linux distro partners and upstream to ensure cloud-init functions with the OS on Azure, then the packages are updated and made publicly available in the distro package repositories.

在 Azure 上於經過背書的 Linux 發行版本 OS 上提供 cloud-init 有兩個階段:套件支援,然後映像支援:There are two stages to making cloud-init available to the endorsed Linux distro OS's on Azure, package support, and then image support:

  • 「Azure 上的 cloud-init 套件支援」會記載接下來有哪些 cloud-init 套件會受到支援或處於預覽狀態,讓您可以在自訂映像中將這些套件與 OS 搭配使用。'cloud-init package support on Azure' documents which cloud-init packages onwards are supported or in preview, so you can use these packages with the OS in a custom image.
  • 「映像 cloud-init 就緒」會記載映像是否已設定為可以使用 cloud-init。'image cloud-init ready' documents if the image is already configured to use cloud-init.

CanonicalCanonical

發行者 / 版本Publisher / Version 供應項目Offer SKUSKU 版本Version 映像 cloud-init 就緒image cloud-init ready Azure 上的 cloud-init 套件支援cloud-init package support on Azure
Canonical 20.04Canonical 20.04 UbuntuServerUbuntuServer 18.04-LTS18.04-LTS 最新latest yes yes
Canonical 18.04Canonical 18.04 UbuntuServerUbuntuServer 18.04-LTS18.04-LTS 最新latest yes yes
Canonical 16.04Canonical 16.04 UbuntuServerUbuntuServer 16.04-LTS16.04-LTS 最新latest yes yes
Canonical 14.04Canonical 14.04 UbuntuServerUbuntuServer 14.04.5-LTS14.04.5-LTS 最新latest yes yes

RHELRHEL

發行者 / 版本Publisher / Version 供應項目Offer SKUSKU 版本Version 映像 cloud-init 就緒image cloud-init ready Azure 上的 cloud-init 套件支援cloud-init package support on Azure
RedHat 7.6RedHat 7.6 RHELRHEL 7-RAW-CI7-RAW-CI 7.6.20190724187.6.2019072418 yes 是 - 從下列套件版本開始支援:18.2-1.el7_6.2yes - support from package version: 18.2-1.el7_6.2
RedHat 7.7RedHat 7.7 RHELRHEL 7-RAW-CI7-RAW-CI 7.7.20190816017.7.2019081601 是 (注意:這是預覽映射 ,不能再使用 ,這將于2020年9月1日移除) yes (note: this is a preview image, and must not be used anymore, this will be removed 1st September 2020) N/AN/A
RedHat 7.7 (Gen1)RedHat 7.7 (Gen1) RHELRHEL 7.77.7 7.7.20200519127.7.2020051912 yes 是 - 從下列套件版本開始支援:18.5-6.el7yes - support from package version: 18.5-6.el7
RedHat 7.7 (Gen2)RedHat 7.7 (Gen2) RHELRHEL 77-gen277-gen2 7.7.20200519137.7.2020051913 yes 是 - 從下列套件版本開始支援:18.5-6.el7yes - support from package version: 18.5-6.el7
RedHat 7.7 (Gen1)RedHat 7.7 (Gen1) RHELRHEL 7-LVM7-LVM 7.7.20200519217.7.2020051921 yes 是 - 從下列套件版本開始支援:18.5-6.el7yes - support from package version: 18.5-6.el7
RedHat 7.7 (Gen2)RedHat 7.7 (Gen2) RHELRHEL 7lvm-gen27lvm-gen2 7.7.20200519227.7.2020051922 yes 是 - 從下列套件版本開始支援:18.5-6.el7yes - support from package version: 18.5-6.el7
RedHat 7.7 (Gen1)RedHat 7.7 (Gen1) rhel-byosrhel-byos rhel-lvm77rhel-lvm77 7.7.202004167.7.20200416 yes 是 - 從下列套件版本開始支援:18.5-6.el7yes - support from package version: 18.5-6.el7
RedHat 8.1 (Gen1)RedHat 8.1 (Gen1) RHELRHEL 8.1-ci8.1-ci 8.1.20200425118.1.2020042511 是 (注意:這是預覽映射,一旦所有 RHEL 8.1 映射都支援 cloud init 之後,這將會在 8 2020 月1日之前移除) yes (note: this is a preview image, and once all RHEL 8.1 images support cloud-init, this will be removed 1st August 2020) 否,預計於 2020 年 6 月完整支援No, ETA for full support June 2020
RedHat 8.1 (Gen2)RedHat 8.1 (Gen2) RHELRHEL 81-ci-gen281-ci-gen2 8.1.20200425248.1.2020042524 是 (注意:這是預覽映射,一旦所有 RHEL 8.1 映射都支援 cloud init 之後,這將會在 8 2020 月1日之前移除) yes (note: this is a preview image, and once all RHEL 8.1 images support cloud-init, this will be removed 1st August 2020) 否,預計於 2020 年 6 月完整支援No, ETA for full support June 2020
  • 所有 RedHat: RHEL 7.8 和 8.2 (Gen1 和 Gen2) 映射都會使用雲端初始化進行布建。All RedHat:RHEL 7.8 and 8.2 (Gen1 and Gen2) images are provisioned using cloud-init.

CentOSCentOS

發行者 / 版本Publisher / Version 供應項目Offer SKUSKU 版本Version 映像 cloud-init 就緒image cloud-init ready Azure 上的 cloud-init 套件支援cloud-init package support on Azure
OpenLogic 7.7OpenLogic 7.7 CentOSCentOS 7-CI7-CI 7.7.201909207.7.20190920 是 (注意:這是預覽映射 ,不能再使用 ,這將于2020年9月1日移除) yes (note: this is a preview image, and must not be used anymore, this will be removed 1st September 2020) N/AN/A
OpenLogic 7.7OpenLogic 7.7 CentOSCentOS 7.77.7 7.7.20200624007.7.2020062400 yes 是-套件版本的支援: 18.5-6.el7.centos.5yes - support from package version: 18.5-6.el7.centos.5
OpenLogic 7.7 (Gen2) OpenLogic 7.7 (Gen2) CentOSCentOS 7_7-gen27_7-gen2 7.7.20200624017.7.2020062401 yes 是-套件版本的支援: 18.5-6.el7.centos.5yes - support from package version: 18.5-6.el7.centos.5
OpenLogic 7.7OpenLogic 7.7 CentOS-HPCCentOS-HPC 7.77.7 7.6.20200626007.6.2020062600 yes 是-套件版本的支援: 18.5-6.el7.centos.5yes - support from package version: 18.5-6.el7.centos.5
OpenLogic 7.7 (Gen2) OpenLogic 7.7 (Gen2) CentOS-HPCCentOS-HPC 7_7-gen27_7-gen2 7.6.20200626017.6.2020062601 yes 是-套件版本的支援: 18.5-6.el7.centos.5yes - support from package version: 18.5-6.el7.centos.5
OpenLogic 8。1OpenLogic 8.1 CentOSCentOS 8_18_1 8.1.20200624008.1.2020062400 yes 是-套件版本的支援: 18.5-7.el8_1.1yes - support from package version: 18.5-7.el8_1.1
OpenLogic 8.1 (Gen2) OpenLogic 8.1 (Gen2) CentOSCentOS 8_1-gen28_1-gen2 8.1.20200624018.1.2020062401 yes 是-套件版本的支援: 18.5-7.el8_1.1yes - support from package version: 18.5-7.el8_1.1
OpenLogic 8。1OpenLogic 8.1 CentOS-HPCCentOS-HPC 8_18_1 8.1.20200624008.1.2020062400 yes 是-套件版本的支援: 18.5-7.el8_1.1yes - support from package version: 18.5-7.el8_1.1
OpenLogic 8.1 (Gen2) OpenLogic 8.1 (Gen2) CentOS-HPC: 8_1-gen2CentOS-HPC:8_1-gen2 8_1-gen28_1-gen2 8.1.20200624018.1.2020062401 yes 是-套件版本的支援: 18.5-7.el8_1.1yes - support from package version: 18.5-7.el8_1.1
  • 所有的 OpenLogic: CentOS 7.8 和 8.2 (Gen1 和 Gen2) 映射都會使用雲端初始化進行布建。All OpenLogic:CentOS 7.8 and 8.2 (Gen1 and Gen2) images are provisioned using cloud-init.

OracleOracle

發行者 / 版本Publisher / Version 供應項目Offer SKUSKU 版本Version 映像 cloud-init 就緒image cloud-init ready Azure 上的 cloud-init 套件支援cloud-init package support on Azure
Oracle 7.7Oracle 7.7 Oracle-LinuxOracle-Linux 77-ci77-ci 7.7.017.7.01 預覽影像 (注意:這是預覽映射,一旦所有 Oracle 7.7 映射都支援 cloud init 之後,這將會在2020年中移除,並會提供通知) preview image (note: this is a preview image, and it once all Oracle 7.7 images support cloud-init, this will be removed mid 2020, notice will be given) 否,處於預覽狀態,套件為:18.5-3.0.1.el7no, in preview, package is: 18.5-3.0.1.el7

SUSE SLESSUSE SLES

這些 SLES 映射已更新為使用雲端初始化進行布建,Gen2 映射變體也已更新。These SLES images have been updated to provision using cloud-init, the Gen2 image variants have also been updated.

  • suse: sles-15-sp1-{basic/byos/hpc/hpc-byos/chost-byos}: gen1:2020.06.10suse:sles-15-sp1-{basic/byos/hpc/hpc-byos/chost-byos}:gen1:2020.06.10
  • suse: sles-sap-15-sp1: gen1:2020.06.10suse:sles-sap-15-sp1:gen1:2020.06.10
  • suse: sles-sap-15-sp1-byos: gen1:2020.06.10suse:sles-sap-15-sp1-byos:gen1:2020.06.10
  • suse: manager-proxy-4-byos: gen1:2020.06.10suse:manager-proxy-4-byos:gen1:2020.06.10
  • suse: manager-伺服器-4-byos: gen1:2020.06.10suse:manager-server-4-byos:gen1:2020.06.10
  • suse: sles-{byos/sap/sap-byos}:15:2020.06.10suse:sles-{byos/sap/sap-byos}:15:2020.06.10
  • suse: sles-12-sp5: gen1:2020.06.10suse:sles-12-sp5:gen1:2020.06.10
  • suse: sles-12-sp5 {-byos/basic/hpc-byos/hpc}: gen1:2020.06.10suse:sles-12-sp5{-byos/basic/hpc-byos/hpc}:gen1:2020.06.10
  • suse: sles-{byos/sap/sap-byos}: 12-sp4:2020.06.10suse:sles-{byos/sap/sap-byos}:12-sp4:2020.06.10
  • suse: sles-{byos/sap/sap-byos}: 12-sp3:2020.06.10suse:sles-{byos/sap/sap-byos}:12-sp3:2020.06.10
  • suse: sles-{byos/sap/sap-byos}: 12-sp2:2020.06.10suse:sles-{byos/sap/sap-byos}:12-sp2:2020.06.10

DebianDebian

發行者 / 版本Publisher / Version 供應項目Offer SKUSKU 版本Version 映像 cloud-init 就緒image cloud-init ready Azure 上的 cloud-init 套件支援cloud-init package support on Azure
debian (Gen1) debian (Gen1) debian-10debian-10 10-于 cloudinit10-cloudinit cloud-init-previewcloud-init-preview 是 (注意:這是預覽映射 ,不能再使用 ,這將于2021年1月1日移除) yes (note: this is a preview image, and must not be used anymore, this will be removed 1st January 2021) 否,處於預覽狀態。No, in preview.
debian (Gen2) debian (Gen2) debian-10debian-10 10-于 cloudinit-gen210-cloudinit-gen2 cloud-init-previewcloud-init-preview 是 (注意:這是預覽映射 ,不能再使用 ,這將于2021年1月1日移除) yes (note: this is a preview image, and must not be used anymore, this will be removed 1st January 2021) 否,處於預覽狀態。No, in preview.
debian (Gen1) debian (Gen1) debian-10debian-10 10-于 cloudinit10-cloudinit 10:0.20201013.42210:0.20201013.422 yes 是-套件版本的支援: 20.2-2~deb10u1yes - support from package version: 20.2-2~deb10u1
debian (Gen2) debian (Gen2) debian-10debian-10 10-于 cloudinit-gen210-cloudinit-gen2 0.20201013.4220.20201013.422 yes 是-套件版本的支援: 20.2-2~deb10u1yes - support from package version: 20.2-2~deb10u1

目前 Azure Stack 將會支援已啟用 cloud-init 之映像的佈建。Currently Azure Stack will support the provisioning of cloud-init enabled images.

cloud-init 和 Linux 代理程式 (WALA) 之間有哪些差異?What is the difference between cloud-init and the Linux Agent (WALA)?

WALA 是 Azure 平台專屬的代理程式,用於佈建及設定 VM,以及處理 Azure 延伸模組 (部分機器翻譯)。WALA is an Azure platform-specific agent used to provision and configure VMs, and handle Azure extensions.

我們正在加強設定 VM 以使用 cloud-init 取代 Linux 代理程式的工作,以讓現有的 cloud-init 客戶使用其目前的 cloud-init 指令碼,或是讓新客戶利用豐富的 cloud-init 設定功能。We are enhancing the task of configuring VMs to use cloud-init instead of the Linux Agent in order to allow existing cloud-init customers to use their current cloud-init scripts, or new customers to take advantage of the rich cloud-init configuration functionality. 如果您已經對用於設定 Linux 系統所的 cloud-init 指令碼有所投入,則您 不需要其他任何設定 就可以讓 cloud-init 加以處理。If you have existing investments in cloud-init scripts for configuring Linux systems, there are no additional settings required to enable cloud-init process them.

由於 cloud-init 無法處理 Azure 延伸模組,因此映像中仍須 WALA 來處理延伸模組,但必須停用其佈建程式碼;針對正在轉換為透過 cloud-init 佈建之經過背書的 Linux 發行版本映像,其將已安裝 WALA 且正確設定。cloud-init cannot process Azure extensions, so WALA is still required in the image to process extensions, but will need to have its provisioning code disabled, for endorsed Linux distros images that are being converted to provision by cloud-init, they will have WALA installed, and setup correctly.

建立 VM 時,如果您在佈建階段未包含 Azure CLI --custom-data 參數,cloud-init 或 WALA 會採用所需的最低限度 VM 佈建參數來佈建 VM,並使用預設值完成部署。When creating a VM, if you do not include the Azure CLI --custom-data switch at provisioning time, cloud-init or WALA takes the minimal VM provisioning parameters required to provision the VM and complete the deployment with the defaults. 如果您使用 --custom-data 參數來參考 cloud-init 設定,您自訂資料中所包含的所有內容都會在 VM 開機時提供給 cloud-init 使用。If you reference the cloud-init configuration with the --custom-data switch, whatever is contained in your custom data will be available to cloud-init when the VM boots.

套用到 VM 的 cloud-init 設定沒有時間限制,且不會因為逾時而導致部署失敗。WALA 並非如此,如果您變更 WALA 預設值以處理自訂資料,其不能超過 40 分鐘的總 VM 佈建時間額度;如果超過,VM Create 將會失敗。cloud-init configurations applied to VMs do not have time constraints and will not cause a deployment to fail by timing out. This is not true for WALA, if you change the WALA defaults to process custom-data, it cannot exceed the total VM provisioning time allowance of 40mins, if so, the VM Create will fail.

部署支援 cloud-init 的虛擬機器Deploying a cloud-init enabled Virtual Machine

部署支援 cloud-init 的虛擬機器就像在部署期間參考支援 cloud-init 的發行版本一樣簡單。Deploying a cloud-init enabled virtual machine is as simple as referencing a cloud-init enabled distribution during deployment. Linux 發行版本維護者必須選擇啟用,並將 cloud-init 整合到其基礎 Azure 發行映像。Linux distribution maintainers have to choose to enable and integrate cloud-init into their base Azure published images. 一旦確認您想要部署的映像支援 cloud-init,您就可以使用 Azure CLI 來部署映像。Once you have confirmed the image you want to deploy is cloud-init enabled, you can use the Azure CLI to deploy the image.

部署此映像的第一個步驟是使用 az group create 命令建立資源群組。The first step in deploying this image is to create a resource group with the az group create command. Azure 資源群組是在其中部署與管理 Azure 資源的邏輯容器。An Azure resource group is a logical container into which Azure resources are deployed and managed.

下列範例會在 eastus 位置建立名為 myResourceGroup 的資源群組。The following example creates a resource group named myResourceGroup in the eastus location.

az group create --name myResourceGroup --location eastus

下一個步驟是在您目前的殼層中建立名為 cloud-init.txt 的檔案,並貼上下列設定。The next step is to create a file in your current shell, named cloud-init.txt and paste the following configuration. 針對此案例,在 Cloud Shell 中 (而不是本機電腦上) 建立該檔案。For this example, create the file in the Cloud Shell not on your local machine. 您可以使用任何您想要的編輯器。You can use any editor you wish. 輸入 sensible-editor cloud-init.txt 可建立檔案,並查看可用的編輯器清單。Enter sensible-editor cloud-init.txt to create the file and see a list of available editors. 建議首先選擇使用 nano 編輯器。Choose #1 to use the nano editor. 請確定已正確複製整個 cloud-init 檔案,特別是第一行:Make sure that the whole cloud-init file is copied correctly, especially the first line:

#cloud-config
package_upgrade: true
packages:
  - httpd

注意

cloud init 具有多個 輸入類型,而雲端初始會使用 CustomData/userData 的第一行來指出它應該如何處理輸入,例如, #cloud-config 表示應將內容視為雲端初始設定處理。cloud-init has multiple input types, cloud-init will use first line of the customData/userData to indicate how it should process the input, for example #cloud-config indicates a the content should be processed as a cloud-init config.

按下 ctrl-X 以結束檔案、輸入 y 以儲存檔案,然後按下 enter 以在結束時確認檔案名稱。Press ctrl-X to exit the file, type y to save the file and press enter to confirm the file name on exit.

最後一個步驟是使用 az vm create 命令來建立 VM。The final step is to create a VM with the az vm create command.

下列範例會建立名為 centos74 的 VM,並建立 SSH 金鑰 (如果它們不存在於預設金鑰位置)。The following example creates a VM named centos74 and creates SSH keys if they do not already exist in a default key location. 若要使用一組特定金鑰,請使用 --ssh-key-value 選項。To use a specific set of keys, use the --ssh-key-value option. 使用 --custom-data 參數以傳入 cloud-init 組態檔。Use the --custom-data parameter to pass in your cloud-init config file. 如果您將檔案儲存於目前工作目錄之外的位置,請提供 cloud-init.txt 組態的完整路徑。Provide the full path to the cloud-init.txt config if you saved the file outside of your present working directory.

az vm create \
  --resource-group myResourceGroup \
  --name centos74 \
  --image OpenLogic:CentOS-CI:7-CI:latest \
  --custom-data cloud-init.txt \
  --generate-ssh-keys 

建立 VM 後,Azure CLI 會顯示您部署專屬的資訊。When the VM has been created, the Azure CLI shows information specific to your deployment. 記下 publicIpAddressTake note of the publicIpAddress. 此位址用來存取 VM。This address is used to access the VM. 系統需要花一些時間建立 VM、安裝套件以及啟動應用程式。It takes some time for the VM to be created, the packages to install, and the app to start. 在 Azure CLI 將您返回提示字元之後,背景工作會繼續執行。There are background tasks that continue to run after the Azure CLI returns you to the prompt. 您可以透過 SSH 連線到 VM,並使用「疑難排解」一節中所述的步驟來檢視 cloud-init 記錄。You can SSH into the VM and use the steps outlined in the Troubleshooting section to view the cloud-init logs.

您也可以 在 ARM 範本中傳遞參數,以部署已啟用雲端初始化的 VM。You can also deploy a cloud-init enabled VM by passing the parameters in ARM template.

針對 cloud-init 進行疑難排解Troubleshooting cloud-init

一旦佈建 VM,Cloud-init 將會執行 --custom-data 中定義的所有模組和指令碼,以設定 VM。Once the VM has been provisioned, cloud-init will run through all the modules and script defined in --custom-data in order to configure the VM. 如果您需要針對設定中的任何錯誤或遺漏進行疑難排解,則需要在 cloud-init 記錄 (位於 /var/log/cloud-init.log) 中搜尋模組名稱 (例如 disk_setupruncmd)。If you need to troubleshoot any errors or omissions from the configuration, you need to search for the module name (disk_setup or runcmd for example) in the cloud-init log - located in /var/log/cloud-init.log.

注意

並非每個模組失敗都會導致嚴重的 cloud-init 整體設定失敗。Not every module failure results in a fatal cloud-init overall configuration failure. 例如,使用 runcmd 模組時,如果指令碼失敗,cloud-init 會因為執行了 runcmd 模組而仍然報告佈建成功,。For example, using the runcmd module, if the script fails, cloud-init will still report provisioning succeeded because the runcmd module executed.

如需 cloud-init 記錄的詳細資訊,請參閱 cloud-init 文件 (英文)For more details of cloud-init logging, refer to the cloud-init documentation

後續步驟Next steps

針對雲端初始化的問題進行疑難排解Troubleshoot issues with cloud-init.

如需設定變更的 cloud-init 範例,請參閱下列文件:For cloud-init examples of configuration changes, see the following documents: