Load Balancing Tutorial with Python and Nginx

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys it's a saying in this video we will discuss what is the meaning of lowered balancing we will go through an example quickly explain what is the benefit of load balancing what's an application how you can build an application on the backend and then we will go through the technical part where we will actually install our own load balancer we'll spin up multiple versions of our applications and then we will actually physically do the load balancer using engine acts alright well that's it let's just jump into it so load balancing is is the process of receiving requests from clients and these cars could be mobile phones more web applications or desktop applications to a single node or instance and that instance that application start distributing that request to multiple different applications usually this is referred to as the IP and the port ok so with that said let's just jump into our actual example so classically what were you what we always done and I would write an application on the backend ok using Python JavaScript or PHP and then I will spin up and a web server alright serving my application ok and that usually will take the machine name and the port and then essentially clients make requests to this IP address slash colon port and then start consuming the application but depending on the implication of your application it is per process each process is a single port IP then you'll start overloading obviously or your application you can only serve one client per request right and once this a client is served you will get back the results and then you can serve the either time so essentially your obtain some sort of a queue all these clients will start waiting until you start reserving so what the natural things to do is you basically spin up another version of the application is very simple right just literally spin up a new process with a different port ok puts the same application right very simple okay but but now there's a slight problem then the clients you have to teach them to now connect to the new port right to this new URL HTTP la port flash application they don't know that I mean that that's just cumbersome to do right so what we did is actions Chile is established this middle layer of interpreter called the reverse proxy I made a lot of videos between proxy reverse proxy and load balancer check them out I'm gonna link them here on the card but essentially what we do is we have this load balancer and then all the requests the clients only knows the address of this load balancer ok in this case the machine name or the domain name is called load balance but it could be like WWE below anything dot-com right and that is the domain points to this machine which which this machine or this contain or anything that one's the load balancer and that load bars are small enough to register what different versions of application you have and it will start forwarding request to to e to your application based on which which may which which application is actually busy or which different algorithms really the basic ones is we're gonna do today is actually just wrong Robin any requests take it to the first one second request a third a third one and and so on right one two three one two three that's the basic thing there are many algorithms AI can trigger one algorithm could be the least connected like the the client or the application that has least number of connections going forward that stuff to it that works as well right so what that say let's just jump with an actual live example here okay so I have here guys a Python example here I put a code in the Python that I'm gonna link the card the YouTube video that we did on the Python on the back and how can we write our own application but listen to a port right so imagine this is your very complex library but you want to expose it through HTTP right through a client okay but this application is when someone or a user makes a request to it it does does something at the back end all right let's take could be like image processing could be training your neural network could be doing geo complex geocoding or geo algorithms geo analysis it does something on the back end right something that you don't want your users to do compression anything right thinking for anything peek at you know you can write that it's compute heavy and you want to run it on the server so in mainz case that my application is very simple doesn't do any of that all it does is it actually whenever it makes a request it only returns back hey you were served by the process ID that I'm currently running and the reason I'm doing that is just I want to show you that each process is different right so this is that just a process ID of the Python version that is running right that the process ID of the Python application okay so let's go ahead and just open a new terminal here and I'm gonna go to this directory PI word and what I'm gonna do is literally start Python 3 index with PI and then I'm gonna start on port 1 1 1 1 ok and that says like my application is ready listening to port 1 1 1 I'll go ahead and do HTTP localhost 1 1 1 1 that's my application right and my application is listening to the slash basic that's my application name so so slash basic I have to do is slash basic and when I do that I immediately get the response right the response is served from 6 0 7 0 and no matter how long I will refresh I will always get the same result obviously we're refreshing here and the reason is because this application is served with this process the process whatever that process running is called 6 zero seven so what happened if now I went ahead and created another five and three indexes boy to do it with you I want to listen to another port so I have two applications running one on port 1 1 1 1 and the other is port 2 2 2 2 ok obviously it has a different port right refreshing 4 1 1 1 I was 6 7 0 7 from this port I get 6 7 2 0 okay and so on guys right I can I can keep spinning applications but what I want is actually I want to load balancer right so to do that ok and my Mac in this case alright for the for the application just for a Mac we can install nginx for Windows it's slightly different thing you have to install it from the engine x.org right but for Mac we all have to use brew which is another package manager for a Mac so once you have brew we can just do a brew install nginx ok and that will take care of installing engine X will have everything and looks like we have nginx ready alright guys now that we have installed engine X it's ready right we have to actually run it alright and to run nginx Ingenix we want to run engine X as pseudo as root right with higher privileges actually because a load balancer will listen to port 80 and port 80 to listener to port 80 you have to have high permissions ok so to do this it's literally to command sudo space and your next okay if your windows you just run right to click as administrator it actually will ask you to run with the variator to elevate it and that's that that is it okay what that said now that I have nginx it will run with the default setting which is port I think port 8080 ok we don't want that we're gonna change that okay but do you nor der to change that we need to create a separate config file just for us ok so let's just go ahead and crease that config file then we'll have our Python applications and the load balancers and all that stuff let's just get to it let's go ahead and start with creating the config file so I'm gonna go ahead and go to open that's created here by web say now sir let's create a folder called load balancer and then let's open in here and I'm gonna call my config file here Python does confer you can call whatever you want okay the trick here is we want first create a server ok so that server will be the entry for our configuration ok and my server will be listening on port 80 ok so I'm listening on port 80 that's a my load balancer ok my load balancer which is this guy is listening on the port 80 which is like the optional port right you don't have to specify it ok once you do that I want to actually create a location and the location is called when you visit slash basic I want you to do the following I want you to proxy pass and go to this link instead right Python web slash basic now we can you'll ask yourself what is Python web Python 1 does not exist right this is just an alias and that's the alias art I want you to know about here is I'm gonna create an upstream service which is called Python web ok and what what inside it is essentially you will specify all your services and guys you know what are our services alright our services remember we just want to replace the - web with the actual serve this combination which is IP port which is you guys did localhost the first service is one one why one about the second one - about the third one localhost 333 and then done so this is it that's the our configuration right there so all we need to do now is nginx is currently running remember okay but I want to just inject this configuration so we're gonna reload that configuration into nginx without taking it down so let's just jump into it alright so the next thing we need to do is just go to slash user slash local slash it's see nginx and then this is where the configuration for nginx is so I'm gonna just use vim here to edit that engine X config and then there is a bunch of config I really recommend not touching that all right but what we need to do is just add an extra configuration here so we'll go scroll down find our include right and then what we're gonna do is include our own right now so let's go ahead and include it's literally paste paste here there you go we can actually paste so I just want to include my own configuration alright guys now that we have made their configuration the change all we have to do is just do nginx - yes reload and that should have reloaded my configuration with the latest one okay so let's go ahead and start so now obviously these applications to running basic - now what I want to do is actually just do localhost / basic would I get anything and look at that we are getting six seven zero seven fire refresh I keep refreshing I hit another port okay so I had the second application so what happened if I now went ahead and starting another one right let's start sudo index the PI we don't need to do it's just the Python Python 3 and X dot pi 333 3 right so 3 3 3 was already configured did we we did configure it right 3 3 3 3 all right so now if I keep refreshing I get 20 and 7 1 97 which is the 3 3 3 I bet that it is right but it's like let's do 33 33 basic we get seven one nine seven six seven twenty and six seven seven so now you have just built an application with a load balancer right your clients your mobile application just can hit this and it will just direct you to the correct location essentially all right regardless of which application is running you can take an entire application down and it will work right so let's take three down and let's take let's take two down okay and now if I keep refreshing I will only get the application that is running how cool is that which is 6 7 0 7 which is 1 1 1 1 right so that's the pretty cool thing about this and if you replace this with the machine name you can just take you can start building an application like if it has a lot of load you can just load balance the request to the correct application and you can just build a highly available application alright guys hope you enjoyed this episode I'm gonna leave the source code below for the for the Python this the small Python script that we wrote and we're gonna see you in the next one consider subscribing to this channel we talked about a lot of software engineering topics load balancing is just one of them we have a lot of courses tutorials we specialized in GIS here in this channel but in general we talk about a lot of programming and software engineering so consider subscribing if you like that stuff I'm gonna see you in there in the next one goodbye
Info
Channel: Hussein Nasser
Views: 27,587
Rating: undefined out of 5
Keywords: Software, Engineering, load balancer, python load balancer, nginx, load balancer configuration, how to configure nginx, how to configure load balancer
Id: 4xGQS8Pv4io
Channel Id: undefined
Length: 15min 39sec (939 seconds)
Published: Sun Dec 23 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.