未加星标

Updating Packer Builds to Use Managed Disks

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二05 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

A few months back we looked at how we can use a tool called Packer to automate the creation of virtual machine images, which could then be used with VM Scale Sets. If this is something you are interested in doing, I would recommend taking a look at theoriginal article, which walks you through the whole process.

At the end of that article, I said we were next going to look at how we can use continuous integration to build this image on a regular schedule, or when applications are updated. I've been a bit lax on creating this article, but you will see that soon. Before we do that we need to update our Packer process a bit. In the original article, we used Packer to create images in the VHD format, stored in a storage account, as this was the only way to do it at the time. Things have moved on now, and Packer now supports managed disks and managed disk images, which is what you want to be using for all your Azure disks.

In this article, we are going to update the process to switch to using managed disks.

Current Process

Let's remind ourselves of the Packer script we used in the previous example (if you're not familiar with Packer terms, please do readthe previous post). In this script we have:

An Azure Builder to Create the VM A PowerShell Provisioner to setup WinRM A DSC Provisioner to install the required software A final PowerShell Provisioner to run Sysprep

Here's the full script:

{ "builders": [ { "type": "azure-arm", "client_id": "<this is the ApplicationID from your service principle>", "client_secret": "<this is the password from your service principle>", "object_id": "<object ID for the service principle>", "tenant_id": "<ID of the Azure AD tenant your SP is in>", "subscription_id": "<subscription ID you want the VM and images created in>", "resource_group_name": "<name of the resource group you created to store images>", "storage_account": "<name of the storage account you created to store images>", "capture_container_name": "<name of the storage container, in your storage account, you created to store images>", "capture_name_prefix": "<prefix to use for your images, up to you>", "os_type": "windows", "image_publisher": "MicrosoftWindowsServer", "image_offer": "WindowsServer", "image_sku": "2016-Datacenter", "communicator": "winrm", "winrm_use_ssl": "true", "winrm_insecure": "true", "winrm_timeout": "3m", "winrm_username": "packer", "location": "West Europe", "vm_size": "Standard_DS2" } ], "provisioners": [ { "type": "powershell", "inline": [ "WINRM QuickConfig -q" ] }, { "type": "dsc", "manifest_file": "DSCConfiguration.ps1", "configuration_name": "WebServer", "install_package_management": true, "install_modules": { "xPSDesiredStateConfiguration": "6.4.0.0" }, "configuration_params": { "-PackageLocation": "https://urlformyfiles.blob.core.windows.net/files", "-AppliationName": "App1" } }, { "type": "powershell", "inline": [ "if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}", "& $Env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /shutdown /quiet" ] } ] }

We don't need to make changes to any of the provisioners, as the change does not impact these (they all occur inside the VM). So we are just going to be looking to change the builder.

Updated Process

To switch to managed disks, we need to make the following changes to the Azure Builder:

Add a managed_image_name parameter that contains the name of the managed image we want to create Add a managed_image_resource_group_name parameter that contains the name of the resource group we want to store the image in Remove the following parameters, as these all relate to the VHD and storage account, which we will no longer be using. resource_group_name storage_account capture_container_name capture_name_prefix We're also going to remove the object_id field, this is not related to managed images, but is no longer required as Packer works it out from the App ID. Image Naming

As mentioned, we need to pass in the managed_image_name parameter, to name the image we are creating. This process differs from the one used to create VHDs, which required a name prefix, rather than a full name, Packer would then add on suffix to keep the name unique. The process to create a Managed Image does not add this suffix, so we need to do that in our name parameter. The easiest them to keep images unique and identifiable is to add a date to the end. We can use the Packer template language to do this. The "isotime" function allows us to return the current date and format it as required, note the formatting uses a magic date of " Mon Jan 2 15:04:05 -0700 MST 2006 ", hence that being used in the format string.

"managed_image_name": "app1OSImage-{{isotime \"200601020304\"}}"

This command creates a managed image with the name of "app1OSImage-201901021259" (given today's date of the 2nd Jan 2019).

New Script

When we put all those changes together, our new script looks like this:

{ "builders": [ { "type": "azure-arm", "client_id": "<this is the ApplicationID from your service principle>", "client_secret": "<this is the password from your service principle>", "tenant_id": "<ID of the Azure AD tenant your SP is in>", "subscription_id": "<subscription ID you want the VM and images created in>", "managed_image_name": "app1OSImage-{{isotime \"200601020304\"}}", "managed_image_resource_group_name": "app1ResourceGroup", "os_type": "Windows", "image_publisher": "MicrosoftWindowsServer", "image_offer": "WindowsServer", "image_sku": "2016-Datacenter", "communicator": "winrm", "winrm_use_ssl": "true", "winrm_insecure": "true", "winrm_timeout": "3m", "winrm_username": "packer", "location": "West Europe", "vm_size": "Standard_DS2" } ], "provisioners": [ { "type": "powershell", "inline": [ "WINRM QuickConfig -q" ] }, { "type": "dsc", "manifest_file": "DSCConfiguration.ps1", "configuration_name": "WebServer", "install_package_management": true, "install_modules": { "xPSDesiredStateConfiguration": "6.4.0.0" }, "configuration_params": { "-PackageLocation": "https://urlformyfiles.blob.core.windows.net/files", "-AppliationName": "App1" } }, { "type": "powershell", "inline": [ "if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}", "& $Env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /shutdown /quiet" ] } ] } Running a Build

We can run this script in the same way as we did previously:

packer build script.json

Once the script runs through you will see a slightly different output, as you are now provided with the resource ID for the Managed image, rather than the URL of the VHD.


Updating Packer Builds to Use Managed Disks

If you look in the resource group, you provided as the destination for the image you will now see a managed image object has been created.


Updating Packer Builds to Use Managed Disks

We can now use this image to create a new VM or create a scale set. Once you can successfully create a managed image using this script your ready to look at using CI to build the image, which will look at in the next article.

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

分页:12
转载请注明
本文标题:Updating Packer Builds to Use Managed Disks
本站链接:https://www.codesec.net/view/628551.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(19)