How to get started with load balancing Docker Swarm mode (2024)

Docker Swarm mode not only makes cluster orchestration a breeze, but it also offers an easy way to publish portsfor services.Usually, containers are only accessible through their host machine’s IP addresses but in a swarm, all nodes participate in an ingress routing mesh.

Try UpCloud for free!

Swarm mode routing mesh

The swarm internal networking mesh allows every node in the cluster to accept connections to any service port published in the swarm by routing all incoming requests to available nodes hosting service with the published port.

The ingress mesh uses the following ports in communication between nodes in the swarm.

  • Port 7946 TCP/UDP for container network discovery.
  • Port 4789 UDP for the container ingress network.

Using the private network betweenyour UpCloud servers when initializing the swarm ensures the connections will remain secure. However, when publishing ports in the swarm, as mentionedbefore, they are accessible through any node’s public IP. In Docker Engine 1.12, it is not possible to bind the published port to a specific IP address. If you wish to prevent direct access to the container ports, set up a firewall blocking the incoming connections to the swarm public IP addresses.

Deploying CoreOS nodes

Start off by logging into your UpCloud control panel and deploying two CoreOS nodes for the Docker Swarm and a third node for the load balancer. If you are not familiar with deploying CoreOS nodes for Docker, take a look at our introductory guide toDocker Swarm Orchestrationfor a quick start guide.

Once you have the three nodes online, log into each of them with SSH.

To enable Docker Swarm mode, you will need to update CoreOS to a version that includes Docker 1.12 or newer. You can find the version numbers in the CoreOS release notes. At the time of the update, the required Docker version was only available on the CoreOS Alpha channel. Perform the following steps on all three of your nodes to update them.

Edit the updated configuration file and change the GROUPfrom stable to alpha.

sudo vi /etc/coreos/update.confGROUP=alpha

Save the file and exit the editor, then reboot the servers.

sudo reboot

Although CoreOS usually performs updates automatically in the background, use the following command to run the update manually.

sudo update_engine_client -update...Update succeeded -- reboot needed.

When the update finishes, it will prompt you to restart the servers.

sudo reboot...CoreOS alpha (1192.1.0)

Withthe CoreOS nodes updated, verify that the Docker is running the required version.

docker -vDocker version 1.12.1, build 7a86f89

You can then continue with configuring the swarm itself.

Configuring the backend nodes

Enable the Swarm mode for two of the three nodes by initializing one of them as the cluster manager and then joining the second node to it. Run the command below on the node you wish to assign as the Swarm manager.

docker swarm init --advertise-addr<manager private IP>

Once the initialization is complete, you will see a docker swarm command towards the end of the output similar to the one below.

docker swarm join --token <SWMTKN token> <manager private IP>

Run the command with your swarm manager token and manager private IP on the second node to join it to the cluster.

You can see the nodes in your cluster with the command below.

docker node lsID HOSTNAME STATUS AVAILABILITY MANAGER STATUS3h39l1ex8c1i5oo2xn4qnwkj6 * node0 Ready Active Leader4bdtzjfa0qw799e6npb4dxvl1 node1 Ready Active

Then start the web host service with replicas for both nodes.

docker service create --name backend --replicas 2 --publish 8080:80 nginx

A short explanation of the command above. Docker creates two replicas of the latest nginx container namedbackend and publishes them to an external port 8080.

Setting up the load balancer

Using a load balancer outsideof the Swarm allows an easy way to connect to your containers without having to worry about the cluster nodes. As all of the published services are available through any of the swarm nodes thanks to the ingress routing, the load balancer can be set to use the swarm private IP addresses without concern of which node is hosting what service.

For consistency, the load balancer will be deployed on its own single-node swarm. Open an SSH connection to your load balancer server and initialize a new swarm on it.

docker swarm init --advertise-addr<load balancer private IP>

Next, prepare the load balancer setup by creating a default.conf file in a new directory.

sudomkdir-p /data/loadbalancersudo vi /data/loadbalancer/default.conf

Enter the following server and upstream segments in the configuration file and replace the <node private IP> placeholders with the private IP addresses of the two swarm nodes hosting your web service.

server { listen 80; location / { proxy_pass http://backend; }}upstream backend { server <node0 private IP>:8080; server <node1 private IP>:8080;}

Save the file and exit the editor.

Then create the load balancer container using the following command to publish it 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

The container will start in a matter of seconds and allow connections to the web services hosted by your Docker Swarm. You can test the load balancer by opening the load balancer server’s public IP address in your web browser.

How to get started with load balancing Docker Swarm mode (1)

If everything is running correctly, you will be greeted by the default nginx welcome page.

Summary

The Docker Swarm mode allows an easy and fast load-balancing setup with minimal configuration. Even though the swarm itself already performs a level of load balancing with the ingress mesh, having an external load balancer makes the setup simple to expand upon.

If you wish to further improve upon the redundancy of your web cluster, you should look into setting up a floating IP address between multiple load balancer servers. Follow this article tofind out more about the floating IPs on UpCloud.

How to get started with load balancing Docker Swarm mode (2024)

FAQs

Does Docker Swarm do load balancing? ›

The Docker Swarm mode allows an easy and fast load-balancing setup with minimal configuration. Even though the swarm itself already performs a level of load balancing with the ingress mesh, having an external load balancer makes the setup simple to expand upon.

How to do load balancing in Docker? ›

There are several ways to implement load balancing in Docker environments. The simplest method is to use Docker's built-in load balancing capabilities provided by Docker Swarm Mode. However, this method may not offer the level of control and flexibility required for complex applications or large-scale deployments.

Is Docker Swarm outdated? ›

There are two versions of swarm, an old one that is deprecated and not maintained, and a newer one that is maintained and works quite well. Unfortunately, the docker company named them both the same thing and the official docs still refer to both without clarifying which one they mean.

How do I enable swarm mode in Docker? ›

When you run the command to create a swarm, Docker Engine starts running in Swarm mode. Run docker swarm init to create a single-node swarm on the current node. The engine sets up the swarm as follows: Switches the current node into Swarm mode.

What are the disadvantages of docker Swarm? ›

Disadvantages of Docker Swarm:
  • Not suitable for complex infrastructures.
  • Customization in Docker Swarm is limited.
  • Due to the tie-in with the Docker API, the functionality of this platform is limited.
  • Community is smaller as compared to other platforms.
Sep 12, 2022

What is the difference between docker swarm and swarm mode? ›

Docker swarm is a service which allows users to create and manage a cluster of docker nodes and schedule container. Each node in docker swarm is a docker daemon and docker daemon interact using docker API. Swarm Mode: When we create a cluster of one or more Docker Engines its called a swarm mode.

How do I set up load balancing? ›

Start your configuration
  1. In the Google Cloud console, go to the Load balancing page. ...
  2. Click Create load balancer.
  3. For Type of load balancer, select Application Load Balancer (HTTP/HTTPS) and click Next.
  4. For Public facing or internal, select Public facing (external) and click Next.

What are the two types of Docker swarm services? ›

Swarm mode has two types of services: replicated and global. For replicated services, you specify the number of replica tasks for the swarm manager to schedule onto available nodes.

What is the advantage of Docker Swarm? ›

Docker Swarm provides high availability as you can easily duplicate the microservices within it. Although it doesn't provide automatic scaling, Docker Swarm does have a faster deployment time. Kubernetes is by nature highly available, fault tolerant, and self-healing.

Why we use Kubernetes instead of Docker Swarm? ›

Overall, both Kubernetes and Docker Swarm provide horizontal scaling capabilities and can scale across multiple nodes. However, Kubernetes offers more advanced scaling tools, including HPA, Cluster Autoscaler, and VPA, making it more suitable for complex and demanding workloads.

What is the future of Docker Swarm? ›

Well, the future of Docker Swarm is not clear. Although Docker Swarm is part of docker-ce distribution, Mirantis (owner of Docker Enterprise since November 2019) said that their main orchestrator would be Kubernetes. The primary orchestrator going forward is Kubernetes.

When to use Docker Swarm? ›

We can use Docker Swarm to make Docker work across multiple nodes, allowing them to share containers with each other. It's an environment where you can have various Docker images running on the same host operating system.

Does docker Swarm have load balancing? ›

This Docker Swarm load balancer will run on every node and will balance load requests as required. Four services are created within the Docker Swarm. A Master Load Balancer service to enable load balancing and the three other services are Microservices for the implemented scenario for a Big Data application.

Is docker Swarm Mode free? ›

Yes you can use docker swarm in production without paying anything. You have to pay to use the following: Docker Trusted Registry - DTR. Universal Control Pane - UCP.

What ports are needed for a docker swarm? ›

Open protocols and ports between the hosts
  • Port 2377 TCP for communication with and between manager nodes.
  • Port 7946 TCP/UDP for overlay network node discovery.
  • Port 4789 UDP (configurable) for overlay network traffic.

What does a docker swarm do? ›

Docker Swarm is a container orchestration tool for clustering and scheduling Docker containers. With Swarm, IT administrators and developers can establish and manage a cluster of Docker nodes as a single virtual system. Docker Swarm lets developers join multiple physical or virtual machines into a cluster.

What is the advantage of docker Swarm? ›

Docker Swarm provides high availability as you can easily duplicate the microservices within it. Although it doesn't provide automatic scaling, Docker Swarm does have a faster deployment time. Kubernetes is by nature highly available, fault tolerant, and self-healing.

Can docker Swarm do auto scaling? ›

Docker Swarm is a platform that simplifies the management of Docker hosts by creating a swarm cluster and using a distributed load-balancing algorithm to distribute traffic across nodes in the cluster. It also allows for automatic scaling based on traffic load.

Does a Kubernetes service do load balancing? ›

In other words, Kubernetes services are themselves the crudest form of load balancing traffic. In Kubernetes the most basic type of load balancing is load distribution. Kubernetes uses two methods of load distribution. Both of them are easy to implement at the dispatch level and operate through the kube-proxy feature.

Top Articles
Latest Posts
Article information

Author: Greg Kuvalis

Last Updated:

Views: 5563

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.