-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreating a Virtual Machine
More file actions
66 lines (60 loc) · 4.18 KB
/
Creating a Virtual Machine
File metadata and controls
66 lines (60 loc) · 4.18 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
First, we'll login using az login
Next, we'll create our VM using az vm create. If we don't pass a location, it defaults to the location of the resource group. This would be fine in most cases, but it's worth noting that sometimes a VM size might not be available in the same region as your resource group, so you'd have to pass in a location for a different region.
az vm create \
--resource-group "resource-group-west" \
--name "linux-vm-west" \
--location "westus2" \
--image "UbuntuLTS" \
--size "Standard_B1ls" \
--admin-username "udacityadmin" \
--generate-ssh-keys \
--verbose
Upon success, you will have a JSON response.
Next we will open port 80 to allow outside traffic to our VM
az vm open-port \
--port "80" \
--resource-group "resource-group-west" \
--name "linux-vm-west"
Upon success, you will receive a JSON response.
Alternatively, you can check the screenshot below for the same approach through the portal.
An example of creating a Linux VM in the Azure Portal. A subscription is selected, with the same resource group as earlier. A VM name is added as `linux-vm-west` (or whatever you would like), with West US region. No infrastructure redundancy is required. Ubuntu Server 18.04 is selected, with "No" on Spot instance, and size of B1ls. A SSH public key is used, with `azureuser` as username and `ssh-rsa` for public key. Allow selected ports with HTTP and SSH selected.
An example of creating a Linux VM in the Azure Portal
Connecting to the VM
We'll need the admin username we set when creating the VM
We'll need the public IP address of our VM. You can use the following command to grab the IP address for a particular VM from the CLI. az vm list-ip-addresses -g resource-group-west -n linux-vm-west
Next we're going to copy a basic Flask app from my local machine to the VM. We'll be using the secure copy utility. scp -r ./web udacityadmin@IPADDRESS:/home/udacityadmin
Now we can connect to the VM with “ssh [username]@[IP Address]” Note: Since we generated SSH keys, you won't be prompted for a password.
Run ls to see the web directory we just uploaded
Python 3 is already installed on the VM. We'll install Python Virtual Environment and NGNIX to use as a reverse proxy sudo apt-get -y update && sudo apt-get -y install nginx python3-venv
Before we run the app, we have to configure Nginx to redirect all incoming connections on port 80 to our app that is running on localhost port 3000
By default, Nginx has a default page that is displayed. If you visit the public IP address in your browser, you should see this page rendered.
We'll navigate to the /etc/nginx/sites-available directory—cd /etc/nginx/sites-available
We’ll first unlink the default site using sudo unlink /etc/nginx/sites-enabled/default
Then we’ll create a new file reverse-proxy.conf in /etc/nginx/sites-available—sudo vim reverse-proxy.conf
We're going to add the following code to this file:
server {
listen 80;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Now we’ll activate the directories by creating a sym link to the /sites-enabled directory sudo ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf
We have to restart nginx so the changes take effect. sudo service nginx restart
Deploying the App to the VM
This section is the same as the earlier walkthrough, so we'll skip a solution video for it.
To deploy the app on the virtual machine, I did the following:
CD to web
Create venv python3 -m venv venv
Activate the env source venv/bin/activate
Upgrade pip in our virtual environment and then Install dependencies—pip install --upgrade pip pip install -r requirements.txt
We'll run our app python application.py
In a web browser, we can visit the publicIpaddress
Type "exit" to disconnect from the VM
Cleanup
If we no longer need a resource, we can delete them through the portal. The quickest way to do this from the CLI is to delete the resource group. This will delete all resources in that group
az group delete -n resource-group-west