{"id":310162,"date":"2021-07-28T15:00:34","date_gmt":"2021-07-28T12:00:34","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-scale-docker-containers-across-servers-using-kubernetes-cloudsavvy-it\/"},"modified":"2021-07-28T15:00:34","modified_gmt":"2021-07-28T12:00:34","slug":"how-to-scale-docker-containers-across-servers-using-kubernetes-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-scale-docker-containers-across-servers-using-kubernetes-cloudsavvy-it\/","title":{"rendered":"#How to Scale Docker Containers Across Servers Using Kubernetes \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 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-6a30778ea3ddd\" 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-6a30778ea3ddd\" 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\/how-to-scale-docker-containers-across-servers-using-kubernetes-cloudsavvy-it\/#Kubernetes_Basics\" >Kubernetes Basics<\/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\/how-to-scale-docker-containers-across-servers-using-kubernetes-cloudsavvy-it\/#Creating_Your_Cluster\" >Creating Your Cluster<\/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\/how-to-scale-docker-containers-across-servers-using-kubernetes-cloudsavvy-it\/#Defining_a_Horizontally_Scaling_Deployment\" >Defining a Horizontally Scaling Deployment<\/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\/how-to-scale-docker-containers-across-servers-using-kubernetes-cloudsavvy-it\/#Dynamically_Adding_Nodes\" >Dynamically Adding Nodes<\/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\/how-to-scale-docker-containers-across-servers-using-kubernetes-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Scale Docker Containers Across Servers Using Kubernetes \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-10076\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/03\/620c8dee.jpg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/03\/620c8dee.jpg?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\/03\/620c8dee.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Illustration showing the Docker and Kubernetes logos\" width=\"1602\" height=\"902\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Docker is a developer-oriented containerization platform that lets you package <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>lications as standalone containers. They\u2019ll run anywhere a compatible container runtime is available.<\/p>\n<p>Docker\u2019s popularity has made it almost synonymous with containers, yet it\u2019s not the perfect <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/technology\/\" data-internallinksmanager029f6b8e52c=\"4\" title=\"Technology\" target=\"_blank\" rel=\"noopener\">technology<\/a> for all use cases. Using Docker as-is presents challenges in production as its CLI is only equipped to manage individual containers.<\/p>\n<p>Kubernetes is an orchestration platform which manages stacks of containers and scales them across multiple servers. You can deploy across a fleet of physical machines, improving your service\u2019s redundancy and resiliency. Here\u2019s how you can start your own cluster to scale your \u201cDocker\u201d containers.<\/p>\n<h2 id=\"kubernetes-basics\"><span class=\"ez-toc-section\" id=\"Kubernetes_Basics\"><\/span>Kubernetes Basics<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Recognizing some key Kubernetes terms will help you understand the differences compared to Docker. The Kubernetes dictionary describes dozens of resources you can add to your cluster. Only the components which control container scheduling are relevant to this article.<\/p>\n<p>At a high level, a Kubernetes installation is a cluster of \u201cnodes.\u201d Nodes are independent physical machines which host your workloads. A single \u201cmaster\u201d node is responsible for coordinating (\u201corchestrating\u201d) the cluster\u2019s operations by \u201cscheduling\u201d new containers to the most appropriate worker node.<\/p>\n<p>Here are some critical terms:<\/p>\n<ul>\n<li><strong>Master<\/strong> \u2013 The master node operates the cluster. This is the machine which you install Kubernetes onto. It runs the control plane and delegates the hosting of containerized apps to worker nodes.<\/li>\n<li><strong>Control Plane<\/strong> \u2013 The control plane is the software component of the master node. It incorporates several services, including an API server, configuration store, and container scheduler.<\/li>\n<li><strong>Node<\/strong> \u2013 A node is a machine which hosts your containers. Each worker runs a Kubernetes component called Kubelet. This stays in contact with the control plane, receiving scheduling instructions which it acts on to provision new containers.<\/li>\n<li><strong>Pod<\/strong> \u2013 A Pod is the smallest compute unit in a Kubernetes cluster, representing a group of container instances. The Pod abstraction lets you interact with multiple running containers in aggregate.<\/li>\n<li><strong>Replica Set<\/strong> \u2013 Replica sets are responsible for scaling Pods to ensure a specified number of replicas are available. If you ask for three replicas of a Pod, the replica set will guarantee the availability constraint is met. Pods are automatically replaced if they fail, letting you automate container replication across servers.<\/li>\n<\/ul>\n<p>Creating a scalable cluster requires a master node, at least two worker nodes, and a deployment that provisions multiple replicas. Kubernetes will be able to schedule your Pods across all the available nodes, giving you resilience in the event one suffers an outage.<\/p>\n<h2 id=\"creating-your-cluster\"><span class=\"ez-toc-section\" id=\"Creating_Your_Cluster\"><\/span>Creating Your Cluster<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Kubernetes is available as a managed offering from most major cloud providers. These provide a one-click way to create your control plane and add a variable number of worker nodes.<\/p>\n<p>You can run a cluster on your own hardware using a self-contained Kubernetes distribution like MicroK8s. You\u2019ll need at least two isolated physical or virtual machines if you want to have redundant scheduling support.<\/p>\n<p>Install MicroK8s on both of the machines. Designate one node as the master and use the <code>add-node<\/code> command to start the node registration process:<\/p>\n<pre><code>microk8s add-node<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13031\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/aaff5aeb.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1404\" height=\"231\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This will emit a <code>microk8s join<\/code> command. Switch to your secondary node and run the command. It will join the first cluster as a new worker. Now both machines are ready to host your containerized workloads.<\/p>\n<h2 id=\"defining-a-horizontally-scaling-deployment\"><span class=\"ez-toc-section\" id=\"Defining_a_Horizontally_Scaling_Deployment\"><\/span>Defining a Horizontally Scaling Deployment<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>An application which runs across multiple servers is described as \u201chorizontally scaled.\u201d It\u2019s spreading itself out across several distinct environments. Vertically scaling a system involves adding resources to an existing environment.<\/p>\n<p>The simplest way to define a Kubernetes workload is <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/tutorials\/kubernetes-basics\/scale\/scale-intro\">with a Deployment<\/a>. This resource type creates Pods from a container image and sets up public networking routes via a service. Deployments default to a single Pod instance but can be configured with multiple replicas.<\/p>\n<p>Here\u2019s a simple Deployment manifest:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yaml\">\n<pre class=\"de1\"><span class=\"co3\">apiVersion<\/span><span class=\"sy2\">: <\/span>apps\/v1<span class=\"co3\">\nkind<\/span><span class=\"sy2\">: <\/span>Deployment<span class=\"co4\">\nmetadata<\/span>:<span class=\"co3\">\n  name<\/span><span class=\"sy2\">: <\/span>nginx<span class=\"co4\">\nspec<\/span>:<span class=\"co3\">\n  replicas<\/span><span class=\"sy2\">: <\/span>3<span class=\"co4\">\n  selector<\/span>:<span class=\"co4\">\n    matchLabels<\/span>:<span class=\"co3\">\n      app<\/span><span class=\"sy2\">: <\/span>nginx<span class=\"co4\">\n  template<\/span>:<span class=\"co4\">\n    metadata<\/span>:<span class=\"co4\">\n      labels<\/span>:<span class=\"co3\">\n        app<\/span><span class=\"sy2\">: <\/span>nginx<span class=\"co4\">\n    spec<\/span>:<span class=\"co4\">\n      containers<\/span>:<span class=\"co3\">\n      - name<\/span><span class=\"sy2\">: <\/span>nginx<span class=\"co3\">\n        image<\/span><span class=\"sy2\">: <\/span>nginx:latest<span class=\"co4\">\n        ports<\/span>:<span class=\"co3\">\n        - containerPort<\/span><span class=\"sy2\">: <\/span>80<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Apply the manifest to your cluster using Kubectl:<\/p>\n<pre><code>microk8s kubectl apply -f .\/manifest.yaml<\/code><\/pre>\n<p>Kubernetes will create three Pods, each hosting an NGINX web server created from the <code>nginx:latest<\/code> image. Port 80 is exposed as a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/services-networking\/connect-applications-service\">container port<\/a> to enable inbound traffic.<\/p>\n<p>Pods will automatically be distributed across your cluster\u2019s nodes. A node will be eligible to host a Pod if it can provide sufficient resources.<\/p>\n<p>You can update the number of replicas at any time. Change the <code>replicas<\/code> field in your manifest and re-apply it to your cluster. The Kubernetes scheduler will take action to provision new Pods or terminate old ones as required. If you scale <code>replicas<\/code> down to 0, you can take your application offline without actually deleting the deployment or its associated resources.<\/p>\n<h2 id=\"dynamically-adding-nodes\"><span class=\"ez-toc-section\" id=\"Dynamically_Adding_Nodes\"><\/span>Dynamically Adding Nodes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Updating the replica count lets you utilize the existing resources within your cluster. Eventually, you might exhaust your combined node capacity altogether, preventing new Pods from being scheduled. Kubernetes offers a cluster auto-scaling facility which can create and destroy <em>Nodes<\/em> , effectively changing the number of Pods that can be scheduled.<\/p>\n<p>Setting up auto-scaling is relatively involved. The exact process is dependent on your cluster\u2019s hosting environment. It requires integration with a Cluster Autoscaler capable of hooking into your host to detect changes in demand. Official documentation is available for <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/blog\/2016\/07\/autoscaling-in-kubernetes\">Google Cloud<\/a>; solutions are also available from <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.aws.amazon.com\/eks\/latest\/userguide\/cluster-autoscaler.html?tag=reviewgeek-20\">Amazon EKS<\/a> and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.google.com\/search?channel=fs&amp;client=ubuntu&amp;q=azure+autoscaler\">Microsoft AKS<\/a>.<\/p>\n<p>Auto-scaling works by maintaining a constant check for Pods that can\u2019t be scheduled due to insufficient cluster capacity. It also assesses whether the successfully scheduled Pods could actually be colocated with a reduced node count. The auto-scaler then utilizes your cloud provider\u2019s API to add and remove compute instances, dynamically adjusting your cluster\u2019s resources. This may impact your bill as creating a new instance is usually a chargeable operation.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Kubernetes makes it easy to distribute container instances across multiple servers. Register your machines as nodes, or use a managed cloud cluster, then create Deployments which have the <code>replicas<\/code> field set. You can scale your workload by updating the requested replicas count.<\/p>\n<p>When severe changes in demand are anticipated, use auto-scaling to dynamically expand capacity. This feature lets you create new Nodes on-the-fly, adding extra resources so you can keep raising your replica count.<\/p>\n<p>When you\u2019re scaling Pods, you do need to remember that some form of inbound traffic distribution is also required. If you\u2019re exposing port 80, Kubernetes needs to be able to map requests to port 80 on <em>any<\/em> of the nodes. This works automatically with Deployments and Services. Kubernetes creates a Load Balancer which continuously monitors Pods to work out where traffic can be directed.\n<\/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\/13039\/how-to-scale-docker-containers-across-servers-using-kubernetes\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Scale Docker Containers Across Servers Using Kubernetes \u2013 CloudSavvy IT&#8221; Docker is a developer-oriented containerization platform that lets you package applications as standalone containers. They\u2019ll run anywhere a compatible container runtime is available. Docker\u2019s popularity has made it almost synonymous with containers, yet it\u2019s not the perfect technology for all use cases. Using&#8230;<\/p>\n","protected":false},"author":1,"featured_media":310163,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/03\/620c8dee.jpg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-310162","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\/310162","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=310162"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/310162\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/310163"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=310162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=310162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=310162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}