• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Friday, May 9, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Linux

Load Balance with Docker

How VPS by How VPS
January 1, 2020
in Linux
0
0
SHARES
28
VIEWS
Share on FacebookShare on Twitter

Contents

  1. Step 1: Create a simple application
  2. Step 2: Create Docker containers
  3. Step 3: Configure Nginx
  4. Step 4: Deploy application
  5. Want to contribute?


When running a web application, you normally want to make the most out of your resources without having to convert your software to use multithreading or complex event loops. Docker, however, does provide a simple way for you to load balance your application internally to make the most out of server resources. This article will show you how to use Nginx to load balance your web application using Docker on CentOS.

Step 1: Create a simple application

We will be using rust to build this simple application. Assuming that you have rust installed, run cargo new webapp –bin. Upon success, you will see a directory called webapp. Inside of webapp, you will see a file called Cargo.toml. Append the following lines to it:

[dependencies.iron]
version = "*"

Next, inside the src/main.rs file, remove everything and populate it with the following:

extern crate iron;

use iron::prelude::*;
use iron::status;

fn main() {
    Iron::new(|_: &mut Request| {
        Ok(Response::with((status::Ok, "Hello Vultr :)")))
    }).http("0.0.0.0:3000").unwrap();
}

Note: Do not change the IP within the application. This is configured so that Docker can listen to your application.

Once you have finished, compile the application by executing cargo build –release. Depending on your server, it may take a few minutes. If there are no errors, test the application by following these steps:

  • Run target/release/webapp.
  • Navigate to http://0.0.0.0:3000/ in your browser. Replace 0.0.0.0 with the IP address of your server.

If everything worked properly, you will see “Hello Vultr :)” on the page.

Step 2: Create Docker containers

Create a Dockerfile and populate it with the following:

FROM centos:latest
MAINTAINER User <user@localhost>
RUN yum update -y
COPY ./webapp/target/release/webapp /opt/
EXPOSE 3000
WORKDIR /opt
CMD ./webapp

Save the file. Then create a file called deploy.sh and populate it with the following:

DEFAULT_PORT=45710
APP_PORT=3000
DEPLOY=5
NAME="webapp"
docker build -t webapp:example . 

for ((i=0; i<DEPLOY; i++)); do
        docker kill $NAME$i ; docker rm $NAME$i
        docker run --name $NAME$i -p 127.0.0.1:$(((i * 1000) + DEFAULT_PORT)):$APP_PORT -d webapp:example
done

When you run this script, it will build the image and deploy the container based on the amount you have set (default is 5). If the container exists, it will kill and remove it from registry before it is deployed again.

Step 3: Configure Nginx

Now, create an Nginx configuration file and populate it with the following:

upstream application {
    server localhost:45710;
    server localhost:46710;
    server localhost:47710;
    server localhost:48710;
    server localhost:49710;
}

server {
    listen 0.0.0.0:80;    
    location / {
    expires 1w;
        proxy_pass http://application;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Replace 0.0.0.0 with the IP address of your server.

Restart Nginx by doing systemctl restart nginx. Resolve any errors, then proceed to the next step.

Step 4: Deploy application

Deploy the application by running bash ./deploy.sh.

You can check the status of your application with docker ps – there will be 5 images created that start with webapp. Now, navigate to http://0.0.0.0:3000/ in your browser, you will see the “Hello, Vultr :)” message again.

So, what difference does this make, exactly?

If you run a benchmark test against the load balancer configuration, you would notice that more of your server resources are being used, which is what you would want, especially if your application is built in languages like Node where it would normally be single threaded. If you ever need to upgrade your application, you can do so and rerun the deploy.sh to rebuild the image and deploy your containers.

Want to contribute?

You could earn up to $300 by adding new articles

Submit your article
Suggest an update
Request an article
Previous Post

Setup a TeamTalk Server on Linux

Next Post

Setup RethinkDB Cluster On Ubuntu 14

Next Post

Setup RethinkDB Cluster On Ubuntu 14

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Thabet