{"id":2341,"date":"2016-03-17T11:59:30","date_gmt":"2016-03-17T09:59:30","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/how-to-configure-docker-swarm\/"},"modified":"2026-04-23T14:16:26","modified_gmt":"2026-04-23T13:16:26","slug":"how-to-configure-docker-swarm","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/how-to-configure-docker-swarm\/","title":{"rendered":"How to configure Docker Swarm"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Distributing your web application over a cluster of cloud computing resources can significantly improve performance and availability. Docker Swarm is the Docker native clustering solution, which can turn a group of distributed Docker hosts into a single large virtual server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Docker Swarm provides a standard Docker API and it can communicate with any tool that already works with Docker daemon allowing easy scaling to multiple hosts. With resources pooled in a Swarm cluster, your application can run as if it was installed on a <a href=\"https:\/\/upcloud.com\/global\/docs\/cloud-servers\/\">high-performance cloud server<\/a> while allowing easy scaling by adding or removing resources at the same time. This guide goes through the steps for setting up a simple Docker Swarm consisting of three servers \u2013 a primary manager and two worker nodes.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/signup.upcloud.com\/\">Try UpCloud for free!<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Deploy&nbsp;your&nbsp;cloud servers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To start with, you are going to need to deploy some servers to run the Swarm. In this guide, the Swarm will consist of three servers; a primary manager and two worker nodes. However, a swarm can have as many nodes as required, and more can easily be added later.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When deploying the cloud servers for the Swarm, note that Docker itself will work on most Linux distributions. CentOS and other Red Hat variants might require additional steps to allow Swarm to communicate because of their stricter default firewall rules.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Log in to the <a href=\"https:\/\/hub.upcloud.com\/deploy\/\" target=\"_blank\" rel=\"noopener\">UpCloud Control Panel<\/a> and deploy three new servers for the Swarm cluster:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>manager1<\/li>\n\n\n\n<li>worker1<\/li>\n\n\n\n<li>worker2<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Once the new hosts are all up and running, perform the usual security preparations e.g. update the system, add users and SSH keys. You can find help with these steps in the\u00a0guides for <a href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/manage-linux-user-account-security\/\" target=\"_blank\" rel=\"noreferrer noopener\">Managing Linux User Account Security<\/a> and <a href=\"https:\/\/upcloud.com\/global\/docs\/guides\/use-ssh-keys-authentication\/\" target=\"_blank\" rel=\"noreferrer noopener\">Using SSH keys for Authentication<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create SDN Private Network (Optional)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Although not necessary, it is possible to have the Swarm contained within a private network.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Private networks allow you to create isolated environments within zones. You can then define custom local networks with the IP ranges of your choosing and attach IPs statically or automatically using DHCP. <a href=\"https:\/\/upcloud.com\/global\/docs\/guides\/configure-sdn-private-networks\/\" target=\"_blank\" rel=\"noreferrer noopener\">See our guide for detailed information on how to create a private network.<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For this guide we\u2019ll be using a private network with the following private IP addresses:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Manager 1: 192.168.1.101<br>Worker 1: 192.168.1.102<br>Worker 2: 192.168.1.103<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We\u2019ll start by creating a new private network from the <a href=\"https:\/\/hub.upcloud.com\/networks\/private\">Private Network<\/a> section under the Network menu. Click the Create SDN Network button to confirm. Then choose a name for the network, select a location, and specify an IP network as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image2.jpg\" alt=\"Creating a private network\" class=\"wp-image-14895\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To add a server to the newly created network, first shut down the server and then navigate to the server\u2019s Network tab. Next, click the Attach SDN private network button as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image1.jpg\" alt=\"Attach SDN private network\" class=\"wp-image-14898\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Select the SDN network that you created earlier and specify the private IP address that you would like to use for the server:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image4.jpg\" alt=\"Attaching a server to a private network\" class=\"wp-image-14901\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">After attaching the server to the private network, you need to also configure a new network interface at your operating system level. Check the guide on <a href=\"https:\/\/upcloud.com\/global\/docs\/guides\/attaching-new-ip-addresses\/\" target=\"_blank\" rel=\"noreferrer noopener\">how to add new IP addresses<\/a> for instructions on how to do this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add the remaining servers to the private network by repeating the same steps from above.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image3.jpg\" alt=\"Servers in private network\" class=\"wp-image-14905\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Install Docker Engine on each node<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With the initial configurations done and a new private network configured, we are now ready to install Docker Engine on each of the servers. You\u2019ll find instructions for Ubuntu and CentOS below. The setup instructions for other operating systems can be found on the <a href=\"https:\/\/docs.docker.com\/engine\/install\/\" target=\"_blank\" rel=\"noopener\">docker website<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing Docker Engine on Ubuntu<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Remove any old versions:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt-get remove docker docker-engine docker.io containerd runc<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Update the apt package index:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt-get update<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Install packages to allow apt to use a repository over HTTPS:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add Docker\u2019s official GPG key:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl -fsSL https:\/\/download.docker.com\/linux\/ubuntu\/gpg | sudo apt-key add -<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Verify that you now have the key with the fingerprint <strong>9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88<\/strong>, by searching for the last 8 characters of the fingerprint:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt-key fingerprint 0EBFCD88<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Set up the stable repository:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo add-apt-repository \"deb [arch=amd64] https:\/\/download.docker.com\/linux\/ubuntu $(lsb_release -cs) stable\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Install the docker engine:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt-get update\nsudo apt-get install docker-ce docker-ce-cli containerd.io<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can refer to the official Docker documentation for more information about <a href=\"https:\/\/docs.docker.com\/engine\/install\/ubuntu\/\" target=\"_blank\" rel=\"noopener\">installing Docker on Ubuntu<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing Docker Engine on CentOS<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Remove any old versions:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Set up the stable repository:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo yum install yum-utils\nsudo yum-config-manager --add-repo https:\/\/download.docker.com\/linux\/centos\/docker-ce.repo<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Install Docker Engine:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo yum install docker-ce docker-ce-cli containerd.io<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You\u2019ll be prompted to accept the GPG key. It should match <strong>060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35<\/strong>. If it does, accept it and continue.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can refer to the official Docker documentation for more information about <a href=\"https:\/\/docs.docker.com\/engine\/install\/centos\/\" target=\"_blank\" rel=\"noopener\">installing Docker on CentOS<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create the Swarm cluster on manager node<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We are now ready to create the Swarm. SSH into the cloud server where you want to run your manager node and run the command below. Be sure to replace it with the IP address of your manager server. In this tutorial, all our servers are part of an SDN private network and the private IP address for the manager node is 192.168.1.101.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are not using a private network replace with the public IP address of your server:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker swarm init --advertise-addr &lt;manager-ip-address&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The output will look like the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker swarm init --advertise-addr 192.168.1.101<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Swarm initialized: current node (dxn1zf6l61qsb1josjja83ngz) is now a manager.\n \nTo add a worker to this swarm, run the following command:\n \n    docker swarm join --token SWMTKN-1-3cg4q489c4fwf0du0ixgfb2e41fmw3mnhsv7zfa9sdy73hjcxu-39ug2o8u70xqimxvjo6zjl3ej 192.168.1.101:2377\n \nTo add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Take note of the join command to add workers to the swarm as we will be using this later in the tutorial when adding our worker nodes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To confirm that the manager node has created the new Swarm successfully, you can run the following command to view information about the Swarm:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker node ls<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">ID                             HOSTNAME     STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION\nk4vfyc4hgzlyp889033mzluob *    manager1     Ready     Active         Leader           19.03.6<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The * next to the node ID indicates that you are currently connected to this node.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add worker nodes to the Swarm<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now that the Swarm has been created, we can start attaching worker nodes to it. To do this, SSH into the server where you want to run a worker node and run the join command that was produced by the docker swarm init output in the previous section.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker swarm join --token SWMTKN-1-3cg4q489c4fwf0du0ixgfb2e41fmw3mnhsv7zfa9sdy73hjcxu-39ug2o8u70xqimxvjo6zjl3ej 192.168.1.101:2377<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you don\u2019t have the command available, you can SSH back into the manager node and run the following command to retrieve the join command for a worker:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker swarm join-token worker<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After running the join command, you will see a message indicating that the worker node has been successfully added to the swarm.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker swarm join --token SWMTKN-1-3cg4q489c4fwf0du0ixgfb2e41fmw3mnhsv7zfa9sdy73hjcxu-39ug2o8u70xqimxvjo6zjl3ej 192.168.1.101:2377<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">This node joined a swarm as a worker.<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Repeat this step for the second worker node to add that to the Swarm as well.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You should now have a total of three nodes in the Swarm. To confirm this, SSH back into the server where the manager node is running and enter the following command to see a list of all the nodes in the swarm:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker node ls<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">ID                            HOSTNAME       STATUS       AVAILABILITY      MANAGER STATUS      ENGINE VERSION\nk4vfyc4hgzlyp889033mzluob *   manager1       Ready        Active            Leader              19.03.6\naavmq0x3usmjygqaupwtt882a     node1          Ready        Active                                19.03.6\nmg6hbmpk8mgx2lowwp7vztnqx     node2          Ready        Active                                19.03.6<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You now have a fully functioning Swarm consisting of a manager node and two worker nodes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Deploy a service to the Swarm<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can now start deploying services to the newly created swarm. For the remainder of this guide, we will be working with the example service called <strong>helloworld<\/strong>. To get started, SSH into the server where your manager node is and run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker service create --replicas 1 --name helloworld alpine ping google.com<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This creates a single container instance of the service called helloworld which then executes a ping call to google.com.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can see the running services with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker service ls<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">ID                  NAME                MODE                REPLICAS            IMAGE               PORTS\nzevs3yd7j9qq        helloworld          replicated          1\/1                 alpine:latest<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With the service now deployed to the swarm, you can begin scaling the number of containers in the service. The containers running in services are called \u201ctasks.\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To scale the number of tasks in the service to 5 for example, enter the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker service scale helloworld=5<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This creates 4 new tasks bringing the total number in the swarm to 5. The tasks are all distributed between the three nodes of the swarm.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run the command below to see the updated task list:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker service ps helloworld<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS\nii4btsv6mlkq        helloworld.1        alpine:latest       worker2             Running             Running 13 minutes ago\nzevs3yd7j9qq        helloworld.2        alpine:latest       manager1            Running             Running 21 seconds ago\nr9fb6bl95uwn        helloworld.3        alpine:latest       worker1             Running             Running 20 seconds ago\ntcumkkobk42d        helloworld.4        alpine:latest       worker2             Running             Running 20 seconds ago\nu19v94j2w72d        helloworld.5        alpine:latest       worker1             Running             Running 20 seconds ago<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can get more details about the service by inspecting it with the command below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker service inspect helloworld<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Running the scale command again with a smaller number will reduce the number of tasks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, to remove the running service run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker service rm helloworld<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Run the command below to verify that the swarm manager has indeed removed the service. You should get back a message saying that the service could not be found:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker service ps helloworld<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">no such service: helloworld<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Docker Swarm is an easy way to get started with computer clusters. It provides high availability regardless of the size of your deployment. Docker boasts results of up to a thousand nodes and fifty thousand containers with no performance degradation. Scaling your cluster is also convenient with the fast deployment of new hosts through your <a href=\"https:\/\/hub.upcloud.com\/\" target=\"_blank\" rel=\"noopener\">UpCloud Control Panel<\/a> or the <a href=\"https:\/\/upcloud.com\/global\/docs\/guides\/getting-started-upcloud-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">UpCloud API<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now that you have a basic Swarm cluster set up, head over to the Docker documentation pages to learn more about <a rel=\"noopener\" href=\"https:\/\/docs.docker.com\/engine\/swarm\/\" target=\"_blank\">Swarm mode<\/a>.<\/p>\n","protected":false},"author":23,"featured_media":14895,"comment_status":"open","ping_status":"closed","template":"","community-category":[241,223],"class_list":["post-2341","tutorial","type-tutorial","status-publish","has-post-thumbnail","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/types\/tutorial"}],"author":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/users\/23"}],"replies":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/comments?post=2341"}],"version-history":[{"count":1,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2341\/revisions"}],"predecessor-version":[{"id":6445,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2341\/revisions\/6445"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2341"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}