-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscript-infra.sh
More file actions
272 lines (212 loc) · 7.57 KB
/
script-infra.sh
File metadata and controls
272 lines (212 loc) · 7.57 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#! /bin/bash
# Load balance VMs across availability zones
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location=francecentral
vNet="msdocs-vnet-lb-myvnet$randomIdentifier"
subnet="msdocs-subnet-lb-subnet$randomIdentifier"
loadBalancerPublicIp="msdocs-public-ip-lb-$randomIdentifier"
ipSku=Standard
zone="1 2 3"
loadBalancer="msdocs-load-balancer-loadbalancer$randomIdentifier"
frontEndIp="msdocs-front-end-ip-lb-$randomIdentifier"
backEndPool="msdocs-back-end-pool-lb-$randomIdentifier"
probe80="msdocs-port80-health-probe-lb-$randomIdentifier"
loadBalancerRuleWeb="msdocs-load-balancer-rule-port80-$randomIdentifier"
loadBalancerRuleSSH="msdocs-load-balancer-rule-port22-$randomIdentifier"
networkSecurityGroup="msdocs-network-security-group-lb-networksecuritygroup"
networkSecurityGroupRuleSSH="msdocs-network-security-rule-port22-lb-$randomIdentifier"
networkSecurityGroupRuleWeb="msdocs-network-security-rule-port80-lb-$randomIdentifier"
nic="msdocs-nic-lb-$randomIdentifier"
image=UbuntuLTS
mdbserv=mdbserv$randomIdentifier
myNATgateway=msdocs-NAT-gateway$randomIdentifier
myNATgatewayIP=msdocs-NAT-gateway-IP$randomIdentifier
read -p "Entrez le nom de votre Groupe de Ressource: " GroupeDeRessource
read -p "Entrez le nombre de VM souhaité: " NumberVM
read -p "Entrez le nom de votre VM {s'il y en a plusieurs, seul le numéro changera}: " vm
read -p "Entrez votre nom d'Utilisateur: " azureuser
read -p "Entrez votre nom d'utilsateur pour MariaDB: " UserMDB
# read -p "Entrez votre mot de passe pour MariaDB: "
# Create a resource group
echo "***************Creating $GroupeDeRessource in France Central***************"
az group create \
--name $GroupeDeRessource \
--location $location
# Create a virtual network and a subnet.
echo "***************Creating $vNet and $subnet***************"
az network vnet create \
--resource-group $GroupeDeRessource \
--name $vNet \
--location $location \
--address-prefixes 10.1.0.0/16 \
--subnet-name $subnet \
--subnet-prefixes 10.1.0.0/24
# Create a zonal Standard public IP address for load balancer.
echo "***************Creating $loadBalancerPublicIp***************"
az network public-ip create \
--resource-group $GroupeDeRessource \
--name $loadBalancerPublicIp \
--sku $ipSku \
--zone $zone
# Create an Azure Load Balancer.
echo "***************Creating $loadBalancer with $frontEndIP and $backEndPool***************"
az network lb create \
--resource-group $GroupeDeRessource \
--name $loadBalancer \
--public-ip-address $loadBalancerPublicIp \
--frontend-ip-name $frontEndIp \
--backend-pool-name $backEndPool \
--sku $ipSku
# Create an LB probe on port 80.
echo "***************Creating $probe80 in $loadBalancer***************"
az network lb probe create \
--resource-group $GroupeDeRessource \
--lb-name $loadBalancer \
--name $probe80 \
--protocol tcp \
--port 80
# Create an LB rule for port 80.
echo "***************Creating $loadBalancerRuleWeb for $loadBalancer***************"
az network lb rule create \
--resource-group $GroupeDeRessource \
--lb-name $loadBalancer \
--name $loadBalancerRuleWeb \
--protocol tcp \
--frontend-port 80 \
--backend-port 80 \
--frontend-ip-name $frontEndIp \
--backend-pool-name $backEndPool \
--probe-name $probe80
# --disable-outbound-snat true \
# --idle-timeout 15 \
# --enable-tcp-reset true
# Create $NumberVM NAT rules for port 22.
echo "***************Creating $NumberVM NAT rules named $loadBalancerRuleSSH***************"
for i in `seq 1 $NumberVM`
do
az network lb inbound-nat-rule create \
--resource-group $GroupeDeRessource \
--lb-name $loadBalancer \
--name $loadBalancerRuleSSH$i \
--protocol tcp \
--frontend-port 422$i \
--backend-port 22 \
--frontend-ip-name $frontEndIp
done
# Create a network security group
echo "***************Creating $networkSecurityGroup***************"
az network nsg create \
--resource-group $GroupeDeRessource \
--name $networkSecurityGroup
# Create a network security group rule for port 22.
echo "***************Creating $networkSecurityGroupRuleSSH in $networkSecurityGroup for port 22***************"
az network nsg rule create \
--resource-group $GroupeDeRessource \
--nsg-name $networkSecurityGroup \
--name $networkSecurityGroupRuleSSH \
--protocol tcp \
--direction inbound \
--source-address-prefix '*' \
--source-port-range '*' \
--destination-address-prefix '*' \
--destination-port-range 22 \
--access allow \
--priority 1000
# Create a network security group rule for port 80.
echo "***************Creating $networkSecurityGroupRuleWeb in $networkSecurityGroup for port 22***************"
az network nsg rule create \
--resource-group $GroupeDeRessource \
--nsg-name $networkSecurityGroup \
--name $networkSecurityGroupRuleWeb \
--protocol tcp \
--direction inbound \
--priority 1001 \
--source-address-prefix '*' \
--source-port-range '*' \
--destination-address-prefix '*' \
--destination-port-range 80 \
--access allow \
--priority 2000
# Create $NumberVM virtual network cards and associate with public IP address and NSG.
echo "***************Creating $NumberVM NICs named $nic for $vNet and $subnet***************"
for i in `seq 1 $NumberVM`
do
az network nic create \
--resource-group $GroupeDeRessource \
--name $nic$i \
--vnet-name $vNet \
--subnet $subnet \
--network-security-group $networkSecurityGroup \
--lb-name $loadBalancer \
--lb-address-pools $backEndPool \
--lb-inbound-nat-rules $loadBalancerRuleSSH$i
done
# Create $NumberVM virtual machines, this creates SSH keys if not present.
echo "***************Creating $NumberVM VMs named $vm with $nic using $image***************"
# for i in $(seq 1 $END)
for i in `seq 1 $NumberVM`
do
az vm create \
--resource-group $GroupeDeRessource \
--name $vm$i \
--zone $i \
--nics $nic$i \
--image $image \
--admin-username $azureuser \
--generate-ssh-keys \
--no-wait
done
for i in `seq 1 $NumberVM`
do
az vm open-port \
--port 80 \
--resource-group $GroupeDeRessource \
--name $vm$i
done
# List the virtual machines
az vm list \
--resource-group $GroupeDeRessource
# Test the load balancer
IpPublic=$(az network public-ip show \
--resource-group $GroupeDeRessource \
--name $loadBalancerPublicIp \
--query ipAddress \
--output tsv)
echo $IpPublic
# Create a NAT Gateway
az network public-ip create \
--resource-group $GroupeDeRessource \
--name $myNATgatewayIP \
--sku Standard \
--zone $zone
az network nat gateway create \
--resource-group $GroupeDeRessource \
--name $myNATgateway \
--public-ip-addresses $myNATgatewayIP \
--idle-timeout 10
az network vnet subnet update \
--resource-group $GroupeDeRessource \
--vnet-name $vNet \
--name $subnet \
--nat-gateway $myNATgateway
# Create a Maria DB server
az mariadb server create \
--resource-group $GroupeDeRessource \
--name $mdbserv \
--ssl-enforcement Disabled \
--location francecentral \
--admin-user $UserMDB \
--admin-password PIcciNO69200!MaRaTEa? \
--sku-name GP_Gen5_2 \
--version 10.2
az mariadb server firewall-rule create \
--resource-group $GroupeDeRessource \
--server $mdbserv \
--name AllowMyIP \
--start-ip-address $IpPublic \
--end-ip-address $IpPublic
az mariadb server show \
--resource-group $GroupeDeRessource \
--name $mdbserv
ssh -i .ssh/id_rsa $azureuser@$IpPublic -p 4221