1. 程式人生 > >Azure Stack運維工具Azure Stack Tools的使用及實戰

Azure Stack運維工具Azure Stack Tools的使用及實戰

Azure Stack

作者:彭毅程

編輯:江柳

源自 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》(http://www.a-stack.com/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 安裝工具

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

否則請執行下列程式碼,安裝 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 folder
New-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 -ApplyUnattend
Restart-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 computer
Set-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 user
Add-AzureStackVpnConnection -ServerAddress $natIp -Password $Password

# Connect to the Azure Stack instance. This command can be used to reconnect
Connect-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 Enviroment
Remove-AzureRmEnvironment -Name AzureStack
Remove-AzureRmEnvironment -Name AzureStackAdmin

# Set AzureStackAdmin ARM Endpoint
Add-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]
Login-AzureRmAccount -EnvironmentName "AzureStackAdmin" -TenantId $TenantID
Login-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.psm1
Import-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 Account
Login-AzureRmAccount -EnvironmentName "AzureStackAdmin" -TenantId $TenantID

此時會彈出一個對話方塊,需要我們輸入 Azure Stack 管理員賬戶密碼:

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 的元件分析,我們可以瞭解到。上傳映象的過程分為三步:

PowerShell

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

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

container

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

部署虛機

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

首先,我們建立資源組。

# Create a resource group
New-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 和端點並不會相同。

emplate 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/$rgName

Remove-AzureRmPolicyAssignment -Name AzureStack -Scope /subscriptions/$subId/resourceGroups/$rgName
Remove-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 一致性體驗的理念。

文章來自微信公眾號:細說雲端計算