Everything you need to know for deploying your first website on Azure
From VMs to Load balancers. A step by step guide for Azure deployment.
I hear and I forget. I see and I remember. I do and I understand
- Confucius
Introduction
Recently I stumbled up a new kind of learning platform known as Crio.do. Their concept is to provide students with a set of well structured tasks and let them figure out how to complete those tasks by self learning and exploring, thus imitating a work like environment. Further in this article , I will explain all you need to know for getting started with azure , which I learned from completing one of their micro-experiences.
Topics to be covered
- Getting a free Azure account
- Creating a Virtual Machine
- Installing a web server
- Assigning static Public IP
- Opening additional ports
- Assigning a domain
- Cloning a Virtual Machine
- Setting up a Load balancer
Getting a free Azure account
Either you want to try Azure for the first time or serious about using auzre for deploying your website, you should open up a free trial account which gives you 13300 INR (aprox. 130$) to spend so you can do all the exploring needed before buying a subscription.
Step 1
For a free account navigate to https://azure.microsoft.com/en-in/free/
Step 2
Click on the “Start Free” button and follow the sign up process.
Note : You need to have a credit card for signing up for a free trial
Step 3
After signing up it will redirect you to https://portal.azure.com which is your workspace.
The portal will look some what similarly to the below screen shot
Step 4
Click on the bell icon on the right side of the top navigation bar to check your balance. Now you are good to go.
Creating a resource group
As Azure explains it : Resource group is a container that holds related resources for an Azure solution. The resource group can include all the resources for the solution, or only those resources that you want to manage as a group. So basically it is a way to organize your VMs, storage disks, load balancers etc as logical groupings. You can skip this step but in the end you have to choose a resource group while creating VMs so why not now.
Steps to create a resource group
Step 1
Click on the icon denoting Resource group on your azure portal home page or search for resource group in the search bar and click on the first result.
This will lead you to a page like below
Step 2
Click the add button on the top left corner below the title Resource Group. It will open up the page below.
Step 3
Give a name to your resource group and choose your region. Then click on Review + create.
Step 4
After creating Review + create , it will validate your inputs and then lead you to a page like below
Step 5
You can review your inputs one last time. If everything seems correct , click on create. This will create a resource group. If you navigate back to resource group page , you will find your resource group listed there.
Note : It may take while. The creation progress will be updated in your notifications which you can access by clicking on the bell icon on the top right corner of your navigation bar.
Creating a Virtual Machine
A virtual machine is a computer file, typically called an image, which behaves like an actual computer. In a simple words it is a computer inside a computer. Azure provides high end virtual machines , both windows and linux based for multiple purposes.
Here we will use one , actually two for deploying our website.
Before we create a virtual machine, we should create an Availability Set , which will be later on assigned to our VM.
What is an availability set?
An Availability Set is a logical grouping capability for isolating VM resources from each other when they’re deployed.
In simple words it is nothing but a logical set to group and identify different collections of VMs.
We can skip this step and still create a VM to deploy our website. But the problem will arise when we try to deploy a load balancer, about which you will read in later parts of the article.
Steps to create an Availability Set
Step 1
On your home page you should be able to see an icon denoting Availability Sets. Click on it . If you can’t find the icon, search for “availability sets” in the search bar and click on the first result.
Step 2
It should open a page like this,
Step 2
Click on the Add button below the title Availability sets. It will lead you to a page like below.
Step 3
Choose the resource group you just created. Give a name to your availability set. Choose your region. Keep rest of the things default , then click on Review + create button.
Step 4
After the validation has passed. Recheck your inputs one last time. Alas, click on the create button. (As shown in the picture)
This will create an availability set. You can go back to the availability sets page and see your availability set listed there.
Now you are all set to create your first virtual machine. So lets start.
Steps to create a virtual machine
Step 1
Click on the virtual machine icon on your home page or search for virtual machines in the search bar, then click on the first result.
Step 2
A virtual machines page will open up like below.
Click on the + Add button on the top left corner below the title Virtual machines.
Step 3
Another page will up on up like the image below
Choose the resource group you just created from the drop down. And give a name to your VM in the name field. Then choose a region in the drop down menu of the Region field.
Step 3.
Choose an operating system for your VM in the image field. In my case I have chosen Ubuntu Server 18.04 LTS.
Step 4
Choose a cpu and memory size for your VM. If you are using a free trial, you will have cap of maximum 4 cpus. So I would suggest you to choose the size of 2 cpus and 8 GiB memory as the picture below.
The reason I am choosing 2 cpus because I will be creating another VM later for load balancing. But if you are not planning to do that, you can go with the 4 cpu option as well.
Step 5
Choose the authentication type as Password. Create a username and password. This username and password will be used for login into your VM so keep a note of them.
Step 6
Choose the inbound ports. If you want to run a web server , you must choose HTTP and HTTPS inbound ports.
Step 7
Now click on the Review + Create button. It will validate your inputs. After the validation has passed , click on create. This will create your VM.
Lets Connect to your VM and see if everything is working fine.
Connecting to your Virtual Machine
Step 1
Go to the virtual machines page and choose the virtual machine you just created.
Step 2
Click on the connect button as show below and choose SSH.
Step 3
Type down a path for SSH private key in the private key path field.
For e.g. ~/.ssh/rakeshswain
Step 4
Now copy and run the below command in your local computer’s CMD or Terminal. Replace <Login username> with the username that you had set during VM creation.
e.g. ssh -i ~/.ssh/rakeshswain rakeshswain@52.255.177.73
It will prompt for a password. Give the same password that you set during the VM creation.
If everything goes correct, you should be able to access the VM like this.
Now that our VM is ready , let set up a web server for deploying your website.
Installing a web server
We will use Nginx as our web server.
Inside your VM run the below commands to install Nginx.
sudo apt-get -y update
sudo apt-get -y install nginx
After the installation is complete, run the below command to start the Nginx server
sudo service nginx start
Check the start of the web server with the command ,
sudo service nginx status
The output of the above command should be like this
Now paste your VM ip (the same you used to login) in your browser address bar.
You should see this page.
Assigning a Static Public IP
Before we do this step lets understand why we are doing it.
When you create your VM , it gets assigned with two kind of IPs by default. one Private IP and one Public IP.
Private IP vs Public IP
Every device on the internet identifies each other and communicate with each other through IP Addresses. When you create a VM, the VM exists inside a private bubble called “Virtual Network”. You can create N- number of VMs inside that private bubble or Virtual Network. Now these VMs inside this bubble can communicate with each other through their Private IPs. But that’s where the scope of Private IPs end , inside that bubble. When you need to access the VM from your “local computer” , which exists outside that bubble, you cannot use that Private IP. That’s when the Public IP comes to use. Any device outside that private virtual network can use that public IP to communicate with a device inside the private virtual network.
So now that you know you have two IPs for your VM , one private and one public, It should also be noted that by default your Public IP is dynamic and Private IP is static.
Static IP Vs Dynamic IP
Judging from the name , you can guess that Static IP means it is a IP that doesn’t change or stays constant over time. If you restart your VM you will observe that your Private IP is the same as before but your Public IP has changed to something else. That’s why Private IP is called Dynamic IP. In your home network , you device most probably has a Dynamic IP which is assigned to you from a pool of IP Addresses by your ISP. Most users do not need their IP to be same every time. But in case you are deploying a website, the IP of your VM needs to be remembered for different reasons. One example is to configure your DNS, meaning to map your ip address to a domain name (www.xyz.com). That’s why for a production level deployment we need to have a static public ip.
Now that you know why you need to have a static public ip, lets get one
Step 1
Navigate to your VM and Click on Networking.
Step 2
Click on the network interface
Step 3
Inside Network Interface click on IP Configuration under Settings.
Step 4
Click on the ip configuration
Step 5
Choose Associate under Public IP Address and Static under Assignment
Then save your settings.
It may take a few minutes but now your IP should be converted to static. You can verify that by noting down the current IP and the IP you get after restarting your VM. Cheers.
Opening Additional Ports
In some scenarios based on your requirements you might need to use different ports than just 80 , 443 or 22 for your website or web application. For that purpose you need to first open those ports before you start using them.
Step 1
Search for virtual machines / click on the VM icon on your home page and open up the page where all your VMs are listed.
Step 2
Click on the VM in which you want to open a new port and go to Networking.
You should land on a page like this.
Now Click on the Add inbound port rule .
Step 3
A dialog box will open up like this.
Type the port number you want to open under Destination port ranges
Choose the protocol. If you are not sure , you can choose Any.
Priority will be assigned by default.
Give a name to the port under Name.
Click on Add.
Wait for a few minutes until the creation process is done. Now your new port should be ready to use.
To check if your new port is working or not in Nginx.
you can open the below file inside your VM.
sudo vim /etc/nginx/sites-enabled/default
This is the configuration file for your web server. Here you can change the default port from 80 to anything you like.
Now restart your Nginx server
sudo service nginx stop
sudo service nginx start
Now to check the port
Open your browser and type http://<VM ip>:<PORT>
and it should open the Nginx welcome page.
Assigning a domain
Now its time to get a little more professional by assigning a domain name to our ip. Before you go any further lets understand what are domain names and DNS and how do they work. You can skip this part if you already know how they work.
In short, every website or web application on the internet is hosted on a device with an IP address. Just like we hosted our dummy website in this tutorial over the IP of our VM. However, when you want people to visit your website , you can not expect them to remember the long IP addresses of your VM. That is where domain name comes into picture. Every IP address for a website is assigned a domain name such as https://www.google.com . Here google.com is a domain name. Every time you type this in your address bar and hit enter, in the backend google.com is converted into the IP of the server where it is deployed.
You can check this by pinging google.com in your CMD.
you see this is one of the IPs behind the domain google.com. If you copy this ip and paste it directly in your browser , this will also open up google.com.
There is more complexity to it. We are just oversimplifying things here for better understanding.
Now lets dive a little deeper.
What is DNS ?
DNS is the phone book of the internet.
It is the back-end system that is responsible for translating a domain name in to the IP address of the device on which the website is hosted.
Whenever you type a url in your browser and hit enter. A query is generated by the browser called DNS query. The purpose of this query is to find the IP address behind the domain url. The first stop of this query is the DNS resolver or DNS recursor. As the name suggest, this DNS recursor recursively searches for the IP address and sends it back to the client which is your browser.
The root server in the above pic is called the DNS root name server. The DNS resolver first sends the query to the root name server. This root name server then directs the DNS resolver to the respective TLD server based on the extension of the URL such as .com , .in or .do .
The TLD server maintains the record of all the domains that share a common extension. For e.g if you have typed google.com , then the root server will redirect the DNS resolver to the TLD server that keeps the record of all the websites ending with .com .
Now in the final the TLD server will redirect the resolver to the Authoritative name server which contains the IP address assigned to your domain. This ip address will be returned to the DNS resolver and DNS resolver will return it back to the browser. Browser will then use that IP to connect with the server hosting the website. Now the DNS resolver will cache this domain name and the IP. This cache will be used when another client sends the same request or hits the same URL.
Configuring a domain
There are thousands of domain name providers on the internet but in my case I purchased a domain from godaddy.com. You can choose to buy from any provider you like. The following steps will be similar for all the providers but the UI layout of you provider might be different.
Step 1
After buying a domain , you need to go to the domain management settings in your provider’s website. In godaddy.com you can find the domain management settings of your domain under My Products.
Step 2
Edit the A record .
Generally in any DNS management page you will find a set of records , most commonly the ones shown in the picture. Here is the most important record that will map your IP with your domain is the A record which stands for Address records. DNS management in your provider’s website might have different number of records but there will definitely be at least one A record.
For pointing your domain to the IP you need to edit the A record and add your IP address. In godaddy , it is done like this,
Under the points to field , we need to add the IP address of our VM and click Save.
Now you can browse to your website through your domain name.
Cloning a Virtual Machine
In this step we will learn how to clone the virtual machine but before that lets discuss why?
Suppose you are running a critical service and even one day of down time can cost you a fortune. You definitely do not want to rely your business completely on only one VM. So what do you do?. The simplest answer is to run an exact replica of your website in another VM. So when one goes down, the client requests are redirect to another one that is up.
However , you do not want to create and set up another VM form scratch and transfer all your old data to the new VM. That’s where cloning comes into picture. By clone an existing VM , the new VM will retain all the data from the source VM and you will be easily able to start the replica of your service on your cloned VM.
Steps to clone your VM
Step 1
Go to the VM you want to clone and navigate to disks.
Step 2
Click on the OS Disk which contains all your OS configuration , installed software or libraries and their configuration as well.
Step 3
Inside the OS Disk view, click on Create Snapshot. This will create a replica of your existing OS Disk.
Step 4
Choose the same resource group as per your source disk.
Give a name to you snapshot.
Choose a Storage Type. For snapshots , the recommended is Standard_ZRS (Zone redundant) for costing reasons.
Click on Review + create.
Step 5
Finally after the validation has passed, click on Create .
Step 6
Search for Disks or Click on the icon denoting disks to go to the Disk management page.
You can see your snapshots listed over there. Click on it.
Step 7
Click on the + Add button located below the Disks title to create a disk
Step 8
This will open up a page like below
Choose your resource group.
Choose the source type as Snapshot.
Choose the Snapshot you just created.
Then click on Review + create
Step 9
After the validation has passed click on create.
Now that your disk has been created. Navigate to Disks to create a new VM with the newly created disk.
Step 10
Now open the new disk you just created from the list of disks.
Inside the new disk, click on + Create VM.
The VM creation process is the same as before.
Important Note: Make sure you choose the availability option as Availability Sets and choose the same availability set as the source VM.
Now that you have created the cloned VM follow the same process as before to assign this VM a static IP as well.
Now you can run the same website from this cloned VM as well.
Setting up a Load Balancer
Now we will learn how to set up a load balancer. Load Balancing is a way of efficiently redistributing the network traffic across multiple backend servers. That is the reason why we cloned the VM which runs the same website as the source VM. To implement load balancing.
Load balancing can be done at different levels. Here will majorly talk about two types of load balancing
- DNS Load balancing
- HTTP Load balancing
DNS Load balancing
Remember how we configured our A record to assign our domain with the IP Address of our VM. For implementing DNS Load balancing all we have to do is add another A record pointing to the cloned VM. With this, if one of our VM is down , our domain will point to the ip of the VM that is up. This is DNS Load balancing.
Go to the DNS management page of your domain provider and add another A record pointing to the new cloned VM.
HTTP Load balancing
By default load balancing refers to HTTP Load balancing. In this type of load balancing , a component known as load balancer sits at Layer 4 (Transport layer) and redirects the incoming TCP/UDP client requests to the operational , or also said to be healthy backend server to serve the requests.
This is how it looks graphically,
Lets understand the load balancers by creating an azure load balancer for our servers.
Step 1
Search for Load Balancers or Click on the Load Balancers icon
This page will have the list of all the load balancers you have created. Initially none.
Step 2
Click on the + Add button.
Now give your Load Balancer a name
Choose a resource group.
Choose a region.
Choose Create New in Public IP Address field
Give the IP Address a name in Public IP Address name field
Choose the Assignment as static.
Click on Review + create
Step 3
Go to your newly created resource i.e. Go to the load balancers page and open your newly create LB.
In this step you need to configure 3 things to set up a load balancer
1. Frotend IP Configuration
2. Backend Pools
3. Health probes
4. Load Balancing rule
Frontend IP
Well, for now you don’t have to configure anything here but just understand what does the Frontend IP means. It will basically act as the public face of your website. Once you have set up your load balancer correctly , instead of directly using the IP Addresses of the VMs, you will use this IP address as the address of your website.
Backend pools
In the backend pools you will create the set of application servers to which the client requests will be redirected.
Go to the backend pools under settings and click on + Add button
Give your backend pool a name.
Choose IP version as IPv4
Choose Associate to Virtual Machines
Now click on the add button to + Add button under the virtual machines title to add your virtual machines to the backend pool
Choose the VMs you want to add and click on add.
Note : One backend pool can contain the VMs belonging to the same Availability Sets. That’s why we created the Availability Set in the first step because once the VMs are created , you cannot change their Availilblity Sets.
Finally click on Add button to create the backend pool.
Health Probes
Health Probes are assigned to backend pools. They are used to analyze and monitor the health of your VMs so that the Load Balancer can send the request to the healthy VM.
Go to the Health probes section under the settings of your Load balancer.
Now click on the + Add button.
Give your health probe a name and keep everything else as default. Click on Ok button .
Note : If you look at the add health probe page, you can see how the health probe will operate by default.Here, it will use TCP protocol to check the health of your VM every 5 seconds until 2 consecutive failures before it declares it as down. It will use port 80 to receive health signals.
Your health probe is now created.
Load Balancing Rules
This is where all the previous components, Frontend IP configuration , Backend Pools and Health Probes come together.
In this step you define how you want your load balancer to behave.
Go to the Load balancer rules settings under the settings section of your load balancer.
Click on the + Add button.
Choose the IP Version as IPv4
Choose the Frotend IP (default)
Choose TCP (in this case 80)
Choose the Backend Port (in this case 80)
Choose the Backend Pool you just created
Choose the Health Probe you just created
Click on OK to create the rule
So what happened here?
You basically set the behavior of the load balancer. You set that if you receive a TCP request on that Frontend IP , on that particular port then the health probe assigned in the rule will check the health of the VMs inside the backend pools that are assigned in the same rule. If everything works great then it will send the request to one of the healthy VMs on the backend port.
Now you can use the Frotend IP to access your website , therefore you can replace the VM IPs inside the A record of your DNS records with the Load Balancer Frotend IP.
Goodbye Note
This is all I had to say about Azure.
It is both surprising and rewarding to realize the amount of things I learned from one micro experience alone.
Give a clap if you liked this article.