<?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>gj |</title>
	<atom:link href="https://blog.gaiterjones.com/category/docker/scaled-services/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.gaiterjones.com/category/docker/scaled-services/</link>
	<description>gaiterjones</description>
	<lastBuildDate>Thu, 14 Feb 2019 12:58:13 +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 Magento 2 Service Scaling and Dynamic Load Balancing with VARNISH</title>
		<link>https://blog.gaiterjones.com/docker-mono-host-magento-2-service-scaling-and-dynamic-load-balancing-with-varnish/</link>
					<comments>https://blog.gaiterjones.com/docker-mono-host-magento-2-service-scaling-and-dynamic-load-balancing-with-varnish/#comments</comments>
		
		<dc:creator><![CDATA[PAJ]]></dc:creator>
		<pubDate>Sun, 18 Jun 2017 16:30:30 +0000</pubDate>
				<category><![CDATA[Docker]]></category>
		<category><![CDATA[Magento2]]></category>
		<category><![CDATA[Scaled Services]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[magento2]]></category>
		<category><![CDATA[scale]]></category>
		<category><![CDATA[varnish]]></category>
		<guid isPermaLink="false">http://blog.gaiterjones.com/?p=1674</guid>

					<description><![CDATA[As I started building Docker service container images for Magento 2 CE development I got interested in the concept of dynamic service load balancing on a single host. Having already...<a class="more-link" href="https://blog.gaiterjones.com/docker-mono-host-magento-2-service-scaling-and-dynamic-load-balancing-with-varnish/" title="Continue reading">Continue reading</a>]]></description>
										<content:encoded><![CDATA[<p>As I started building <a href="https://blog.gaiterjones.com/docker-magento-2-development-deployment-php7-apache2-4-redis-varnish-scaleable/">Docker service container images for Magento 2</a> CE development I got interested in the concept of dynamic service load balancing on a single host. Having already created a <a href="https://blog.gaiterjones.com/docker-mono-host-service-scaling-and-dynamic-load-balancing-with-nginx/">dynamic load balancer using NGINX</a> I wanted to do the same for Magento 2 using <strong>Varnish</strong>.</p>
<p>My Magento 2 service container images include REDIS services for the Magento session and system cache and a Varnish service for the full page cache. Varnish is an HTTP accelerator designed for content-heavy dynamic web sites and is the recommended full page cache system for Magento 2.</p>
<figure id="attachment_1690" aria-describedby="caption-attachment-1690" style="width: 1280px" class="wp-caption aligncenter"><img fetchpriority="high" decoding="async" src="https://blog.gaiterjones.com/wp-content/uploads/2017/06/magento2-docker-network.png" alt="" width="1280" height="1024" class="size-full wp-image-1690" /><figcaption id="caption-attachment-1690" class="wp-caption-text">Magento 2 Docker Network</figcaption></figure>
<p>Varnish is configured via the Varnish Configuration Language (VCL) config file. By configuring multiple backend servers in VCL and grouping them into a director we can create a Varnish <a href="https://varnish-cache.org/docs/trunk/users-guide/vcl-backends.html">load balancer</a>.</p>
<span class="collapseomatic collapse" id="id660cd11705419"  tabindex="0" title="Varnish Magento 2 load balancing VCL"    >Varnish Magento 2 load balancing VCL</span><div id="target-id660cd11705419" class="collapseomatic_content ">
<pre>[text]
backend magento2_php_apache_1 {
.host = &quot;x.x.x.2&quot;;
.port = &quot;80&quot;;
.probe = {.request =
&quot;GET /healthcheck.php HTTP/1.1&quot; &quot;Host: magento2.gaiterjones.com&quot; &quot;Connection: close&quot; &quot;Accept: text/html&quot;;.timeout = 1s;.interval = 30s;.window = 10;.threshold = 8;}
}
backend magento2_php_apache_2 {
.host = &quot;x.x.x.8&quot;;
.port = &quot;80&quot;;
.probe = {.request =
&quot;GET /healthcheck.php HTTP/1.1&quot; &quot;Host: magento2.gaiterjones.com&quot; &quot;Connection: close&quot; &quot;Accept: text/html&quot;;.timeout = 1s;.interval = 30s;.window = 10;.threshold = 8;}
}
backend magento2_php_apache_3 {
.host = &quot;x.x.x.10&quot;;
.port = &quot;80&quot;;
.probe = {.request =
&quot;GET /healthcheck.php HTTP/1.1&quot; &quot;Host: magento2.gaiterjones.com&quot; &quot;Connection: close&quot; &quot;Accept: text/html&quot;;.timeout = 1s;.interval = 30s;.window = 10;.threshold = 8;}
}
backend magento2_php_apache_4 {
.host = &quot;x.x.x.11&quot;;
.port = &quot;80&quot;;
.probe = {.request =
&quot;GET /healthcheck.php HTTP/1.1&quot; &quot;Host: magento2.gaiterjones.com&quot; &quot;Connection: close&quot; &quot;Accept: text/html&quot;;.timeout = 1s;.interval = 30s;.window = 10;.threshold = 8;}
}
backend magento2_php_apache_5 {
.host = &quot;x.x.x.9&quot;;
.port = &quot;80&quot;;
.probe = {.request =
&quot;GET /healthcheck.php HTTP/1.1&quot; &quot;Host: magento2.gaiterjones.com&quot; &quot;Connection: close&quot; &quot;Accept: text/html&quot;;.timeout = 1s;.interval = 30s;.window = 10;.threshold = 8;}
}
sub vcl_init {
new cluster1 = directors.round_robin();
cluster1.add_backend(magento2_php_apache_1);
cluster1.add_backend(magento2_php_apache_2);
cluster1.add_backend(magento2_php_apache_3);
cluster1.add_backend(magento2_php_apache_4);
cluster1.add_backend(magento2_php_apache_5);
}
[/text]</pre>
</div>
<h2>Scale Manager</h2>
<p>I use a service container called <em>php-apache</em> to run the Magento core code. Docker compose allows you to scale containers on a single host, so for example with the command</p>
<p><em>docker-compose scale php-apache=5</em></p>
<p>I can quickly scale the Magento2 php-apache service up to 5 containers, that is 5 instances of the php-apache service running the Magento2 core php code and apache web server.</p>
<p>The scale manager service monitors docker processes and detects when the php-apache service is scaled up or down. When a change is detected it dynamically creates the VCL configuration required to tell Varnish about the changes and applies these to the Varnish server.</p>
<figure id="attachment_1676" aria-describedby="caption-attachment-1676" style="width: 500px" class="wp-caption aligncenter"><img decoding="async" style="padding: 1px; border: 1px solid #021a40;" src="https://blog.gaiterjones.com/wp-content/uploads/2017/06/magento2-scale-demo.jpg" alt="" width="500" height="487" class="size-full wp-image-1676" /><figcaption id="caption-attachment-1676" class="wp-caption-text">Magento 2 Scale Demo</figcaption></figure>
<h2>Magento 2 Scale Demo</h2>
<p>In the video below I start with one running instance of the Magento 2 service, I then scale the service up to 5 and using the scale demo page (which is not cached by Varnish) on my <a href="http://magento2.gaiterjones.com/">Magento 2 dev site</a> to analyse the scale manager output data and display the running Magento 2 service containers.</p>
<p>As I refresh the page the Magento 2 container rendering the page is highlighted in yellow. you can see how Varnish round robins between the 5 available containers. Finally I scale the service back down and manually update the Varnish server config via the command line php script on the manager.</p>
<p>Another way to see the round robin connection distribution from varnish is to load up a <em>phpinfo()</em> page, you will see the SERVER_ADDR variable changing with each container that processes the request.</p>
<p><img decoding="async" src="https://blog.gaiterjones.com/dropbox/docker-magento2-scaledemox2.gif" alt="docker scale service dynamic load balancing with nginx demo video" class="aligncenter" /></p>
<p>Would you actually run 5 Magento 2 php-apache services? Good question! For true resilience and load balancing you would need multiple hosts and a Docker swarm. Multiple service containers on a single host do at least provide a simple level of redundancy and host server load balancing, I would definitely consider using it in production especially with a multi core server.</p>
<p>Many thanks to <a href="https://github.com/fballiano/docker-magento2">Fabrizio Balliano</a> for the inspiration and assistance.</p>
<p>See <a href="https://blog.gaiterjones.com/docker-magento-2-development-deployment-php7-apache2-4-redis-varnish-scaleable/">this post</a> for instructions on how to deploy this Magento 2 development environment.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.gaiterjones.com/docker-mono-host-magento-2-service-scaling-and-dynamic-load-balancing-with-varnish/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<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 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>
