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

使用 Desired State Configuration 配置 Linux 虚拟机Configure a Linux virtual machine with Desired State Configuration

启用 Desired State Configuration (DSC) 即可管理和监视 Windows 和 Linux 服务器的配置。By enabling Desired State Configuration (DSC), you can manage and monitor the configurations of your Windows and Linux servers. 可以标识或自动更正不符合所需配置的配置。Configurations that drift from the desired configuration can be identified or auto-corrected. 本快速入门逐步介绍了如何使用 DSC 载入 Linux VM 并部署 LAMP 堆栈。This quickstart steps through onboarding a Linux VM and deploying a LAMP stack with DSC.

先决条件Prerequisites

若要完成本快速入门,你需要:To complete this quickstart, you need:

登录 AzureSign in to Azure

登录 Azure (https://portal.azure.com)Sign in to Azure at https://portal.azure.com

载入虚拟机Onboard a virtual machine

可以通过多种不同的方法载入计算机并启用 Desired State Configuration。There are many different methods to onboard a machine and enable Desired State Configuration. 本快速入门介绍如何通过自动化帐户进行载入。This quickstart covers onboarding through an Automation account. 可以阅读此载入文章,详细了解如何通过不同的方法将计算机载入到 Desired State Configuration。You can learn more about different methods to onboard your machines to Desired State Configuration by reading the onboarding article.

  1. 在 Azure 门户的左窗格中,选择“自动化帐户”。In the left pane of the Azure portal, select Automation accounts. 如果该选项未显示在左窗格中,请单击“所有服务”,然后在出现的视图中进行搜索。If it is not visible in the left pane, click All services and search for it in the resulting view.
  2. 在列表中选择一个自动化帐户。In the list, select an Automation account.
  3. 在自动化帐户的左窗格中,选择“状态配置(DSC)”。In the left pane of the Automation account, select State configuration (DSC).
  4. 单击“添加”以打开“VM 选择”页。Click Add to open the VM select page.
  5. 找到要启用 DSC 的虚拟机。Find the virtual machine you would like to enable DSC for. 可以使用搜索栏和筛选器选项来查找特定的虚拟机。You can use the search field and filter options to find a specific virtual machine.
  6. 单击虚拟机,然后选择“连接”。Click on the virtual machine, and then select Connect
  7. 选择适用于虚拟机的 DSC 设置。Select the DSC settings appropriate for the virtual machine. 如果已准备了一个配置,可以通过“节点配置名称”的形式来指定该配置。If you have already prepared a configuration, you can specify it as Node Configuration Name. 可以将配置模式设置为控制计算机的配置行为。You can set the configuration mode to control the configuration behavior for the machine.
  8. 单击 “确定”Click OK

将 Azure VM 载入到 DSC

当 Desired State Configuration 扩展部署到虚拟机时,会显示“正在连接”。While the Desired State Configuration extension is deployed to the virtual machine, it shows Connecting.

导入模块Import modules

模块包含 DSC 资源,许可可以在 PowerShell 库中找到。Modules contain DSC Resources and many can be found on the PowerShell Gallery. 在配置中使用的任何资源都必须在编译前导入到自动化帐户中。Any resources that are used in your configurations must be imported to the Automation Account before compiling. 本教程需要名为 nx 的模块。For this tutorial, the module named nx is required.

  1. 在自动化帐户的左窗格中,选择“模块库”(位于“共享资源”下)。In the left pane of the Automation account, select Modules Gallery (under Shared Resources).
  2. 搜索要导入的模块,方法是键入其名称的一部分:nxSearch for the module that you would like to import by typing part of its name: nx
  3. 单击要导入的模块Click on the module you would like to import
  4. 单击“导入”Click Import

导入 DSC 模块

导入配置Import the configuration

本快速入门使用 DSC 配置在计算机上配置 Apache HTTP Server、MySQL 和 PHP。This quickstart uses a DSC configuration that configures Apache HTTP Server, MySQL, and PHP on the machine.

有关 DSC 配置的详细信息,请参阅 DSC 配置For information about DSC configurations, see DSC configurations.

在文本编辑器中,键入以下命令,并在本地将其保存为 LAMPServer.ps1In a text editor, type the following and save it locally as LAMPServer.ps1.

configuration LAMPServer {
   Import-DSCResource -module "nx"

   Node localhost {

        $requiredPackages = @("httpd","mod_ssl","php","php-mysql","mariadb","mariadb-server")
        $enabledServices = @("httpd","mariadb")

        #Ensure packages are installed
        ForEach ($package in $requiredPackages){
            nxPackage $Package{
                Ensure = "Present"
                Name = $Package
                PackageManager = "yum"
            }
        }

        #Ensure daemons are enabled
        ForEach ($service in $enabledServices){
            nxService $service{
                Enabled = $true
                Name = $service
                Controller = "SystemD"
                State = "running"
            }
        }
   }
}

若要导入配置,请执行以下操作:To import the configuration:

  1. 在自动化帐户的左窗格中,选择“状态配置(DSC)”,然后单击“配置”选项卡。In the left pane of the Automation account, select State configuration (DSC) and then click the Configurations tab.
  2. 单击“+ 添加”Click + Add
  3. 选择在前面的步骤中保存的配置文件。Select the Configuration file that you saved in the prior step
  4. 单击 “确定”Click OK

编译配置Compile a configuration

必须先将 DSC 配置编译为节点配置(MOF 文档),然后才能将其分配给节点。DSC Configurations must be compiled to a Node Configuration (MOF document) before being assigned to a node. 编译时会验证配置,并允许输入参数值。Compilation validates the configuration and allows for the input of parameter values. 若要详细了解如何编译配置,请参阅:在 Azure 自动化 DSC 中编译配置To learn more about compiling a configuration, see: Compiling Configurations in Azure Automation DSC

若要编译配置,请执行以下操作:To compile the configuration:

  1. 在自动化帐户的左窗格中,选择“状态配置(DSC)”,然后单击“配置”选项卡。In the left pane of the Automation account, select State Configuration (DSC) and then click the Configurations tab.
  2. 选择在前面的步骤中导入的配置“LAMPServer”。Select the configuration you imported in a prior step, "LAMPServer"
  3. 在菜单选项中单击“编译”,然后单击“是”。From the menu options, click Compile and then Yes
  4. 在“配置”视图中,可以看到排入队列的新编译作业。In the Configuration view, you see a new Compilation job queued. 成功完成作业以后,即可转到下一步。When the job has completed successfully, you are ready to move on to the next step. 如果出现故障,可以单击该编译作业来了解详细信息。If there are any failures, you can click on the Compilation job for details.

分配节点配置Assign a node configuration

可以向 DSC 节点分配编译的节点配置。A compiled Node Configuration can be assigned to DSC Nodes. 可以通过分配操作将配置应用到计算机,并监视(或自动更正)出现的与该配置不符的任何偏差。Assignment applies the configuration to the machine and monitors (or auto-corrects) for any drift from that configuration.

  1. 在自动化帐户的左窗格中,选择“状态配置(DSC)”,然后单击“节点”选项卡。In the left pane of the Automation account, select **State Configuration (DSC) and then click the Nodes tab.
  2. 选择要为其分配配置的节点Select the node you would like to assign a configuration to
  3. 单击“分配节点配置”Click Assign Node Configuration
  4. 选择要分配的节点配置 - LAMPServer.localhost,然后单击“确定”Select the Node Configuration - LAMPServer.localhost - to assign and click OK
  5. 此时会向节点分配编译的配置,节点状态更改为“挂起”。The compiled configuration is now be assigned to the node, and the node status changes to Pending. 下一次进行定期检查时,节点会检索配置,并在应用该配置后将状态报告回来。On the next periodic check, the node retrieves the configuration, apply it, and report status back. 节点检索配置可能需要长达 30 分钟的时间,具体取决于节点的设置。It can take up to 30 minutes for the node to retrieve the configuration, depending on the node's settings. 若要强制进行即时检查,可以在 Linux 虚拟机上以本地方式运行以下命令:sudo /opt/microsoft/dsc/Scripts/PerformRequiredConfigurationChecks.pyTo force an immediate check, you can run the following command locally on the Linux virtual machine: sudo /opt/microsoft/dsc/Scripts/PerformRequiredConfigurationChecks.py

分配节点配置

查看节点状态Viewing node status

所有托管节点的状态都可以在“状态配置(DSC)”中找到,然后在自动化帐户的“节点”选项卡下找到。The status of all managed nodes can be found in the State Configuration (DSC) and then under the Nodes tab in the Automation Account. 可以通过状态、节点配置或名称搜索的方式来筛选显示结果。You can filter the display by status, node configuration, or name search.

DSC 节点状态

后续步骤Next steps

本快速入门介绍了如何将 Linux VM 载入到 DSC、如何为 LAMP 堆栈创建配置,以及如何将其部署到 VM。In this quickstart, you onboarded a Linux VM to DSC, created a configuration for a LAMP stack, and deployed it to the VM. 若要了解如何通过自动化 DSC 启用持续部署,请继续阅读以下文章:To learn how you can use Automation DSC to enable continuous deployment, continue to the article: