-
Notifications
You must be signed in to change notification settings - Fork 33
/
vmss-iperf.azcli
154 lines (133 loc) · 6.25 KB
/
vmss-iperf.azcli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
############################################################################
# Created by Jose Moreno
# July 2024
#
# The script creates two VMSS of Ubuntu VMs running iperf. One will act as
# client and the other one as server.
############################################################################
# Variables
rg=vmss
location=eastus2
vnet1_name=vnet1
vnet1_prefix=10.13.76.0/24
subnet1_name=vm
subnet1_prefix=10.13.76.0/24
vnet2_name=vnet2
vnet2_prefix=10.13.77.0/24
subnet2_name=vm
subnet2_prefix=10.13.77.0/24
vmss1_name=client
vmss1_cloudinit_file=/tmp/client.txt
vmss2_name=server
vmss2_cloudinit_file=/tmp/server.txt
vm_size=Standard_B1s
vmss_image=Ubuntu2204
vmss_instance_count=3
# Create cloudinit files
cat <<'EOF' > $vmss1_cloudinit_file
#cloud-config
packages:
- iperf3
write_files:
- content: |
#!/bin/bash
# Controls whether printing debugging info
debug=no
dev_name=eth0
remote_cidr=__subnet2_prefix__
# Get local IP address and split it in ip and mask
local_cidr=$(ip address show dev $dev_name | grep inet | grep $dev_name | awk '{print $2}')
if [[ "$debug" == "yes" ]]; then echo "Local CIDR: $local_cidr"; fi
local_ip=$(echo $local_cidr | cut -d/ -f 1)
local_mask_int=$(echo $local_cidr | cut -d/ -f 2)
if [[ "$debug" == "yes" ]]; then echo "Local IP address: $local_ip, mask: $local_mask_int"; fi
# Convert the /24 mask to 255.255.255.0 format
cidr_to_netmask() {
local cidr=$1
local mask=""
local full_octets=$(( cidr / 8 ))
local partial_octet=$(( cidr % 8 ))
for ((i=0; i<4; i++)); do
if [ $i -lt $full_octets ]; then
mask+="255"
elif [ $i -eq $full_octets ]; then
mask+=$(( 256 - (1 << (8 - partial_octet)) ))
else
mask+="0"
fi
[ $i -lt 3 ] && mask+="."
done
echo $mask
}
local_mask=$(cidr_to_netmask $local_mask_int)
if [[ "$debug" == "yes" ]]; then echo "Local mask: $local_mask"; fi
# Convert to hex, and then to dec
local_ip_hex=$(printf '%.2X%.2X%.2X%.2X\n' `echo $local_ip | sed -e 's/\./ /g'`)
local_mask_hex=$(printf '%.2X%.2X%.2X%.2X\n' `echo $local_mask | sed -e 's/\./ /g'`)
if [[ "$debug" == "yes" ]]; then echo "Local IP address in hex: $local_ip_hex, mask: $local_mask_hex"; fi
# Now we can get the subnet with a binary AND between the IP and the mask
local_subnet_hex=$(printf %.8X `echo $(( 0x$local_ip_hex & 0x$local_mask_hex ))`)
if [[ "$debug" == "yes" ]]; then echo "Local subnet in hex: $local_subnet_hex"; fi
# Now we have the offset
local_offset=$(printf %.8X `echo $(( 0x$local_ip_hex - 0x$local_subnet_hex ))`)
if [[ "$debug" == "yes" ]]; then echo "Local offset: $local_offset"; fi
# Before adding it to the remote subnet, we need to convert it to hex too
remote_subnet=$(echo $remote_cidr | cut -d/ -f 1)
if [[ "$debug" == "yes" ]]; then echo "Remote subnet address: $remote_subnet"; fi
remote_subnet_hex=$(printf '%.2X%.2X%.2X%.2X\n' `echo $remote_subnet | sed -e 's/\./ /g'`)
if [[ "$debug" == "yes" ]]; then echo "Remote subnet address in hex: $remote_subnet_hex"; fi
# Now we can have the remote IP in hex adding the offset to the remote subnet
remote_ip_hex=$(printf %.8X `echo $(( 0x$remote_subnet_hex + 0x$local_offset ))`)
if [[ "$debug" == "yes" ]]; then echo "Remote IP address in hex: $remote_ip_hex"; fi
# Finally we convert the remote IP in hex to an IPv4 format
remote_ip=$(printf '%d.%d.%d.%d\n' `echo $remote_ip_hex | sed -r 's/(..)/0x\1 /g'`)
if [[ "$debug" == "yes" ]]; then echo "Remote IP address: $remote_ip"; fi
# Output
echo $remote_ip
path: /root/get_remote_ip.sh
runcmd:
- iperf3 -c $(/usr/bin/bash /root/get_remote_ip.sh) -tinf | tee /root/iperf.log
EOF
# Replace token "__remote_subnet_prefix__" with the right value
sed -i "s|__subnet2_prefix__|${subnet2_prefix}|g" $vmss1_cloudinit_file
cat <<EOF > $vmss2_cloudinit_file
#cloud-config
packages:
- iperf3
runcmd:
- iperf3 -s | tee /root/iperf.log
EOF
# Create RG, VNets and subnets
az group create -n $rg -o none
az network vnet create -g $rg -n $vnet1_name --address-prefixes $vnet1_prefix --subnet-name $subnet1_name --subnet-prefixes $subnet1_prefix -o none
az network vnet create -g $rg -n $vnet2_name --address-prefixes $vnet2_prefix --subnet-name $subnet2_name --subnet-prefixes $subnet2_prefix -o none
# Create server VMSS first
az vmss create -n $vmss2_name -g $rg -l $location --image $vmss_image --generate-ssh-keys \
--vnet-name $vnet2_name --subnet $subnet2_name --public-ip-per-vm --orchestration-mode Uniform \
--vm-sku ${vm_size} --custom-data "$vmss2_cloudinit_file" --instance-count $vmss_instance_count -o none
# Create client VMSS
az vmss create -n $vmss1_name -g $rg -l $location --image $vmss_image --generate-ssh-keys \
--vnet-name $vnet1_name --subnet $subnet1_name --public-ip-per-vm --orchestration-mode Uniform \
--vm-sku ${vm_size} --custom-data "$vmss1_cloudinit_file" --instance-count $vmss_instance_count -o none
###############
# Scale out #
###############
# Scale out
vmss_instance_count=10
az vmss scale -n $vmss1_name -g $rg --new-capacity $vmss_instance_count --no-wait
az vmss scale -n $vmss2_name -g $rg --new-capacity $vmss_instance_count --no-wait
# Scale up
vm_size=Standard_F2s_v2
az vmss update -n $vmss1_name -g $rg --vm-sku $vm_size -o none
az vmss update-instances -n $vmss1_name -g $rg --instance-ids '*' -o none
az vmss update -n $vmss2_name -g $rg --vm-sku $vm_size -o none
az vmss update-instances -n $vmss2_name -g $rg --instance-ids '*' -o none
###############
# Diagnostics #
###############
az vmss list-instance-public-ips -n $vmss1_name -g $rg -o table
az vmss list-instance-public-ips -n $vmss2_name -g $rg -o table
az vmss list-instance-public-ips -n $vmss1_name -g $rg --query '[].{IP:ipAddress, IPConfig:ipConfiguration.id}' -o table
az vmss list-instance-public-ips -n $vmss2_name -g $rg --query '[].{IP:ipAddress, IPConfig:ipConfiguration.id}' -o table
az vmss nic list -g $rg --vmss-name $vmss1_name --query '[].{IP:ipConfigurations[0].privateIPAddress, instanceId:virtualMachine.id}' -o table
az vmss nic list -g $rg --vmss-name $vmss2_name --query '[].{IP:ipConfigurations[0].privateIPAddress, instanceId:virtualMachine.id}' -o table