Automating Service Template deployments in SCVMM 2012

One of the great new capabilities in System Center Virtual Machine Manager (SCVMM) 2012 is the feature of Service Templates. A service template allows us to design, deploy and manage a group of virtual machines as a cohesive service. In case you’re not familiar with Service Templates yet, see these posts for more information:

This service can be a single-tier or a multi-tier application. In the case of a multi-instance web tier the service template feature can also update a load balancer automatically ( as I wrote about in my previous posting “Fabric Management in SCVMM 2012”. ) 

When working with Service Templates in a scripting / automation capacity its important to understand the object model that is exposed by VMM. There are 3 steps you will walk through in order to create and deploy a service:

  1. Create the service template – this is a model of your service and you will want to use the graphical service template designer to do this
  2. Create a service deployment configuration – this represents your service template translated to a real service, except it isn’t running yet. This is the deployment plan for the service, and you also save this in the VMM Library and deploy later. 
  3. Deploy a service – this creates the running service composed of 1 or more tiers with 1 or more VMs. 

See Figure 1 below for the diagram of this, showing the relationships.

image

 

Now from a Powershell standpoint, you have some new objects you’ll need to get familiar with.

  • SCService – represents a running service
  • SCServiceConfiguration – represents a configured Service not yet deployed
  • SCVirtualMachine – represents a running VM (either as part of a service or standalone. If you did any scripting with SCVMM 2008 you’re already familiar with this.
  • SCVMConfiguration – represents a configured VM not yet deployed
  • ServiceTemplate  - represents your Service as designed in the Service Template Designer

Each of these Powershell objects correspond directly to the configuration items shown in the diagram here; this is from the posting (also mentioned above) Creating Service Templates, by JC Hornbeck. 

image

Architecture best practices for automation

Before we jump into deeper level discussion of these automation steps, its important to set the architecture context for all this.

Automated service deployments should be traceable and should map to business services. This gets us into the notion of IT Service Management and ITIL’s “good practices”, and all the wonderful issues around people and process.  Here are some scripting examples using the concepts and structures we just discussed. This could be a long and involved discussion so I’ll take a few large shortcuts to show a conceptual / logical view of how this automation fits into the overall technology solutions context.

arch sys of record

See also the Reference Architecture as explained in Cloud Computing: Architecting a Microsoft Private Cloud, which is based on the well-known NIST cloud definitions

Powershell CommandLets for Services

The commands – Powershell cmdlets - available will depend on which sort of object you’re working with. When you’re working with just the templates and the configuration objects you have a base set of cmdlets to Create/Read/Update/Delete the objects. Once you deploy a service you also have operational cmdlets like Start/Stop/Suspend/Resume:

Object Type Configuration Commands Operational Commands
ServiceTemplate

Get-SCServiceTemplate New-SCServiceTemplate Read-SCServiceTemplate Remove-SCServiceTemplate Resolve-SCServiceTemplate Set-SCServiceTemplate Test-SCServiceTemplate

(N/A)
ServiceConfiguration

Get-SCServiceConfiguration New-SCServiceConfiguration Remove-SCServiceConfiguration Set-SCServiceConfiguration Update-SCServiceConfiguration

(N/A)

VMConfiguration

Get-SCVMConfiguration New-SCVMConfiguration Remove-SCVMConfiguration Set-SCVMConfiguration Update-SCVMConfiguration

(N/A)
Service

Get-SCService New-SCService Read-SCService Remove-SCService Set-SCService Update-SCService

Start-SCService Stop-SCService Suspend-SCService Resume-SCService

VirtualMachine

Get-SCVirtualMachine Move-SCVirtualMachine New-SCVirtualMachine Read-SCVirtualMachine Register-SCVirtualMachine Remove-SCVirtualMachine Repair-SCVirtualMachine Reset-SCVirtualMachine Set-SCVirtualMachine

Resume-SCVirtualMachine Save-SCVirtualMachine Start-SCVirtualMachine Stop-SCVirtualMachine Suspend-SCVirtualMachine

Scripting Examples

Here are some scripting examples using the concepts and structures we just discussed.

Create a service configuration from a Service Template:

## this assumes we have arranged our VM servers into a cloud….
$cloud = Get-SCCloud -Name "MyCloud"

## This reads a service template we have already designed in VMM12
$serviceTemplate = get-scservicetemplate -Name "My Service Template" | where ‘
{ $_.release -eq "1.0" }

## ServiceConfig is a temporary object,
## naming with date/time ensures uniqueness
$svcCfgName = "Tmp_Srvc_Config_" + (get-date -format "ddHHmmss")

## create the service config object
## the –Cloud arg is the deployment destination of the service
$serviceConfig = New-SCServiceConfiguration -ServiceTemplate $serviceTemplate ‘
-Name $svcCfgName -Cloud $Cloud -Description "My Service"

Rename the VMs in the Service Configuration:

## get the web tier object
$tierConfig = Get-SCComputerTierConfiguration ‘
-ServiceConfiguration $serviceConfig ‘
| where { $_.name –like “Web*” }

## get the VMConfigurations within the tier
$vmConfig = Get-SCVMConfiguration -ComputerTierConfiguration $tierConfig

## Set a naming convention so VMs will be named like
## "MyVMServer001”, “MyVMServer002” etc.
$namePattern = “MyVMServer”

## get number of VMs already deployed w/ this name pattern
$vmList = Get-SCVirtualMachine -cloud $cloud | where-object { $_.Name –like ($namePattern + *) }
$seed = $vmList.count + 1

## set vm names in this tier according to the pattern
$z = 0
do
{
$vm_name = $namePattern + ($z + $seed)
$computer_name = $vm_name
Set-SCVMConfiguration -VMConfiguration $VMConfig[$Z] -name $vm_name -computerName $computer_name
$z += 1
}
until ($z -ge $vmConfig.count)

Deploy the service configuration to a running service:

##

 

## Update the Service Config
Update-SCServiceConfiguration -ServiceConfiguration $serviceconfig
## Deploy the service
## note include –RunAsynchronously to
$newSvc = New-SCService -ServiceConfiguration $serviceconfig

## check deployment state and status
write-host “Deployment state: “ + $newSvc.deploymentState
write-host “Service status: “ + $newSvc.overallStatus

Closing thoughts 

Within SCVMM it is very educational to watch the Jobs window as your scripts are running, this allows you to see how the script commands are carried out within SCVMM.  Also SCVMM always exposes the “Powershell" button so you can hop in to PS and try things out.

the following Get-Help topics will be very helpful, especially as of now when the formal documentation of the VMM cmdlets have not yet been released.

  • about_VMM_2012
  • about_VMM_2012_Cmdlet_and_Parameter_Name_Mapping
  • about_VMM_2012_Cmdlet_Backward_Compatibility
  • about_VMM_2012_Connecting_to_the_VMM_Server
  • about_VMM_2012_Host_Platforms
  • about_VMM_2012_Library
  • about_VMM_2012_Role_Based_Security
  • about_VMM_2012_Run_As_Accounts
  • about_VMM_2012_Virtual_Networking

image

Also note, if you’re using Powershell Remoting you’ll need to begin your script with:

import-module -name "virtualmachinemanager"

We are entering into a tremendously exciting evolution of computing, where datacenter automation is essential. And through the capabilities in SCVMM 2012 (and other new-generation toolsets) this automation is something that can be achieved pretty easily.  The virtual machine metaphor allows us to treat servers like software objects, which they are.  With the power of SCVMM 2012 and others in the System Center 2012 wave, we can build solid reliable solutions for the automation, management and orchestration of the virtual machine layer.  This set of capabilities will allow for business applications to be deployed and managed in a dynamic and cost-effective manner. 

.

Glenn Walton | Senior Infrastructure Architect, Microsoft Services