{"id":274,"date":"2016-02-07T18:41:46","date_gmt":"2016-02-07T17:41:46","guid":{"rendered":"https:\/\/blog.mi.hdm-stuttgart.de\/?p=274"},"modified":"2023-06-07T11:33:57","modified_gmt":"2023-06-07T09:33:57","slug":"more-docker-more-power-part-3-setting-up-the-loadbalancer","status":"publish","type":"post","link":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/2016\/02\/07\/more-docker-more-power-part-3-setting-up-the-loadbalancer\/","title":{"rendered":"More docker = more power? \u2013 Part 3: Setting up the loadbalancer"},"content":{"rendered":"<p>To benefit from using a loadbalancer we need several machines to distribute the traffic on, evidently.<br \/>\nThanks to Docker we simply run<\/p>\n<pre class=\"prettyprint lang-sh\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">docker run -d -p 81:80 testwebsite:1 \n<\/pre>\n<p>to get a second machine. This time the container port of the webserver is mapped to port 81. If you now visit &lt;IP OF YOUR VM&gt;:81 you should see your test website.<br \/>\nYou can have as many machines as you want to. Simply pay attention to the ports.<br \/>\nOf course we don\u2019t want to write this command manually each time when we want to create a new container. Especially not when we want about 100 new containers. That\u2019s why we wrote a small bash script, which does the job for us.<\/p>\n<p><!--more--><\/p>\n<pre class=\"prettyprint lang-sh\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">#!\/bin\/bash\nnum=$1\nfor (( i=0; i &lt; $num; i++))\ndo\n    port=$((80+i))\n    docker run -d -p $port:80 testwebsite:4\ndone<\/pre>\n<p>All the script does is executing&nbsp;<code class=\"\" data-line=\"\">docker run<\/code> as many times as you specified as a commandline parameter. It also increments the port by one each time.<br \/>\nSo when you run the script with <code class=\"\" data-line=\"\">.\/myscript.sh 25<\/code>, you\u2019ll get 25 containers with ports from 80 to 104.<br \/>\nYou can check this by accessing the IP of your Dockerhost with your browser and looking at the different ports. You should get the same webpage for each port.<\/p>\n<p>Now we can generate as many web servers as we want. So now it\u2019s time to add some load balancing in front of it.<br \/>\nWith a loadbalancer we specify all the servers we can use, and the balancer automatically chooses one for us. There are several mechanisms for choosing the best server for your request. However, we will just use the \u201cleast-connection\u201d mechanism which chooses the server with the least connections.<\/p>\n<p>There are hardware and software load balancers. Since we don\u2019t have the hardware, we will choose a software to do the balancing for us.<br \/>\nLuckily, configuring nginx as a load balancer is pretty easy.<\/p>\n<p>So on your balancer-VM install Nginx like we did on the other VM as well:<\/p>\n<pre class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">sudo apt-get install nginx<\/pre>\n<p>In the nginx-config you should remove most of the lines, since they host their own website. This is not what we want. Insert the following into \/etc\/nginx\/nginx.conf<\/p>\n<pre class=\"prettyprint lang-sh\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">user www-data;\nworker_processes 4;\npid \/run\/nginx.pid;\n\nevents {\n    worker_connections 768;\n}\n\nhttp {\n    upstream loadbalancer {\n        least_conn;\n        server dockerhost:80;\n        server dockerhost:81;\n        # Add servers here\n    }\n\n    server {\n        listen 80;\n        location \/ {\n            proxy_pass http:\/\/loadbalancer;\n        }\n    }\n}\n\n<\/pre>\n<p>In the section \u201cupstream loadbalancer\u201d you have to insert all available servers. So if you have 25 containers running, you have to insert 25 lines.<\/p>\n<p>If you restart nginx now, you should have your load balancer running.<br \/>\nIf you access the IP of your load balancer with your browser you should see the website hosted by your Docker containers.<br \/>\nEvery requests will be redirected to another container.<br \/>\nYou could try to deploy different websites to the containers. When you do this, you\u2019ll see different websites, each time you access your loadbalancer.<\/p>\n<p>However, it\u2019s not very handy to write the IPs of all containers into your nginx.conf. That\u2019s why we created a Python script, which generates an nginx config, depending on the number of containers you want to create.<\/p>\n<pre class=\"prettyprint lang-python\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">#!\/usr\/bin\/python\nfrom sys import argv\n\npart_a=\"\"\"user www-data;\\nworker_processes 4;\\npid \/run\/nginx.pid;\\n\\nevents {\\n\\tworker_connections 768;\\n\\t# multi_accept on;\\n}\\n\\nhttp {\\n\\tlog_format compression '$upstream_addr';\\n\\tupstream loadbalancer {\\n\\t\\tleast_conn;\\n\"\"\"\n\npart_b=\"\"\"\\t}\\n\\tserver {\\n\\t\\tlisten 80;\\n\\t\\tlocation \/ {\\n\\t\\t\\tproxy_pass http:\/\/loadbalancer;\\n\\t\\t}\\n\\t}\\n}\\n\"\"\"\n\ncounter = int(argv[1])\nconfig = open(\"nginx.conf\", \"w\")\nconfig.write(part_a)\nfor i in range(counter):\n    port=80+i\n    config.write(\"\\t\\tserver slave.fritz.box:%i;\\n\" % port)\nconfig.write(part_b)\nconfig.close()<\/pre>\n<p>All you have to do, is to copy the generated file to \/etc\/nginx\/nginx.conf and restart your loadbalancer with<\/p>\n<pre class=\"prettyprint lang-sh\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">sudo service nginx restart<\/pre>\n<p>In <a href=\"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/2016\/02\/14\/more-docker-more-power-part-4-problems-arise\/\" target=\"_blank\" rel=\"noopener\">part four<\/a> we finally get to do some load testing and struggle with a few problems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To benefit from using a loadbalancer we need several machines to distribute the traffic on, evidently. Thanks to Docker we simply run docker run -d -p 81:80 testwebsite:1 to get a second machine. This time the container port of the webserver is mapped to port 81. If you now visit &lt;IP OF YOUR VM&gt;:81 you [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":182,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1,651,2,657],"tags":[3,11,12],"ppma_author":[689],"class_list":["post-274","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-allgemein","category-system-designs","category-system-engineering","category-teaching-and-learning","tag-docker","tag-loadbalancing","tag-nginx"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2016\/01\/1429543497dockerimg.png","jetpack-related-posts":[{"id":170,"url":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/2016\/01\/07\/more-docker-more-power-part-2-setting-up-nginx-and-docker\/","url_meta":{"origin":274,"position":0},"title":"More docker = more power? \u2013 Part 2: Setting up Nginx and Docker","author":"Moritz Lottermann","date":"7. January 2016","format":false,"excerpt":"This is Part 2 of a series of posts. You can find Part 1 here:\u00a0https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/2016\/01\/03\/more-docker-more-power-part-1-setting-up-virtualbox\/ In the first part of this series we have set up two VirtualBox machines. One functions as the load balancer and the other will house our services. As the next step we want to install\u2026","rel":"","context":"In &quot;System Designs&quot;","block_context":{"text":"System Designs","link":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/category\/system-designs\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2016\/01\/1429543497dockerimg.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2016\/01\/1429543497dockerimg.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2016\/01\/1429543497dockerimg.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2016\/01\/1429543497dockerimg.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":149,"url":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/2016\/01\/03\/more-docker-more-power-part-1-setting-up-virtualbox\/","url_meta":{"origin":274,"position":1},"title":"More docker = more power? \u2013 Part 1: Setting up VirtualBox","author":"Tobias Schneider","date":"3. January 2016","format":false,"excerpt":"This series of blogposts will focus on the effects on response times when performing different tasks running on a variable number of docker containers in a virtual machine. What will be the performance differences running a small or large number of containers on the same machine? These posts will function\u2026","rel":"","context":"In &quot;System Designs&quot;","block_context":{"text":"System Designs","link":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/category\/system-designs\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2016\/01\/1429543497dockerimg.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2016\/01\/1429543497dockerimg.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2016\/01\/1429543497dockerimg.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2016\/01\/1429543497dockerimg.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1060,"url":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/2016\/08\/06\/exploring-docker-security-part-1-the-whales-anatomy\/","url_meta":{"origin":274,"position":2},"title":"Exploring Docker Security &#8211; Part 1: The whale&#8217;s anatomy","author":"Patrick Kleindienst","date":"6. August 2016","format":false,"excerpt":"When it comes to Docker, most of us\u00a0immediately start thinking of current trends like Microservices, DevOps, fast deployment, or scalability. Without a doubt, Docker seems to hit the road towards establishing itself\u00a0as\u00a0the\u00a0de-facto standard for lightweight application containers, shipping not only with lots of features and tools, but also great usability.\u2026","rel":"","context":"In &quot;Secure Systems&quot;","block_context":{"text":"Secure Systems","link":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/category\/system-designs\/secure-systems\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2023\/08\/ballena-de-alas-largas-240873.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2023\/08\/ballena-de-alas-largas-240873.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2023\/08\/ballena-de-alas-largas-240873.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2023\/08\/ballena-de-alas-largas-240873.jpg?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2023\/08\/ballena-de-alas-largas-240873.jpg?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2023\/08\/ballena-de-alas-largas-240873.jpg?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":308,"url":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/2016\/03\/10\/more-is-always-better-building-a-cluster-with-pies\/","url_meta":{"origin":274,"position":3},"title":"More is always better: building a cluster with Pies","author":"Benjamin Binder","date":"10. March 2016","format":false,"excerpt":"So you have written the uber-pro-web-application with a bazillion of active users. But your requests start to get out of hand and the Raspberry Pi under your desk can't handle all the pressure on its own. Finally,\u00a0the time for rapid expansion has come! If you have already containerized your application,\u2026","rel":"","context":"In &quot;Scalable Systems&quot;","block_context":{"text":"Scalable Systems","link":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/category\/scalable-systems\/"},"img":{"alt_text":"Raspberry Pi 2","src":"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/3\/3d\/Raspberry_PI.jpeg","width":350,"height":200,"srcset":"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/3\/3d\/Raspberry_PI.jpeg 1x, https:\/\/upload.wikimedia.org\/wikipedia\/commons\/3\/3d\/Raspberry_PI.jpeg 1.5x, https:\/\/upload.wikimedia.org\/wikipedia\/commons\/3\/3d\/Raspberry_PI.jpeg 2x, https:\/\/upload.wikimedia.org\/wikipedia\/commons\/3\/3d\/Raspberry_PI.jpeg 3x, https:\/\/upload.wikimedia.org\/wikipedia\/commons\/3\/3d\/Raspberry_PI.jpeg 4x"},"classes":[]},{"id":282,"url":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/2016\/03\/10\/docker-running-on-a-raspberry-pi-hypriot\/","url_meta":{"origin":274,"position":4},"title":"Docker on a Raspberry Pi: Hypriot","author":"Jonathan Peter","date":"10. March 2016","format":false,"excerpt":"Raspberry Pis are small, cheap\u00a0and easy to come by. But what if you want to use Docker on them? Our goal was to run Docker on several Raspberry Pis and combine them to a cluster with Docker Swarm. To achieve this, we first\u00a0needed to get Docker running on the Pi.\u2026","rel":"","context":"In &quot;System Designs&quot;","block_context":{"text":"System Designs","link":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/category\/system-designs\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3342,"url":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/2018\/03\/28\/ci-cd-infrastructure-choosing-and-setting-up-a-server-with-jenkins-as-docker-image\/","url_meta":{"origin":274,"position":5},"title":"CI\/CD infrastructure: Choosing and setting up a server with Jenkins as Docker image","author":"cp054","date":"28. March 2018","format":false,"excerpt":"Related articles:\u00a0\u25baTake Me Home - Project Overview\u00a0 \u25baAndroid SDK and emulator in Docker for testing\u00a0 \u25baAutomated Unit- and GUI-Testing for Android in Jenkins\u00a0 \u25baTesting a MongoDB with NodeJS, Mocha and Mongoose This article will run you through the motivation for a continuous integration and delivery, choosing a corresponding tool and\u2026","rel":"","context":"In &quot;Allgemein&quot;","block_context":{"text":"Allgemein","link":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/category\/allgemein\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2018\/03\/puttygen.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2018\/03\/puttygen.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2018\/03\/puttygen.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/blog.mi.hdm-stuttgart.de\/wp-content\/uploads\/2018\/03\/puttygen.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"jetpack_sharing_enabled":true,"authors":[{"term_id":689,"user_id":9,"is_guest":0,"slug":"ml106","display_name":"Moritz Lottermann","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/4297127a3920bb2d4f850630d30bdfd6a438f4cfee9730be28763772ffc90088?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/wp-json\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/wp-json\/wp\/v2\/comments?post=274"}],"version-history":[{"count":8,"href":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/wp-json\/wp\/v2\/posts\/274\/revisions"}],"predecessor-version":[{"id":24653,"href":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/wp-json\/wp\/v2\/posts\/274\/revisions\/24653"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/wp-json\/wp\/v2\/media\/182"}],"wp:attachment":[{"href":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/wp-json\/wp\/v2\/media?parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/wp-json\/wp\/v2\/categories?post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/wp-json\/wp\/v2\/tags?post=274"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blog.mi.hdm-stuttgart.de\/index.php\/wp-json\/wp\/v2\/ppma_author?post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}