如何在 PowerShell 中缩放云服务How to scale a cloud service in PowerShell

可以使用 Windows PowerShell 通过添加或删除实例来缩放 web 角色或辅助角色。You can use Windows PowerShell to scale a web role or worker role in or out by adding or removing instances.

登录到 AzureLog in to Azure

若希望通过 PowerShell 对订阅执行任何操作,必须首先登录:Before you can perform any operations on your subscription through PowerShell, you must log in:

Add-AzureAccount

如果有多个订阅与帐户关联,可能需要根据云服务的所在位置更改当前订阅。If you have multiple subscriptions associated with your account, you may need to change the current subscription depending on where your cloud service resides. 若要查看当前订阅,请运行:To check the current subscription, run:

Get-AzureSubscription -Current

如果需要更改当前订阅,请运行:If you need to change the current subscription, run:

Set-AzureSubscription -SubscriptionId <subscription_id>

查看角色的当前实例计数Check the current instance count for your role

若要查看角色的当前状态,请运行:To check the current state of your role, run:

Get-AzureRole -ServiceName '<your_service_name>' -RoleName '<your_role_name>'

应重新获取有关角色的信息,包括其当前 OS 版本和实例计数。You should get back information about the role, including its current OS version and instance count. 在本例中,角色具有单个实例。In this case, the role has a single instance.

有关角色的信息

通过添加更多实例来扩展角色Scale out the role by adding more instances

若要扩展角色,请将所需的实例数作为 Count 参数传递给 Set-AzureRole cmdlet:To scale out your role, pass the desired number of instances as the Count parameter to the Set-AzureRole cmdlet:

Set-AzureRole -ServiceName '<your_service_name>' -RoleName '<your_role_name>' -Slot <target_slot> -Count <desired_instances>

当预配并启动新实例时,此 cmdlet 立即阻止。The cmdlet blocks momentarily while the new instances are provisioned and started. 在此期间,如果打开一个新的 PowerShell 窗口,并调用 Get-AzureRole (如前面所示),将看到新的目标实例计数。During this time, if you open a new PowerShell window and call Get-AzureRole as shown earlier, you will see the new target instance count. 如果在门户中查看角色状态,应看到新实例正在启动:And if you inspect the role status in the portal, you should see the new instance starting up:

VM 实例正在门户中启动

新实例启动后,该 cmdlet 将成功返回:Once the new instances have started, the cmdlet will return successfully:

角色实例增加成功

通过删除实例缩小角色Scale in the role by removing instances

同样,可通过删除实例缩小角色。You can scale in a role by removing instances in the same way. 将 Set-AzureRole 中的 Count 参数设置为缩小操作完成后所需的实例数。Set the Count parameter on Set-AzureRole to the number of instances you want to have after the scale in operation is complete.

后续步骤Next steps

不能从 PowerShell 为云服务配置自动缩放。It is not possible to configure auto-scale for cloud services from PowerShell. 若要实现此操作,请参阅如何自动缩放云服务To do that, see How to auto scale a cloud service.