{"id":311123,"date":"2021-07-29T15:00:31","date_gmt":"2021-07-29T12:00:31","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-is-docker-swarm-mode-and-when-should-you-use-it-cloudsavvy-it\/"},"modified":"2021-07-29T15:00:31","modified_gmt":"2021-07-29T12:00:31","slug":"what-is-docker-swarm-mode-and-when-should-you-use-it-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-is-docker-swarm-mode-and-when-should-you-use-it-cloudsavvy-it\/","title":{"rendered":"#What is Docker Swarm Mode and When Should You Use It? \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2ec579ca3f1\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2ec579ca3f1\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-docker-swarm-mode-and-when-should-you-use-it-cloudsavvy-it\/#Creating_Your_Own_Swarm\" >Creating Your Own Swarm<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-docker-swarm-mode-and-when-should-you-use-it-cloudsavvy-it\/#Deploying_a_Container\" >Deploying a Container<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-docker-swarm-mode-and-when-should-you-use-it-cloudsavvy-it\/#Scaling_Services\" >Scaling Services<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-docker-swarm-mode-and-when-should-you-use-it-cloudsavvy-it\/#Rolling_Updates\" >Rolling Updates<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-docker-swarm-mode-and-when-should-you-use-it-cloudsavvy-it\/#Managing_Services\" >Managing Services<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-docker-swarm-mode-and-when-should-you-use-it-cloudsavvy-it\/#Exposing_Network_Ports\" >Exposing Network Ports<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-docker-swarm-mode-and-when-should-you-use-it-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#What is Docker Swarm Mode and When Should You Use It? \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-10864\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Illustration showing the Docker logo\" width=\"1600\" height=\"900\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Swarm Mode is Docker\u2019s built-in orchestration system for scaling containers across a cluster of physical machines. Multiple independent clients running Docker Engine pool their resources, forming a swarm.<\/p>\n<p>The feature comes bundled with Docker and includes everything you need to deploy <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>s across nodes. Swarm Mode has a declarative scaling model where you state the number of replicas you require. The swarm manager takes action to match the actual number of replicas to your request, creating and destroying containers as necessary.<\/p>\n<p>Swarms have <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/swarm\">much more functionality<\/a> too. Clusters benefit from integrated service discovery functions, support for rolling updates, and network traffic routing via external load balancers.<\/p>\n<p>Here\u2019s how you can use Swarm mode to set up simple distributed workloads across a fleet of machines. You should use Swarm if you want to host scalable applications with redundancy using a standard Docker installation, no other dependencies required.<\/p>\n<h2 id=\"creating-your-own-swarm\"><span class=\"ez-toc-section\" id=\"Creating_Your_Own_Swarm\"><\/span>Creating Your Own Swarm<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Make sure you\u2019ve got Docker installed before you continue. You\u2019ll need the full Docker CE package on each machine you want to add to the swarm.<\/p>\n<p>Designate one of your hosts as the swarm manager. This node will orchestrate the cluster by issuing container scheduling requests to the other nodes. Run <code>docker swarm init<\/code> on the manager to start the cluster setup process:<\/p>\n<pre>docker swarm init --advertise-addr 192.168.0.1<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13051\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/af9d7b66.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1106\" height=\"364\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Replace the IP address with your manager node\u2019s real IP. The command will emit a <code>docker swarm join<\/code> command which you should run on your secondary nodes. They\u2019ll then join the swarm and become eligible to host containers.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13053\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/0ec83c27.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"910\" height=\"509\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Once you\u2019ve added your nodes, run <code>docker info<\/code> on the manager to inspect the cluster\u2019s status. The Swarm section of the command\u2019s output should be listed as \u201cactive.\u201d Check the \u201cNodes\u201d count matches the number of nodes you\u2019ve added.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13052\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/1c780b44.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1441\" height=\"117\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can get more details about a node by running <code>docker node ls<\/code>. This shows each node\u2019s unique ID, its hostname, and its current status. Nodes that show an availability of \u201cactive\u201d with a status of \u201cready\u201d are healthy and ready to support your workloads. The Manager Status column indicates nodes that are also acting as swarm managers. The \u201cleader\u201d is the node with overall responsibility for the cluster.<\/p>\n<h2 id=\"deploying-a-container\"><span class=\"ez-toc-section\" id=\"Deploying_a_Container\"><\/span>Deploying a Container<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Once your nodes are ready, you can deploy a container into your swarm. Swarm mode uses the concept of \u201cservices\u201d to describe container deployments. Each service configuration references a Docker image and a replica count to create from that image.<\/p>\n<pre>docker service create --replicas 3 --name apache httpd:latest<\/pre>\n<p>This command creates a service using the <code>httpd:latest<\/code> image for the Apache web server. Three independent replicas will be created, giving you resiliency against container terminations and node outages. Container replicas are called \u201ctasks\u201d in Docker Swarm parlance.<\/p>\n<p>Docker will continually maintain the requested state. If one of the nodes drops offline, the replicas it was hosting will be rescheduled to the others. You\u2019ll have three Apache containers running throughout the lifetime of the service.<\/p>\n<h2 id=\"scaling-services\"><span class=\"ez-toc-section\" id=\"Scaling_Services\"><\/span>Scaling Services<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can scale services at any time using the <code>docker service scale<\/code> command:<\/p>\n<pre>docker service scale apache=5<\/pre>\n<p>Docker will add two new container instances so the number of replicas continues to match the requested count. The extra instances will be scheduled to nodes with enough free capacity to support them.<\/p>\n<p>Services can also be scaled with <code>docker service update<\/code>:<\/p>\n<pre>docker service update apache --replicas=5<\/pre>\n<p>When you use this variant, you can rollback the change using a dedicated command:<\/p>\n<pre>docker service rollback apache<\/pre>\n<p>The service will get reverted back to three replicas. Docker will destroy two container instances, allowing the live replica count to match the previous state again.<\/p>\n<h2 id=\"rolling-updates\"><span class=\"ez-toc-section\" id=\"Rolling_Updates\"><\/span>Rolling Updates<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Swarm mode supports rolling updates where container instances are scaled incrementally. You can specify a delay between deploying the revised service to each node in the swarm. This gives you time to act on regressions if issues are noted. You can quickly rollback as not all nodes will have received the new service.<\/p>\n<p>Add the <code>--update-delay<\/code> flag to a <code>docker service scale<\/code> command to activate rolling updates. The delay is specified as a combination of hours <code>h<\/code>, minutes <code>m<\/code> and seconds <code>s<\/code>. The swarm manager will update each container instance individually. You can adjust the number of tasks updated in a single operation with the <code>--update-parallelism<\/code> flag.<\/p>\n<p>Here\u2019s how to scale a service to 10 replicas, updating three containers at a time with a five minute delay between each batch:<\/p>\n<pre>docker service scale --name apache --replicas=10 --update-delay 5m --update-parallelism 3<\/pre>\n<h2 id=\"managing-services\"><span class=\"ez-toc-section\" id=\"Managing_Services\"><\/span>Managing Services<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Many familiar Docker commands also work with services. Prepend regular container management commands with <code>docker service<\/code> to list services, view their logs, and delete them.<\/p>\n<ul>\n<li><strong><code>docker service inspect<\/code><\/strong> \u2013 Inspect the technical data of a named service.<\/li>\n<li><strong><code>docker service logs<\/code><\/strong> \u2013 View log output associated with a named service.<\/li>\n<li><strong><code>docker service ls<\/code><\/strong> \u2013 List all running services.<\/li>\n<li><strong><code>docker service ps<\/code><\/strong> \u2013 Show the individual container instances encapsulated by a specific service.<\/li>\n<li><strong><code>docker service rm<\/code><\/strong> \u2013 Remove a service with all its replicas. There is no confirmation prompt.<\/li>\n<\/ul>\n<p>Besides the basic management operations described so far, services come with a rich set of <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/reference\/commandline\/service_update\">configuration options<\/a>. These can be applied when creating a service or later with the <code>docker service update<\/code> command.<\/p>\n<p>Service options include environment variables, health check commands, DNS settings, labels, and restart conditions. Running container instances don\u2019t usually get destroyed on <code>docker service update<\/code> unless you\u2019re changing settings that require a recreation to take effect.<\/p>\n<h2 id=\"exposing-network-ports\"><span class=\"ez-toc-section\" id=\"Exposing_Network_Ports\"><\/span>Exposing Network Ports<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Container network ports are exposed with the <code>--publish<\/code> flag for <code>docker service create<\/code> and <code>docker service update<\/code>. This lets you specify a target container port and the public port to expose it as.<\/p>\n<pre>docker service create --name apache --replicas 5 --publish published=8080,target=80 nginx:latest<\/pre>\n<p>Now you can connect to port 8080 on <em>any<\/em> of your worker nodes to access an instance of the NGINX service. This works even if the node you connect to isn\u2019t actually hosting one of the service\u2019s tasks. You simply interact with the swarm and it takes care of the network routing. This approach is called the \u201crouting mesh.\u201d<\/p>\n<p>An alternative option lets you publish container ports on the individual nodes where tasks are running. Add <code>mode=host<\/code> to the <code>--published<\/code> flag to enable this. The service will only be exposed on the nodes which host it. This is useful in cases where you want to connect to a specific instance of the service. The routing mesh randomizes the instance you connect to, irrespective of the node you use to connect.<\/p>\n<p>Docker Swarm supports overlay networks too. These are similar to regular Docker networks. Joining a service to a network lets its containers communicate with any other services on the network.<\/p>\n<pre>docker service create --name service1 --network demo-network my-image:latest&#13;\ndocker service create --name service2 --network demo-network my-image:latest<\/pre>\n<p>Tasks created by <code>service1<\/code> and <code>service2<\/code> will be able to reach each other via the overlay network. A default network called <code>ingress<\/code> provides the standard routing mesh functionality described above.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Swarm mode is a container orchestrator that\u2019s built right into Docker. As it\u2019s included by default, you can use it on any host with Docker Engine installed.<\/p>\n<p>Creating a swarm lets you replicate containers across a fleet of physical machines. Swarm also lets you add <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/swarm\/admin_guide\">multiple manager nodes<\/a> to improve fault tolerance. If the active leader drops out of the cluster, another manager can take over to maintain operations.<\/p>\n<p>Docker Swarm mode compares favorably to alternative orchestration platforms such as Kubernetes. It\u2019s easier to get started with as it\u2019s integrated with Docker and there are fewer concepts to learn. It\u2019s often simpler to install and maintain on self-managed hardware, although pre-packaged Kubernetes solutions like MicroK8s have eroded the Swarm convenience factor. Even so, Swarm mode remains a viable orchestrator for self-hosted workloads, particularly if you\u2019re looking for a developer-oriented CLI-driven solution that\u2019s less demanding on operations teams.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/13049\/what-is-docker-swarm-mode-and-when-should-you-use-it\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#What is Docker Swarm Mode and When Should You Use It? \u2013 CloudSavvy IT&#8221; Swarm Mode is Docker\u2019s built-in orchestration system for scaling containers across a cluster of physical machines. Multiple independent clients running Docker Engine pool their resources, forming a swarm. The feature comes bundled with Docker and includes everything you need to deploy&#8230;<\/p>\n","protected":false},"author":1,"featured_media":311124,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-311123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/311123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=311123"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/311123\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/311124"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=311123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=311123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=311123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}