2016年8月4日 星期四

Microsoft Azure 管理筆記 - 使用 Image (Resource Manage - template.json) 快速建構、搬遷 Data Center

這個概念是這樣,為了服務世界各地的朋友們,可能一開始在日本服務,過了一陣子想要在美西服務,這時在 AWS 上可以很便利地將 AMI 複製到其他 Region ,而對 Azure 咧?概念是一樣,但在 Azure Resource Manage 的思維下,做法多了很多步。

參考資料:
步驟如下(在此有做 storage account 的切換,若是要沿用同一個 storage account 可以不用做第三步):
  1. 建立新的 Resource:US,歸屬定 West US
  2. 建立 Virtual Network、Subnet
  3. 建立 Storage account,此乃資料儲存帳號
  4. 將原本所在的 Image (vhd) 複製到新的 Storage account
  5. 建立 Network Interface Controller
  6. 將原本的 template.json 修改,把storage account更新至新的區域
  7. 即可建置新機器
細部流程,大多以 Azure cli 運行:

建立位於 West US 的 Resource Group: US

$ azure group create US westus --subscription MySubscription
info:    Executing command group create
+ Getting resource group US                                                  
+ Creating resource group US                                                
info:    Created resource group US
data:    Id:                  /subscriptions/MySubscription/resourceGroups/US
data:    Name:                US
data:    Location:            westus
data:    Provisioning State:  Succeeded
data:    Tags: null
data:  
info:    group create command OK


建立 Virtual Network (也可多用 -a 10.1.0.0/16 來限制 address prefix)

$ azure network vnet create US USVirtualNetwork westus  --subscription MySubscription
info:    Executing command network vnet create
warn:    Using default address prefix: 10.0.0.0/8
+ Looking up the virtual network "USVirtualNetwork"                          
+ Creating virtual network "USVirtualNetwork"                                
data:    Id                              : /subscriptions/MySubscription/resourceGroups/US/providers/Microsoft.Network/virtualNetworks/USVirtualNetwork
data:    Name                            : USVirtualNetwork
data:    Type                            : Microsoft.Network/virtualNetworks
data:    Location                        : westus
data:    Provisioning state              : Succeeded
data:    Address prefixes:
data:      10.0.0.0/8
info:    network vnet create command OK


建立 Subnet

$ azure network vnet subnet create US USVirtualNetwork default -a 10.1.0.0/16 --subscription MySubscription
info:    Executing command network vnet subnet create
+ Looking up the virtual network "USVirtualNetwork"                          
+ Looking up the subnet "default"                                            
+ Creating subnet "default"                                                  
data:    Id                              : /subscriptions/MySubscription/resourceGroups/US/providers/Microsoft.Network/virtualNetworks/USVirtualNetwork/subnets/default
data:    Name                            : default
data:    Provisioning state              : Succeeded
data:    Address prefix                  : 10.1.0.0/16
info:    network vnet subnet create command OK


接著建議從 Azure Portal Web UI 建立 storage account ,大多採預設即可,需留意區域位置以及採用的 resource group,此例帳號名稱為 myusstorageaccount,而使用 azure 操作 storage 行為時,必須使用 storage access key,於是就 azure cli 先列出一下(也可以在 Azure Portal 找到)

$ azure storage account keys list myusstorageaccount --resource-group US --subscription MySubscription
info:    Executing command storage account keys list
+ Getting storage account keys                                              
data:    Name  Key             Permissions
data:    ----  --------------  -----------
data:    key1  111111111111==  Full    
data:    key2  222222222222==  Full    
info:    storage account keys list command OK


建立 system container(後續 AZURE_STORAGE_ACCOUNT 跟 AZURE_STORAGE_ACCESS_KEY 都會接在指令前面,方便 debug 用途)

$ AZURE_STORAGE_ACCOUNT=myusstorageaccount AZURE_STORAGE_ACCESS_KEY=111111111111== azure storage container create system
info:    Executing command storage container create
+ Creating storage container system                                          
+ Getting storage container information                                      
data:    {
data:        name: 'system',
data:        metadata: {},
data:        etag: '"################"',
data:        lastModified: 'Thu, 04 Aug 2016 12:01:14 GMT',
data:        lease: { status: 'unlocked', state: 'available' },
data:        requestId: '############################',
data:        publicAccessLevel: 'Off'
data:    }
info:    storage container create command OK


接著,要把 Image copy 過來,這邊有些小細節要留意的,第一存取權限,第二這是非同步複製的。對於存取權限部分,請先將來源的 storage account 裡的 system container 的存取權限更新為 Blob 模式(此模式是必須知道完整的 URL 才能下載),預設是 private 模式(需搭配AZURE_STORAGE_ACCOUNT/AZURE_STORAGE_ACCESS_KEY才能),還有另一個是 Container 模式(這也是公開的,比 Blob 模式多了 listing 功能)。由於 azure storage blob 指令操作,對於 source 是 private 權限一直沒有成功 orz 且範例都是 Blob 的,只好忍痛跟著用了 Orz

以下是 source 為 private 權限,但 copy 一直沒成功(狀態會顯示success),但找不到資料的:

$ AZURE_STORAGE_ACCOUNT=myusstorageaccount AZURE_STORAGE_ACCESS_KEY=111111111111== azure storage blob copy start https://sourceaccount.blob.core.windows.net/system/Microsoft.Compute/Images/vhds/my-vm-osDisk.12345-23456-34567-45678.vhd system -a sourceaccount -k sourceaccount_KEY
info:    Executing command storage blob copy start
- Start copying blob https://sourceaccount.blob.core.windows.net/system/Microsoft.Compute/Images/vhds/my-vm-osDisk.12345-23456-34567-45678.vhd
data:    Copy ID                               Status
data:    ------------------------------------  -------
data:    ####################################  success
info:    storage blob copy start command OK


而將 source 更新為 Blob 存取權限後,就可以看到 Status 會顯示 pending 且約2~3分鐘,連 Azure Portal Web UI / Azure cli 都會找到!

$ AZURE_STORAGE_ACCOUNT=myusstorageaccount AZURE_STORAGE_ACCESS_KEY=111111111111== azure storage blob copy start https://sourceaccount.blob.core.windows.net/system/Microsoft.Compute/Images/vhds/my-vm-osDisk.12345-23456-34567-45678.vhd system
info:    Executing command storage blob copy start
- Start copying blob https://sourceaccount.blob.core.windows.net/system/Microsoft.Compute/Images/vhds/my-vm-osDisk.12345-23456-34567-45678.vhd
data:    Copy ID                               Status
data:    ------------------------------------  -------
data:    ####################################  pending
info:    storage blob copy start command OK

$ AZURE_STORAGE_ACCOUNT=myusstorageaccount AZURE_STORAGE_ACCESS_KEY=111111111111== azure storage blob show
info:    Executing command storage blob show
Container name:  system
Blob name:  Microsoft.Compute/Images/vhds/my-vm-osDisk.12345-23456-34567-45678.vhd
+ Getting storage blob information                                          
data:    Property       Value                                                                                            
data:    -------------  ----------------------------------------------------------------------------------------------------
data:    container      system                                                                                            
data:    name           Microsoft.Compute/Images/vhds/my-vm-osDisk.12345-23456-34567-45678.vhd
data:    blobType       PageBlob                                                                                          
data:    contentLength  1234567890                                                                                      
data:    contentType    application/octet-stream                                                                          
data:    contentMD5     ############==                                                                          
info:    storage blob show command OK


當複製完畢後(其實我也看不懂 azure storage blob show 的回報,也沒寫複製進度...),剩下就是建構一台機器的流程,包含建立 public ip、網路卡(NIC)、使用 template.json 發布。

如果你沒有把 Image 搬過來,直接用原本 template.json 開機器,會看到以下訊息(若同一個 storage account 則沒有問題):

error:   Deployment provisioning state was not successful
error:   Source and destination storage accounts for disk my-vm-osDisk.12345-23456-34567-45678.vhd are different.


若資料還沒搬完,會看到:

error:   Deployment provisioning state was not successful
error:   The resource operation completed with terminal provisioning state 'Failed'.
error:   Disk image https://myusstorageaccount.blob.core.windows.net/system/Microsoft.Compute/Images/vhds/my-vm-osDisk.12345-23456-34567-45678.vhd is in Pending state. Please retry when image is ready.


建立 IP:

$ azure network public-ip create US ip-my-server -l westus --subscription MySubscription

建立網卡:

$ azure network nic create US nic-my-server -k default -m USVirtualNetwork -l westus --subscription MySubscription

或給予 public ip:

$ azure network nic create US nic-my-server -k default -m USVirtualNetwork -l westus --subscription MySubscription -p ip-my-server

建立機器:

$ azure group deployment create US -f my-servier-template.json --subscription MySubscription -p '{"vmName":{"value":"my-server"},"networkInterfaceId":{"value":"/subscriptions/MySubscription/resourceGroups/US/providers/Microsoft.Network/networkInterfaces/nic-my-server"}}'

沒有留言:

張貼留言