使用vCenter Orchestrator实现VMware vSphere的自动化任务

摘要:

环境:

vCenter Server 4.1 On Windows 2008 R2

Sql2008 R2

Windows 2003 Active Directory

涉及vCenter Orchestrator,PowerShell with PowerCLI,ESX shell scripts的配置

通过vCenter Orchestrator访问vCenter Server API

在安装vCenter Server的时候,vCenter Orchestrator已经随着vCenter Server安装在系统上,在使用之前,需要对它进行配置。

配置vCenterOrchestrator

配置过程主要有如下几步:

1.启动vCenter Orchestrator Configuration服务

2.配置vCenter Orchestrator 网络连接

3.创建一个LDAP 服务器,本环境使用活动目录

4.配置后台数据库,本环境使用Sql2008R2

5.为vCenter Orchestrator创建SSL证书

6.输入vCenter Server的许可证

7.配置缺省插件

 

1. 启动vCenter Orchestrator Configuration服务

缺省该服务启动状态为手动,修改为自动,并启动它。

sc config vCOConfiguration start= auto

sc start vCOConfiguration

服务启动之后,通过http://vcenter Server的IP:8282来访问,缺省的用户名及密码是vmware/vmware

clip_p_w_picpath002

clip_p_w_picpath004

红色部分提示,配置未完成,配置完成之后,状态将改为绿色

2. 配置vCenter Orchestrator 网络连接

点击上图的Network,在右侧的IP Address栏,选择vCenter Orchestrator使用的IP,然后点击Apply Changes。可以看到,状态变绿。

clip_p_w_picpath006

3. 配置LDAP

点击LDAP,

在右侧LDAP Client选择Active Directory,

Primary LDAP Host输入域控的FQDN,

Root文本框里面输入活动目录的root DN,形如DC=xyz,DC=com,

在Username文本框,输入用户名,该用户名为Orchestrator登录活动目录的凭据,用户名的格式要么是DN 格式 (cn=username,cn=Users,dc=domain,dc=com) 要么是UPN格式 (username@domain.com),此处是vcadmin@xyz.com

User Lookup Base 文本框,提供Orchestrator用来搜索用户帐户的base DN,可配置成与root DN一致,DC=xyz,DC=com

Group Lookup Base 文本框,可配置成与root DN一致,DC=xyz,DC=com

vCO Admin Group文本框,指定具有vCenter Orchestrator管理员权限的DN,可以使用域管理员组cn=Administrators,cn=Builtin,dc=domain,dc=com,本实验指定CN=vmadmin,OU=vmwareOU,DC=xyz,DC=com,将域管理员及xyz\vcadmin用户添加到该组,

点击Apply Changes,然后状态变为绿色。

clip_p_w_picpath008

clip_p_w_picpath010

用ldap方式访问AD域的的错误一般会如下格式:

LDAP: error code 49 - 80090308: LdapErr: DSID-0C090334, comment: AcceptSecurityContext error, data 52e, vece

其中红字部分的意思如下:

525 - 用户没有找到

52e - 证书不正确,或者凭据不正确

530 - not permitted to logon at this time

532 - 密码期满

533 - 帐户不可用

701 - 账户期满

773 - 用户必须重设密码

4. 配置后台数据库

先在Sql2008R2 服务器上新建一空的数据库vcodb,Db_owner为xyz\vcadmin

点击Database,选择SqlServer

信息输入完成之后,点Apply Changes,然后点上方的Install the database

clip_p_w_picpath012

点击Instal the database之后,Database标签状态变绿

5. 配置服务器证书

即可使用自签名的证书,也可使用第三方证书

clip_p_w_picpath014

创建自签名证书

clip_p_w_picpath016

使用vCenter Server的FQDN

clip_p_w_picpath018

6. 输入vCenter Server的许可证

clip_p_w_picpath020

导入vCenter Server的证书

clip_p_w_picpath022

此处应该使用IP地址,不能使用FQDN

clip_p_w_picpath024

C:\Documents and Settings\All Users\Application Data\VMware\VMware VirtualCenter\SSL\rui.crt

clip_p_w_picpath026

7. 配置插件

vCenter Orchestrator使用插件连接工作流引擎。缺省情况下,vCenter Orchestrator提供了一一套插件,但需要使用具有vCenter Orchestrator管理员权限的用户来执行。

单击Plug-Ins

指定属于vCO Administration组的用户

Apply Changes

clip_p_w_picpath028

此步骤完成之后,Startup Options标签随之变绿

8. 添加vCenter Server主机

与vCenter Orchestrator通讯的vCenter Server

不配置vCenter Server,vCenter Orchestrator同样能工作,但将失去在vCenter Server内自动化任务的能力

clip_p_w_picpath030

clip_p_w_picpath032

9. 安装和启动vCenter Orchestrator Server服务

单击Startup Options标签

单击Install vCO Server As A Service链接,当状态再次变绿,单击启动服务链接

clip_p_w_picpath034

clip_p_w_picpath036

clip_p_w_picpath038

10. 使用Orchestrator工作流

vCenter Orchestrator Client是配置工作的工具软件

运行vCenter Orchestrator Client,会看到vCenter Orchestrator已经提供了预安装的工作流。

clip_p_w_picpath040

如果系统内存低于4GB,VMware vCenter Orchestrator Server有可能无法启动

clip_p_w_picpath042

11. 使用PowerShell和PowerCLI实现自动化任务

Windows 2008 R2已经内置PowerShell2

安装PowerCLI,首先提示安装VIX

clip_p_w_picpath044

 

clip_p_w_picpath046

clip_p_w_picpath048

clip_p_w_picpath050

clip_p_w_picpath052

在使用PowerCLI之前,需要设置PowerShell执行策略允许运行PowerCLI的组件

clip_p_w_picpath054

Set-ExecutionPolicy RemoteSigned

Get-ExecutionPolicy

运行PowerCLI

Everything is object.Esx主机、虚拟机、快照等等均为对象。

首先需要连接到vCenter Server或者Esx/Esxi主机

Connect-VIServer -Server <vCenter Server hostname> -User <Username> -Password <password>

Get-VMHost

Get-VMHost <Hostname> | Get-VM

Get-VMHost <Hostname> | Get-VM | Get-NetworkAdapter

Get-VMHost <Hostname> | Get-VM | Get-NetworkAdapter | Select NetworkName, Type

Get-Datacenter Datacenter

Get-Datastore Datastore

Get-Folder Folder

Get-HardDisk Virtual hard disk

Get-NetworkAdapter Virtual network adapter

Get-ResourcePool Resource pool

Get-Snapshot Virtual machine snapshot

Get-Template Virtual machine template

Get-VirtualPortGroup Port group

Get-VirtualSwitch Virtual switch

Get-VM Virtual machine

Get-VMGuest Virtual machine guest operating system

Get-VMHost ESX/ESXi host

Move-VM Moves a VM to a new host, cluster, or datastore

Move-ResourcePool Moves a resource pool

New-Cluster Creates a new cluster of ESX/ESXi hosts

New-HardDisk Creates a new hard disk on a VM

New-NetworkAdapter Creates a new network adapter on a VM

New-ResourcePool Creates a new resource pool

New-Snapshot Creates a new snapshot on a VM

New-VM Creates a new VM

Remove-Snapshot Removes a VM snapshot or snapshots

Restart-VMGuest Restarts a VM guest operating system

Set-CDDrive Changes VM’s virtual CD configuration

Set-Datacenter Changes the name of the datacenter

Set-Datastore Changes the name of the datastore

Set-FloppyDrive Updates a VM’s virtual floppy configuration

Set-HardDisk Changes VM’s hard disk configuration

Set-NetworkAdapter Modifies VM’s network adapter configuration

Set-ResourcePool Updates resource pool configuration

Set-VirtualPortGroup Updates virtual port group configuration

Set-VirtualSwitch Changes virtual switch configuration

Set-VM Changes virtual machine configuration

Set-VMHost Updates ESX/ESXi host configuration

Set-VMResourceConfiguration Changes resource allocation for a VM

Shutdown-VMGuest Shuts down a VM guest operating system

Suspend-VMGuest Suspends a VM guest operating system

一些示例:

Get-VMHost <FirstHost> | Get-VM | Move-VM –destination (Get-VMHost <SecondHost>)
利用vmotion移动主机1上的虚拟机到主机2
Get-VMHost <Hostname> | Get-VM | New-Snapshot -Name "Target-Snapsht"
主机上所有的虚拟机都创建一份快照
Get-VMHost <Hostname> | Get-VM | Get-Snapshot -Name "Target-Snapsht" | Remove-Snapshot
删除快照
Get-VMHost <Hostname> | Get-VM | Get-Snapshot
列出快照
Get-VM | Get-NetworkAdapter | Where-Object { $_.NetworkName -like "OldPortGroupName" } | Set-NetworkAdapter –NetworkName "NewPortGroupName" –Confirm:$false
将某个端口组的虚拟机移动到另外一个端口组
Get-ResourcePool <ResourcePoolName> | Get-VM | Get-VMGuest
Get-ResourcePool <ResourcePoolName> | Get-VM | Get-VMGuest | Where-Object { $_.OSFullName –match " ? Microsoft Windows.*" }
列出资源池中操作系统为windows的虚拟机
\MoveWindowsVMs.ps1
$VMs = Get-VM -Location (Get-ResourcePool Infrastructure)
foreach ($vm in $VMs) {
$vmguest = Get-VMGuest -VM $vm
if ($vmguest.OSFullName -match " ^ Microsoft Windows.*") {
Move-VM -VM $vm -Destination (Get-ResourcePool "Windows VMs") } }