You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SSH keys provide a secure connection without the need to enter username and password every time. For GitHub repositories, the latter approach is used with HTTPS URLs (e.g., `https://github.com/azat-co/rpjs.git`) and the former with SSH URLs (e.g., `[email protected]:azat-co/rpjs.git`).
Copy file name to clipboardExpand all lines: chapter15/chapter15.md
+36-32Lines changed: 36 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -16,11 +16,20 @@ There's a fancy term in computer science which will make you look smart (if you
16
16
17
17
As with many tech concepts, microservices technology goes through the cycle of over hype. There are advantages and disadvantages. Uber for example has over 2500 microservices and its engineers hate it cause of complexity and other issue of managing so many separate apps. Hate it or love it, the best thing is to know how to use it and use microservices when you see a fit. Again, Node is brilliant at that cause it's light weight, fast and because most developers are lazy to learn or code in a normal server-side language like Go or Java.
18
18
19
-
Before doing the exercise in this chapter, make sure you have the following:
19
+
The project of creating microservice in container and deploying it to cloud is divided into three parts:
20
+
21
+
1. Creating a local Node project which is a microservice RESTful API that connects to MongoDB
22
+
1. Dockerizing Node project, i.e., turning a local project into a Docker image
23
+
1. Use Docker networks for multi-container setup
24
+
1. Deploying Docker microservice image to cloud namely Amazon Web Services Elastic Container Service
25
+
26
+
# Installing Installations
27
+
28
+
But before doing the exercise in this chapter, make sure you have the following:
Python at least 2.6.5 or 3.x (recommended), see here: <http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html>. At <https://www.python.org/downloads/> you can download Python for your OS.
126
135
127
136
128
-
### Other AWS CLI Installations
137
+
There are a few other AWS CLI Installation options:
129
138
130
139
*[Install the AWS CLI with Homebrew](http://docs.aws.amazon.com/cli/latest/userguide/cli-install-macos.html#awscli-install-osx-homebrew) - for macOS
131
140
*[Install the AWS CLI Using the Bundled Installer (Linux, macOS, or Unix)](http://docs.aws.amazon.com/cli/latest/userguide/awscli-install-bundle.html) - just download, unzip and execute
132
141
133
-
### Verify AWS CLI
134
-
135
-
Run the following command to verify AWS CLI installation and its version (1+ is ok):
142
+
You might be wondering how to verify AWS CLI installation. Run the following command to verify AWS CLI installation and its version (1+ is ok):
136
143
137
144
```bash
138
145
aws --version
@@ -142,13 +149,8 @@ aws --version
142
149
143
150
Before deploying anything in the cloud let's build Node app Docker images locally. Then run container in development and production modes locally as well. When you finish this project, you will get 🍍.
144
151
145
-
The project is divided into three parts:
146
152
147
-
1. Create Node project
148
-
1. Dockerize Node project
149
-
1. Use Docker networks for multi-container setup
150
-
151
-
## 1. Create/Copy Node project
153
+
## 1. Creating/Copying the Node Project
152
154
153
155
Firstly, you need to have the application code itself before you can containerize anything. Of course, you can copy the existing code from code/banking-api but it's better for learning to create the project from scratch.
154
156
@@ -308,9 +310,7 @@ CMD ["npm", "start"]
308
310
309
311
Next we will learn what these statements mandate Docker to do.
310
312
311
-
## Create app directory
312
-
313
-
The next "commands" in your Dockerfile will tell Docker to create a folder and the to set up a default folder for subsequent commands:
313
+
Firstly, create app directory in the docker container. So the next "commands" in your Dockerfile will tell Docker to create a folder and the to set up a default folder for subsequent commands:
314
314
315
315
```
316
316
# Create api directory
@@ -344,7 +344,7 @@ CMD [ "npm", "start" ]
344
344
345
345
By now the Dockerfile, which is a blueprint for your Node microservice, is ready. The code for the microservice is ready too. It's REST API with Express.
346
346
347
-
## Build and Verify Container
347
+
Next, building, running and verifying the container.
348
348
349
349
Build the image from the banking-api folder where you should have Dockerfile and the `api` folder:
350
350
@@ -401,6 +401,7 @@ Put the IP in the environment variable in the run command for the Docker build o
Hit save in your editor on your host and boom. You'll see the changes in the response from the app container:
424
+
Hit save in your editor on your host and boom! You'll see the change in the response from the app container. The change is the `a:1` response instead of the empty response `[]` as before. This means that the code *in the container* is changing because of the volume and the changes *in the host*. See what I have here as the CURL request and microservice's response:
424
425
425
426
```
426
427
curl localhost/accounts
427
428
[{"a":1}]%
428
429
```
429
430
430
-
To stop the container, run
431
+
To stop the container, simply run the `stop` command with the container name which you specified when you run the `docker run` command. Here's the stop command for the `banking-api` name:
431
432
432
433
```
433
434
docker stop banking-api
@@ -544,8 +545,7 @@ Using the similar approach, you can launch other apps and services into the same
544
545
545
546
Note: The older `--link` flag/option is deprecated. Don't use it. See <https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks>
546
547
547
-
548
-
## Troubleshooting
548
+
Let me share some of the common issues and their solutions for easy and effortless troubleshooting. Here's the top list:
549
549
550
550
* No response: Check that the port is mapped in the `docker run` command with `-p`. It's not enough to just have `EXPOSE` in Dockerfile. Developers need to have both.
551
551
* The server hasn't updated after my code change: Make sure you mount a volume with `-v`. You don't want to do it for production though.
@@ -554,9 +554,9 @@ Note: The older `--link` flag/option is deprecated. Don't use it. See <https://d
554
554
555
555
# Node Containers in AWS with EC2 ECS
556
556
557
-
For the next topics, we will learn how to deploy Node microservices into cloud. ☁️
557
+
For the next topics, we will learn how to deploy Node microservices into cloud. ☁️ When you are done, write me a post card and send it with a pigeon.
558
558
559
-
Deploy two containers (API and DB) which connect using ECR and EC2 ECS is achieved with the following steps:
559
+
The goal is to deploy two containers (API and DB) which connect using ECR and EC2 ECS is achieved with the following steps:
560
560
561
561
1. Create registry (ECR)
562
562
1. Upload the app image to ECR
@@ -690,7 +690,7 @@ Go to the Task Definitions in EC2 ECS and as you might guess, press on the butto
690
690
691
691

692
692
693
-
## Main Task settings for the example
693
+
###Main Task settings for the example
694
694
695
695
Use the following settings for the task to make sure your project is running (because some other values might make the project nonfunctional):
696
696
@@ -701,7 +701,7 @@ Use the following settings for the task to make sure your project is running (be
701
701
702
702
Let's define the first container — app.
703
703
704
-
## First container—App
704
+
###First container—App
705
705
706
706
Enter the name: banking-api-container.
707
707
@@ -734,7 +734,7 @@ See the screengrab below:
734
734

735
735
736
736
737
-
## Second container—Database
737
+
###Second container—Database
738
738
739
739
Analogous to the previous container, define name and URL with these values:
740
740
@@ -781,7 +781,7 @@ ECS creates a lot of EC2 resources for you such as Internet Gateway, VPC, securi
781
781

782
782
783
783
784
-
## 4. Create Service and Verify
784
+
## 4. Creating Cloud Container Service and Verifying
785
785
786
786
The last step is to create a service which will take the task and the cluster and make the containers in the task run in the specified cluster. It's oversimplified explanation because service will do more such as monitor health and restart containers.
787
787
@@ -790,26 +790,30 @@ Go to Create Services which is under Task Definition -> banking-api-task -> Acti
790
790

791
791
792
792
793
-
## Everything is ready
793
+
###Everything is ready
794
794
795
795
Phew. Everything should be ready by now. To verify, we need to grab a public IP or public DNS. To do so, click Clusters -> banking-api-cluster (cluster name) -> ESC Instances (tab) and Container instance:
796
796
797
797

798
798
799
799
Copy public IP or DNS 📝. We will need it for testing.
800
800
801
-
## Dynamic Test
801
+
###Dynamic Test
802
802
803
803
To test the dynamic content (content generated by the app with the help of a database), open in browser with `{PUBLIC_DNS}/accounts`. Most likely the response will be `[]` because the database is empty but that's a good response. The server is working and can connect to the database from a different container.
804
804
805
805
806
-
## Static Test
806
+
###Static Test
807
807
808
808
To test the static content such as an image which was downloaded from the Internet by Docker (ADD in Dockerfile) and baked into the image, navigate to http://{PUBLIC_DNS}/node-university-logo.png to see the images with Docker downloaded via `ADD`. Using ADD, you can fetch any data such as HTTPS certificates (from a private S3 for example).
809
809
810
-
## Terminate Service and Cluster/Instances
810
+
## 5. Terminate Service and Cluster/Instances
811
811
812
-
Don't forget to terminate your service and instances. You can do it from the web console.
812
+
Don't forget to terminate your service and instances. Otherwise you will be stil paying dinero for running those could resources. (I am sure you can find a better way to spend the money. For example, buy some DOGECOIN.) You can terminate resources from the AWS web console. Do so for ECS first. Make sure you remove tasks.
813
813
814
814
Summary
815
815
=======
816
+
817
+
Microservices is an old concepts when you think about it as decoupling and loose coupling. The less functionality you pack into an application, the more flexible and easier it will be to scale different parts of the app(s) or to maintain them (make changes). There are certain downsides of microservices as well. Uber engineers are crying because they have 2,500+ microservices with all the overhead involved in managing their environments, provisioning, patches and deployments. Luckily, containers and cloud services such as Docker and AWS ECS are here to help in reducing this complexity and management of microservices.
818
+
819
+
In this chapter you've built your own microservices which connect to another service (MongoDB) both locally and in the cloud. You used Docker by the way of making an image. What's great about this dockerization is that your project is extremely portable. It's mostly independent of OS or any other discrepancies which often can bite a developer in a tail.
0 commit comments