Skip to content

Commit 4c607b1

Browse files
authored
Merge pull request #219 from Lemoncode/feature/docker-compose-updates
Feature/docker compose updates
2 parents 5641363 + fb2fa1b commit 4c607b1

File tree

115 files changed

+31636
-27133
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+31636
-27133
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM nginx
2+
3+
ADD ./nginx.conf /etc/nginx/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
user root;
2+
error_log /var/log/nginx/error.log;
3+
pid /var/run/nginx.pid;
4+
events {
5+
worker_connections 1024;
6+
use epoll;
7+
}
8+
http {
9+
upstream nodeapp {
10+
server host.docker.internal:8081;
11+
server host.docker.internal:8082;
12+
server host.docker.internal:8088;
13+
}
14+
server {
15+
server_name localhost;
16+
listen 80;
17+
error_log /var/log/nginx/errorhttp.log;
18+
access_log /var/log/nginx/accesshttp.log;
19+
location / {
20+
proxy_pass http://nodeapp;
21+
}
22+
}
23+
}
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
## Load Balancer
2+
3+
Contruimos nuestra aplicación:
4+
5+
```bash
6+
cd server
7+
```
8+
9+
```bash
10+
docker build -t myapp .
11+
```
12+
13+
Ahora podemos ejecutar nuestra aplicación:
14+
15+
```bash
16+
docker run -d --rm -p 8080:8080 --name myapp myapp
17+
```
18+
19+
We can make a simple load testing by
20+
21+
```bash
22+
ab -n 1000 -c 100 http://localhost:8080/
23+
```
24+
25+
```bash
26+
Requests per second: 273.68 [#/sec] (mean)
27+
Time per request: 365.390 [ms] (mean)
28+
Time per request: 3.654 [ms] (mean, across all concurrent requests)
29+
Transfer rate: 56.39 [Kbytes/sec] received
30+
31+
Connection Times (ms)
32+
min mean[+/-sd] median max
33+
Connect: 0 1 1.5 0 7
34+
Processing: 55 354 67.9 339 492
35+
Waiting: 33 288 68.4 284 459
36+
Total: 55 354 67.6 339 492
37+
38+
Percentage of the requests served within a certain time (ms)
39+
50% 339
40+
66% 385
41+
75% 407
42+
80% 425
43+
90% 448
44+
95% 464
45+
98% 472
46+
99% 480
47+
100% 492 (longest request)
48+
```
49+
50+
Para escalar el número de peticiones vamos a utilizar un balanceador de carga.
51+
52+
Create __load-balancer/nginx.conf__
53+
54+
```
55+
user root;
56+
error_log /var/log/nginx/error.log;
57+
pid /var/run/nginx.pid;
58+
events {
59+
worker_connections 1024;
60+
use epoll;
61+
}
62+
http {
63+
upstream nodeapp {
64+
server localhost:8081;
65+
server localhost:8082;
66+
server localhost:8088;
67+
}
68+
server {
69+
server_name localhost;
70+
listen 80;
71+
error_log /var/log/nginx/errorhttp.log;
72+
access_log /var/log/nginx/accesshttp.log;
73+
location / {
74+
proxy_pass http://nodeapp;
75+
}
76+
}
77+
}
78+
```
79+
80+
Create __load-balancer/Dockerfile__
81+
82+
```Dockerfile
83+
FROM nginx
84+
85+
ADD ./nginx.conf /etc/nginx/
86+
```
87+
88+
Create multiple instances
89+
90+
```bash
91+
docker build -t myapp .
92+
docker run -d --rm -p 8081:8080 myapp
93+
docker run -d --rm -p 8082:8080 myapp
94+
docker run -d --rm -p 8083:8080 myapp
95+
```
96+
97+
We can check that the apps are up and running
98+
99+
```
100+
curl http://localhost:8081
101+
curl http://localhost:8082
102+
curl http: //localhost:8083
103+
```
104+
105+
Now we can build our load balancer
106+
107+
```bash
108+
cd load-balancer
109+
110+
docker build -t myloadbalancer .
111+
```
112+
113+
And run the balancer as follows
114+
115+
```bash
116+
docker run -d --net=host --name mylb myloadbalancer
117+
```
118+
119+
Check the load balancer
120+
121+
```bash
122+
curl http://localhost/
123+
```
124+
125+
Let's do another load test
126+
127+
```bash
128+
ab -n 1000 -c 100 http://localhost/
129+
```
130+
131+
```bash
132+
Requests per second: 2405.65 [#/sec] (mean)
133+
Time per request: 41.569 [ms] (mean)
134+
Time per request: 0.416 [ms] (mean*)
135+
136+
min mean[+/-sd] median max
137+
Connect: 0 0 0.4 0 2
138+
Processing: 6 39 11.2 38 88
139+
Waiting: 6 39 11.2 38 88
140+
Total: 8 40 11.2 38 89
141+
142+
Percentage of the requests served within a certain time (ms)
143+
50% 38
144+
66% 43
145+
75% 45
146+
80% 46
147+
90% 53
148+
95% 65
149+
98% 70
150+
99% 75
151+
100% 89 (longest request)
152+
```
153+
154+
## macos
155+
156+
- https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host
157+
158+
Refactorizamos `load-balancer/nginx.conf`
159+
160+
```diff
161+
http {
162+
upstream nodeapp {
163+
- server localhost:8081;
164+
- server localhost:8082;
165+
- server localhost:8088;
166+
+ server host.docker.internal:8081;
167+
+ server host.docker.internal:8082;
168+
+ server host.docker.internal:8088;
169+
}
170+
server {
171+
server_name localhost;
172+
listen 80;
173+
error_log /var/log/nginx/errorhttp.log;
174+
access_log /var/log/nginx/accesshttp.log;
175+
location / {
176+
proxy_pass http://nodeapp;
177+
}
178+
}
179+
}
180+
```
181+
182+
```bash
183+
docker kill mylb
184+
```
185+
186+
```bash
187+
docker rm mylb
188+
```
189+
190+
```bash
191+
cd load-balancer
192+
193+
docker build -t myloadbalancer .
194+
```
195+
196+
```bash
197+
docker run -d -p 80:80 --name mylb myloadbalancer
198+
```
199+
200+
```bash
201+
ab -n 1000 -c 100 http://localhost/
202+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM node:22-alpine
2+
3+
WORKDIR /opt/app
4+
5+
COPY index.js .
6+
COPY package.json .
7+
COPY package-lock.json .
8+
9+
RUN npm install --only=production
10+
11+
EXPOSE 8080
12+
13+
CMD [ "npm", "start" ]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const express = require('express');
2+
3+
const PORT = 8080;
4+
5+
const app = express();
6+
7+
app.get('/', (req, res) => {
8+
console.log(req);
9+
res.send('Hello world\n');
10+
});
11+
12+
app.listen(PORT);
13+
14+
console.log(`Running on port: ${PORT}`);
15+

0 commit comments

Comments
 (0)