How to scale docker containers using Nginx as reverse proxy and load balancer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay guys so welcome back again so in this video let's see that how do we scale our docker containers so before we begin make sure you hit that subscribe button and do like the video at the end in case you like the video so let's dive into vs code and let's see that how do we scale our docker containers so as you can see that for demonstration purposes i have created this node.js express application and it is a very simple app.js file and we only have this expressed as a dependency here and then we are only making a use of a single route here which handles all the routes that is app.use forward slash and then we have the request response and then we are simply sending back the json response object so if we simply run this application we can check that if it is all working fine so we can do node.app.js and now we see that our server is running on port 3000 and if we make a call to that localhost port 3000 we should be getting back our response here and we see that we get back a message here that says okay it works and the hostname is my macbook's name here and it is for a reason that i am providing this host name as a response here because when we are running multiple docker containers then this would be the container id of the running container so therefore i provided in this host name here so now let's have a detailed look at the files that are present inside this application so we see that we have a docker file here we have a dockercompose.ml file and we have a docker ignore file so this video is not about explaining you that how do you use a docker compose file to run your nodejs application in case if you don't know that how do you use talker file and docker compose to run your node.js applications then you can watch my video that is linked above but here we only want to see that how do we scale the containers that is i want to run this node.js application in multiple containers at the very same time so that i can have horizontal scaling available inside our application so we see that here we have the docker file which which simply imports the node alpine image and then we have set the workday here then we are simply copying the package.json file and then we are simply running npmci and if you think that why i'm using npmci and not npm install it is because that using npmci make sure that you have the same versions of the packages that are present inside your local machine and that same packages would be present inside your docker container so therefore i am simply using npmci and ci stands for continuous integration and then we are simply copying all the files to a docker image and then see we are simply executing this command that is node app.js and now we if we have a look at the docker ignore file we see that while creating the images i do not want to push the node modules folder i do not want to push the docker file and the docker igno file and the dockercompose.cml file to our docker container and now if you have a look at the dockercompose.yaml file we see that we are using the version 3.9 and then we have the services here and it is simply a node.js application and we have given this service a name called api and the container name that is nodejs api and we are simply building this docker file that is present here and then we are exposing this port that is port 3000 of our docker container to our local machine that is again on port 3000. so now let's use this dockercompose.cml file to see that everything is working fine so let's go to our terminal here and let me clear out the screen once again so it should be clear and now i can simply do docker compose up and now we should be seeing that our application should be up and running and firstly it is simply creating the docker file and now we are we see that our application is up and running on port 3000 so let me open another tab here and now if i do simply call localhost 3000 we see that again we get back a message from our docker container and this time we see that hostname is the id of the container that is running so we can verify that by doing docker ps so we see that this host name is simply matching the container id in which our node.js application is running so we can verify this and this thing here so everything seems to be working fine so let me clear out the screen let me go to this tab here and let me stop this application by doing control c so now the application is stopped or the container is stopped so let me clear out the screen once again and now if we go back we see that we want to run this api service or this uh apk api service in multiple containers at the very same time so what we can do we can use a scale flag here that provide that is provided by docker compose so now let's try to scale this service so now let's do docker compose a hyphen hyphen scale and then we need to provide an api equal to 2 because the service name is api and the container name inside our application is node.js api so watch what would happen so if i do this we see that we get an error here and it the error says that docker requires each container to have a unique name so to fix this what we need to do we need to simply remove the custom container name that we provided in our service here so the first thing we need to do is that that we need to simply comment this line out from here like this and this is only necessary if you are scaling up the containers that is if you are if you want to run multiple instances of the container so then you do not need to provide in a container name we only want to provide a service name here and now if we go back and if we again do this thing here that is docker composer hyphen fn scale api equal to 2 then we are about to get one more error and we would be seeing that also so we see that we created this container here that is this is created and this is created and we should be getting some error here and this is the error here that is error response from demand and it says that since we are exposing this port here from each of the containers to our local machine and our local machines port 3000 can only be mapped to a single container so this is not the way to do things you cannot directly use a local machine port that is 3000 and you can map it to different container ports that is even if i provide here 4000 then each of the containers would be running on port 4000 but our local machine only has a single port 3000 here and you cannot map a single port to multiple container ports so what you can do you can simply comment this ports out from here so now since we do not map any ports from a local machine to our docker containers so we do not have any idea that on which port the api service would be started so to fix this issue what we need to do we need to use nginx as a reverse proxy and a load balancer at the very same time to map our local machine sport 3000 to the nginx container port 3000 and that nginx container would be like proxying our request to the individual node.js containers that are running on a docker network so let's see that how do we do that so for that what i need to do i need to define one more service here and that is called nginx like this and then what i need to do i need to provide in the image name so the image name would be nginx at latest like this and now we need to map the volumes from our local machine that would be containing the nginx con file so we can say volumes like this and we would be mapping a folder called conf dot d so conf dot d and this should be mapped to forward slash hc engine x forward slash conf dot d because all the uh all the files that are present inside the folder dot is that is called control d so let me create that folder first here it is conf dot d like this so each and every nginx configuration file that is present inside this folder would be mapped to this path that is nginx conf dot t and this is the default path in which nginx or your custom configuration file should be kept inside any virtual machine so this is the path that is hard coded here and this should be this part exactly here and this folder that is control d can be any name depending on your choice but i prefer to call it contrary and now this container should only start only when this api service is up and running so what we can do we can simply say depends on or we can provide in a depends on directive and here we can simply provide in the service name that is api in our case and now what we need to do we need to map the port 3000 of this nginx container to our local machine so we can say ports like this and we need to map port 3000 of our local machine to 3000 of the nginx container not the api container but the nginx container so this is you should note that here we are mapping a port that is 3000 of our local machine to the nginx container on port 3000 so now we can save this file and now we haven't created any file inside this control t directory so let me create one configuration file so you can call this file by any name so let me simply call it my conf dot conf file like this and this is a nginx configuration file and now inside this file what we can have we can simply create a server block that is a typical server block in nginx so we can say server and then this should listen on some port so it should be listening on port 3000 like this and then we need to provide a location path here and this is all nginx stuff and if you don't know that how to write configuration files or the server blocks then again you can watch my video in which i have explained in detail that is how to set up an nginx server so here we can simply provide in a location block here which would be proxy all the requests coming to port 3000 to this proxy here and here we can simply provide in the proxy pass so proxy pass like this and this should be http forward slash forward slash api colon 3000 like this and now you might be wondering that why provided in this api colon 3000 is because that each of this request that is coming from local machine would be proxied to nginx on port 3000 and this is handled by this thing here and then whenever it sees a request that is coming here it would proxy that request to this api service that is running on port 3000 that is a nodejs container and it would do that in a round-robin approach that is firstly it would proxy to first container and secondly it will proxy the second container and then if the third request arrives it would be proxy to the first container again the fourth request would be proxy to the second container again so fifth sixth seventh eighth so this is the round robin approach that this nginx container would follow so that's all what we need to do here so let's save this file that is my control one con file and now what we can do we can go back to our application and let me clear out the screen and now what i need to do docker compose up hyphen iphone scale api equal to two and here we are not scaling the nginx container we only want to scale the api service container so let me do it this way and now as soon as i press enter what it would do it would say that there is no such image as nginx latest and it is because instead of add i need to provide in colon here it should not be like that so that was my mistake here and it should be colon latest like this so let's save it once again and now let me do docker compose up scale api equal to 2 so let's press enter and it should be done in a moment or so so now we see that our server is running on port 3000 and we see this two times here that is api 1 and api 2 and server on port 3000 and server and put 3000 again and now what i can do i can open another tab here that is terminal tab here and if i do call local host port 3000 we should be getting back our response here and we see that we get this host name here and if i do curl once again then we should be getting a response back from the different container or from the second container here so we see that this time we get back from this container here and if i can show it to you so let me simply open another tab here and here if i'll simply do docker container or docker's ps we see that we have these two containers here so we have these two containers here firstly we have the nginx container and then we have this container here that is this container name here container id here that is h6c88 we see that we have this container id here that is sending back our response so we see that we have these two in node.js containers that start with 86c and we have e007 so if we look back at our response here we see firstly we are getting back our response from this container and then we are getting back up from response from this container so if i again do curl localhostport3000 we should be getting back from this container again and if i make a request once again we should be getting back our response from the second con container here like this and we are also seeing the lock statements here that is i am sending a response and it is coming because of because of a log statement that i provided inside our app.js file so here we can see that we are getting this thing here that is os.hostname which simply provides us the container id here so now what i can do i can simply scale these containers to as many containers as i want so let me simply go back to my terminal once again and let me close these windows here and now let me stop the application once again or the containers once again so now let me clear the screen once again so this time what i'm going to do i'm going to scale for 10 containers so if i do simply docker ps we see that we do not have any running containers and if i do docker ps hyphen a we see that we have this nginx latest we have this these two containers that were created on the previous go and now i am going to again use the scale flag to run up to 10 containers here so docker compose up so where is it so docker compose up scale api equal to 10 so if i do this this time we would be having 10 containers running so we see that we have these 10 containers running that is one two three four five six you can see the numbers here then they are all colored so let me open another tab here once again so if i do docker ps we see that we have in in all about 10 containers running here and you can verify it yourself i'm not going into too much detail about that thing so let me clear out and if i simply make a request here that is called local host port 3000 we get back response from first one container from second container from third container from fourth container and we see that we are getting back a response in a round robin approach and you can verify it yourself but the whole idea of this uh doing this was to show you that is how do you scale your nodejs application or in case any other any application using multiple docker containers so guys if you have liked the video do hit the like button now and if you haven't subscribed to the channel do subscribe to the channel so thank you bye tata take care and have a good day and have a great day you
Info
Channel: Mafia Codes
Views: 13,759
Rating: undefined out of 5
Keywords: yourstruly, how to scale docker container, how to use nginx as reverse proxy in docker, how to use Nodejs app behind a reverse proxy, scaling docker
Id: 9aOpRhm33oM
Channel Id: undefined
Length: 15min 26sec (926 seconds)
Published: Mon Jan 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.