0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

エフェメラル OS ディスクの Azure VM を作ってみた

Posted at

背景と目的

エフェメラルディスクは、VM が起動する物理マシン上に割り当てたディスク領域で、VM に障害があって別の物理マシンに移動してしまったり VM を割り当て解除すると、物理マシン上に割り当てたディスク領域が使用出来なくなります。

エフェメラル OS ディスクは、OS がエフェメラルディスクという事で、ディスク利用料金がディスクを Azure Storage に保存しないので無料であったり、リモートの Azure Storage にアクセスしない分、ローカル物理マシン上にディスクがあるので高速、といったメリットがあります。

前提条件

コマンドの実施環境は、Mac + Azure CLI です。

bash
$ sw_vers
ProductName:    macOS
ProductVersion: 12.0.1
BuildVersion:   21A559

$ az version
{
  "azure-cli": "2.29.1",
  "azure-cli-core": "2.29.1",
  "azure-cli-telemetry": "1.0.6",
  "extensions": {}
}

エフェメラル OS ディスクの Azure VM を作る

# 環境変数をセットします
region=japaneast
prefix=mnredv

# リソースグループを作成します
az group create \
  --name ${prefix}-rg \
  --location $region

# VNET を作成します
az network vnet create \
  --resource-group ${prefix}-rg \
  --name ${prefix}-vnet \
  --address-prefixes 10.1.0.0/16 \
  --subnet-name default-subnet \
  --subnet-prefix 10.1.0.0/24

# NSG を作成します
az network nsg create \
  --resource-group ${prefix}-rg \
  --name ${prefix}-nsg

# NSG に自身のパブリック IP から SSH 接続出来るようにします
az network nsg rule create \
    --resource-group ${prefix}-rg \
    --name Allow-SSH \
    --nsg-name ${prefix}-nsg \
    --priority 100 \
    --source-address-prefixes $(curl -s inet-ip.info) \
    --destination-port-ranges 22 \
    --access Allow \
    --protocol Tcp

# NSG をサブネットに登録します
az network vnet subnet update \
  --resource-group ${prefix}-rg \
  --vnet-name ${prefix}-vnet \
  --name default-subnet \
  --network-security-group ${prefix}-nsg

# VM のパスワードを生成します
vmpass=$(openssl rand -base64 8)
echo $vmpass

# VM を作成します
# --ephemeral-os-disk を true
# --os-disk-caching を ReadOnly
az vm create \
  --resource-group ${prefix}-rg \
  --name ${prefix}-vm \
  --ephemeral-os-disk true \
  --os-disk-caching ReadOnly \
  --image CentOS \
  --size Standard_F2s_v2 \
  --admin-username azureuser \
  --admin-password $vmpass \
  --vnet-name ${prefix}-vnet \
  --subnet default-subnet \
  --nsg "" \
  --storage-sku Standard_LRS

# VM に SSH 接続します
ssh azureuser@$(az vm show \
  --resource-group ${prefix}-rg \
  --name ${prefix}-vm \
  --show-detail \
  --query publicIps \
  --output tsv)

# パーティションとサイズを確認します
$ df -h
ファイルシス   サイズ  使用  残り 使用% マウント位置
/dev/sda2         30G  1.2G   29G    5% /
devtmpfs         2.0G     0  2.0G    0% /dev
tmpfs            2.0G     0  2.0G    0% /dev/shm
tmpfs            2.0G  9.0M  2.0G    1% /run
tmpfs            2.0G     0  2.0G    0% /sys/fs/cgroup
/dev/sda1        497M   81M  417M   17% /boot
/dev/sdb1         16G   45M   15G    1% /mnt/resource
tmpfs            396M     0  396M    0% /run/user/1000

# sda2 と sdb1 のパーティションに書き込んでみる
$ date > date.txt
$ cat date.txt 
2021年 10月 31日 日曜日 01:38:50 UTC
$ date | sudo tee /mnt/resource/date.txt
$ cat /mnt/resource/date.txt 
2021年 10月 31日 日曜日 01:39:53 UTC

# VM から抜けます
$ exit

VM の割り当て解除をやってみる

エフェメラル OS ディスクの VM は割り当て解除出来ない、とエラーメッセージが表示されました。

# VM を割り当て解除します
az vm deallocate \
  --resource-group ${prefix}-rg \
  --name ${prefix}-vm

(OperationNotAllowed) Operation 'deallocate' is not supported for VMs or VM Scale Set instances using an ephemeral OS disk.

VM の停止と起動をやってみる

停止してもローカル物理マシン上のディスク領域は確保されていました。

# VM を停止します
az vm stop \
  --resource-group ${prefix}-rg \
  --name ${prefix}-vm

# VM を起動します
az vm start \
  --resource-group ${prefix}-rg \
  --name ${prefix}-vm

# VM に SSH 接続します
ssh azureuser@$(az vm show \
  --resource-group ${prefix}-rg \
  --name ${prefix}-vm \
  --show-detail \
  --query publicIps \
  --output tsv)

# 停止前に作成したファイルを確認します
$ cat date.txt 
2021年 10月 31日 日曜日 01:38:50 UTC
$ cat /mnt/resource/date.txt 
2021年 10月 31日 日曜日 01:39:53 UTC

# VM から抜けます
$ exit

# VM を削除します
az vm delete \
  --resource-group ${prefix}-rg \
  --name ${prefix}-vm

参考

作成したリソースを削除します。

bash
# リソースグループを削除します
az group delete \
  --name ${prefix}-rg

下記は、参考サイトです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?