Azure上批量创建OS Disk大于30G的Linux VM

Azure上VM的OS盘的大小在创建时是固定的。Windows是127G,Linux是30G。如果需要批量创建的VM的OS Disk有更大的容量。可以考虑用下面的方法实现。

1 创建一台有Data-disk的CentOS VM,对其进行客户化,安装软件,挂载磁盘

2 扩大OS Disk的容量

3 在VM内resize OS Disk的容量

4 把这台VM捕获成Azure的Image

5 通过这个Image批量创建VM。创建VM的OS Disk容量是刚刚调整的容量

本文将采用Azure CLI 2.0实现这些操作:

 

一 创建VM

1 创建Resource Group

az group create --name hwmd --location chinanorth

2 创建VM

az vm create -g hwmd -n hwmd01 --image CentOS --authentication-type password --admin-username hengwei --admin-password xxxx --size Standard_D1 --storage-sku Standard_LRS

3 挂载Data-Disk

az vm disk attach --vm-name hwmd01 --resource-group hwmd --size-gb 30 --sku Standard_LRS --caching None --new --disk hwmd01data01 --lun 1

4 SSH到这台VM,进行客户化工作

iptables -F
yum install -y httpd
fdisk /dev/sdc
mkfs.ext4 /dev/sdc1
vim /etc/sysconfig/selinux
setenforce 0
mount /dev/sdc1 /var/www/html/
df -h
cd /var/www/html
echo "Hello World" > index.html
systemctl enable httpd
systemctl start httpd
systemctl status httpd
vim /etc/fstab
mount -a

 

二 扩大OS Disk的容量

1 VM停机

az vm deallocate -g hwmd -n hwmd01

2 扩大OS Disk的Size

查看disk情况;

az disk list --o table

扩大Disk的size:

az disk update --resource-group hwmd --name osdisk_3yQQnL1V5E --size-gb 60

 

三 在VM中Resize OS Disk的容量

1 start vm

az vm start -g hwmd -n hwmd01

2 ssh到VM进行删除partition,重新创建partition(数据不会丢失)

fdisk /dev/sda
Command (m for help): u
Changing display/entry units to cylinders (DEPRECATED!).
Command (m for help): p
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 sectors
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
/dev/sda2              64        3917    30944256   83  Linux
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2):
First cylinder (64-7832, default 64):
Using default value 64
Last cylinder, +cylinders or +size{K,M,G} (64-7832, default 7832):
Using default value 7832
Partition 2 of type Linux and of size 59.5 GiB is set
Command (m for help): p
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 sectors
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
/dev/sda2              64        7832    62397516   83  Linux

此时所有的容量都用满了。保存后重新启动

3 resize OS Disk

CentOS7以上机器的命令为:

xfs_growfs -d /dev/sda2

CentOS6的机器命令为:

resize2fs /dev/sda

可以看到OS Disk已经是60G的容量了。

[root@hwmd01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 60G 1.3G 59G 3% /

 

四 捕获Image

1 VM内通用化

waagent -deprovision

2 Azure平台对VM进行通用化

az vm deallocate -g hwmd -n hwmd01
az vm generalize -g hwmd -n hwmd01

3 捕获Image

az image create -g hwmd --name hwmdimage --source hwmd01

 

五 从这个Image创建VM

1 创建VM

az vm create -g hwmd -n hwmd03 --authentication-type password --admin-user hengwei --admin-password xxxx --image hwmdimage

2 SSH到VM查看Disk和访问的情况

[root@hwmd03 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 60G 1.3G 59G 3% /
/dev/sdc1 30G 45M 28G 1% /var/www/html

可以看到OS Disk已经是60G了,同时Image中挂载的30G的Disk也在。

由于Azure CLI会自动加载NSG到VM的网卡上,且Linux的NSG只允许22端口的访问,所以要开放80端口,或删除NSG,才能访问httpd的内容。

可以看到之前加载的内容在新建的机器中也运行起来了。

 

六 总结

通过把客户化的VM捕捉成Image,可以方便的进行复制。客户化的内容不光包括安装软件和用户数据,增添的数据盘、扩充的OS Disk,都可以被不准下来。

posted @ 2017-08-11 23:49  衡子  阅读(1042)  评论(0编辑  收藏  举报