美文网首页
Create custom image with Azure C

Create custom image with Azure C

作者: 华阳_3bcf | 来源:发表于2019-11-26 14:39 被阅读0次

Prepare environment

Create resource group

$ az group create -n roy-image -l northeurope
{
  "id": "/subscriptions/xxx-xxx/resourceGroups/roy-image",
  "location": "northeurope",
  "managedBy": null,
  "name": "roy-image",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": null
} 

Create vm

$ az vm create --resource-group roy-image --name roy-base-image --image CentOS
{
  "fqdns": "",
  "id": "/subscriptions/xxx-xxx/resourceGroups/roy-image/providers/Microsoft.Compute/virtualMachines/roy-base-image",
  "location": "northeurope",
  "macAddress": "00-0D-3A-B3-A1-DD",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.4",
  "publicIpAddress": "137.135.205.90",
  "resourceGroup": "roy-image",
  "zones": ""
}

Install a new package on the VM

$ ssh 137.135.205.90
[roy-base-image ~]$ which java
/usr/bin/which: no java in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/edaizen/.local/bin:/home/edaizen/bin)
[roy-base-image ~]$ sudo yum install java-1.8.0-openjdk -y
...
[roy-base-image ~]$ java -version
openjdk version "1.8.0_232"
OpenJDK Runtime Environment (build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)

Create a custom image

Deprovision the VM.

Deprovisioning generalizes the VM by removing machine-specific information. This generalization makes it possible to deploy many VMs from a single image.

sudo waagent -deprovision+user -force

[roy-base-image ~]$ sudo waagent -deprovision+user -force
WARNING! The waagent service will be stopped.
WARNING! All SSH host key pairs will be deleted.
WARNING! Cached DHCP leases will be deleted.
WARNING! root password will be disabled. You will not be able to login as root.
WARNING! /etc/resolv.conf will be deleted.
WARNING! edaizen account and entire home directory will be deleted.
2019/11/25 07:30:57.664644 INFO Examine /proc/net/route for primary interface
2019/11/25 07:30:57.668427 INFO Primary interface is [eth0]
2019/11/25 07:30:57.671188 INFO interface [lo] has flags [73], is loopback [True]
2019/11/25 07:30:57.685403 INFO Interface [lo] skipped
2019/11/25 07:30:57.688520 INFO interface [eth0] has flags [4163], is loopback [False]
2019/11/25 07:30:57.692744 INFO Interface [eth0] selected
[roy-base-image ~]$ exit

Deallocate and mark the VM as generalized

az vm deallocate --resource-group myResourceGroup --name myVM

$ az vm deallocate --resource-group roy-image -n roy-base-image

Set the state of the VM as generalized, so the Azure platform knows the VM has been generalized. You can only create an image from a generalized VM.

az vm generalize --resource-group myResourceGroup --name myVM

$ az vm generalize --resource-group roy-image -n roy-base-image

Create the image

az image create --resource-group myResourceGroup --name myImage --source myVM

$ az image create --resource-group roy-image --name roy-java-image --source roy-base-image
{
  "id": "/subscriptions/xxx-xxx/resourceGroups/roy-image/providers/Microsoft.Compute/images/roy-java-image",
  "location": "northeurope",
  "name": "roy-java-image",
  "provisioningState": "Succeeded",
  "resourceGroup": "roy-image",
  "sourceVirtualMachine": {
    "id": "/subscriptions/xxx-xxx/resourceGroups/roy-image/providers/Microsoft.Compute/virtualMachines/roy-base-image",
    "resourceGroup": "roy-image"
  },
  "storageProfile": {
    "dataDisks": [],
    "osDisk": {
      "blobUri": null,
      "caching": "ReadWrite",
      "diskSizeGb": 30,
      "managedDisk": {
        "id": "/subscriptions/xxx-xxx/resourceGroups/roy-image/providers/Microsoft.Compute/disks/roy-base-image_OsDisk_1_29cd00659e7c4c1ba86b41a2d18d9702",
        "resourceGroup": "roy-image"
      },
      "osState": "Generalized",
      "osType": "Linux",
      "snapshot": null,
      "storageAccountType": "Premium_LRS"
    },
    "zoneResilient": null
  },
  "tags": {},
  "type": "Microsoft.Compute/images"
}

Create VMs from the image

Create VM in the same resource group

az vm create
--resource-group myResourceGroup
--name myVMfromImage
--image myImage
--admin-username azureuser
--generate-ssh-keys

$ az vm create -g roy-image -n royfromImage --image roy-java-image
{
  "fqdns": "",
  "id": "/subscriptions/xxx-xxx/resourceGroups/roy-image/providers/Microsoft.Compute/virtualMachines/royfromImage",
  "location": "northeurope",
  "macAddress": "00-0D-3A-65-B1-9C",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.5",
  "publicIpAddress": "137.135.189.32",
  "resourceGroup": "roy-image",
  "zones": ""
}

verify java on new vm

$ ssh 137.135.189.32
The authenticity of host '137.135.189.32 (<no hostip for proxy command>)' can't be established.
ECDSA key fingerprint is SHA256:YwGLE8jZzh4GijSCUxZxwHZ/XAxPQSkyTdZHYHG31pQ.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '137.135.189.32' (ECDSA) to the list of known hosts.
[royfromImage ~]$ which java
/usr/bin/java
[royfromImage ~]$ java -version
openjdk version "1.8.0_232"
OpenJDK Runtime Environment (build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)

Create VM in another resource group

You should specify the image object ID. You can obtain the image ID with az image show

$ az group create -n roy-new -l northeurope
{
  "id": "/subscriptions/xxx-xxx/resourceGroups/roy-new",
  "location": "northeurope",
  "managedBy": null,
  "name": "roy-new",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": null
}
$ az vm create -g roy-new -n royfromImage --image /subscriptions/xxx-xxx/resourceGroups/roy-image/providers/Microsoft.Compute/images/roy-java-image
{
  "fqdns": "",
  "id": "/subscriptions/xxx-xxx/resourceGroups/roy-new/providers/Microsoft.Compute/virtualMachines/royfromImage",
  "location": "northeurope",
  "macAddress": "00-0D-3A-64-EA-06",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.4",
  "publicIpAddress": "137.116.250.114",
  "resourceGroup": "roy-new",
  "zones": ""
}

Limitation

Custom image can be only used in the same region.

Reference:

https://docs.microsoft.com/en-us/azure/virtual-machines/linux/tutorial-custom-images

相关文章

网友评论

      本文标题:Create custom image with Azure C

      本文链接:https://www.haomeiwen.com/subject/sqgewctx.html