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

在 Azure 上优化 Linux VMOptimize your Linux VM on Azure

通过命令行或门户创建运行 Linux 虚拟机 (VM) 是一项很简单的操作。Creating a Linux virtual machine (VM) is easy to do from the command line or from the portal. 本教程说明如何在 Microsoft Azure 平台上设置 VM 以确保优化其性能。This tutorial shows you how to ensure you have set it up to optimize its performance on the Microsoft Azure platform. 本主题使用 Ubuntu Server VM,不过你也可以将自己的映像作为模板来创建 Linux 虚拟机。This topic uses an Ubuntu Server VM, but you can also create Linux virtual machine using your own images as templates.

先决条件Prerequisites

本主题假设用户已有一个有效的 Azure 订阅(注册免费试用版),并已在 Azure 订阅中预配 VM。This topic assumes you already have a working Azure subscription (free trial signup) and have already provisioned a VM into your Azure subscription. 创建 VM 之前,请确保已安装最新的 Azure CLI 并使用 az login 登录到 Azure 订阅。Make sure that you have the latest Azure CLI installed and logged in to your Azure subscription with az login before you create a VM.

Azure OS 磁盘Azure OS Disk

在 Azure 中创建 Linux VM 后,它将具有两个与之关联的磁盘。Once you create a Linux VM in Azure, it has two disks associated with it. /dev/sda 是 OS 磁盘,/dev/sdb 是临时磁盘。/dev/sda is your OS disk, /dev/sdb is your temporary disk. 请勿将主要 OS 磁盘 (/dev/sda) 用于操作系统以外的用途,因为它已针对快速启动 VM 进行优化,无法为工作负荷提供良好的性能。Do not use the main OS disk (/dev/sda) for anything except the operating system as it is optimized for fast VM boot time and does not provide good performance for your workloads. 要获得持久且经过优化的数据存储,可以将一个或多个磁盘附加到 VM。You want to attach one or more disks to your VM to get persistent and optimized storage for your data.

添加磁盘以实现大小和性能目标Adding Disks for Size and Performance targets

根据 VM 大小,可以分别在 A 系列、D 系列和 G 系列计算机上额外附加最多 16 个、32 个和 64 个磁盘,每个磁盘最大可为 1 TB。Based on the VM size, you can attach up to 16 additional disks on an A-Series, 32 disks on a D-Series and 64 disks on a G-Series machine - each up to 1 TB in size. 可以根据空间和 IOps 要求以及自己的需要添加额外的磁盘。You add extra disks as needed per your space and IOps requirements. 标准存储的每个磁盘的性能目标为 500 IOps,高级存储的每个磁盘的性能目标最高为 5000 IOps。Each disk has a performance target of 500 IOps for Standard Storage and up to 5000 IOps per disk for Premium Storage. 有关高级存储磁盘的详细信息,请参阅高级存储:适用于 Azure VM 的高性能存储For more information about Premium Storage disks, see Premium Storage: High-Performance Storage for Azure VMs

对于缓存设置为“ReadOnly”或“None”的高级存储磁盘,必须在 Linux 中装入文件系统时禁用“barrier”(屏障)才能达到最高 IOps。To achieve the highest IOps on Premium Storage disks where their cache settings have been set to either ReadOnly or None, you must disable barriers while mounting the file system in Linux. 不需要屏障,因为写入高级存储支持的磁盘对于这些缓存设置是持久的。You do not need barriers because the writes to Premium Storage backed disks are durable for these cache settings.

  • 如果使用的是 reiserFS,请使用装入选项 barrier=none 禁用屏障(若要启用屏障,请使用 barrier=flushIf you use reiserFS, disable barriers using the mount option barrier=none (For enabling barriers, use barrier=flush)
  • 如果使用的是 ext3/ext4,请使用装入选项 barrier=0 禁用屏障(若要启用屏障,请使用 barrier=1If you use ext3/ext4, disable barriers using the mount option barrier=0 (For enabling barriers, use barrier=1)
  • 如果使用的是 XFS,请使用装入选项 nobarrier 禁用屏障(若要启用屏障,请使用 barrierIf you use XFS, disable barriers using the mount option nobarrier (For enabling barriers, use the option barrier)

非托管存储帐户注意事项Unmanaged storage account considerations

使用 Azure CLI 创建 VM 时的默认操作是使用 Azure 托管磁盘。The default action when you create a VM with the Azure CLI is to use Azure Managed Disks. 这些磁盘由 Azure 平台处理,无需任何准备或位置来存储它们。These disks are handled by the Azure platform and do not require any preparation or location to store them. 非托管磁盘需要存储帐户,且需要进行更多的性能考虑。Unmanaged disks require a storage account and have some additional performance considerations. 有关托管磁盘的详细信息,请参阅 Azure 托管磁盘概述For more information about managed disks, see Azure Managed Disks overview. 以下部分概述的性能注意事项仅适用于用户使用非托管磁盘的情况。The following section outlines performance considerations only when you use unmanaged disks. 同样,默认的和建议的存储解决方案是使用托管磁盘。Again, the default and recommended storage solution is to use managed disks.

如果使用非托管磁盘创建 VM,请务必从区域与 VM 相同的存储帐户附加磁盘,以确保高度邻近性并降低网络延迟。If you create a VM with unmanaged disks, make sure that you attach disks from storage accounts residing in the same region as your VM to ensure close proximity and minimize network latency. 每个标准存储帐户最多有 20k IOps 和 500 TB 大小的容量。Each Standard storage account has a maximum of 20k IOps and a 500 TB size capacity. 此限制大约相当于 40 个重度使用的磁盘,包括 OS 磁盘和用户创建的任何数据磁盘。This limit works out to approximately 40 heavily used disks including both the OS disk and any data disks you create. 高级存储帐户没有 IOps 上限,但有 32 TB 的大小限制。For Premium Storage accounts, there is no Maximum IOps limit but there is a 32 TB size limit.

在处理 IOps 很高的工作负荷时,如果为磁盘选择了标准存储,则可能需要将磁盘拆分到多个存储帐户才能避免达到标准存储帐户 20,000 IOps 的限制。When dealing with high IOps workloads and you have chosen Standard Storage for your disks, you might need to split the disks across multiple storage accounts to make sure you have not hit the 20,000 IOps limit for Standard Storage accounts. VM 中可以混合来自不同存储帐户和不同存储帐户类型的磁盘,以实现最佳配置。Your VM can contain a mix of disks from across different storage accounts and storage account types to achieve your optimal configuration.

VM 临时驱动器Your VM Temporary drive

默认情况下,创建 VM 时,Azure 将提供 OS 磁盘 (/dev/sda) 和临时磁盘 (/dev/sdb)。By default when you create a VM, Azure provides you with an OS disk (/dev/sda) and a temporary disk (/dev/sdb). 额外添加的所有磁盘显示为 /dev/sdc/dev/sdd/dev/sde,依此类推。All additional disks you add show up as /dev/sdc, /dev/sdd, /dev/sde and so on. 临时磁盘 (/dev/sdb) 上的所有数据均不具有持久性,因此当发生 VM 调整大小、重新部署或维护等特定事件,从而迫使 VM 重新启动时,数据可能会丢失。All data on your temporary disk (/dev/sdb) is not durable, and can be lost if specific events like VM Resizing, redeployment, or maintenance forces a restart of your VM. 临时磁盘的类型和大小与在部署时选择的 VM 大小相关。The size and type of your temporary disk is related to the VM size you chose at deployment time. 所有高级大小的 VM(DS、G 和 DS_V2 系列),临时驱动器均由本地 SSD 提供支持,因此可以实现最高 48k IOps 的附加性能。All of the premium size VMs (DS, G, and DS_V2 series) the temporary drive are backed by a local SSD for additional performance of up to 48k IOps.

Linux 交换文件Linux Swap File

如果 Azure VM 来自 Ubuntu 或 CoreOS 映像,则可以使用 CustomData 将 cloud-config 发送到 cloud-init。If your Azure VM is from an Ubuntu or CoreOS image, then you can use CustomData to send a cloud-config to cloud-init. 如果已上传使用 cloud-init 的自定义 Linux 映像,则还可以使用 cloud-init 配置交换分区。If you uploaded a custom Linux image that uses cloud-init, you also configure swap partitions using cloud-init.

在 Ubuntu 云映像上,必须使用 cloud-init 配置交换分区。On Ubuntu Cloud Images, you must use cloud-init to configure the swap partition. 有关详细信息,请参阅 AzureSwapPartitionsFor more information, see AzureSwapPartitions.

对于不带 cloud-init 支持的映像,从 Azure 市场部署的 VM 映像具有与 OS 集成的 VM Linux 代理。For images without cloud-init support, VM images deployed from the Azure Marketplace have a VM Linux Agent integrated with the OS. 此代理使 VM 可以与各种 Azure 服务进行交互。This agent allows the VM to interact with various Azure services. 假设已从 Azure 市场部署标准映像,则需执行以下操作来正确配置 Linux 交换文件设置:Assuming you have deployed a standard image from the Azure Marketplace, you would need to do the following to correctly configure your Linux swap file settings:

找出并修改 /etc/waagent.conf 文件中的两个条目。Locate and modify two entries in the /etc/waagent.conf file. 这些条目控制专用交换文件的存在状态和交换文件的大小。They control the existence of a dedicated swap file and size of the swap file. 要修改的参数是 ResourceDisk.EnableSwap=NResourceDisk.SwapSizeMB=0The parameters you are looking to modify are ResourceDisk.EnableSwap=N and ResourceDisk.SwapSizeMB=0

将参数更改为以下设置:Change the parameters to the following settings:

  • ResourceDisk.EnableSwap=YResourceDisk.EnableSwap=Y
  • ResourceDisk.SwapSizeMB={符合需要的大小,以 MB 为单位}ResourceDisk.SwapSizeMB={size in MB to meet your needs}

做出更改后,需要重新启动 waagent 或重新启动 Linux VM 才能反映这些更改。Once you have made the change, you need to restart the waagent or restart your Linux VM to reflect those changes. 使用 free 命令查看可用空间时,即可知道已实现更改并已创建交换文件。You know the changes have been implemented and a swap file has been created when you use the free command to view free space. 以下示例在修改 waagent.conf 文件后创建了 512MB 的交换文件:The following example has a 512MB swap file created as a result of modifying the waagent.conf file:

azuseruser@myVM:~$ free
            total       used       free     shared    buffers     cached
Mem:       3525156     804168    2720988        408       8428     633192
-/+ buffers/cache:     162548    3362608
Swap:       524284          0     524284

高级存储的 I/O 调度算法I/O scheduling algorithm for Premium Storage

随着 2.6.18 Linux 内核的推出,默认 I/O 调度算法已从 Deadline 更改为 CFQ(完全公平的队列算法)。With the 2.6.18 Linux kernel, the default I/O scheduling algorithm was changed from Deadline to CFQ (Completely fair queuing algorithm). 对于随机访问 I/O 模式,CFQ 与 Deadline 之间的性能差异可忽略不计。For random access I/O patterns, there is negligible difference in performance differences between CFQ and Deadline. 对于磁盘 I/O 模式以循序为主的基于 SSD 的磁盘,切换回到 NOOP 或 Deadline 算法可以实现更好的 I/O 性能。For SSD-based disks where the disk I/O pattern is predominantly sequential, switching back to the NOOP or Deadline algorithm can achieve better I/O performance.

查看当前的 I/O 调度器View the current I/O scheduler

请使用以下命令:Use the following command:

cat /sys/block/sda/queue/scheduler

用户会看到以下输出,指示当前的计划程序。You see following output, which indicates the current scheduler.

noop [deadline] cfq

更改当前设备 (/dev/sda) 的 I/O 计划算法Change the current device (/dev/sda) of I/O scheduling algorithm

使用以下命令:Use the following commands:

azureuser@myVM:~$ sudo su -
root@myVM:~# echo "noop" >/sys/block/sda/queue/scheduler
root@myVM:~# sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX_DEFAULT="quiet splash elevator=noop"/g' /etc/default/grub
root@myVM:~# update-grub

备注

/dev/sda 单独应用此设置毫无用处。Applying this setting for /dev/sda alone is not useful. 在有序 I/O 支配 I/O 模式的所有数据磁盘上设置。Set on all data disks where sequential I/O dominates the I/O pattern.

用户应该会看到以下输出,指示已成功重新生成 grub.cfg 并且默认计划程序已更新为 NOOP。You should see the following output, indicating that grub.cfg has been rebuilt successfully and that the default scheduler has been updated to NOOP.

Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.13.0-34-generic
Found initrd image: /boot/initrd.img-3.13.0-34-generic
Found linux image: /boot/vmlinuz-3.13.0-32-generic
Found initrd image: /boot/initrd.img-3.13.0-32-generic
Found memtest86+ image: /memtest86+.elf
Found memtest86+ image: /memtest86+.bin
done

对于 Red Hat 分发系列,只需运行以下命令:For the Red Hat distribution family, you only need the following command:

echo 'echo noop >/sys/block/sda/queue/scheduler' >> /etc/rc.local

使用软件 RAID 来实现更高的 I/OpsUsing Software RAID to achieve higher I/Ops

如果工作负荷所需的 IOps 超过单个磁盘的极限,则需要使用包含多个磁盘的软件 RAID 配置。If your workloads require more IOps than a single disk can provide, you need to use a software RAID configuration of multiple disks. 由于 Azure 已在本地结构层执行磁盘复原,因此可以通过 RAID-0 条带化配置获得最高级别的性能。Because Azure already performs disk resiliency at the local fabric layer, you achieve the highest level of performance from a RAID-0 striping configuration. 在 Azure 环境中预配和创建磁盘,将这些磁盘附加到 Linux VM,然后分区、格式化并装入驱动器。Provision and create disks in the Azure environment and attach them to your Linux VM before partitioning, formatting and mounting the drives. 有关在 Azure 中针对 Linux VM 配置软件 RAID 设置的详细信息,请参阅 Configuring Software RAID on Linux(在 Linux 上配置软件 RAID)文档。More details on configuring a software RAID setup on your Linux VM in azure can be found in the Configuring Software RAID on Linux document.

后续步骤Next Steps

请记住,如同有关优化的所有文章中所述,需要在每次更改之前和之后执行测试,以衡量更改所造成的影响。Remember, as with all optimization discussions, you need to perform tests before and after each change to measure the impact the change has. 优化是一个逐序渐进的过程,在环境中不同的计算机上会产生不同的效果。Optimization is a step by step process that has different results across different machines in your environment. 对某一项配置有用的做法不一定适用于其他配置。What works for one configuration may not work for others.

其他有用资源的链接:Some useful links to additional resources: