1. 程式人生 > >Azure Stack技術深入淺出系列3: Azure Stack運維工具Azure Stack Tools的使用及實戰

Azure Stack技術深入淺出系列3: Azure Stack運維工具Azure Stack Tools的使用及實戰

azure stack 雲計算 微軟 azure

源自 Azure 的 Azure stack 作為一款業界唯一的和領先的公有雲平臺一致的混合雲平臺,能夠幫助企業客戶從自有數據中心交付 Azure 雲服務。作為《Azure Stack 技術深入淺出系列》的第三篇文章,本文將介紹 Azure Stack 部署完成以後,如何快速的管理和維護這套系統環境,包括虛擬機的管理、遠程 VPN 連接、傳輸部署鏡像、遠程監控等。本文將通過一套 Azure Stack 運維工具集——Azure Stack Tools 來嘗試使用和管理 Azure Stack 集群環境。

Azure Stack Tools 是在 GitHub 上維護的一套 Azure Stack 工具集,由微軟 Azure Stack 團隊為系統開發者和運維者提供的一款基於 Azure Resources Manager 的腳本工具。為了混合雲管理開發運維一致性的設計原理,它沿襲了 Azure Resource Managers 設計風格的指令集,可以使開發者與運維者能夠像使用 AzureRM 模塊一樣使用 Azure Stack Tools。不同於 Portal 界面,Azure Stack Tools 能夠批處理相關運維、監控操作,為相關人員節省不必要的工作。


這一篇,我們將探討以下幾個方面:

  • 準備工作:部署前準備、獲取 Azure Stack Tools

  • 功能介紹及實例:輔助 Azure Stack 安裝工具、遠程連接、上傳鏡像、部署虛機、部署模板

就在本文撰寫過程中,2017 年 7 月 10 日 Azure Stack Development Kit(Azure Stack PoC 版本 GA 之後的名字) GA,Azure Stack Tools 也進行了一定程度的更新。後續,我們會根據更新內容測試之後,更新相關技術文檔。

一、部署前準備

在部署之前需要確認用於部署的物理環境是否能夠滿足部署的基本要求,建議參閱《Azure-Stack-PoC-Deployment》中第一章節“部署前準備”。

二、獲取 Azure Stack Tools

首先我們通過 git 工具從代碼庫中下載 Azure Stack Tools。

git clone https://github.com/Azure/AzureStack-Tools.git --recursive

打開 AzureStack-Tools 文件夾,我們看到 Azure Stack Tools 子模塊,他們涵蓋了部署、連接 VPN、虛機管理、監控等多個不同功能。本篇我們將主要介紹部署、連接和虛機管理這三大功能。
技術分享

三、輔助 Azure Stack 安裝工具

Azure Stack Tools 的裏提供一些腳本用於 Azure Stack 部署流程, Azure Stack Development Kit 發布之後,新推出了基於 GUI 的部署模式,簡化了部署流程,有興趣的讀者可以研究一下 Azure Stack Tool 提供的相關腳本,相信對於理解 Azure Stack 基礎架構會有一定的幫助。

使用 Azure Stack Tools 之前,我們必須安裝 PowerShell 模塊。可以使用如下代碼測試是否已安裝模塊:

Find-Module -Name azurerm.*
Find-Module -Name AzureStack.*

如果出現下列情況,說明已經安裝 AzureRM 模塊。

技術分享

否則請執行下列代碼,安裝 AzureRM

Install-Module -Name ‘AzureRm.Bootstrapper‘ -Scope CurrentUser
Install-AzureRmProfile -profile ‘2017-03-09-profile‘ -Force -Scope CurrentUser
Install-Module -Name AzureStack -RequiredVersion 1.2.9 -Scope CurrentUser

現在我們打開 PowerShell, 開始進行安裝過程。

首先安裝 Azure Stack TP3 支持文件。然後 cd 到該文件目錄下,最後執行“PrepareBootFromVHD.ps1”。

# Variables$Uri = ‘https://raw.githubusercontent.com/Azure/AzureStack-Tools/master/Deployment/‘$LocalPath = ‘c:\AzureStack_TP3_SupportFiles‘# Create folderNew-Item $LocalPath -Type directory# Download files‘BootMenuNoKVM.ps1‘, ‘PrepareBootFromVHD.ps1‘, ‘Unattend.xml‘, ‘unattend_NoKVM.xml‘ | foreach { Invoke-WebRequest ($uri + $_)`    
-Out File ($LocalPath + ‘\‘ + $_) } 

# Deploy from VHD.\PrepareBootFromVHD.ps1 -CloudBuilderDiskPath C:\CloudBuilder.vhdx -ApplyUnattendRestart-Computer -Force

執行完上述操作後,需要重新啟動。

Restart-Computer-force

四、遠程連接

訪問 Azure Stack 服務,有兩種途徑。一種是通過遠程桌面(RDP)訪問 Azure Stack 宿主機中的 MSA-CON01 機器。第二種方式通過 VPN 隧道的形式,使我們使用的電腦成為 Azure Stack 網絡中的一部分,對 Azure Stack 進行訪問。連接 VPN 的好處在於可以讓多個用戶在同一時間對 Azure Stack 進行使用。這一節,我們會介紹如何使用 VPN 連接 Azure Stack。

我們了解下 Azure Stack VPN 連接的流程

技術分享

Azure Stack Tools 工具包中有多個模塊,導入 Connect 文件夾中的 AzureStack.Connect.psm1。

Import-Module .\ComputeAdmin\AzureStack.ComputeAdmin.psm1

使用 window remote manager,在本地電腦中添加 Azure Stack 主機和 MAS-CA 服務信任模式。這裏的 $hostIP 就是安裝 Azure Stack 平臺的主機 IP 地址。

# 在 Powershell 中執行 winrm quickconfig# Add Azure Stack One Node host & CA to the trusted hosts on your client computerSet-Item wsman:\localhost\Client\TrustedHosts -Value $hostIP -Concatenate
Set-Item wsman:\localhost\Client\TrustedHosts -Value mas-ca01.azurestack.local -Concatenate

連接 BGP-NAT 服務器,獲取 NAT IP 地址。$password 為安裝 Azure Stack 平臺部署時所設定的基礎架構虛擬機的管理員密碼

# Update Azure Stack host address to be the IP Address of the Azure Stack POC Host$natIp = Get-AzureStackNatServerAddress -HostComputer $hostIP -Password $Password

當我們獲取 NAT IP 之後,添加 VPN 連接,使用如下代碼即可。

# Create VPN connection entry for the current userAdd-AzureStackVpnConnection -ServerAddress $natIp -Password $Password# Connect to the Azure Stack instance. This command can be used to reconnectConnect-AzureStackVpn -Password $Password

我們打開 windows 電腦顯示網絡界面,此時我們的電腦已經與 Azure Stack 網絡聯通。

技術分享

完成上述操作之後,我們打開 windows 電腦顯示網絡界面,此時我們的電腦已經與 Azure Stack 網絡聯通。

# To test the portal connection, open an Internet browser and navigate to either the user portal https://publicportal.local.azurestack.external 
# the administrator portal, sign in and create resources.  https://portal.local.azurestack.external

我們現在可以在瀏覽器中打開下列地址。

技術分享

在結束 VPN 連接之前,我們可以為 Azure Stack RM 添加兩個環境,這樣做的好處在於。如果我們使用 PowerShell 進行 Azure Stack 腳本部署和模板搭建,這兩個環境能夠方便我們獲取租戶 Id 信息,從而通過 AzureRM 模塊工具對 Azure Stack 進行開發。

# Remove existed EnviromentRemove-AzureRmEnvironment -Name AzureStack
Remove-AzureRmEnvironment -Name AzureStackAdmin# Set AzureStackAdmin ARM EndpointAdd-AzureStackAzureRmEnvironment -Name "AzureStack" -ArmEndpoint "https://management.local.azurestack.external" # Set AzureStackAdmin ARM Endpoint Add-AzureStackAzureRmEnvironment -Name "AzureStackAdmin" -ArmEndpoint "https://adminmanagement.local.azurestack.external"

Azure Stack 從 TP3 開始,順應客戶的要求,分離了 Admin 和 Tenant 的門戶和端點,以確保安全。只有管理員在執行管理類的操作,例如添加 Resource Provider、Image 等,才需要登錄到 AzureStackAdmin 環境;而租戶自己執行操作,則登錄到 AzureStackUser,租戶沒有 Admin 賬戶權限,也不可能登錄到 AzureStackAdmin。

# Get the AAD Tenant GUID$TenantID = Get-DirectoryTenantID -AADTenantName "<mydirectorytenant>.onmicrosoft.com" -EnvironmentName AzureStackAdmin 
#Get the ADFS Tenant GUID$Tenantid = Get-DirectoryTenantID -ADFS -EnvironmentName AzureStackAdmin# Sign-in Admin to AzureStackAdmin ARM Endpoint# e.g username: [email protected] -EnvironmentName "AzureStackAdmin" -TenantId $TenantIDLogin-AzureRmAccount -EnvironmentName "AzureStack" -TenantId $TenantID

五、上傳鏡像

當我們安裝完成 Azure Stack 之後,會發現 Azure Stack 內部什麽都沒有,鏡像庫裏沒有 VHD 鏡像資源,如果我們需要在 MarketPlace 中添加鏡像,可以使用 Azure Stack Tools 中的 ComputeAdmin 模塊進行鏡像的上傳。

在下圖中,我們看到 Virtual Machines 選項中顯示出兩個鏡像資源,分別為 Ubuntu14.02 和 Windows Server 2016。對於 Linux 和 Windows 系統,制作鏡像的途徑不一樣。在本文中,我們著重進行鏡像上傳的操作。如果有童鞋需要了解鏡像制作的方法,可以點擊該鏈接。

技術分享

在創建鏡像資源之前,我們先要載入 Connect 模塊和 ComputeAdmin 模塊

Import-Module .\Connect\AzureStack.Connect.psm1Import-Module .\ComputeAdmin\AzureStack.ComputeAdmin.psm

在上一節,我們已經通過 VPN 的方式為 Azure Stack 打通隧道,我們繼續使用上述環境,利用 VPN 環境獲取租戶的 id。

# Get tenant id$Tenantid = Get-DirectoryTenantID -ADFS -EnvironmentName AzureStackAdmin

接著通過 AzureRM 模塊,登錄 Azure Stack Admin 環境。這邊我們是不是看到一個熟悉的使用環境了?對的,這就是 PowerShell Azure 開發中最基本的腳本運行代碼。為了使 Azure 和 Azure Stack 開發和運維保持一致性,微軟基本沿用了 Azure Resource Manager 的指令。包括在下一章節,我們創建虛機使用的代碼都符合這個規範。

回歸正題,使用下列代碼登錄 Azure Stack:

# Login Administrator AccountLogin-AzureRmAccount -EnvironmentName "AzureStackAdmin" -TenantId $TenantID

此時會彈出一個對話框,需要我們輸入 Azure Stack 管理員賬戶密碼:
技術分享

  • 對於 ADFS 模式:Azure Stack 上傳鏡像需要通過 [email protected] 賬戶進行上傳,不能使用 azure\AzureStackAdmin 域賬戶。

  • 對於 AAD 模式:需要提供安裝 Azure Stack 是提供的國際版 Azure 賬戶密碼。

對於 Windows 鏡像,我們使用下列代碼進行安裝:

$ISOPath = "<Path to ISO>"New-Server2016VMImage -ISOPath $ISOPath -TenantId $TenantID -EnvironmentName "AzureStackAdmin"

對於 Linux 鏡像,我們使用這段代碼進行安裝:

$ISOPath = "<Path to ISO>"Add-VMImage -publisher "Canonical" -offer "UbuntuServer" -sku "14.04.3-LTS" -version "1.0.1" -osType Linux`  
-osDiskLocalPath $ISOPath -tenantID $TenantId -EnvironmentName "AzureStackAdmin"

由於鏡像的容量通常比較大,所以通過遠程 VPN 的方式上傳鏡像會比較慢。這邊有個討巧的方法,通過 RDP 登錄 Azure Stack MAS-CON01 的虛擬機。現將鏡像拷過去,然後在該虛擬機內進行安裝。

當我們運行相關代碼之後,PowerShell 就會顯示出如下信息。通過對 Azure Stack 的組件分析,我們可以了解到。上傳鏡像的過程分為三步:

技術分享

完成鏡像上傳之後,將自動創建 Gallery items 用來通過 GUI 界面讓用戶通過自服務的形式創建資源。相關自定義鏡像創建流程請關註後續推出的《Azure Stack 技術深入淺出系列》第四篇。下圖顯示的是鏡像在上傳的過程,我們可以看到 Azure Stack Tools 顯示了一個 JSON 格式的信息組,這是上傳之後鏡像的基本信息。

技術分享

在下面的截圖中,我們可以看到上傳到鏡像會放置在一個叫 addvmimagestorageaccount 存儲賬號中一個 blob 存儲中的 container 裏面。

技術分享

當結束上述所有過程之後,我們可以看到 StatusCode 顯示 Created,說明鏡像已經上傳成功。此時我們回到 Azure Stack 門戶頁面,可以隨心所欲的創建各種定制化的鏡像。

六、部署虛機

在 Azure Stack 部署虛機與在 Azure 上面部署虛機一樣,我們通過以下代碼通過 Azure Stack 部署。

首先,我們創建資源組。

# Create a resource groupNew-AzureRmResourceGroup -Name ‘myResourceGroup1‘ -Location "local"

註意:-Location 的參數需要使用 local 或用戶自定義區域位置信息,與 Azure 公有雲有所區別

然後創建虛擬網絡以及子網,並為網絡配置公共 IP。

############### Create networking resources ############################### Create a subnet configuration$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name ‘mySubnet01‘`
                -AddressPrefix 10.0.1.0/24# Create a virtual network$vnet = New-AzureRmVirtualNetwork -ResourceGroupName ‘myResourceGroup1‘`
        -Location ‘local‘ -Name myResourceGroup1-vnet`
        -AddressPrefix 10.0.0/16 -Subnet $subnetConfig# Create a public IP address and specify a DNS name$pip = New-AzureRmPublicIpAddress -ResourceGroupName ‘myResourceGroup1‘`
       -Location ‘local‘ -AllocationMethod Static`
       -IdleTimeoutInMinutes 4 -Name "test1-ip"

添加完公網 ip 後,我們為虛機添加網絡安全組。

# Create a virtual network card and associate with public IP address and NSG$nic = New-AzureRmNetworkInterface -Name ‘myNic‘`
       -ResourceGroupName ‘myResourceGroup1‘ -Location ‘local‘ `
       -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id`
       -NetworkSecurityGroupId $nsg.Id# Create an inbound network security group rule for port 3389$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig`
              -Name myNetworkSecurityGroupRuleRDP  -Protocol Tcp `
              -Direction Inbound -Priority 1000 -SourceAddressPrefix *`
              -SourcePortRange * -DestinationAddressPrefix * `
              -DestinationPortRange 3389 -Access Allow# Create an inbound network security group rule for port 80$nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig -Name`
              myNetworkSecurityGroupRuleWWW  -Protocol Tcp `
              -Direction Inbound -Priority 1001 -SourceAddressPrefix *`
              -SourcePortRange * -DestinationAddressPrefix * `
              -DestinationPortRange 80 -Access Allow# Create a network security group$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName myResourceGroup1`
       -Location local -Name test1-nsg -SecurityRules $nsgRuleRDP,$nsgRuleWeb# Create a virtual network card and associate with public IP address and NSG$nic = New-AzureRmNetworkInterface -Name myNic` 
       -ResourceGroupName myResourceGroup1 -Location local `
       -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id`
       -NetworkSecurityGroupId $nsg.Id

最後在 Azure Stack 中添加虛機。

################################ Create VM ############################### Define a credential object$cred = Get-Credential# Create a virtual machine configuration$vmConfig = New-AzureRmVMConfig -VMName myVM -VMSize Standard_D1 | `
    Set-AzureRmVMOperatingSystem -Windows -ComputerName myVM `
    -Credential $cred | Set-AzureRmVMSourceImage -PublisherName `
    MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter `
    -Version latest | Add-AzureRmVMNetworkInterface -Id $nic.Id 

New-AzureRmVM -ResourceGroupName myResourceGroup -Location ‘local‘ -VM $vmConfig

最後我們會在 Azure Stack 界面上看到如下新建資源,說明已經添加成功。

技術分享

七、部署模板

在 Azure Stack 模板工具和權限工具的共同作用下,我們可以通過修改從 Azure 上獲取的模板,將其轉化成 Azure Stack 的模板,從而促進 Azure Stack 的更好開發。

對於 Azure Stack 的 ARM 模板參數,例如 Location、存儲端點等時,目前的使用的範例中特指 POC 環境 (POC 環境端點默認是 azurestack.external、Location 默認是 Local),如果是生產環境,這些都是客戶自己定義,每個 Azure Stack 實例的 Location 和端點並不會相同。

技術分享

Template Validator Tool(模板驗證工具): 用於 Azure 和 Azure Stack 的資源和配置有所不同,所以 Azure Stack Tools 中提供了模板驗證工具。它的主要功能在於驗證從 Azure 中移植的 ARM 是否正確、是否符合 Azure Stack 的規範。我們可以在Azure Stack Template 中找到符合 Azure Stack 規範的模板。

Azure Stack 規範如下:

1、 Azure Stack 資源位置(location)

在 ARM 模板中通常從資源組中獲取 Azure 服務的位置,通過 -location 來確定模板所要使用資源的物理位置。在 Azure 中,我們會獲得 40 多個位置,如 “chinaeast、chinanorth、westus”等。在 Azure Stack 環境下,默認為“local”或者用戶自定義位置,如下面例子:

{ "resources":[
        {            "name": XXXXXXXX,            "type": XXXXXXXXXXXXXX,            "apiVersion": XXXXXXXXXXXXXXXX,            "location": "[resourceGroup().location]", //Incorrect Version: chinaeast            "comments": "This storage account is used to store the VM disks",            "properties":{                "accountType": "Standard_LRS"
            }
        }
    ]
}

2、Blob 存儲 Endpoint

在 Azure 中,我們使用.blob.core.windows.net 來表示 Azure 的 blob 存儲端點。而在 Azure Stack 中,我們使用 .blob.local.azurestack.external 來標識存儲地址

"osDisk":
{    "name": "osDisk",    "vhd": {        "uri": "[
            concat(‘Microsoft.Storage/storageAcounts/‘,
            parameters(‘newStroageAccountName‘), //‘.blob.core.windows.net‘
            variables(‘vmStorageAccountContainerName‘),‘/‘
            variables(‘OSDiskName‘),‘.vhd‘)
        ]",
    }    "caching": "ReadWrite",
    “createOption”: "FromImage"}

3、提供最新 API 版本管理

Azure Stack 的底層技術棧始終遵循 Azure 的技術,並使用 Azure 最新的 API 版本。下表為目前使用的最新 API 接口。

技術分享

更多最新的 API 版本差異請查看官網文檔:

  • https://docs.microsoft.com/en-us/azure/azure-stack/azure-stack-vm-considerations

  • https://docs.microsoft.com/en-us/azure/azure-stack/azure-stack-acs-differences

4、提示支持資源的類型

同時 Azure Stack 還會校驗資源的 sku 和鏡像、擴展等給了開放的功能。

Policy Tool(權限策略工具):隨著 Azure Stack 開發的不斷深入,越來越多 Azure 提供的 IaaS、PaaS 服務也會逐步在 Azure Stack 中開發,其中不乏無服務框架(App function)、區塊鏈(Blockchain)等。在 Azure Stack 眾多的服務中,如果我們希望限定某個資源組的服務和資源使用量,我們可以導入下面是 Policy 文件。

"policyRule": {      
     "if": {        
        "not": {          
        "field": "location",          
        "in": "[parameters(‘allowedLocations‘)]"
        }
      },      
        "then": {        
        "effect": "deny"
      }
    }

而在 Azure Stack Tools 中我們可以到使用 Azure Stack Tools 中的 AzureStack.Policy.psm1 文件

Import-Module .\AzureStack.Policy.psm1

Login-AzureRmAccount$s = Select-AzureRmSubscription -SubscriptionName "<sub name>"$subId = $s.Subscription.SubscriptionId$policy = New-AzureRmPolicyDefinition -Name AzureStack -Policy (Get-AzureStackRmPolicy)

New-AzureRmPolicyAssignment -Name AzureStack -PolicyDefinition $policy -Scope /subscriptions/$subId#Specify the resource group where you would like to apply the policy$rgName = ‘AzureStack‘New-AzureRmPolicyAssignment -Name AzureStack -PolicyDefinition $policy -Scope /subscriptions/$subID/resourceGroups/$rgNameRemove-AzureRmPolicyAssignment -Name AzureStack -Scope /subscriptions/$subId/resourceGroups/$rgNameRemove-AzureRmPolicyAssignment -Name AzureStack -Scope /subscriptions/$subId

八、其他工具

在 Azure Stack Tool 中,提供了一套管理員工具。正如之前提及的上傳鏡像功能一樣,在管理員工具中有服務管理模塊(ServiceAdmin)、計算模塊(ComputeAdmin)、設備模塊(InfraAdmin)。

  • 服務管理模塊: 管理員可以通過該工具創建預定(offer)、計劃(plan)和引用(quota)等服務模塊。

  • 計算模塊:添加和刪除虛機鏡像。

  • 設備模塊:在 Azure Stack 設備中提供監控界面和數據接口

九、總結

通過這一講,我們了解了 Azure Stack Tools 的基本功能,希望用戶閱讀之後,能夠使用 Azure Stack Tools 上傳虛機鏡像和創建虛擬機。對於 Azure Stack 的管理員,能夠通過 Azure Stack Tools 對 Azure Stack 進行資源的限制、訂閱中 offer 的分發以及對 Azure Stack 基礎設施的監控有一定的幫助。相信在使用 Azure Stack Tools 的過程中,童鞋們可以體會 Azure 和 Azure Stack 一致性體驗的理念。


作者:彭毅程


如果對文章內容感興趣請聯系:
儀電(集團)有限公司Azure Stack技術支持團隊( [email protected] / [email protected])
轉載請註明:轉載自Azure Stack Notes博客(http://a-stack.com)



Azure Stack技術深入淺出系列3: Azure Stack運維工具Azure Stack Tools的使用及實戰