How VPS - How to use/setup VPS
  • 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
25
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 <[email protected]>
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
How VPS

How VPS

Related Posts

How to Install Apache Tomcat 8 on CentOS 7
Linux

How to Install Apache Tomcat 8 on CentOS 7?

February 11, 2020
Linux

Setup IonCube Loader on Ubuntu 14

January 1, 2020
Linux

Converting from MySQL to MariaDB on Ubuntu

January 1, 2020
Next Post

Setup RethinkDB Cluster On Ubuntu 14

Installing Gentoo Linux on a Vultr Server

Installing Ruby on Rails on Ubuntu 14.04

Leave a Reply Cancel reply

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

Follow Us

  • 121 Followers
  • 87.2k Followers

Recommended

How to Install Proxmox on Debian Jessie

3 years ago

How to Install and Configure Memcached on CentOS 7

3 years ago

10 Useful Linux Command Line Tricks for Newbies – Part 2

4 years ago

How to Install Cuberite on a CentOS 6 or 7 Server

3 years ago

Instagram

    Please install/update and activate JNews Instagram plugin.

Categories

  • 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

Topics

Apache Web Server Bluehost Review 2019 Bluehost Review 2020 Bluehost Review 2021 Centmin Mod CentminMod centos install htop fsck htop install HTTP DoS attack Install Snort on an Ubuntu install Zabbix on CentOS install Zabbix on CentOS 7 Linux Commands linux guide linux install htop linux vps setup guide MariaDB MariaDB Error Mysql mysqld error optimize MariaDB optimize Mysql snort Ubuntu
No Result
View All Result

Highlights

Top Free Web Hosting Control Panels To Manage VPS/Dedicated Servers

Webmin Reviews

Virtualmin Reviews

CentOS Web Panel Reviews

Ajenti Reviews

ISPConfig Reviews

Trending

Failed to download metadata for repo 'appstream' on Centos 8
CentOS

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

by How VPS
February 25, 2022
0

I tried to update some extensions by use yum on centOs which I specified in Dockerfile. After...

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
Top Free Web Hosting Control Panels To Manage VPS/Dedicated Servers

Top Free Web Hosting Control Panels To Manage VPS/Dedicated Servers

February 17, 2020
Webmin Reviews

Webmin Reviews

February 17, 2020
How VPS – How to use/setup VPS

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Visit our landing page to see all features & demos.
LEARN MORE »

Recent News

  • 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”? November 17, 2020
  • How to optimize Mysql or MariaDB November 3, 2020

Categories

  • 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

[mc4wp_form]

© 2018 JNews - City News Magazine WordPress theme. All rights belong to their respective owners.
JNews is a top selling 2018 WordPress News, Blog, Newspaper & Magazine Theme.

No Result
View All Result
  • Home

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