High Availability Webserver & Database setup by using only AWS EC2!
This blog aims to explain the process of setting up a High Availability Webserver & Database (MySQL) architecture by only using AWS EC2 with webserver monitoring support.
In today’s world, the biggest irony is that there are multiple things that are helping a person, those are the same things that are destroying the same person at a very rapid pace. The most prominent example of this is Cloud Computing, Cloud helps & acts as the backbone of almost all businesses today, but if we look from a different perspective, that is knowledge, then most cloud engineers lack the core concepts because they are only been trained to complete the work irrespective of how much core concepts are been known to them.
For Example, Anyone can set up a VPC is just a few clicks by using AWS WebUI, but in contrast to it, if someone is been asked to create that setup manually, then it will be very hard, because there are tons of networking concepts that are required to build it, & these concepts are not been known by most of the people.
Keeping the same thing in mind, I will set up the whole highly available architecture of the webserver & database without using managed services like RDS, only EC2 will be used for the same. (VPC setup has been done directly using AWS WebUI because that is not the goal of this blog to explain that setup step-by-step. Do let me know in the comments if a blog on setting up VPC step-by-step is required, I will publish a blog on that super soon).
Very basic knowledge of some of the very common services of AWS is required for the implementation of the highly available architecture, which is as follows:
Pre-requisites for this blog
- Basic knowledge of networking & AWS VPC.
- Basic knowledge of PHP.
- Basic knowledge of AWS EC2.
Let’s start with the implementation.
Implementation of High Availability Architecture of Webserver & Database!
The steps required to accomplish the goal are explained below with their practical screenshots.
1. Setting up a VPC!
In this step, a VPC is created with 2 Public Subnets & 2 Private Subnets. In the Public Subnets, the webserver will be hosted using AWS EC2 service, whereas, in the private subnets, database instances will be hosted using AWS EC2 service.
To create the VPC as required, do the following:
- Open the VPC service in the AWS Console & click on “Create VPC”.
- Select “VPC and more” in the “Resources to create” section of the VPC Console. Provide the name of the VPC, for this implementation, I have mentioned “Highly Available Architecture” to the name of the VPC.
- Select 2 Public & 2 Private subnets.
- Select None in the NAT Gateways (NAT Gateway is only required when there is a need for the EC2 instance running in the private subnet to use the internet).
- Select None in the VPC Endpoints as well (In this use case, there is no need for the VPC Endpoint, if you have the knowledge of VPC Endpoint & you feel that it's necessary in your usecase, then you can create them).
- Keep the rest of the settings as it is, & click on “Create VPC” (AWS will take care of the proper connectivity as required).
VPC creation is complete at this point.
2. Database setup using AWS EC2!
In this step, a database (MySQL) machine will be hosted in the private subnet of our custom-created VPC using EC2.
To setup the database as required, do the following:
- Open the EC2 service in the AWS, & click on “Launch Instance”, then again click on “Launch Instance”.
- Provide any name to this EC2 instance, for better management, I have given the name “Database” to it.
- Select Amazon Linux as the AMI.
- Select “t2.micro” as the instance type to use the free tier (as in this demonstration, high resource instances are not required) & select any pre-existing key pair or create a new one.
- From a new tab, create a security group for the webserver that will allow the traffic at port 80. This security group is created to make sure only those ec2 instances which are attached to this security group are allowed to access the database.
- In the Network Settings section, click on “edit”.
- Select the newly created VPC (“Highly Available Architecture” in my case)
- Select any private subnet that exists in the VPC.
- Create a new security group, & give a name to the security group, in my case, I have given “Database-SG”
- Add an Inbound security group rule that will allow access to the database instances from the instances that are attached to the Webserver Security Group.
- Keep the rest of the Network settings as it is.
- Keep the storage configuration settings as it is.
- Click on “Launch Instance” & launch the instance.
Till this point, EC2 Instance for the Database is launched, up & running. We need to setup the database in it now.
Now, let’s install & setup the database in it.
- Since this instance is in a private subnet & we have to install the database in it, therefore, it needs access to the Internet. So, we need to use a NAT Gateway for the same. To create the NAT Gateway, go to VPC settings, & click on NAT Gateway, & then click on “Create NAT Gateway”.
- Assign an elastic IP to it, & also select a public subnet of the VPC that is created by us. Then click on Create nat gateway & the gateway will be created.
- Edit the route table (by going into VPC settings) that is associated with the private subnet in which the database EC2 Instance is running. The Route of NAT Gateway needs to be added there.
- You have to now connect to this EC2 Instance for the setup. Since it's in a private subnet & the public IP Address is not assigned, therefore we need to launch an EC2 instance in the public subnet, copy the ssh key to that & then connect to this database instance using the newly created EC2 Instance in the public subnet. This newly created EC2 Instance is also known as “Bastion Host”. (I am not covering the creation of Bastion Host in this blog as it's not required).
- Switch to the root user for the installation with the below-mentioned command.
sudo su - root
- Install the required database packages with the below-mentioned command.
yum install mariadb105-server -y
- Start & enable the MariaDB services.
- Create a database to store the data.
- Create a user & provide that user with all the privileges to all the databases. This is been done to make the webserver properly connect to the database. (otherwise, we have to change the configuration of the database to allow the webserver to connect to it using the root account).
Note: Make sure to note the username & password (both case sensitive), these will be used in the configuration of the webserver.
This concludes the database creation & configuration process!
3. Configuring High Availability Webserver using AWS EC2!
In this step, a group of Apache webserver machines will be hosted in the public subnet of our custom-created VPC using EC2.
To setup the webservers as required, do the following:
- Open the EC2 service in the AWS, & click on “Launch Templates”, then click on “Create Launch Template”.
- Provide a name for the “Launch Template”.
- Select the Launch AMI for the Launch Template.
- Select the Instance Type and the key pair.
- Select the public subnet of the custom VPC created, & the security group to allow the webserver (created in step 2 of this blog).
- Keep the by-default storage. (Change if it’s required).
- In “Advanced Details”, write the below script in the “user data”, so that the webserver is automatically properly setup/configured in every auto-scaled instance.
In the above script, make sure to change the IP Address, username, password, & database name with the one that you have in your case.
- Click on “Create Launch Template”.
- Go to EC2 Page, & then click on “Auto Scaling Groups”.
- Click on “Create Auto Scaling Group”. Then give this Auto Scaling Group some name, & select the Launch Template & its version.
- In the Network Settings, select our custom VPC, & select all the public subnets to ensure High Availability.
- Keep the Instance Type Requirements as it is, & click on “Next”.
- For this demonstration, I have not used any Load Balancer, in case you want that, do let me know in the comments/responses of this blog, I will create a blog in that. For the implementation without Load Balancer, just click on “No Load Balancer”, & keep the rest of the settings as it is. Then click on “Next”.
- Keep the “Desired”, “Minimum”, & “Maximum” capacity as per your requirements. In my case, I have kept them as 1, 1, & 5 respectively.
- For the Scaling Policy, you can set it based on your requirements, for this implementation, I have kept it at 50% average CPU utilization, which means that if the average CPU utilization of the instances crosses the 50% mark, then 1 new instance will be added (scale-out), & the reverse is true for (scale-out), that is if the average CPU utilization of the instances drops below 50% mark, then an instance will be removed/terminated.
- Skip the rest of the things, in case you need them, you can configure them as per your requirement. Just come to review, & then click on “Create Auto Scaling Group”.
- After the Auto Scaling Group is launched, it will create the EC2 Instances as per the specifications.
- Click on “Instance Management” to check the EC2 Instances launched.
- Finally, the setup will look as showcased below. (Make sure to hit on the HTTP Protocol, not on the HTTPS)
- You can interact on a real-time basis with this setup.
This concludes the final Implementation of the project.
Do let me know your thoughts/views/opinions in responses/comments on this blog.
If you loved this blog, or even if this blog helped you, do clap for this & do not forget to mention your thoughts in the responses.
I hope my article explains each and everything related to the topic with all the detailed concepts and explanations. Thank you so much for investing your time in reading my blog & boosting your knowledge. If you like my work, please applaud this blog & follow me on Medium, GitHub, & LinkedIn for more fantastic content on multiple technologies and their integration!
Also, subscribe to me on Medium to get updates on all my blogs!