Step by Step Basic Microservices System (3 NodeJS + 1 Load Balancer containers) with Docker Compose

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you will learn how to build a small micro-services system that has four services a single load balancer and a three identical node.js applications that we have built before in this feed in this channel if you're interested to know how this is done stay tuned if you new here welcome my name is Hasan and this general we discuss all sorts of software engineering by example so if you and become a better software engineer consider subscribing barkansas you get notified every time I upload a new video that's it let's just jump into micro-services okay so this application that you're seeing right now I'm gonna reference the code below guys I'm gonna stop building on this essentially it's a simple node.js application that I have doke arised and we have talked about how exactly we did that how do we can how we move this application into a docker container but in reference the video here there right I'm not gonna go through details it's a very simple application it's an Express application it takes an environment variable and some some rest endpoints and literally just echo back the application ID and it's very critical to know that so we can identify which micro services are we hitting okay so that's what's actually what it does yep and I'm listening always to port nine nine nine nine alright and in the docker file I am exposing that nine nine nine nine port okay and when you spin up a new docker container from that image that you have built from this docker container you can specify which port your host can map to okay and we start doing this all this goofy stuff right we spend like three of these images but every time we have to expose it to the host which is kind of ugly right because every to every communication is going through the host right just like you're mapping this stuff right so Wow actually we can build all of this stuff in its own bubble microservices stuff right because micro services are awesome right who doesn't want their application to be to run actually slower and become harder to debug right everybody want that right all right people are gonna get past all right oh yeah is that I'll save my opinions about microservices later a video but yeah yeah so what we need to do here essentially we're no longer need to expose the poor because guess what these will be completely isolated internal containers that we as an external entity have no access to what we will have access to is the load balancer that will load balanced those puppies okay and what load balancer we'll use what is the best load balancer available out there and wrong it's not in Jeanette it's a proxy okay again this is subjective but I like it a proxy better because it's just easier all right so let's get it started all right so I'm gonna remove that part I am going to change this thing so if you provide me when you spin up this container of this beautiful image the app ID becomes the port that we're gonna listen to so for example if I spin up this container with one one one one as a container as an application or ID that will be the listening port okay and it will just change that part so we'll just know that all right that's what I'm gonna do I spit up a bunch of stuff so that's the first change we're gonna do yeah once we do this change how about we actually build this puppy all right build that Coco contain how do we build docker images guys docker build - tea I don't know I still - tea right for the image name and it's called node app and then dot right and it's built so node app image is now this application that's a first part the second part is I have my application how about we actually build my H a proxy container in a proxy container obviously in order to build an H a proxy container you would need a configuration so how about we create a folder called H a proxy and then we inside this fall there we're gonna create a niche a proxy dot CFG which means a container the configuration for a proxy what is the configuration for a proxy let's do a front end that is HTTP let's call it and then bind on port 8080 because its finest right and let's made the mode is HTTP and let's do a timeout we already need time I'll do it but sure timeout a client timeout is 10 seconds bad by the way but sure use back and all and then we're gonna define a back and we're gonna call it all and then here's the beauty part how many services would we want to spin up how many micro services remember this the whole architecture will be spin up in its own bubble so it's gonna have it there horn how I P addresses their own host names and we're gonna craft the host names right here and then we're gonna build the actual micro services uses dark attic empowers which is an awesome tool so let's do that so server that's one the first server is gonna call it s1 and what is the host name we have no idea we're gonna make up names right now okay I'm gonna call it node app mm1 and it's listening on port 1 1 1 1 okay again we're assuming that because we need to build this thing we need three of those okay three of those s2 potato and it's 3 3 3 3 3 and this is the host name guys right so there will be a host name in the container that is called node app 1 and you won't say how do you do that we're gonna do that with docker compare words in a second right so now we have the configuration we have a proxy ready we have node.js the image ready everything is ready what's what's missing the docker compose itself so let's go ahead and create an order in this folder is actually on the parent foreign I always and this is always a problem of this docker composed of the ammo right get it drag it up right if anyone knows how we actually go back to the road because I have no idea right every time I go back I create a folder it's those crates and the subfolder no never mind so the cup compose is essentially a Yambol file that you give it to docker compose this this tool which is darker by the way you have to have a docker for this tutorial it goes without saying I mean and it takes that file and spin up everything in its own bubble of a network internal network and you can ask it to expose certain port and that's what we're interested in because guess what the load balancer which is a proxy that's the only thing that we need to see publicly me as the runner of this application so let's start this start with the version 3 that's the first thing you do I'm using version 3 the latest and greatest of them ok and now we're gonna have a bunch of services right and here's the cool part about this each service that you defined becomes a hostname becomes a container that has a hostname that you define so I'm gonna create a one service one container called LD right that's the first container that's the load balancer I need another container called node app one why this is the puppy node app one that's another container right I need another one another one app - did you call it app 3 look at that so we need this is these are my floor services 1 2 3 4 are these enough no sir because you gotta tell me this first container what it's config what the heck is it well it as an image I'll fetch your proxy right that's the public image of a cheap proxy Ike at least right and you need to tell me what Poor's do you want expose because that's the only Cybil container to the outside water so we're gonna do ports and then we'll do exactly like you're spinning a new container right I am going to use 80 80 80 80 right is that what we used yes we listened in the container and port 8080 so that's it and I want to expose right port 8080 on my house so you can change this to 80 for example of your fancy right but you just keep it I want to keep it just as it is the next thing is volumes and the volume here is very very interesting because H a proxy require the configuration which we wrote but how do we pass it along well we need to actually specify the path right well where is us which is dot / H a proxy that's my folder I want to map this folder to - this is fixed user local user local let's see H a proxy this guy which is moi this folder right which has this file maps to this which is a constant in the container sweet that will map it up that will pick up the configuration hopefully we don't have any errors it's gonna complain that we don't have time or timeouts but yeah sure about know adapt node up record an image and guess what our image is called node app that's exactly what we built remember guys okay well what else does it require guys it requires some environment variables right because remember we need to pass app ID which is AB Capital ID so we can listen to the palette right so to do that you do environment right and then you hit enter and then - literally just do wipe ID equal the environment variable so in this case one one one one so that's my first node app it's looking a listen to port where we want and it's gonna be identified at 1 1 1 1 so if I make a get request it's gonna say hey I am application ID 1 1 1 1 sweet guys how do I do the next well copy and paste copy and paste still the same under occasion but this guy is - - - - and this guy's same damage but this guy's 3333 and guess what just like that you're darker compose is ready to rumble let's do that so how do we actually build that docker compose well you go to terminal and you make sure you are in the same folder and then do docker - compose up if you run docker compose is gonna look for a file called docker compose dad yamo and it's again run the thing and look at that beautiful stuff look at this look at this it's good I was gonna get some error errors found in configuration fatal errors what is my errors well my configuration first of all I didn't map that the mode has to be the same mode that's the first thing for sure and we have also a typo back and all alright so so we fix their mode we fix the typo and then we go we all we have to do is essentially go here and it's always a good idea to do docker compose down so that means bring don't bring them services down and then you do docker compose up let's see do we get an error Oh looking good yeah it's complaining that we have timeout sure whatever now we're listening to port 1 1 1 1 2 2 2 2 3 3 3 these are floating containers and we have a beautiful edge a proxy load balancer that is loading a layer 7 associated playing at the layer 7 and just bouncing things how about we actually test that thing are we full of beers or actually this thing is working I'm gonna do a localhost iti T and then hit enter and look at that app 1 we got a result from app 1 and if we refresh right that will send out a quest or another J proxy that will essentially use the pool to swap to the upper container and then refresh another container and they also on 3 3 3 what what what algorithm is this called it's called round-robin you can use IP hash you can do sticky sessions all that fancy stuff how do I Jose um I have million requests I need to spin out another container how do we do that well no tired at all well sir we're gonna spin up another another one did you haul it another one right it's for we're gonna spin up another container well we added it to the config right it's for is now note app for go back to the Yamin and then sue cow P and god I hate ya mo because copying and pasting doesn't actually align correctly this is not app for that we do kill this thing and then when you kill it it's a good idea to do a detach but I like to actually see what's going on right when you stop this container is gonna turn tear down everything and then do docker compose down baby gonna down down down down down [Music] what year is this apparently this is not 2020 all right we go back and then refresh do we see four four four four sir yes we do yes we do do you see this guys is this clear enough for you do me to zoom further now all right that was docker containers in whatever minutes did we all right guys hi guys all right hope you're gonna have a nice beautiful weekend hope you enjoy this a little bit of a tutorial of microservices right I'm gonna reference the microservice videos there are pros and cons for microservices you don't want to do microservices all the time you need to understand what are you doin obviously check out that micro-services video but then I'll leave you there and see you in the next one the code will be available in the description below guys stay awesome
Info
Channel: Hussein Nasser
Views: 62,634
Rating: undefined out of 5
Keywords: microservices, microservices docker, microservices node, microservices load balancer
Id: 9sAg7RooEDc
Channel Id: undefined
Length: 15min 14sec (914 seconds)
Published: Fri Mar 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.