---
# System prepended metadata

title: Azure 指令
tags: [Azure, 筆記, 公司]

---

# Azure 指令
## 查詢 subnet id
```
az network vnet subnet list \
    --resource-group Digiwincloud-Production \
    --vnet-name Digiwincloud-Production-AKS \
    --query "[0].id" --output tsv
```
## 建立 AKS
```
az aks create \
    --resource-group Digiwincloud-Production \
    --name Digiwincloud-Production-Kubernetes \
    --network-plugin azure \
    --vnet-subnet-id /subscriptions/941fd7aa-a74b-4d78-9540-b4531e61f8cf/resourceGroups/Digiwincloud-Production/providers/Microsoft.Network/virtualNetworks/Digiwincloud-Production-AKS/subnets/AKS \
    --docker-bridge-address 172.32.0.1/16 \
    --dns-service-ip 10.2.0.10 \
    --service-cidr 10.2.0.0/24 \
    --generate-ssh-keys \
    --max-pods 20 \
    --kubernetes-version 1.15.10 \
    --node-vm-size Standard_D2_v3
    

```
## 連線到 AKS Node
```bash
az vmss extension set  \
    --resource-group MC_Digiwincloud-Kubernetes-Test_Digiwincloud-Kubernetes-test_southeastasia \
    --vmss-name aks-middleware-23667879-vmss \
    --name VMAccessForLinux \
    --publisher Microsoft.OSTCExtensions \
    --version 1.4 \
    --protected-settings "{\"username\":\"azureuser\", \"ssh_key\":\"$(cat ~/.ssh/id_rsa.pub)\"}"

az vmss update-instances --instance-ids '*' \
    --resource-group MC_Digiwincloud-Kubernetes-Test_Digiwincloud-Kubernetes-test_southeastasia \
    --name aks-middleware-23667879-vmss
```
## 取得 k8s config
```bash
az aks get-credentials --resource-group Digiwincloud-Kubernetes-Production --name Digiwincloud-k8s-Production
```
## 移除 addons http routing
```bash
az aks disable-addons --addons http_application_routing --name Digiwincloud-k8s-Production --resource-group Digiwincloud-Kubernetes-Production --no-wait
```
## 建立 Ingress Controller
```bash=
helm install stable/nginx-ingress \
    --namespace kube-system \
    --set controller.replicaCount=2 \
    --set controller.service.annotations."service\.beta\.kubernetes\.io/azure-load-balancer-internal"=true \
    --set controller.publishService.enabled=true \
    --set controller.service.externalTrafficPolicy=Local \
    --set controller.service.loadBalancerIP=172.31.255.255 
    
    
    helm install stable/nginx-ingress \
    --namespace kube-system \
    -f svc.yaml \
    --set controller.replicaCount=2 \
    --set controller.publishService.enabled=true \
    --set controller.service.externalTrafficPolicy=Local \
    --set controller.nodeSelector."beta\.kubernetes\.io/os"=linux \
    --set defaultBackend.nodeSelector."beta\.kubernetes\.io/os"=linux
    
    helm upgrade cold-nightingale stable/nginx-ingress -f svc.yaml --set controller.publishService.enabled=true  --set controller.nodeSelector."beta\.kubernetes\.io/os"=linux  --set defaultBackend.nodeSelector."beta\.kubernetes\.io/os"=linux
```
## 跨區轉移 VM
### 1. 將 VM 進行快照
```powershell=
$resourceGroupName = 'digiwincloud_VM' 
$location = 'japan_east' 
$vmName = 'MobileAPP'
$snapshotName = 'mobile_20190610'

$vm = get-azvm -ResourceGroupName $resourceGroupName -Name $vmName

$snapshot =  New-AzSnapshotConfig `
-SourceUri $vm.StorageProfile.OsDisk.ManagedDisk.Id `
-Location $vm.location `
-CreateOption copy

New-AzSnapshot `
-Snapshot $snapshot `
-SnapshotName $snapshotName `
-ResourceGroupName $resourceGroupName 
```
### 2. 透過第一步所建立的 Snapshot 建立新的硬碟
```powershell=
$subscriptionId = '941fd7aa-a74b-4d78-9540-b4531e61f8cf'
$resourceGroupName ='digiwincloud_VM'
$snapshotName = 'mobile_20190610_data'
$osDiskName = 'mobile_20190610_DataDisk'
select-AzSubscription -SubscriptionId $SubscriptionId
$snapshot = Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
$diskConfig = New-AzDiskConfig -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy
$disk = New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $osDiskName

```
### 3. 硬碟儲存至儲存體的 blob
```powershell=
# 參數說明
## SubscriptionId        : 訂閱帳號 ID
## resourceGroupName     : 資源群組名稱
## diskName              : 硬碟名稱
## storageAccountName    : 儲存體帳戶的名稱
## storageContainerName  : 儲存體容器的名稱
## storageAccountKey     : 儲存體帳戶的 Key
## destinationVHDFileName: 儲存 vhd 的檔案名稱

$SubscriptionId = "941fd7aa-a74b-4d78-9540-b4531e61f8cf"
$resourceGroupName = 'digiwincloud_VM'
$diskName = 'mobile_20190610_DataDisk'
$sasExpiryDuration = "3600"
$storageAccountName = "translatevm"
$storageContainerName = "diskvhd"
$storageAccountKey = '6e/ZL8GnPpXwO1hW9wRz6ClXlEc7+WImTCWQaun9N3FHVobOxoIBeN6iQVrXghRXVEGx2GiMWYNryK+VIJVZjQ=='
$destinationVHDFileName = "mobile_20190610_data.vhd"

Select-AzSubscription -SubscriptionId $SubscriptionId
$sas = Grant-AzDiskAccess -ResourceGroupName $ResourceGroupName -DiskName $diskName -DurationInSecond $sasExpiryDuration -Access Read 
$destinationContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
Start-AzStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer $storageContainerName -DestContext $destinationContext -DestBlob $destinationVHDFileName

```

查看目前複製進度
```
Get-AzStorageBlobCopyState -Blob $destinationVHDFileName -Container $storageContainerName  -Context $destinationContext -WaitForComplete
```
### 4.從儲存體建立硬碟
```powershell=
# 參數說明
## subscriptionId        : 訂閱帳號 ID
## resourceGroupName     : 資源群組名稱
## diskName              : 要建立的硬碟名稱
## diskSize              : 要建立的硬碟大小，單位為 GB
## storageType           : 硬碟類別，分為 Premium_LRS or Standard_LRS
## location              : 區域
## sourceVHDURI          : 來源 vhd 位置
## storageAccountId      : 儲存體帳戶的 ID

$subscriptionId = '941fd7aa-a74b-4d78-9540-b4531e61f8cf'
$resourceGroupName ='鼎捷雲正式區'
$diskName = 'Mobile_OSDisk'
$diskSize = '64'
$storageType = 'Standard_LRS'
$location = 'southeastasia'
$sourceVHDURI = 'https://translatevm.blob.core.windows.net/diskvhd/mobile_20190610_os.vhd'
$storageAccountId = '/subscriptions/941fd7aa-a74b-4d78-9540-b4531e61f8cf/resourceGroups/VM轉移/providers/Microsoft.Storage/storageAccounts/translatevm'

Select-AzSubscription -SubscriptionId $SubscriptionId

$diskConfig = New-AzDiskConfig -AccountType $storageType -Location $location -CreateOption Import -StorageAccountId $storageAccountId -SourceUri $sourceVHDURI

New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $diskName

```

###### tags: `Azure` `公司` `筆記` 