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 1 year has passed since last update.

1 つの VNET に複数の CIDR アドレス空間を登録して相互に通信できるか試してみた

Posted at

背景と目的

既存の VNET がある前提で、既存のアドレス空間に隣接する CIDR が使えるとしたら、アドレス空間を拡張してサブネットを追加すると思います。では隣接しない CIDR なら使える場合は、別の VNET を作成して VNET ピアリングでしょうか?別リージョンに VNET を作成してピアリングしたい場合は良いのですが、同じリージョン内の VNET ピアリングでも送信と受信のデータ転送料金が発生します。そんな勿体無いことをせずとも、1 つの VNET に複数の CIDR アドレス空間を登録して相互に通信できるはずなので、実際に試してみました。

検証用 VNET を作成

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

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

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

# アドレス空間に 172.16.0.0/24 を追加します
az network vnet update \
  --resource-group ${prefix}-rg \
  --name ${prefix}-vnet \
  --add addressSpace.addressPrefixes 172.16.0.0/24

# アドレス空間に 192.168.0.0/24 を追加します
az network vnet update \
  --resource-group ${prefix}-rg \
  --name ${prefix}-vnet \
  --add addressSpace.addressPrefixes 192.168.0.0/24

# アドレス空間を確認します
az network vnet show \
  --resource-group ${prefix}-rg \
  --name ${prefix}-vnet \
  --query addressSpace

{
  "addressPrefixes": [
    "10.0.0.0/24",
    "172.16.0.0/24",
    "192.168.0.0/24"
  ]
}

各サブネットに共通の NSG を作成し 2 つのサブネットを追加

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

# NSG ルールを作成します
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 subnet1 \
  --network-security-group ${prefix}-nsg

# 2 つ目サブネットを作成します
az network vnet subnet create \
  --vnet-name ${prefix}-vnet \
  --resource-group ${prefix}-rg \
  --name subnet2 \
  --address-prefix 172.16.0.0/24 \
  --network-security-group ${prefix}-nsg

# 3 つ目サブネットを作成します
az network vnet subnet create \
  --vnet-name ${prefix}-vnet \
  --resource-group ${prefix}-rg \
  --name subnet3 \
  --address-prefix 192.168.0.0/24 \
  --network-security-group ${prefix}-nsg

検証用の仮想マシンをそれぞれのサブネットに作成

bash
# 仮想マシン作成します
for i in {1..3}; do
  az vm create \
    --resource-group ${prefix}-rg \
    --name ${prefix}-vm${i} \
    --os-disk-name ${prefix}-vm${i}OSDisk \
    --image UbuntuLTS \
    --size Standard_B1ls \
    --admin-username azureuser \
    --generate-ssh-keys \
    --vnet-name ${prefix}-vnet \
    --subnet subnet${i} \
    --nsg "" \
    --public-ip-address-dns-name ${prefix}${i} \
    --storage-sku Standard_LRS
done

それぞれの仮想マシンに SSH 接続して別の VM と通信できるか試す

bash
$ ssh azureuser@${prefix}1.$region.cloudapp.azure.com "ping -c 4 172.16.0.4; ping -c 4 192.168.0.4"

PING 172.16.0.4 (172.16.0.4) 56(84) bytes of data.
64 bytes from 172.16.0.4: icmp_seq=1 ttl=64 time=1.57 ms
64 bytes from 172.16.0.4: icmp_seq=2 ttl=64 time=1.31 ms
64 bytes from 172.16.0.4: icmp_seq=3 ttl=64 time=0.974 ms
64 bytes from 172.16.0.4: icmp_seq=4 ttl=64 time=1.29 ms

--- 172.16.0.4 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 0.974/1.291/1.577/0.213 ms

PING 192.168.0.4 (192.168.0.4) 56(84) bytes of data.
64 bytes from 192.168.0.4: icmp_seq=1 ttl=64 time=2.16 ms
64 bytes from 192.168.0.4: icmp_seq=2 ttl=64 time=0.869 ms
64 bytes from 192.168.0.4: icmp_seq=3 ttl=64 time=1.96 ms
64 bytes from 192.168.0.4: icmp_seq=4 ttl=64 time=1.70 ms

--- 192.168.0.4 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3014ms
rtt min/avg/max/mdev = 0.869/1.676/2.167/0.494 ms

$ ssh azureuser@${prefix}2.$region.cloudapp.azure.com "ping -c 4 10.0.0.4; ping -c 4 192.168.0.4"

PING 10.0.0.4 (10.0.0.4) 56(84) bytes of data.
64 bytes from 10.0.0.4: icmp_seq=1 ttl=64 time=1.27 ms
64 bytes from 10.0.0.4: icmp_seq=2 ttl=64 time=1.07 ms
64 bytes from 10.0.0.4: icmp_seq=3 ttl=64 time=1.76 ms
64 bytes from 10.0.0.4: icmp_seq=4 ttl=64 time=1.61 ms

--- 10.0.0.4 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 1.074/1.434/1.768/0.274 ms

PING 192.168.0.4 (192.168.0.4) 56(84) bytes of data.
64 bytes from 192.168.0.4: icmp_seq=1 ttl=64 time=0.995 ms
64 bytes from 192.168.0.4: icmp_seq=2 ttl=64 time=1.40 ms
64 bytes from 192.168.0.4: icmp_seq=3 ttl=64 time=2.16 ms
64 bytes from 192.168.0.4: icmp_seq=4 ttl=64 time=1.63 ms

--- 192.168.0.4 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 0.995/1.550/2.163/0.421 ms

$ ssh azureuser@${prefix}3.$region.cloudapp.azure.com "ping -c 4 10.0.0.4; ping -c 4 172.16.0.4"

PING 10.0.0.4 (10.0.0.4) 56(84) bytes of data.
64 bytes from 10.0.0.4: icmp_seq=1 ttl=64 time=1.73 ms
64 bytes from 10.0.0.4: icmp_seq=2 ttl=64 time=1.20 ms
64 bytes from 10.0.0.4: icmp_seq=3 ttl=64 time=1.04 ms
64 bytes from 10.0.0.4: icmp_seq=4 ttl=64 time=1.22 ms

--- 10.0.0.4 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 1.043/1.302/1.736/0.261 ms

PING 172.16.0.4 (172.16.0.4) 56(84) bytes of data.
64 bytes from 172.16.0.4: icmp_seq=1 ttl=64 time=2.42 ms
64 bytes from 172.16.0.4: icmp_seq=2 ttl=64 time=2.04 ms
64 bytes from 172.16.0.4: icmp_seq=3 ttl=64 time=1.89 ms
64 bytes from 172.16.0.4: icmp_seq=4 ttl=64 time=2.17 ms

--- 172.16.0.4 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 1.896/2.133/2.427/0.203 ms

検証環境を削除

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

参考

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?