您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

为 Azure 准备基于 CentOS 的虚拟机Prepare a CentOS-based virtual machine for Azure

备注

Azure 具有用于创建和处理资源的两个不同的部署模型:Resource Manager 和经典Azure has two different deployment models for creating and working with resources: Resource Manager and classic. 这篇文章介绍如何使用这两种模型,但 Microsoft 建议大多数最新部署使用 Resource Manager 模型。This article covers using both models, but Microsoft recommends that most new deployments use the Resource Manager model.

先决条件Prerequisites

本文假设已在虚拟硬盘中安装了 CentOS(或类似的衍生产品)Linux 操作系统。This article assumes that you have already installed a CentOS (or similar derivative) Linux operating system to a virtual hard disk. 存在多个用于创建 .vhd 文件的工具,例如 Hyper-V 等虚拟化解决方案。Multiple tools exist to create .vhd files, for example a virtualization solution such as Hyper-V. 有关说明,请参阅安装 Hyper-V 角色和配置虚拟机For instructions, see Install the Hyper-V Role and Configure a Virtual Machine.

CentOS 安装说明CentOS installation notes

  • 另请参阅常规 Linux 安装说明,获取更多有关如何为 Azure 准备 Linux 的提示。Please see also General Linux Installation Notes for more tips on preparing Linux for Azure.
  • Azure 不支持 VHDX 格式,仅支持固定大小的 VHDThe VHDX format is not supported in Azure, only fixed VHD. 可使用 Hyper-V 管理器或 convert-vhd cmdlet 将磁盘转换为 VHD 格式。You can convert the disk to VHD format using Hyper-V Manager or the convert-vhd cmdlet. 如果使用 VirtualBox,则意味着选择的是“固定大小”,而不是在创建磁盘时动态分配默认大小。If you are using VirtualBox this means selecting Fixed size as opposed to the default dynamically allocated when creating the disk.
  • 在安装 Linux 系统时,建议使用标准分区而不是 LVM(通常是许多安装的默认值)。When installing the Linux system it is recommended that you use standard partitions rather than LVM (often the default for many installations). 这会避免 LVM 与克隆 VM 发生名称冲突,特别是在 OS 磁盘需要连接到另一台相同的 VM 进行故障排除的情况下。This will avoid LVM name conflicts with cloned VMs, particularly if an OS disk ever needs to be attached to another identical VM for troubleshooting. LVMRAID 可以在数据磁盘上使用。LVM or RAID may be used on data disks.
  • 需要装载 UDF 文件系统的内核支持。Kernel support for mounting UDF file systems is required. 在 Azure 上首次启动时,预配配置将通过附加到来宾的 UDF 格式媒体传递到 Linux VM。At first boot on Azure the provisioning configuration is passed to the Linux VM via UDF-formatted media that is attached to the guest. Azure Linux 代理必须能够装载 UDF 文件系统才能读取其配置和预配 VM。The Azure Linux agent must be able to mount the UDF file system to read its configuration and provision the VM.
  • 低于 2.6.37 的 Linux 内核版本不支持具有更大 VM 大小的 Hyper-V 上的 NUMA。Linux kernel versions below 2.6.37 do not support NUMA on Hyper-V with larger VM sizes. 此问题主要影响使用上游 Red Hat 2.6.32 内核的旧分发版,在 RHEL 6.6 (kernel-2.6.32-504) 中已解决。This issue primarily impacts older distributions using the upstream Red Hat 2.6.32 kernel, and was fixed in RHEL 6.6 (kernel-2.6.32-504). 运行版本低于 2.6.37 的自定义内核的系统,或者版本低于 2.6.32-504 的基于 RHEL 的内核必须在 grub.conf 中的内核命令行上设置启动参数 numa=offSystems running custom kernels older than 2.6.37, or RHEL-based kernels older than 2.6.32-504 must set the boot parameter numa=off on the kernel command-line in grub.conf. 有关详细信息,请参阅 Red Hat KB 436883For more information see Red Hat KB 436883.
  • 不要在操作系统磁盘上配置交换分区。Do not configure a swap partition on the OS disk. 可以配置 Linux 代理,以在临时资源磁盘上创建交换文件。The Linux agent can be configured to create a swap file on the temporary resource disk. 可以在下面的步骤中找到有关此内容的详细信息。More information about this can be found in the steps below.
  • Azure 上的所有 VHD 必须已将虚拟大小调整为 1MB。All VHDs on Azure must have a virtual size aligned to 1MB. 从原始磁盘转换为 VHD 时,必须确保在转换前原始磁盘大小是 1MB 的倍数。When converting from a raw disk to VHD you must ensure that the raw disk size is a multiple of 1MB before conversion. 有关详细信息,请参阅 Linux 安装说明See Linux Installation Notes for more information.

CentOS 6.xCentOS 6.x

  1. 在 Hyper-V 管理器中,选择虚拟机。In Hyper-V Manager, select the virtual machine.

  2. 单击“连接”打开该虚拟机的控制台窗口。Click Connect to open a console window for the virtual machine.

  3. 在 CentOS 6 中,NetworkManager 可能会干扰 Azure Linux 代理。In CentOS 6, NetworkManager can interfere with the Azure Linux agent. 运行以下命令卸载此包:Uninstall this package by running the following command:

     # sudo rpm -e --nodeps NetworkManager
    
  4. 创建或编辑文件 /etc/sysconfig/network 并添加以下文本:Create or edit the file /etc/sysconfig/network and add the following text:

     NETWORKING=yes
     HOSTNAME=localhost.localdomain
    
  5. 创建或编辑文件 /etc/sysconfig/network-scripts/ifcfg-eth0 并添加以下文本:Create or edit the file /etc/sysconfig/network-scripts/ifcfg-eth0 and add the following text:

     DEVICE=eth0
     ONBOOT=yes
     BOOTPROTO=dhcp
     TYPE=Ethernet
     USERCTL=no
     PEERDNS=yes
     IPV6INIT=no
    
  6. 修改 udev 规则,以避免产生以太网接口的静态规则。Modify udev rules to avoid generating static rules for the Ethernet interface(s). 在 Microsoft Azure 或 Hyper-V 中克隆虚拟机时,这些规则可能会引发问题:These rules can cause problems when cloning a virtual machine in Microsoft Azure or Hyper-V:

     # sudo ln -s /dev/null /etc/udev/rules.d/75-persistent-net-generator.rules
     # sudo rm -f /etc/udev/rules.d/70-persistent-net.rules
    
  7. 通过运行以下命令,确保网络服务会在引导时启动:Ensure the network service will start at boot time by running the following command:

     # sudo chkconfig network on
    
  8. 如果要使用 Azure 数据中心托管的 OpenLogic 镜像,则将 /etc/yum.repos.d/CentOS-Base.repo 文件替换为以下存储库。If you would like to use the OpenLogic mirrors that are hosted within the Azure datacenters, then replace the /etc/yum.repos.d/CentOS-Base.repo file with the following repositories. 这还会添加包含 Azure Linux 代理等其他包的 [openlogic] 存储库:This will also add the [openlogic] repository that includes additional packages such as the Azure Linux agent:

     [openlogic]
     name=CentOS-$releasever - openlogic packages for $basearch
     baseurl=http://olcentgbl.trafficmanager.net/openlogic/$releasever/openlogic/$basearch/
     enabled=1
     gpgcheck=0
    
     [base]
     name=CentOS-$releasever - Base
     #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
     baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/os/$basearch/
     gpgcheck=1
     gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    
     #released updates
     [updates]
     name=CentOS-$releasever - Updates
     #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
     baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/updates/$basearch/
     gpgcheck=1
     gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    
     #additional packages that may be useful
     [extras]
     name=CentOS-$releasever - Extras
     #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
     baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/extras/$basearch/
     gpgcheck=1
     gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    
     #additional packages that extend functionality of existing packages
     [centosplus]
     name=CentOS-$releasever - Plus
     #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
     baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/centosplus/$basearch/
     gpgcheck=1
     enabled=0
     gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    
     #contrib - packages by Centos Users
     [contrib]
     name=CentOS-$releasever - Contrib
     #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib&infra=$infra
     baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/contrib/$basearch/
     gpgcheck=1
     enabled=0
     gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    

    备注

    本指南的余下部分假设你至少会使用 [openlogic] 存储库,下面将使用该存储库安装 Azure Linux 代理。The rest of this guide will assume you are using at least the [openlogic] repo, which will be used to install the Azure Linux agent below.

  9. 将下列行添加到 /etc/yum.conf:Add the following line to /etc/yum.conf:

     http_caching=packages
    
  10. 运行以下命令清除当前 yum 元数据并使用最新包更新系统:Run the following command to clear the current yum metadata and update the system with the latest packages:

    # yum clean all
    

    除非要创建较旧版本的 CentOS 的映像,否则建议将所有包更新到最新:Unless you are creating an image for an older version of CentOS, it is recommended to update all the packages to the latest:

    # sudo yum -y update
    

    运行此命令后,可能需要重新启动。A reboot may be required after running this command.

  11. (可选)安装适用于 Linux Integration Services (LIS) 的驱动程序。(Optional) Install the drivers for the Linux Integration Services (LIS).

    重要

    此步骤对于 CentOS 6.3 和更早版本是必需的,对于之后的版本是可选的。The step is required for CentOS 6.3 and earlier, and optional for later releases.

    # sudo rpm -e hypervkvpd  ## (may return error if not installed, that's OK)
    # sudo yum install microsoft-hyper-v
    

    此外,可以按照 LIS 下载页上的手动安装说明操作将 RPM 安装到 VM 中。Alternatively, you can follow the manual installation instructions on the LIS download page to install the RPM onto your VM.

  12. 安装 Azure Linux 代理和依赖项:Install the Azure Linux Agent and dependencies:

    # sudo yum install python-pyasn1 WALinuxAgent
    

    如果没有按步骤 3 中所述删除 NetworkManager 包和 NetworkManager-gnome 包,则安装 WALinuxAgent 包时会删除它们。The WALinuxAgent package will remove the NetworkManager and NetworkManager-gnome packages if they were not already removed as described in step 3.

  13. 在 grub 配置中修改内核引导行,以使其包含 Azure 的其他内核参数。Modify the kernel boot line in your grub configuration to include additional kernel parameters for Azure. 为此,请在文本编辑器中打开 /boot/grub/menu.lst,并确保默认内核包含以下参数:To do this, open /boot/grub/menu.lst in a text editor and ensure that the default kernel includes the following parameters:

    console=ttyS0 earlyprintk=ttyS0 rootdelay=300
    

    这还将确保所有控制台消息都发送到第一个串行端口,从而可以协助 Azure 支持人员调试问题。This will also ensure all console messages are sent to the first serial port, which can assist Azure support with debugging issues.

    除此之外,建议删除以下参数:In addition to the above, it is recommended to remove the following parameters:

    rhgb quiet crashkernel=auto
    

    图形引导和无人参与引导不适用于云环境,在该环境中我们想要将所有日志都发送到串行端口。Graphical and quiet boot are not useful in a cloud environment where we want all the logs to be sent to the serial port. 根据需要可以配置 crashkernel 选项,但请注意此参数会使虚拟机中的可用内存量减少 128MB 或更多,这在较小的虚拟机上可能会出现问题。The crashkernel option may be left configured if desired, but note that this parameter will reduce the amount of available memory in the VM by 128MB or more, which may be problematic on the smaller VM sizes.

    重要

    CentOS 6.5 和更早版本还必须设置内核参数 numa=offCentOS 6.5 and earlier must also set the kernel parameter numa=off. 请参阅 Red Hat KB 436883See Red Hat KB 436883.

  14. 请确保已安装 SSH 服务器且已将其配置为在引导时启动。Ensure that the SSH server is installed and configured to start at boot time. 这通常是默认设置。This is usually the default.

  15. 不要在 OS 磁盘上创建交换空间。Do not create swap space on the OS disk.

    Azure Linux 代理可使用在 Azure 上设置后附加到虚拟机的本地资源磁盘自动配置交换空间。The Azure Linux Agent can automatically configure swap space using the local resource disk that is attached to the VM after provisioning on Azure. 请注意,本地资源磁盘是临时磁盘,并可能在取消预配 VM 时被清空。Note that the local resource disk is a temporary disk, and might be emptied when the VM is deprovisioned. 在安装 Azure Linux 代理(请参见前一步骤)后,相应地在 /etc/waagent.conf 中修改以下参数:After installing the Azure Linux Agent (see previous step), modify the following parameters in /etc/waagent.conf appropriately:

    ResourceDisk.Format=y
    ResourceDisk.Filesystem=ext4
    ResourceDisk.MountPoint=/mnt/resource
    ResourceDisk.EnableSwap=y
    ResourceDisk.SwapSizeMB=2048    ## NOTE: set this to whatever you need it to be.
    
  16. 运行以下命令可取消对虚拟机的设置并且对其进行准备以便在 Azure 上进行设置:Run the following commands to deprovision the virtual machine and prepare it for provisioning on Azure:

    # sudo waagent -force -deprovision
    # export HISTSIZE=0
    # logout
    
  17. 在 Hyper-V 管理器中单击“操作”->“关闭”。Click Action -> Shut Down in Hyper-V Manager. Linux VHD 现已准备好上传到 Azure。Your Linux VHD is now ready to be uploaded to Azure.


CentOS 7.0+CentOS 7.0+

CentOS 7(和类似衍生产品)中的更改Changes in CentOS 7 (and similar derivatives)

为 Azure 准备 CentOS 7 虚拟机非常类似于 CentOS 6,但有几个值得注意的重要区别:Preparing a CentOS 7 virtual machine for Azure is very similar to CentOS 6, however there are several important differences worth noting:

  • NetworkManager 包不再与 Azure Linux 代理冲突。The NetworkManager package no longer conflicts with the Azure Linux agent. 默认情况下将安装此包,建议不要删除它。This package is installed by default and we recommend that it is not removed.
  • GRUB2 现在用作默认引导加载程序,因此用于编辑内核参数的过程已更改(请参见下文)。GRUB2 is now used as the default bootloader, so the procedure for editing kernel parameters has changed (see below).
  • XFS 现在是默认文件系统。XFS is now the default file system. 如果需要,仍可以使用 ext4 文件系统。The ext4 file system can still be used if desired.

配置步骤Configuration Steps

  1. 在 Hyper-V 管理器中,选择虚拟机。In Hyper-V Manager, select the virtual machine.

  2. 单击“连接”打开该虚拟机的控制台窗口。Click Connect to open a console window for the virtual machine.

  3. 创建或编辑文件 /etc/sysconfig/network 并添加以下文本:Create or edit the file /etc/sysconfig/network and add the following text:

     NETWORKING=yes
     HOSTNAME=localhost.localdomain
    
  4. 创建或编辑文件 /etc/sysconfig/network-scripts/ifcfg-eth0 并添加以下文本:Create or edit the file /etc/sysconfig/network-scripts/ifcfg-eth0 and add the following text:

     DEVICE=eth0
     ONBOOT=yes
     BOOTPROTO=dhcp
     TYPE=Ethernet
     USERCTL=no
     PEERDNS=yes
     IPV6INIT=no
     NM_CONTROLLED=no
    
  5. 修改 udev 规则,以避免产生以太网接口的静态规则。Modify udev rules to avoid generating static rules for the Ethernet interface(s). 在 Microsoft Azure 或 Hyper-V 中克隆虚拟机时,这些规则可能会引发问题:These rules can cause problems when cloning a virtual machine in Microsoft Azure or Hyper-V:

     # sudo ln -s /dev/null /etc/udev/rules.d/75-persistent-net-generator.rules
    
  6. 如果要使用 Azure 数据中心托管的 OpenLogic 镜像,则将 /etc/yum.repos.d/CentOS-Base.repo 文件替换为以下存储库。If you would like to use the OpenLogic mirrors that are hosted within the Azure datacenters, then replace the /etc/yum.repos.d/CentOS-Base.repo file with the following repositories. 这还会添加包含 Azure Linux 代理包的 [openlogic] 存储库:This will also add the [openlogic] repository that includes packages for the Azure Linux agent:

     [openlogic]
     name=CentOS-$releasever - openlogic packages for $basearch
     baseurl=http://olcentgbl.trafficmanager.net/openlogic/$releasever/openlogic/$basearch/
     enabled=1
     gpgcheck=0
    
     [base]
     name=CentOS-$releasever - Base
     #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
     baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/os/$basearch/
     gpgcheck=1
     gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
     #released updates
     [updates]
     name=CentOS-$releasever - Updates
     #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
     baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/updates/$basearch/
     gpgcheck=1
     gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
     #additional packages that may be useful
     [extras]
     name=CentOS-$releasever - Extras
     #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
     baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/extras/$basearch/
     gpgcheck=1
     gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
     #additional packages that extend functionality of existing packages
     [centosplus]
     name=CentOS-$releasever - Plus
     #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
     baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/centosplus/$basearch/
     gpgcheck=1
     enabled=0
     gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    

    备注

    本指南的余下部分假设你至少会使用 [openlogic] 存储库,下面将使用该存储库安装 Azure Linux 代理。The rest of this guide will assume you are using at least the [openlogic] repo, which will be used to install the Azure Linux agent below.

  7. 运行以下命令以清除当前 yum 元数据并安装所有更新:Run the following command to clear the current yum metadata and install any updates:

     # sudo yum clean all
    

    除非要创建较旧版本的 CentOS 的映像,否则建议将所有包更新到最新:Unless you are creating an image for an older version of CentOS, it is recommended to update all the packages to the latest:

     # sudo yum -y update
    

    运行此命令后,可能需要重新启动。A reboot maybe required after running this command.

  8. 在 grub 配置中修改内核引导行,以使其包含 Azure 的其他内核参数。Modify the kernel boot line in your grub configuration to include additional kernel parameters for Azure. 为此,请在文本编辑器中打开 /etc/default/grub 并编辑 GRUB_CMDLINE_LINUX 参数,例如:To do this, open /etc/default/grub in a text editor and edit the GRUB_CMDLINE_LINUX parameter, for example:

     GRUB_CMDLINE_LINUX="rootdelay=300 console=ttyS0 earlyprintk=ttyS0 net.ifnames=0"
    

    这还将确保所有控制台消息都发送到第一个串行端口,从而可以协助 Azure 支持人员调试问题。This will also ensure all console messages are sent to the first serial port, which can assist Azure support with debugging issues. 此外,还会关闭 NIC 的新 CentOS 7 命名约定。It also turns off the new CentOS 7 naming conventions for NICs. 除此之外,建议删除以下参数:In addition to the above, it is recommended to remove the following parameters:

     rhgb quiet crashkernel=auto
    

    图形引导和无人参与引导不适用于云环境,在该环境中我们想要将所有日志都发送到串行端口。Graphical and quiet boot are not useful in a cloud environment where we want all the logs to be sent to the serial port. 根据需要可以配置 crashkernel 选项,但请注意此参数会使虚拟机中的可用内存量减少 128MB 或更多,这在较小的虚拟机上可能会出现问题。The crashkernel option may be left configured if desired, but note that this parameter will reduce the amount of available memory in the VM by 128MB or more, which may be problematic on the smaller VM sizes.

  9. 按照上面所示完成编辑 /etc/default/grub 后,运行以下命令以重新生成 grub 配置:Once you are done editing /etc/default/grub per above, run the following command to rebuild the grub configuration:

     # sudo grub2-mkconfig -o /boot/grub2/grub.cfg
    
  10. 如果要从 VMWare、VirtualBox 或 KVM 中生成映像:请确保 initramfs 中包含 Hyper-V 驱动程序:If building the image from VMWare, VirtualBox or KVM: Ensure the Hyper-V drivers are included in the initramfs:

    编辑 /etc/dracut.conf,添加内容:Edit /etc/dracut.conf, add content:

    add_drivers+=”hv_vmbus hv_netvsc hv_storvsc”
    

    重新生成 initramfs:Rebuild the initramfs:

    # sudo dracut –f -v
    
  11. 安装 Azure Linux 代理和依赖项:Install the Azure Linux Agent and dependencies:

    # sudo yum install python-pyasn1 WALinuxAgent
    # sudo systemctl enable waagent
    
  12. 不要在 OS 磁盘上创建交换空间。Do not create swap space on the OS disk.

    Azure Linux 代理可使用在 Azure 上设置后附加到虚拟机的本地资源磁盘自动配置交换空间。The Azure Linux Agent can automatically configure swap space using the local resource disk that is attached to the VM after provisioning on Azure. 请注意,本地资源磁盘是临时磁盘,并可能在取消预配 VM 时被清空。Note that the local resource disk is a temporary disk, and might be emptied when the VM is deprovisioned. 在安装 Azure Linux 代理(请参见前一步骤)后,相应地在 /etc/waagent.conf 中修改以下参数:After installing the Azure Linux Agent (see previous step), modify the following parameters in /etc/waagent.conf appropriately:

    ResourceDisk.Format=y
    ResourceDisk.Filesystem=ext4
    ResourceDisk.MountPoint=/mnt/resource
    ResourceDisk.EnableSwap=y
    ResourceDisk.SwapSizeMB=2048    ## NOTE: set this to whatever you need it to be.
    
  13. 运行以下命令可取消对虚拟机的设置并且对其进行准备以便在 Azure 上进行设置:Run the following commands to deprovision the virtual machine and prepare it for provisioning on Azure:

    # sudo waagent -force -deprovision
    # export HISTSIZE=0
    # logout
    
  14. 在 Hyper-V 管理器中单击“操作”->“关闭”。Click Action -> Shut Down in Hyper-V Manager. Linux VHD 现已准备好上传到 Azure。Your Linux VHD is now ready to be uploaded to Azure.

后续步骤Next steps

现在,可以使用 CentOS Linux 虚拟硬盘在 Azure 中创建新的 Azure 虚拟机。You're now ready to use your CentOS Linux virtual hard disk to create new virtual machines in Azure. 如果是首次将 .vhd 文件上传到 Azure,请参阅从自定义磁盘创建 Linux VMIf this is the first time that you're uploading the .vhd file to Azure, see Create a Linux VM from a custom disk.