For running container :
sudo docker run -p 8080:8080 -p 8081:8081 -p 8082:8082 -p 8083:8083 -p 8084:8084 lbalancer-app
This example demonstrated how to use Nginx to load balance traffic. Provision n backend servers running the Node.js application (running multiple instances with a service monitor like forever). I am used a predefined number of backend servers. After running the container, you can check Nginx’s load balancing by running: curl http://localhost:8080 You can check if all four Node.js instances are running using forever list: forever list (output should be like:
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] 4Ov0 /usr/bin/node /app/app.js 8081 18 32 /root/.forever/4Ov0.log 0:0:0:45.477
data: [1] apag /usr/bin/node /app/app.js 8082 43 57 /root/.forever/apag.log 0:0:0:45.252
data: [2] Cvz9 /usr/bin/node /app/app.js 8083 67 82 /root/.forever/Cvz9.log 0:0:0:45.074
data: [3] 2ir0 /usr/bin/node /app/app.js 8084 93 102 /root/.forever/2ir0.log 0:0:0:44.921
) and, finally you can test it by : autocannon -c 100 -d 10 http://localhost:8080 (at first you need to install autocannon by npm install -g autocannon), for this purpose Node.js running simple testing app.js files :
import { createServer } from 'http'
const { pid } = process
const server = createServer((req, res) => { let i = 1e7; while (i > 0) { i-- }
console.log(Handling request from ${pid})
res.end(Hello from ${pid}\n)
})
const port = Number.parseInt( process.env.PORT || process.argv[2] ) || 8080
server.listen(port, () => console.log(Started at ${pid}))
Content type
Image
Digest
sha256:6b96916e8…
Size
128 MB
Last updated
almost 2 years ago
docker pull artemr87/lbalancer-app