顯示具有 VM 標籤的文章。 顯示所有文章
顯示具有 VM 標籤的文章。 顯示所有文章

2016年8月2日 星期二

Microsoft Azure 管理筆記 - 使用 Azure CLI 建立自訂的映像檔與開新機器的方式 (create a custom image/launch a server)

由於 Azure Portal 對 Resource manager 機器尚為提供 Web UI 可以方便點擊處理,就還是下海摸一下 command line 啦。

參考文件:

對 server 建立 image 過程中,server 狀態改變後會無法重啟,得用建立好的 Resource template 重新建立,需分外留意!機器就這樣下去領便當了 囧 (Azure/azure-powershell: UN-generalize a VM)

首先,遠端登入機器,執行此道指令

$ sudo waagent -deprovision+user

此命令會嘗試清除系統,使之適合重新佈建。這項作業會執行下列工作:

移除 SSH 主機金鑰 (如果組態檔中的 Provisioning.RegenerateSshHostKeyPair 是 'y')
清除 /etc/resolv.conf 中的名稱伺服器設定
移除 /etc/shadow 中的 root 使用者密碼 (如果組態檔中的 Provisioning.DeleteRootPassword 是 'y')
移除快取的 DHCP 用戶端租用
將主機名稱重設為 localhost.localdomain
刪除最後佈建的使用者帳戶 (取自於 /var/lib/waagent) 和相關聯的資料。


接著,再回到自己的常用的機器,改用 azure cli 對該機器設置以下流程:

$ azure config mode arm
info:    Executing command config mode
info:    New mode is arm
info:    config mode command OK


// Shutdown a virtual machine in a resource group and release the compute resources
$ azure vm deallocate -g MyResource -n MyCurrentVM --subscription MySubscription
info:    Executing command vm deallocate
+ Looking up the VM "MyCurrentVM"                            
+ Deallocating the virtual machine "MyCurrentVM"            
info:    vm deallocate command OK


// Set the state of a VM in a resource group to Generalized.
$ azure vm generalize MyResource MyCurrentVM --subscription MySubscription
info:    Executing command vm generalize
+ Looking up the VM "MyCurrentVM"                            
+ Generalizing the virtual machine "MyCurrentVM"            
info:    vm generalize command OK


$ azure vm capture MyResource MyCurrentVM MyImageID -t MyImageID-base.json --subscription MySubscription
info:    Executing command vm capture
+ Looking up the VM "MyCurrentVM"                            
+ Capturing the virtual machine "MyCurrentVM"                
info:    Saved template to file "MyImageID-base.json"
info:    vm capture command OK


接著,可以建立新開機器!只是開機器前又得好好管理"Resouce"建立,由於我已經有常用的 Resource 跟 Location 了,在此只需建立 IP 跟 NIC 即可!

$ azure network public-ip create MyResource MyImageID-ip-1 -l westus --subscription MySubscription
info:    Executing command network public-ip create
warn:    Using default --idle-timeout 4
warn:    Using default --allocation-method Dynamic
warn:    Using default --ip-version IPv4
+ Looking up the public ip "MyImageID-ip-1"                              
+ Creating public ip address "MyImageID-ip-1"                            
data:    Id                              : /subscriptions/MySubscription/resourceGroups/MyResource/providers/Microsoft.Network/publicIPAddresses/MyImageID-ip-1
data:    Name                            : MyImageID-ip-1
data:    Type                            : Microsoft.Network/publicIPAddresses
data:    Location                        : westus
data:    Provisioning state              : Succeeded
data:    Allocation method               : Dynamic
data:    IP version                      : IPv4
data:    Idle timeout in minutes         : 4
info:    network public-ip create command OK


$ azure network nic create MyResource MyImageID-nic-1 -k default -m MyResource -p MyImageID-ip-1 -l westus --subscription MySubscription
info:    Executing command network nic create
+ Looking up the network interface "MyImageID-nic-1"                    
+ Looking up the subnet "default"                                            
+ Looking up the public ip "MyImageID-ip-1"                              
+ Creating network interface "MyImageID-nic-1"                          
data:    Id                              : /subscriptions/MySubscription/resourceGroups/MyResource/providers/Microsoft.Network/networkInterfaces/MyImageID-nic-1
data:    Name                            : MyImageID-nic-1
data:    Type                            : Microsoft.Network/networkInterfaces
data:    Location                        : westus
data:    Provisioning state              : Succeeded
data:    Internal domain name suffix     : #############.dx.internal.cloudapp.net
data:    Enable IP forwarding            : false
data:    IP configurations:
data:      Name                          : default-ip-config
data:      Provisioning state            : Succeeded
data:      Private IP address            : 10.0.0.6
data:      Private IP version            : IPv4
data:      Private IP allocation method  : Dynamic
data:      Public IP address             : /subscriptions/MySubscription/resourceGroups/MyResource/providers/Microsoft.Network/publicIPAddresses/MyImageID-ip-1
data:      Subnet                        : /subscriptions/MySubscription/resourceGroups/MyResource/providers/Microsoft.Network/virtualNetworks/MyResource/subnets/default
data:  
info:    network nic create command OK


建立機器吧!

$ azure --version
0.10.2 (node: 4.2.4)


$ azure group deployment create MyResource -f MyImageID-base.json --subscription MySubscription
$ azure group deployment create MyResource -f MyImageID-base.json --subscription MySubscription -p '{"vmName":{"value":"MyVM"},"adminUserName":{"value":"ubuntu"},"adminPassword":{"value":"MyPassword"},"networkInterfaceId":{"value":"/subscriptions/MySubscription/resourceGroups/MyResource/providers/Microsoft.Network/networkInterfaces/MyImageID-nic-base"}}'


然而,因為預設產出的 template 定義了以下資訊,以至於開機器必須填寫以下資訊:

$ cat template.json | jq '.parameters'
{
  "vmName": {
    "type": "string"
  },
  "vmSize": {
    "type": "string",
    "defaultValue": "Standard_A1"
  },
  "adminUserName": {
    "type": "string"
  },
  "adminPassword": {
    "type": "securestring"
  },
  "networkInterfaceId": {
    "type": "string"
  }
}


用在這邊:

$ cat template.json | jq '.resources[0].properties.osProfile'
{
  "computerName": "[parameters('vmName')]",
  "adminUsername": "[parameters('adminUsername')]",
  "adminPassword": "[parameters('adminPassword')]"
}


接著,稍微修改來支援 ssh keypair 登入方式,將 template.json 中的 parameters 多增加個 adminPublicKey/adminPublicKeyPath:

$ cat template.json | jq '.parameters'
{
  "vmName": {
    "type": "string"
  },
  "vmSize": {
    "type": "string",
    "defaultValue": "Standard_A1"
  },
  "adminUserName": {
    "type": "string"
  },
  "adminPassword": {
    "type": "securestring",
    "defaultValue": null
  },
  "networkInterfaceId": {
    "type": "string"
  },
  "adminPublicKey": {
    "type": "array"
  },
  "adminPublicKeyPath": {
    "type": "string"
  }
}


並修改 properties.osProfile 區:

$ cat template.json | jq '.resources[0].properties.osProfile'
{
  "computerName": "[parameters('vmName')]",
  "adminUsername": "[parameters('adminUsername')]",
  "adminPassword": "[parameters('adminPassword')]",
  "linuxConfiguration": {
    "disablePasswordAuthentication": true,
    "ssh": {
      "publicKeys": [
         {
           "path": "[parameters('adminPublicKeyPath')]",
           "keyData": "[parameters('adminPublicKey')]"
         }
      ]
    }
  }
}


如此一來,就開機能用 keypair 登入機器:

$ azure group deployment create MyResource -f MyImageID-base.json --subscription MySubscription -p '{"adminPassword":{"value":""},"vmName":{"value":"MyVM"},"adminUserName":{"value":"ubuntu"},"adminPublicKey":{"value":"ssh-rsa ########"},"adminPublicKeyPath":{"value":"/home/ubuntu/.ssh/authorized_keys"},"networkInterfaceId":{"value":"/subscriptions/MySubscription/resourceGroups/MyResource/providers/Microsoft.Network/networkInterfaces/MyImageID-nic-base"}}'

2014年8月19日 星期二

[Linux] 初次使用 Docker 筆記 @ Ubuntu 14.04

最近找尋系統監控的方式時,無意間看到 Docker 的消息,雖然以前用過 LXC 一陣子,久了沒用又都忘光光,這次 Docker 還滿紅的,就順手學一下吧!若安裝前,想體驗一下,可以試看看官網推的教學模式:https://www.docker.com/tryit/

在使用前,想說找一下 Docker 的商業模式,找了很久都沒看到 XD 直到使用 Docker hub 時才發現,商業模式就像 github/bitbucket 一樣,想要建立 private repo 的則需要付費,此例就是可以打包自己的開發環境送到 Docker hub 上使用,免費方案有提供 1 個 private 單位哦。


回到筆記,根據官網介紹 https://docs.docker.com/installation/ubuntulinux/,依序幾個動作就完成安裝了:

$ sudo apt-get update
$ sudo apt-get install docker.io
$ sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker
$ sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io


開始使用:

$ sudo docker run ubuntu:12.04 echo "hello world"
Unable to find image 'ubuntu:12.04' locally
Pulling repository ubuntu
822a01ae9a15: Download complete
511136ea3c5a: Download complete
93c381d2c255: Download complete
a5208e800234: Download complete
9fccf650672f: Download complete
1186c90e2e28: Download complete
f6a1afb93adb: Download complete
hello world


此命令是說要跑一個 ubuntu 12.04 環境,在其環境執行 echo "hello world" 的意思,整個過程就是包含初始化(下載image)後,最後在執行 echo "hello world",有興趣可以再執行一次,這次就不會再去下載 image 了。

然而,上述的動作只是一次性的,舉例來說,初始環境執行 apt-get install curl 是會出錯的:

$ sudo docker run ubuntu:12.04 apt-get install curl
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package curl


但只要 apt-get update 後就會正常,但不能分開執行:

$ sudo docker run ubuntu:12.04 apt-get update
Get:1 http://archive.ubuntu.com precise Release.gpg [198 B]
Get:2 http://archive.ubuntu.com precise-updates Release.gpg [198 B]
Get:3 http://archive.ubuntu.com precise-security Release.gpg [198 B]
...
Reading package lists...
$ sudo docker run ubuntu:12.04 apt-get install curl
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package curl


因此,正式的使用其實是開個 terminal 進去連續動作:

$ sudo docker run -t -i ubuntu:12.04
root@431bb00c701d:/#


其中 431bb00c701d 則是此 container ID,

root@431bb00c701d:/# apt-get install wget
Reading package lists... Done
Building dependency tree    
Reading state information... Done
E: Unable to locate package wget
root@431bb00c701d:/# apt-get update
...
root@431bb00c701d:/# apt-get install wget
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following extra packages will be installed:
  libidn11
The following NEW packages will be installed:
  libidn11 wget
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 391 kB of archives.
After this operation, 966 kB of additional disk space will be used.
Do you want to continue [Y/n]?


接著按 exit 離開後,其實就跟上述單行指令操作一樣,什麼都沒留下,因此在離開之前,想要保存環境則是要進行 commit。

此時,必須額外再開一個 terminal 用 docker ps 查看目前已執行的 image 環境:

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5db62a9bf492        ubuntu:14.04        /bin/bash           11 seconds ago      Up 10 seconds                           goofy_curie      
431bb00c701d        ubuntu:12.04        /bin/bash           35 seconds ago      Up 34 seconds                           agitated_thompson


此例代表有2個 container 在運行,此例目標是 ubuntu:12.04 的 431bb00c701d ,想要儲存環境變化,就來個 commit 吧

$ sudo docker commit -m 'ubuntu 12.04 with wget' 431bb00c701d
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              aac0f5ce2936        9 minutes ago       137.7 MB
ubuntu              14.04               c4ff7513909d        7 days ago          213 MB
ubuntu              12.04               431bb00c701d        7 days ago          108 MB


想要有更佳的描述,就在 docker commit  時,多加一點資訊 REPOSITORY[:TAG] 吧:

$ sudo docker commit -m 'ubuntu 12.04 with wget' e7e35769932d MyUbuntu:12.04
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
MyUbuntu            12.04               d3466dce0e51        About a minute ago   108 MB
<none>              <none>              aac0f5ce2936        10 minutes ago       137.7 MB
ubuntu              14.04               c4ff7513909d        7 days ago           213 MB
ubuntu              12.04               431bb00c701d        7 days ago           108 MB


下次要用時:

$ sudo docker run -i -t MyUbuntu:12.04
root@600d3fee924d:/# wget
wget: missing URL
Usage: wget [OPTION]... [URL]...

Try `wget --help' for more options.


@ 2014-10-24 加映場:[Linux] Docker 使用筆記 - 常用指令 @ Ubuntu 14.04