<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scaled services Archives - gj</title>
	<atom:link href="https://blog.gaiterjones.com/tag/scaled-services/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.gaiterjones.com/tag/scaled-services/</link>
	<description>gaiterjones</description>
	<lastBuildDate>Tue, 20 Jun 2017 10:40:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.3</generator>
	<item>
		<title>Docker Mono Host Service Scaling and Dynamic Load Balancing with NGINX</title>
		<link>https://blog.gaiterjones.com/docker-mono-host-service-scaling-and-dynamic-load-balancing-with-nginx/</link>
					<comments>https://blog.gaiterjones.com/docker-mono-host-service-scaling-and-dynamic-load-balancing-with-nginx/#respond</comments>
		
		<dc:creator><![CDATA[PAJ]]></dc:creator>
		<pubDate>Sun, 11 Jun 2017 11:39:16 +0000</pubDate>
				<category><![CDATA[Docker]]></category>
		<category><![CDATA[NGINX]]></category>
		<category><![CDATA[Scaled Services]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[dynamic load balancing]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[scaled services]]></category>
		<guid isPermaLink="false">http://blog.gaiterjones.com/?p=1663</guid>

					<description><![CDATA[Whilst building containers for Magento 2 I came across the docker compose scale command which allows you to easily duplicate and scale containers running a service in Docker. For example...<a class="more-link" href="https://blog.gaiterjones.com/docker-mono-host-service-scaling-and-dynamic-load-balancing-with-nginx/" title="Continue reading">Continue reading</a>]]></description>
										<content:encoded><![CDATA[<p>Whilst building containers for Magento 2 I came across the docker compose <a href="https://docs.docker.com/compose/reference/scale/">scale command</a> which allows you to easily duplicate and scale containers running a service in Docker.</p>
<p>For example if I have a web application service called php-apache</p>
<p><em>docker-compose scale php-apache=10</em></p>
<p>Will start another 9 containers running the php-apache service giving me 10 containers running the same php-apache web application service.</p>
<p>Of course if you want to have real resilience and load sharing <a href="https://docs.docker.com/engine/swarm/">Docker swarm</a> is the solution so the question is, <strong>are there any benefits from scaling a service in mulitple containers on a single host</strong>?</p>
<p>Well, yes, because you can still load balance the traffic to your application and achieve a basic level of container resilience.</p>
<p>Scaled services will only work for applications that use private ports as multiple Docker containers cannot share the same public port. I use an NGINX service container as a reverse proxy to my web application services with NGINX listening on the host public http port, proxying traffic to the service containers on their private internal container http ports.</p>
<p>This scenario can scale easily and there are some <a href="http://southworks.com/blog/2015/07/17/docker-compose-scaling-multi-container-applications/">documented examples</a> that describe how to load balance Docker scaled web application services, however all the examples I found require manual configuration and a restart of the load balancer to update it when service containers are scaled up or down.</p>
<p>I wanted to create a <em>dynamic</em> scaled service where the load balancer or proxy is dynamically updated when services are scaled up or down.</p>
<p>NGINX as a reverse proxy <a href="http://nginx.org/en/docs/http/load_balancing.html">will also load balance</a> when multiple upstream hosts are configured for a website but the default configuration file must be manually edited, saved and NGINX restarted before the changes take affect.</p>
<p>A bit of <em>googling</em> around uncovered a <a href="https://github.com/yzprofile/ngx_http_dyups_module">module</a> that allows you to dynamically update the NGINX upstream host configuration. I compiled the module into my existing NGINX service container and tested it and was able to dynamically add and remove upstream hosts, perfect for dynamically scaling Docker services!</p>
<p><img fetchpriority="high" decoding="async" style="padding: 1px; border: 1px solid #021a40;" src="https://blog.gaiterjones.com/wp-content/uploads/2017/06/docker-scaledemo-app-1.jpg" alt="" width="500" height="535" class="aligncenter wp-image-1670 size-full" /></p>
<h1>Scaledemo Application</h1>
<p>To demonstrate how Docker Mono Host Service Scaling and Dynamic Load Balancing with NGINX works I put together a group of containers to simulate a scaled web application :</p>
<ul>
<li>manager
<ul>
<li>A php service container that acts as the manager for the scaled services project.</li>
</ul>
</li>
<li>nginx
<ul>
<li>An NGINX container compiled with the <em>ngx_http_dyups</em> dynamic upstream module.</li>
</ul>
</li>
<li>php-apache
<ul>
<li>A php7, Apache2 service container that will run my demo &#8216;Bean Counter&#8217; web application.</li>
</ul>
</li>
<li>memcached
<ul>
<li>Cache server used by the manager to store Docker data, and by the web application to store the &#8216;Bean Counter&#8217; data.</li>
</ul>
</li>
</ul>
<h1>Manager</h1>
<p>Dynamic updates to NGINX are carried out by the manager service container. It pulls Docker system information from the Docker host via a cron job every 60 seconds, parses the data for running service containers that match the configured project web application service container (php-apache) and generates an NGINX upstream host configuration.</p>
<p>The manager compares the upstream host configuration running on NGINX with the generated configuration from Docker. If they differ it updates NGINX via php-curl with the new configuration. The parsed manager Docker data is stored on the memcache server so that it is available to other services.</p>
<p>When I scale the php-apache service up or down NGINX is dynamically configured with the changes at the next manager update, or heartbeat. I can also manually force the update via a cli command on the manager.</p>
<h1>Demo</h1>
<p>Watch the video below to see the scale demo in action.</p>
<p>In the video I start the containers with docker compose, scale php-apache up to 10 instances and check the dynamic update and round robin load balancing via the demo web application that parses the manager data from memcache and also increments a counter.</p>
<p>I then scale it up again to 20 instances and manually update the nginx configuration via the manager. You can see NGINX load balancing the web application with round robin requests to each of the 20 service containers as I click the refresh button to increment the bean counter and reload the page. The container that served the page is highlighted in yellow.</p>
<p>Finally I scale the service back down to 2 instances and stop the containers.</p>
<p><img decoding="async" src="https://blog.gaiterjones.com/dropbox/docker-scaledemox2.gif" alt="docker scale service dynamic load balancing with nginx demo video" class="aligncenter" /><br />
You can build the Docker containers and try out the demo yourself using the docker-compose files from <a href="https://github.com/gaiterjones/docker-scaledemo">GitHub</a>.</p>
<ul>
<li>Clone the project files
<ul>
<li>git clone https://github.com/gaiterjones/docker-scaledemo</li>
</ul>
</li>
<li>Build the containers
<ul>
<li>cd scaledemo</li>
<li>docker-compose build</li>
</ul>
</li>
<li>Make sure there are no other service using port 80 on your host and start the containers
<ul>
<li>docker-compose up -d</li>
</ul>
</li>
<li>Point your browser at http://scaledemo.dev.com (which should resolve to your Docker host) to see the demo page</li>
<li>Scale the php-apache container
<ul>
<li>docker-compose scale php-apache=5</li>
</ul>
</li>
</ul>
<p>To manually update NGINX from the scale manager</p>
<ul>
<li>Connect to the manager container
<ul>
<li> docker exec -t -i scaledemo_manager_1 /bin/bash
<ul>
<li>or <em>docker exec magento2_manager_1 php cron.php scalemanager</em></li>
</ul>
</li>
</ul>
</li>
<li>run the scalemanager php script
<ul>
<li>php cron.php update scalemanager</li>
</ul>
</li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.gaiterjones.com/docker-mono-host-service-scaling-and-dynamic-load-balancing-with-nginx/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
