zoukankan      html  css  js  c++  java
  • Azure VMSS ---- PowerShell创建标准镜像的VMSS集群

    VMSS的创建可以采用PortalPowershellAzure CLI或者Template

    但目前Portal创建有很多限制,本文将介绍如何用PowerShell来创建VMSS的集群。

    具体的创建过程有以下几个步骤:

    1 创建Resource Group

    2 创建Vnet

    3 创建Public IPLoadBalancer

    4 创建VMSS

    5 增加VMSSAutoscale规则

    创建Resource Group

    具体命令如下:

    $loc = 'chinanorth';
    $rgname = 'hwvmss';
    New-AzureRmResourceGroup -Name $rgname -Location $loc -Force;

    二创建Vnet

    具体命令如下:

    $subnetName = 'vlan1'
    $subnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName `
    -AddressPrefix "10.1.1.0/24"; $vnet = New-AzureRmVirtualNetwork -Force -Name $rgname `
      -ResourceGroupName $rgname -Location $loc `
      -AddressPrefix "10.1.0.0/16" -Subnet $subnet; $vnet = Get-AzureRmVirtualNetwork -Name $rgname `
      -ResourceGroupName $rgname; $subnetId = $vnet.Subnets[0].Id;

    三创建Public IPLoadBalancer

    1 创建Public IP

     

    $pubip = New-AzureRmPublicIpAddress -Force -Name ('pubip' + $rgname) `
      -ResourceGroupName $rgname -Location $loc -AllocationMethod Dynamic `
      -DomainNameLabel ('pubip' + $rgname); $pubip = Get-AzureRmPublicIpAddress -Name ('pubip' + $rgname) `
      -ResourceGroupName $rgname;

     

    2 创建LoadBalancer

     

    $frontendName = 'fe' + $rgname
    $backendAddressPoolName = 'bepool' + $rgname
    $probeName = 'vmssprobe' + $rgname
    $inboundNatPoolName = 'innatpool' + $rgname
    $lbruleName = 'lbrule' + $rgname
    $lbName = 'vmsslb' + $rgname
    
    $frontend = New-AzureRmLoadBalancerFrontendIpConfig `
      -Name $frontendName -PublicIpAddress $pubip $backendAddressPool = New-AzureRmLoadBalancerBackendAddressPoolConfig `
      -Name $backendAddressPoolName $probe = New-AzureRmLoadBalancerProbeConfig -name $probeName `
      -Protocol Tcp -Port 80 -IntervalInSeconds 15 -ProbeCount 2 $frontendpoolrangestart = 22100 $frontendpoolrangeend = 22200 $backendvmport = 22 $inboundNatPool = New-AzureRmLoadBalancerInboundNatPoolConfig `
      -Name $inboundNatPoolName `
      -FrontendIPConfigurationId $frontend.Id -Protocol Tcp `
      -FrontendPortRangeStart $frontendpoolrangestart `
      -FrontendPortRangeEnd $frontendpoolrangeend `
      -BackendPort $backendvmport; $protocol = 'Tcp' $feLBPort = 80 $beLBPort = 80 $lbrule = New-AzureRmLoadBalancerRuleConfig -Name $lbruleName `   -FrontendIPConfiguration $frontend `
      -BackendAddressPool $backendAddressPool `   -Probe $probe -Protocol $protocol `
      -FrontendPort $feLBPort -BackendPort $beLBPort `   -IdleTimeoutInMinutes 15 -LoadDistribution SourceIP

    $actualLb = New-AzureRmLoadBalancer -Name $lbName `
      -ResourceGroupName $rgname `
      -Location $loc-FrontendIpConfiguration $frontend `
      -BackendAddressPool $backendAddressPool-Probe $probe  `
      -LoadBalancingRule $lbrule `
      -InboundNatPool $inboundNatPool

    $expectedLb = Get-AzureRmLoadBalancer -Name $lbName `
      -ResourceGroupName $rgname

     

    创建VMSS

     

    $vmssName =  $rgname;
    
    $adminUsername = 'hengwei';
    $adminPassword = "xxxxxxx";
    
    $PublisherName = 'OpenLogic'
    $Offer         = 'CentOS'
    $Sku          = '7.2'
    $Version       = 'latest'
    $vmNamePrefix = 'centosvmss'
    
    
    $ipCfg = New-AzureRmVmssIPConfig -Name 'nic' `
    -LoadBalancerInboundNatPoolsId $actualLb.InboundNatPools[0].Id `
    -LoadBalancerBackendAddressPoolsId $actualLb.BackendAddressPools[0].Id `
    -SubnetId $subnetId;
    
    $numberofnodes = 3
    
    $vmss = New-AzureRmVmssConfig -Location $loc -SkuCapacity $numberofnodes `
      -SkuName 'Standard_D1' -UpgradePolicyMode 'automatic' `
      
    | Add-AzureRmVmssNetworkInterfaceConfiguration -Name $subnetName `
        -Primary $true -IPConfiguration $ipCfg `
      
    | Set-AzureRmVmssOSProfile -ComputerNamePrefix $vmNamePrefix `
        -AdminUsername $adminUsername -AdminPassword $adminPassword `
      
    | Set-AzureRmVmssStorageProfile -OsDiskCreateOption 'FromImage' `
        -OsDiskCaching 'None' -ImageReferenceOffer $Offer `
        -ImageReferenceSku $Sku -ImageReferenceVersion $Version `
        
    -ImageReferencePublisher $PublisherName New-AzureRmVmss -ResourceGroupName $rgname -Name $vmssName -VirtualMachineScaleSet $vmss

    五增加VMSSAutoscale规则

    VMSSAutoscale是在VMSS主机进行性能的监控的基础上,根据VM的性能进行自动的增加VM和减少VM

    有两种方式进行VM性能的监视:

    1 Azure Host上取到的信息

    通过Azure Monitor取得VM的信息

    可以支持的监控内容如以下链接:

    https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/monitoring-supported-metrics

    可以看到,在Microsoft.Compute/virtualMachineScaleSets中支持的内容有:

    Percentage CPUNetwork InNetwork OutDisk Read BytesDisk Write BytesDisk Read Operations/SecDisk Write Operations/Sec这些。

    Azure中的VMSS可以采用上面的这些指标来进行AutoScale的控制。

    但目前AzureChina还不支持这种模式。

    2 Azure VM Diagnostics信息

    通过在VM中安装Diagnostics Extension,获得诊断信息,通过这种诊断信息中的VM资源情况进行自动扩展。

    目前AzureChina支持这种模式。

     

    具体实现步骤如下:

    1 创建一个Storage Account,其中的Table用来收集VMDagnostic的信息

    创建一个General PurposeStorage Account,本例子中的名字叫hwvmss

    具体创建过程就不描述了。

     

    2 给已有的VMSS中的VM安装Linux DiagnosticsExtension

     

    $rgname = "hwvmss"
    $vmssname = "hwvmss"
    
    $vmss = Get-AzureRmVmss -ResourceGroupName $rgname `
      -VMScaleSetName $vmssname $xmlcfg = Get-Content D:AzureVMSSdiag_nosubid.xml $xmlCfg = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($xmlCfg)) $storageAccountName = "hwvmss" $storageAccountKey = (Get-AzureRmStorageAccountKey `
      -ResourceGroupName $rgname -Name $storageAccountName)[0].Value $settings = @{"xmlCfg" = $xmlCfg; `
      "storageAccount" = $storageAccountName} $exname = "Microsoft.Insights.VMDiagnosticsSettings" $publisher = "Microsoft.OSTCExtensions" $type = "LinuxDiagnostic" $typeHandlerVersion = "2.3" $protectedSettings = @{ "storageAccountName" = $storageAccountName;`
      "storageAccountKey" = $storageAccountKey; `
      "storageAccountEndPoint" = "https://core.chinacloudapi.cn"; `
      "endpoint" = "table.core.chinacloudapi.cn"} Add-AzureRmVmssExtension -VirtualMachineScaleSet $vmss -Name $exname `
      -Publisher $publisher -Type $type `
      -TypeHandlerVersion $typeHandlerVersion `
      -AutoUpgradeMinorVersion $true `
      -Setting $settings `
      -ProtectedSetting $protectedSettings Update-AzureRmVmss -VirtualMachineScaleSet $vmss `
      -ResourceGroupName $rgname -VMScaleSetName $vmssname

     

    其中diag_nosubid.xml文件的链接如下:

    https://hwshare.blob.core.chinacloudapi.cn/soft/diag_nosubid.xml

    62行的用户资源ID需要自己更改。就是上面脚本中$vmss.id的内容。

    由于是自己定义,这里的监控信息有非常多,查看xml文件就可以知道,有MemoryProcessorDisk等多种指标。

     

    3 查看存储账户的监控信息

    由于VMSS创建时,UpgradePolicyMode选择的是automatic。所以在配置更新了Extension后,VM会自动更新配置。

    观察StorageTable的信息,可以看到我们配置的table

    如果用Azure Blob Explorer,可以看到我们定义的各种指标的信息已经吐到table中了:

    有了这些信息就可以添加AutoScale了!

     

    4 添加AutoScaleSeeting

    具体脚本如下:

     

    #增加机器的规则
    $rule1 = New-AzureRmAutoscaleRule -MetricName "ProcessorPercentUserTime" `
      -MetricResourceId $vmss.id -Operator GreaterThan `
      -MetricStatistic Average -Threshold 60 -TimeGrain 00:01:00 `
      -TimeWindow 00:05:00 -ScaleActionCooldown 00:05:00 `
      -ScaleActionDirection Increase -ScaleActionValue 1 #减少机器的规则 $rule2 = New-AzureRmAutoscaleRule -MetricName "ProcessorPercentUserTime" `
      -MetricResourceId $vmss.id -Operator LessThan `
      -MetricStatistic Average -Threshold 30 -TimeGrain 00:01:00 `
      -TimeWindow 00:05:00 -ScaleActionCooldown 00:05:00 `
      -ScaleActionDirection Decrease -ScaleActionValue 1 $profile1 = New-AzureRmAutoscaleProfile -DefaultCapacity 1 `
      -MaximumCapacity 10 -MinimumCapacity 1 `
      -Rules $rule1,$rule2 `
      -Name "autoprofile1" #添加AutoScale的Setting Add-AzureRmAutoscaleSetting -Location $location -Name "autosetting1" `
      -ResourceGroup $rgname `
      -TargetResourceId $vmss.id `
      -AutoscaleProfiles $profile1

    添加完Autoscale Setting后,可以看到,VMSS中的Instance会自动减少到1台。通过添加stress工具,进行CPU的压力测试,可以看到instance的数量会增加。

     

    总结:

    通过PowerShell创建VMSS的集群,通过添加VMDiagnostic,把Instance的资源消耗信息吐出来。再通过Autoscale进行自动的减少和增加Instance

  • 相关阅读:
    Python分布式+云计算
    Python实例31[批量对目录下文件重命名]
    python类库26[sqlite]
    Python-Django的windows环境
    DBA常用SQL总结梳理
    查看db_buffer_cache相关参数
    构建直方图
    单实例数据库DataGuard主库与备库切换
    Linux下查找并关闭进程
    如何创建ASM磁盘?
  • 原文地址:https://www.cnblogs.com/hengwei/p/7391178.html
Copyright © 2011-2022 走看看