Bobcares

Docker Swarm Load Balancing Nginx | All About

by | Jul 10, 2022

Docker Swarm Load Balancing Nginx enables a quick and simple load balancing setup with little configuration. Read further to find out more.

As part of our Docker Hosting Support Service, Bobcares responds to all inquiries, no matter how small or large they may be.

Let’s take a closer look at Docker Swarm Load Balancing Nginx.

Docker Swarm Load Balancing Nginx

In addition to making cluster scheduling effortless, Docker Swarm mode provides a simple method for publishing ports for services. Normally, containers can only be accessed via the IP addresses of their host machines, but in a swarm, every node takes part in an ingress routing mesh.

Swarm Mode Routing Mesh

By directing all incoming requests to available nodes hosting service with the published port, the internal networking mesh of the swarm enables every node in the cluster to accept connections to any service port published in the swarm. The ingress mesh communicates with the swarm’s nodes via the following ports.

  • Port 7946 TCP/UDP: Container network discovery.
  • Port 4789 UDP: Container ingress network.

When initializing the swarm, use the private network between the UpCloud servers to ensure the connections remain secure. However, when ports are published in the swarm, they are accessible via any node’s public IP.

Binding the published port to a specific IP address is not possible in Docker Engine 1.12.  So set up a firewall to block incoming connections to the swarm public IP addresses if we want to prevent direct access to the container ports.

How to deploy CoreOS nodes?

Let’s see the steps our Support team provides to deploy CoreOS nodes and enable Docker Swarm mode:

  • To begin, log into the user’s UpCloud control panel and deploy two CoreOS nodes for the Docker Swarm and one for the load balancer.
  • Then log in to each of them with SSH after all the three become available.
  • To enable Docker Swarm mode, we must update CoreOS to a version that includes Docker 1.12 or later. To update the nodes, continue with the below steps.
  • Change the GROUP from stable to alpha in the update configuration file.
  • Save the file. Then exit the editor before restarting the servers.
  • Even though CoreOS updates automatically, run the update manually.
  • When the update is complete, we need to restart the servers.
  • Check that the Docker is running the required version now that the CoreOS nodes have been updated.
  • Now proceed with configuring the swarm.

Backend Node Configuration

Configure Swarm mode on two of the three nodes by making one of them the cluster manager and then connecting the other to it. After the initialization is over, we will see a docker swarm command towards the end of the output.

To join the cluster, run the command on the second node with the swarm manager token and manager private IP. Then, with replicas for both nodes, start the web host service. Docker generates two backend replicas of the most latest nginx container and publishes them to an external port 8080.

Configuring the Load Balancer

A load balancer outside of the Swarm allows connecting the containers without worrying about the cluster nodes. The load balancer will be deployed on its own single node swarm for consistency. Now connect to the load balancer server via SSH and create a new swarm on it using the below command:

docker swarm init --advertise-addr

After that, create a default.conf file in a new directory to prepare the load balancer setup with the command:

sudo mkdir -p /data/loadbalancer
sudo vi /data/loadbalancer/default.conf

In the configuration file, enter the following server and upstream segments, replacing the node private IP> placeholders with the private IP addresses of the two swarm nodes hosting the web service.

server {
listen 80;
location / {
proxy_pass http://backend;
}
}
upstream backend {
server :8080;
server :8080;
}

Save the file and exit the editor. Then run the following command to publish the load balancer container to port 80 and mount the configuration directory in the container.

docker service create
--name loadbalancer
--mount type=bind,source=/data/loadbalancer,target=/etc/nginx/conf.d
--publish 80:80
nginx

In just a few seconds, the container will launch and enable connections to the web services that the Docker Swarm hosts. By entering the load balancer server’s public IP address in the web browser, we can test the load balancer. The default nginx welcome page will appear if everything is functioning properly.

[Seeking an answer to another question? We’re only a click away.]

Conclusion

The Docker Swarm mode enables a simple, quick, and minimally configured load balancing setup. Even though the ingress mesh already helps the swarm balance load to some extent, having an external load balancer makes it easy to expand the setup.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

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

Never again lose customers to poor
server speed! Let us help you.