Auto Scaling and Load Balancing on AWS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video I'm going to teach you about autoscaling and low balancing on AWS and show you how you can build an autoscaling group and set up lad balancing to a web-based application in the cloud and it's relatively easy to do and you can follow along as I do it and I'll teach you a little bit of theory first to help you understand what it is I'm talking about now if you like what I'm doing here please make sure you like And subscribe and we'll bring many more videos to you so here's a diagram of a config ation in the cloud where we have an autoscaling group and a load balancer so what we have here is we have these two availability zones in AWS within a region you have availability zones essentially each one of these is a separate data centor can be more than one data center but it's at least one data centor so they're separate from each other you can see we've got two availability zones here that means our ec2 instances running Linux or Windows are deployed across two different data censors at a minimum and we've got public subnet so we can access those instances publicly if we want to now the Autos scaning group takes care of launching instances across different availability zones and make sure that there's at least the right number whatever that desired count is that we can specify and it will watch metrics like performance metrics with cloudwatch to see how the instances are being utilized and if they're highly utilized we can set some thresholds so the Autos scanning group will scale automatically and elastically it's going to launch instances when it needs to and terminate instances when it needs to as well to save you cost then what we do is we put a low balancer in front and the low balancer is going to direct incoming connections so here user one as being connected through to instance one user two to instance two and user three to instance three now if an instance were to fail for some reason then it would be taken out of action of course the user will need to be rerouted immediately to another instance now I'm just showing one connection to each instance of course there could be hundreds or even thousands so user one is now being reconnected to a different ec2 instance the elb has taken instance one out of service because it failed a health check so it's constantly checking the health for the instances it saw that it wasn't available and so it stopped sending traffic likewise the autoscaling group noticed the same thing now the low balancer can actually notice the auto uh notify the auto scaling group but also the auto scaling group can also run ec2 status checks or it looks up the ec2 status checks to see if the instance is healthy so it's going to terminate the instance in this case and then it's going to launch a new instance and now new traffic can be sent to that new instance so that's what's happening we're constantly maintaining the right number of instances based on ensuring that they're healthy and that they're the minimum that we want to have running any one time and also making sure that if any instances fail they get replaced and and if the load on the application increases then we need more instances and Autos scanning will do that for us so let's go over to AWS we'll build out an Autos scaling group and a low balancer so I'm in AWS here and what I'm going to do first is head over to VPC I'm going to set up a custom VPC just for this lab so I can isolate everything so I'm creating a new one and I'll give this one a name ASG ALB and I'm going to leave the default CER block to availability zones we're going to have public and private subnets that's fine I don't need a net Gateway and I don't need a VPC Endo for S3 so I can just create the VPC that sets everything up for me once we've got that what I'm going to do is just head in and let's view the VPC here we're going to create a security group that we can use now you can separate your security groups if you want that that is a security best practice so for example what I can do is I'm going to create one for my app for my ALB so that will be ALB let's call it alb1 and then we're going to put that into the ASG ALB VPC add a rule now this rule is going to be for HTTP because we want incoming traffic and it needs to be from anywhere because we we want to accept traffic from the internet so I'll create that Security Group then I'm going to create another Security Group it's going to be a separate Security Group this one is going to be for my web servers so I'll call this web- one again just copy that down into the description again we need HTTP but this time I'm going to only allow traffic from the ALB security group that we just created oh you know what I haven't done I haven't changed the VPC so make sure you do that that's why I can't see the correct Security Group there we go alb1 so this one is only going to allow traffic from the low balancer so it's a bit more secure we leave the outbound rule they're fine create the security group so now we have the security groups the next thing I'm going to do I'm going to create the low balancer first because it takes a little bit longer to set up so what we want to do is now we need to go to ec2 in the ec2 console we're going to go down to Target groups we're going to create a Target group so here the target group is the collection of instances that we're going to send traffic to so we choose instances we don't want to use IP addresses or functions or low B aners here and I'll call this one just simply tg1 and then for PR call and Port I'm leaving that at 80 it's HTTP 80 because this is a web server that we're going to run ipv4 that's fine VPC needs to change so I do need to specify my VPC here and http1 that's good as well the health check will also be on the uh the default path on the HTTP web server so I'll leave those defaults as well as well and then we click on next and just create the target group we could have registered targets if we had any there but I don't want to do that because we're going to attach the target group to the autoscaling group after we've created the auto scaling group but first as I mentioned I want to create my low balancer because it takes a few minutes for these to initialize so I'm going to create a load balancer it's going to be an application load balancer and if I went too fast there excuse me um just come down on the left hand side to load balancers create load balancer and then choose create I'll call this one alb1 cuz I'm not too imaginative and it's going to be internet facing which means it will have a public IP and DNS name and it's going to be accessible from the Internet ipv4 is good of course I need to change my security group and what I'm going to do is I'm going to choose um a couple of different availability zones now I've only got two of course for this particular VPC so USC 1 a and 1B make sure these are the public subnets that's what we want even if your instances are in private subnets you got to specify the public subnets in the same availability Zone that's the way it works next I need to come down to security groups choose alb1 that's the one for my web uh for my ALB front end and then the target group is specified Under The Listener so the listener is going to listen on HTTP 80 it's going to listen on that port and it's going to be publicly available so any webs service connections coming into the DNS endpoint for the low balancer on Port 80 we want to then forward them to Target group one and that's basically everything we don't need to enable W or Global accelerator at this point so we just create the load balancer excellent that's being created so what we've just deployed is just the low balancer and the target Group which will contain the instances but we don't have any instances yet so we need to create the autoscaling group there's a couple of components that we need we we need something called a launch template and in the launch template we're going to specify the Amazon machine image and some settings including some user data to install a web server then we can create the autoscaling group and then we need to attach the target group to the autoscaling group so that when Autos scaning launches instances it's going to automatically register them to the Target group and then the low balancer can send traffic to them all right so let's come back what we need to do make sure we're in the ec2 Management console under instances choose launch templates we're going to create a launch template so this one's just going to be called um my LT1 there we go and for application OS images I am going to choose browse more Amis just choose the Amazon Linux at the moment it's the 2023 Ami so I'll select that one you can choose whatever the latest one is scroll down a little way okay so everything's good there instance type I want to choose T2 micro so it's free tier I don't want to include a key pair and for network settings we're not going to include those in the launch template except for the security group so here we can choose web one that was the security group which I chose uh for the web servers themselves and it's in the correct VPC then we're going to scroll down and under Advanced details I'm going to scroll way to the bottom here here and we're going to put in some user data so I've got this user data that will be available with the video for download and I'm going to copy all of this user data from line 4 to line 29 I suggest you use Visual Studio code or some other Editor to open this markdown file if you use um some other application sometimes the formatting gets broken this code is going to update the system with patches and then install Apache the web server it's going to start it up and enable it so that it runs after reboot and then these couple of commands here basically what they're going to do these free commands is the first one here is just getting a token for the instance metadata service then we're specifying an environment variable that's going to equal the instance ID so the specific ID of the instance then we're going to Echo and use that environment variable so that we're creating an index.html page that will be the web server page and that web server page will say this instance has the ID and then the specific instance ID of the individual instance now this is going to run this user data will run on every Auto every instance that's launched by the autoscaling group that means they all have different instance IDs so when we're refreshing our page um connecting to the load balancer we'll see the different instance IDs and we'll know that we're being load balanced okay so I've copied that code so let's just come back and paste that in and then we're going to create our launch template that's done so I have my launch template the launch launch template is now ready now there's one thing that I need to do because when we actually created our VPC what you will actually find is the subnets the public subnets here let me just give this a refresh there we go there's my public subnets and my private subnets for my custom VPC now if I choose one of these public subnets let's go to actions and we want to edit subnet settings enable aut to assign public IP is not there I do want to enable that so I want able that for both of my public subnets okay so public two there and then we've got public one right next to it you have to do these one at a time just enable autoassign public IP so that our instances get those public addresses okay now that's done we're ready to go and create our autoscaling group so now we're ready to create our Auto scaling group let's head down to autoscaling in the uc2 Management console and click on autoscaling groups create autoscaling group and I'll just call this my ASG and then I need to specify the launch template so my LT1 and we can see it's picked up the Ami the security group and so on click on next and what we want to do here is make sure we choose the correct VPC so the custom VPC I'm using and I want to select the public one and public two so these are the the subnets in the public the public subnets in two different availability zones click on next we do want to attach to a load balancer so I choose the option attached to an existing low balancer and then specify the target group now it will automatically pick up the instances and register them when the autoscaling group changes they'll be registered into the target group and then the low balancer can forward traffic on we want to enable the load balancing health checks that means that the autoscaling group is going to pick up when the low balancer stops sending traffic so the low balancer will essentially notify autoscaling that it's stop sending traffic because an instance is not healthy and it will this will happen in addition to the ec2 status checks I'm going to click on next and here if I just scroll up cuz it Scrolls me to the bottom of the screen for some reason I'm going to set my design capacity to two minimum to two I always want to have minimum of two and then let's put maximum as four okay so now it's going to automatically launch two it's never going to go below two and the maximum is going to be four you can set that to what makes sense for you but I do want to set up some autoscaling so we're going to use Target tracking and the target tracking is going to monitor the performance metrics associated with the specific settings so average CPU for example or it can use the load balance of request count that's what I'm going to use so we're going to generate some connections to the instances I'll leave that to 50 and then just scroll down in fact we can skip to review now and then scroll down and create the autoscaling group so that's going to launch we can see it's updating capacity if you click on the autoscaling group and go to activity you can see some of the events happening here it's a good place to go and check out what's actually happening looks good it's launching two instances as we would expect if I head back up to ec2 let's go to instances I got some terminated ones from previous exercises and here we got two pending instances so they're actually launching right now we've got our load balancer the load balancer should now be active I've got the DNS name that's what we're going to connect to the Target groups here we can click on targets and once they're yeah they're already there so if you click on refresh we can see that they've been registered health status is initial now only when the health status is actually healthy will the low balancer start sending traffic so at the moment the instance is booting up it's going to run the user data then the health checks happen then eventually they're ready to actually um have receive traffic from the low balancer so just keep an eye on this keep refreshing eventually you'll see those turn healthy now mine just took a few minutes and now they have a health status of healthy so that's really good now just in case you have done something wrong couple of troubleshooting steps that you can do so if the instances aren't healthy one thing is to connect to the public IP problem is our security groups not set up for that at the moment so what we would have to do is we rather than allowing connections only from the load balancer we would have to allow connections using HTTP from anywhere um and that will enable us to be able to troublesho I would also add SSH from anywhere okay let's just save those rules so now if we go back to instances I should be able to copy the public IP and put that into my browser and I see that it says the instance has the ID and that's good that that's all worked that's populated with the ID the other thing is I can also use instance connect now so I can connect to the instance it has a public IB we've just opened SSH from anywhere so we can connect to the instance for example I can change to v dub dub dub HTML that's the server directory we can see the index file there and if I C that index file I can see the content so I know everything's working great let's make it secure again so what I'm going to do is come back to my Security Group which is web one and let's put it back as it was I like to make sure things are secure so let's come down at HTTP SG alb1 we're only allowing connections from the low balancer we'll save those rules come down to the low balancer copy the DNS name put that into my browser and now I'm just going to refresh my browser we can see I'm cycling between different instances okay we see different instance IDs coming up so that's low balancing essentially working now we want to see autoscaling in action so I've got this command here and what we're going to do is we need to populate the command with the DNS name of the load balancer I already have that on my clipboard so make sure you got HTT TP col on SL slash you haven't doubled up anything there it's got a slash on the end that's fine and I'm going to copy this to my clipboard come back and we're going to open up the cloud Shell service you can run this from your computer at home as well that's fine we're still making connections I'm just going to use cloud shell so I'm going to load the cloud shell environment it's a nice way of being able to run commands in the cloud and once that's ready I can paste this command in and hit enter and you can see it's just connected and it's cycling between those instances it's going to make 500 connections I can run this a couple of times now what happens is we'll see that now that we created the autoscaling configuration the target tracking policy if we go to Cloud watch what you'll find in Cloud watch is that there's if you go to all alarms there's an alarm here okay so we've got the target tracking my ASG alarm low and alarm High okay so alarm high is okay alarm Lo saying insufficient data not enough information yet what we want to do is trigger this one so that the target tracking alarm high is going to go into an alarm State and that's going to happen but you can see it's going to happen after the request count per Target is greater than 50 for fre data points within three minutes so it's not going to be instant I'm just going to rerun this command several times and then I'm just going to wait okay so I'm just going to wait a few minutes and come back to cloudwatch and have a look at what's going on and hopefully we'll see that alarm has changed before that happens just make a note of how your environment is currently configured I have two running instances if we go to auto scaling here we've got the Min two desired two and Max 2 current is two your max has to be obviously higher than your current desire capacity uh otherwise it won't be able to launch new instances so run those commands a few times and then let's give it a couple of minutes and we should be able to see the alarm trigger and the auto scaling group will start to scale so it's just been a few minutes and I can see that my alarm high is now in the in alarm State and if I click on this alarm in Cloud watch we can see we can see this massive jump in connections so you can see that's the request count let me just hover in the right place request count per Target and that went way way up way up here to um over 300 so that's in the alarm State let's go back to autoscaling and let's refresh here and see what's going on and now you can see it says four instances desire capacity is four Let's uh just go back over the left there click on the Autos scaling group go to activity and we can see what's going on okay so here it says the it's referencing the alarm is in the state alarm triggered by this policy changing the desire capacity from 2 to four so we got two instances that are launching there waiting for instance warmup so if we head back up to instances we should see we've got four instances running now we then need to go and check that are they going to be registered into the target group and are they healthy so let's come down to our Target group refresh we got four healthy instances already so I'm going to copy my load balance a DNS name again pop that in my clipboard and now when I refresh I should see more instance IDs so now I've got four different instance IDs so there we go we now have autoscaling working so that's basically what I wanted to show you in this particular lab now I'm just going to close out of these the other thing that you can do as a test is just terminate an instance and see what happens when you terminate an instance it should be replaced by the low balancer okay so I finished with this lab so what I'm going to do is I'm going to go to my Autos scaling group and delete the Autos scaling group now one of the things you'll notice because it's actually connected to a low balancer it's not going to happen instantly so you'll you can go up to instances and they're not going to be terminated immediately uh and the reason is we have this thing called connection draining so it's going to wait for about 5 minutes for the instances um just in case they might have some connections we know that's not the case so if you want to speed things up you can just go and terminate the instances but it will do it for you anyway other thing of course is to make sure that you go in and delete your load balancer because that does cost money so I'm going to delete my load balancer so I don't end up with any charges Target groups don't cost you anything once the load balancer is gone you can delete the target group and once the instances are also terminated you'll be able to delete the VPC if you created a custom VPC see and then that's all of your chargeable resources gone the chargeable resources are really the load balancer and the instances nothing else that we configured actually costs you anything so make sure you got those terminated and deleted and you'll be all [Music] good
Info
Channel: Digital Cloud Training
Views: 2,733
Rating: undefined out of 5
Keywords: AWS, Amazon Web Services, AWS Amazon, AWS Certified, AWS Training, AWS tutorial, Amazon AWS, free aws, free aws tutorials, AWS Cloud, Cloud Computing, AWS for beginners, Introduction to AWS, Load Balancing on AWS, Auto Scaling, deploy fault tolerant servers, deploy web servers, Availability Zones, Amazon VPC, Application Load Balancer
Id: 0mwgbiJae5Q
Channel Id: undefined
Length: 22min 53sec (1373 seconds)
Published: Tue Feb 27 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.