Skip to content

Commit bd214dd

Browse files
committed
Add instructions to create a database virtual
machine and access it using Azure Data Studio
1 parent c9def73 commit bd214dd

4 files changed

Lines changed: 218 additions & 3 deletions

File tree

04-cloud/azure/iaas/01-db-vm/README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
# Crear máquina virtual para la base de datos
22

3-
Ahora vamos a crear la máquina virtual para la base de datos. Para ello, vamos a crear una variable con el nombre de la máquina virtual:
3+
Ahora vamos a crear la máquina virtual para la base de datos. Para ello, vamos a necesitar las siguientes variables de entorno:
4+
5+
```bash
6+
# SQL Server VM on Azure
7+
DB_VM_NAME="db-vm"
8+
DB_VM_IMAGE="MicrosoftSQLServer:sql2022-ws2022:sqldev-gen2:16.0.230613"
9+
DB_VM_ADMIN_USERNAME="dbadmin"
10+
DB_VM_ADMIN_PASSWORD="Db@dmin123!$"
11+
DB_VM_NSG_NAME="db-vm-nsg"
12+
```
13+
14+
o si estás en Windows:
15+
16+
```pwsh
17+
# SQL Server VM on Azure
18+
$DB_VM_NAME="db-vm"
19+
$DB_VM_IMAGE="MicrosoftSQLServer:sql2022-ws2022:sqldev-gen2:16.0.230613"
20+
$DB_VM_ADMIN_USERNAME="dbadmin"
21+
$DB_VM_ADMIN_PASSWORD="Db@dmin123!$"
22+
$DB_VM_NSG_NAME="db-vm-nsg"
23+
```
424

525
```bash
626
echo -e "Create a database vm named $DB_VM_NAME with image $DB_VM_IMAGE"
@@ -20,7 +40,7 @@ az vm create \
2040

2141
o si estás en Windows:
2242

23-
```bash
43+
```pwsh
2444
echo -e "Create a database vm named $DB_VM_NAME with image $DB_VM_IMAGE"
2545
2646
az vm create `
@@ -209,6 +229,8 @@ az network nsg rule create `
209229
--direction Inbound
210230
```
211231

232+
Para probar el acceso te recomiendo usar [Azure Data Studio](https://docs.microsoft.com/en-us/sql/azure-data-studio/download-azure-data-studio?view=sql-server-ver15), el cual es multiplataforma. Para ello, descárgalo e instálalo en tu máquina local. Una vez instalado, ejecútalo y en la pantalla de bienvenida, haz clic en **New Connection** y añade la IP pública de la máquina virtual de la base de datos, el usuario y la contraseña. Si no sabes la IP pública de la máquina virtual de la base de datos.
233+
212234
Si por otro lado lo que quieres es acceder a la máquina virtual tendrías que crear una regla de seguridad de red para el puerto 3389. Para ello, ejecuta el siguiente comando:
213235

214236
```bash
@@ -239,4 +261,6 @@ az network nsg rule create `
239261
--protocol Tcp `
240262
--source-address-prefixes $MY_IP_ADDRESS `
241263
--direction Inbound
242-
```
264+
```
265+
266+
Ahora que ya tenemos la base de datos creada, necesitamos una API que interactúe con ella. Puedes continuar en el siguiente [paso](../02-api-vm/README.md).
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Crear la máquina virtual para la API en .NET
2+
3+
Para esta pieza de la arquitectura de Tour of Heroes vamos a usar una máquina virtual que utilice como sistema operativo Ubuntu. Para este componente vamos a necesitas las siguientes variables:
4+
5+
```bash
6+
# API VM on Azure
7+
API_VM_NAME="api-vm"
8+
API_VM_IMAGE="Ubuntu2204"
9+
API_VM_ADMIN_USERNAME="apiadmin"
10+
API_VM_ADMIN_PASSWORD="Api@dmin1232!"
11+
API_VM_NSG_NAME="api-vm-nsg"
12+
```
13+
14+
o si estás en Windows:
15+
16+
```pwsh
17+
# API VM on Azure
18+
$API_VM_NAME="api-vm"
19+
$API_VM_IMAGE="Ubuntu2204"
20+
$API_VM_ADMIN_USERNAME="apiadmin"
21+
$API_VM_ADMIN_PASSWORD="Api@dmin1232!"
22+
$API_VM_NSG_NAME="api-vm-nsg"
23+
```
24+
25+
```bash
26+
27+
Ahora con estas vamos a crear la máquina virtual de la misma forma que lo hicimos con la base de datos:
28+
29+
```bash
30+
echo -e "Create an api vm named $API_VM_NAME with image $API_VM_IMAGE"
31+
32+
FQDN_API_VM=$(az vm create \
33+
--resource-group $RESOURCE_GROUP \
34+
--name $API_VM_NAME \
35+
--image $API_VM_IMAGE \
36+
--admin-username $API_VM_ADMIN_USERNAME \
37+
--admin-password $API_VM_ADMIN_PASSWORD \
38+
--vnet-name $VNET_NAME \
39+
--subnet $API_SUBNET_NAME \
40+
--public-ip-address-dns-name tour-of-heroes-api-vm \
41+
--nsg $API_VM_NSG_NAME \
42+
--size $VM_SIZE --query "fqdns" -o tsv)
43+
44+
echo -e "Api VM created"
45+
```
46+
47+
o si estás en Windows:
48+
49+
```pwsh
50+
echo -e "Create an api vm named $API_VM_NAME with image $API_VM_IMAGE"
51+
52+
$FQDN_API_VM=az vm create `
53+
--resource-group $RESOURCE_GROUP `
54+
--name $API_VM_NAME `
55+
--image $API_VM_IMAGE `
56+
--admin-username $API_VM_ADMIN_USERNAME `
57+
--admin-password $API_VM_ADMIN_PASSWORD `
58+
--vnet-name $VNET_NAME `
59+
--subnet $API_SUBNET_NAME `
60+
--public-ip-address-dns-name tour-of-heroes-api-vm `
61+
--nsg $API_VM_NSG_NAME `
62+
--size $VM_SIZE --query "fqdns" -o tsv
63+
64+
echo -e "Api VM created"
65+
```
66+
67+
Sin embargo, con esto solo no basta ya que por ahora sólo tenemos la máquina virtual pero no está ni configurada para poder hospedar mi API en .NET ni configurado ningún servidor web que la sirva. Para ello vamos a hacer uso del subcomando **run-command** de la CLI de Azure. Este comando nos permite ejecutar comandos en la máquina virtual de forma remota:
68+
69+
```bash
70+
# https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-7.0&tabs=linux-ubuntu
71+
echo -e "Execute script to install nginx, .NET Core, deploy the app and create the service"
72+
az vm run-command invoke \
73+
--resource-group $RESOURCE_GROUP \
74+
--name $API_VM_NAME \
75+
--command-id RunShellScript \
76+
--scripts @04-cloud/azure/iaas/scripts/install-tour-of-heroes-api.sh \
77+
--parameters https://github.com/0GiS0/tour-of-heroes-dotnet-api/releases/download/1.0.5/drop.zip $FQDN_API_VM $DB_VM_ADMIN_USERNAME $DB_VM_ADMIN_PASSWORD
78+
````
79+
80+
o si estás en Windows:
81+
82+
```pwsh
83+
# https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-7.0&tabs=linux-ubuntu
84+
echo -e "Execute script to install nginx, .NET Core, deploy the app and create the service"
85+
az vm run-command invoke `
86+
--resource-group $RESOURCE_GROUP `
87+
--name $API_VM_NAME `
88+
--command-id RunShellScript `
89+
--scripts @04-cloud/azure/iaas/scripts/install-tour-of-heroes-api.sh `
90+
--parameters https://github.com/0GiS0/tour-of-heroes-dotnet-api/releases/download/1.0.5/drop.zip $FQDN_API_VM $DB_VM_ADMIN_USERNAME $DB_VM_ADMIN_PASSWORD
91+
```
92+
93+
Con este comando estamos ejecutando un script que se encuentra en la carpeta **scripts** de este repositorio. Este script se encarga de instalar nginx, .NET Core, desplegar la API y crear un servicio que la mantenga en ejecución. Si quieres ver el contenido del script puedes hacerlo [aquí](04-cloud/azure/iaas/scripts/install-tour-of-heroes-api.sh).
94+
95+
Por último necesitamos crear una *network security rule* para permitir el acceso a través del puerto 80 a la API:
96+
97+
```bash
98+
echo -e "Create a network security group rule for port 80"
99+
az network nsg rule create \
100+
--resource-group $RESOURCE_GROUP \
101+
--nsg-name $API_VM_NSG_NAME \
102+
--name AllowHttp \
103+
--priority 1002 \
104+
--destination-port-ranges 80 \
105+
--direction Inbound
106+
```
107+
108+
o si estás en Windows:
109+
110+
```pwsh
111+
echo -e "Create a network security group rule for port 80"
112+
113+
az network nsg rule create `
114+
--resource-group $RESOURCE_GROUP `
115+
--nsg-name $API_VM_NSG_NAME `
116+
--name AllowHttp `
117+
--priority 1002 `
118+
--destination-port-ranges 80 `
119+
--direction Inbound
120+
```
121+
122+
Para comprobar que la API funciona correctamente puedes acceder a la URL http://tour-of-heroes-api-vm.uksouth.cloudapp.azure.com/api/hero y deberías ver un listado de héroes en formato JSON.
123+
124+
Por otro lado, si instalas la extensión REST Client en tu Visual Studio Code, puedes ejecutar la petición **GET hero** que se encuentra en el fichero [api.http](04-cloud/azure/iaas/02-api-vm/api.http) y verás el listado de héroes en formato JSON.

04-cloud/azure/iaas/02-api-vm/api.http

Whitespace-only changes.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
echo -e "Install nginx server"
2+
sudo apt update && sudo apt install -y nginx unzip
3+
4+
echo -e "Install .NET Core"
5+
wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && sudo dpkg -i packages-microsoft-prod.deb && rm packages-microsoft-prod.deb && sudo apt-get update && sudo apt-get install -y aspnetcore-runtime-7.0
6+
7+
systemctl status nginx
8+
9+
sudo mkdir -p /var/www/tour-of-heroes-api
10+
sudo chown -R $USER:$USER /var/www/tour-of-heroes-api
11+
sudo chmod -R 755 /var/www/tour-of-heroes-api
12+
13+
echo -e "Download the last release of the api app from github"
14+
wget $1 -O drop.zip
15+
16+
echo -e "Unzip the api app"
17+
unzip drop.zip -d /var/www/tour-of-heroes-api
18+
19+
sudo sed -i 's/# server_names_hash_bucket_size 64;/server_names_hash_bucket_size 128;/g' /etc/nginx/nginx.conf
20+
21+
sudo SERVER_NAME=$2 bash -c 'cat > /etc/nginx/sites-available/tour-of-heroes-api.conf <<EOF
22+
server {
23+
listen 80;
24+
server_name $SERVER_NAME;
25+
location / {
26+
proxy_pass http://localhost:5000;
27+
proxy_http_version 1.1;
28+
proxy_set_header Upgrade \$http_upgrade;
29+
proxy_set_header Connection keep-alive;
30+
proxy_set_header Host \$host;
31+
proxy_cache_bypass \$http_upgrade;
32+
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
33+
proxy_set_header X-Forwarded-Proto \$scheme;
34+
}
35+
}
36+
EOF'
37+
38+
sudo ln -s /etc/nginx/sites-available/tour-of-heroes-api.conf /etc/nginx/sites-enabled/
39+
sudo nginx -t
40+
sudo systemctl restart nginx
41+
42+
sudo bash -c "cat <<EOF > /etc/systemd/system/tour-of-heroes-api.service
43+
[Unit]
44+
Description=Tour of heroes .NET Web API App running on Ubuntu
45+
46+
[Service]
47+
WorkingDirectory=/var/www/tour-of-heroes-api
48+
ExecStart=/usr/bin/dotnet /var/www/tour-of-heroes-api/tour-of-heroes-api.dll
49+
50+
RestartSec=10
51+
KillSignal=SIGINT
52+
SyslogIdentifier=dotnet-tour-of-heroes-api
53+
User=www-data
54+
Environment=ASPNETCORE_ENVIRONMENT=Development
55+
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
56+
Environment=ConnectionStrings__DefaultConnection='Server=192.168.1.4,1433;Initial Catalog=heroes;Persist Security Info=False;User ID=$3;Password=$4;TrustServerCertificate=True'
57+
58+
[Install]
59+
WantedBy=multi-user.target
60+
EOF"
61+
62+
sudo systemctl enable tour-of-heroes-api.service
63+
sudo systemctl start tour-of-heroes-api.service
64+
# sudo systemctl disable tour-of-heroes-api.service
65+
sudo systemctl status tour-of-heroes-api.service
66+
67+
# journalctl -u tour-of-heroes-api.service

0 commit comments

Comments
 (0)