{"id":1340,"date":"2019-02-17T09:30:43","date_gmt":"2019-02-17T09:30:43","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw93\/?p=1340"},"modified":"2019-03-07T17:16:28","modified_gmt":"2019-03-07T17:16:28","slug":"magento-and-docker-magento-deployment","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw93\/index.php\/2019\/02\/17\/magento-and-docker-magento-deployment\/","title":{"rendered":"Magento and Docker | Magento Deployment"},"content":{"rendered":"<p><a href=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/02\/02162431\/magento-logo2.png\"><img decoding=\"async\" src=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/02\/02162431\/magento-logo2.png\" alt=\"magento-logo2\" \/><\/a>A<br \/>\nlittle over a month ago I <a href=\"http:\/\/rancher.com\/creating-a-magento-cluster-docker-and-rancher-on-aws\/\">wrote<br \/>\nabout<\/a><br \/>\nsetting up a Magento cluster on Docker using Rancher. At the I<br \/>\nidentified some short comings of Rancher such as its lack of support fot<br \/>\nload-balancing. Rancher released support for <a href=\"http:\/\/rancher.com\/load-balancing-support-for-docker-in-rancher\/\">load<br \/>\nbalancing<\/a> and <a href=\"http:\/\/rancher.com\/rancher-adds-support-for-docker-machine-provisioning\/\">docker<br \/>\nmachine<\/a> with<br \/>\n0.16, and I would like to revisit our Magento deployment to cover the<br \/>\nuse of load balancers for scalability as well as availability.<br \/>\nFurthermore, I would also like to cover how the docker machine<br \/>\nintegration makes it easier to launch Rancher compute nodes directly<br \/>\nfrom the Rancher UI.<\/p>\n<h3>Amazon Setup<\/h3>\n<p>As before we will be running our cluster on top of AWS hence if you have<br \/>\nnot already done so follow the steps outlined in the <em>Amazon Environment<br \/>\nSetup<\/em> section of the earlier tutorial to setup an ssh key pair and a<br \/>\nsecurity group. However, unlike earlier we will be using the Rancher UI<br \/>\nto launch compute nodes and will require an <em>Access Key ID<\/em> and <em>Secret<br \/>\nAccess Key<\/em>. To create your key and secret click through to the <em>IAM<\/em><br \/>\nservice and select <em>Users<\/em> from the menu on the left. Click the <em>Create<br \/>\nUser<\/em> button and specify <em>rancher<\/em> as the user name in the subsequent<br \/>\nscreen and click <em>Create<\/em>. You will be given the Access Key ID and<br \/>\nSecret Access Key in the dialogue shown below, keep the information safe<br \/>\nas there is no way to recover the secret and you will need this later.<\/p>\n<p><a href=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/iam-key.png\"><img decoding=\"async\" src=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/iam-key.png\" alt=\"iam-key\" \/><\/a>Once<br \/>\nyou have created the IAM user you will also need to give it permissions<br \/>\nto create Amazon Ec2 Instances. To do so select rancher from the user<br \/>\nlist and click <em>Attach Policy<\/em> in the <em>Managed Policies<\/em> section<em>.<\/em> Add<br \/>\nthe <em>AmazonEC2FullAccess<\/em> policy to the Rancher user so that we are able<br \/>\nto create the required resources from the Rancher UI when creating<br \/>\ncompute nodes. Full access is a little more permissive tan required<br \/>\nhowever, for the sake of brevity we are not creating custom policy.<\/p>\n<p><a href=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/Screen-Shot-2015-04-27-at-9.03.52-PM.png\"><img decoding=\"async\" src=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/Screen-Shot-2015-04-27-at-9.03.52-PM.png\" alt=\"Screen Shot 2015-04-27 at 9.03.52\nPM\" \/><\/a><\/p>\n<h3>Rancher Setup<\/h3>\n<p>After setting up the AWS environment, follow the steps outlined in the<br \/>\n<em>Rancher Server Launch<\/em> section of the earlier <a href=\"http:\/\/rancher.com\/creating-a-magento-cluster-docker-and-rancher-on-aws\/\">Magento<br \/>\ntutorial<\/a><br \/>\nto bring up your Rancher server and browse to<br \/>\n<a href=\"http:\/\/RANCHER_SERVER_IP:8080\/\">http:\/\/RANCHER_SERVER_IP:8080\/<\/a>. *Be sure you are using a version of<br \/>\nRancher after 0.16.* Load the <em>Hosts<\/em> tab using the respective option<br \/>\nin the left-side menu and click <em>+ Add Host<\/em> to add rancher compute<br \/>\nnodes. The first time you launch a compute node you will be prompted to<br \/>\nconfirm the IP address at which Rancher server is available to your<br \/>\ncompute nodes. Specify the Private IP address of the Amazon node on<br \/>\nwhich Rancher server is running and hit save.<\/p>\n<p><a href=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/Screen-Shot-2015-04-27-at-9.30.14-PM1.png\"><img decoding=\"async\" src=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/Screen-Shot-2015-04-27-at-9.30.14-PM1.png\" alt=\"Screen-Shot-2015-04-27-at-9.30.14-PM\" \/><\/a><\/p>\n<p>In the <em>Add Host<\/em> screen select the Amzon EC2 Icon and specify the<br \/>\nrequired information in order to launch a compute node. The required<br \/>\ninformation is shown below. Enter the access key and secret key that you<br \/>\ncreated earlier for the rancher IAM user. We are using a t2.micro<br \/>\ninstance for our tutorial however you would probably use a larger<br \/>\ninstance for your nodes. Select the same VPC as your Rancher server<br \/>\ninstance and specify <em>Rancher<\/em> as the security group to match the<br \/>\nsecurity group that you created earlier in the Environment Setup<br \/>\nsection. The compute nodes must be launched in a different availability<br \/>\nzone from the rancher server hence we select Zone c (Our Rancher Server<br \/>\nwas in Zone a) . This requirement is due to the fact that Docker Machine<br \/>\nuses the Public IP of compute agents to ssh into them from the Server.<br \/>\nHowever, a nodes public IP is not addressable from within its own<br \/>\nsubnet.<\/p>\n<p><a href=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/machine.png\"><img decoding=\"async\" src=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/machine.png\" alt=\"machine\" \/><\/a><\/p>\n<p>Repeat the steps above to launch five compute nodes; one for the MySQL<br \/>\ndatabase, two for the load-balanced Magento nodes and two for the load<br \/>\nbalancers themselves. I have labeled the nodes as DataNode, Magento1,<br \/>\nMagento2, LB1 and LB2. When all nodes come up you should be able to see<br \/>\nthem in the Rancher Server UI as shown below.<\/p>\n<p><a href=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/Screen-Shot-2015-04-27-at-10.45.09-PM1.png\"><img decoding=\"async\" src=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/Screen-Shot-2015-04-27-at-10.45.09-PM1.png\" alt=\"Screen-Shot-2015-04-27-at-10.45.09-PM\" \/><\/a><\/p>\n<h3>Magento Container Setup<\/h3>\n<p>Now that we have our Rancher deployment launched we can setup our<br \/>\nMagento containers. However before we launch our Magento containers we<br \/>\nmust first launch a MySQL container to serve as our database and<br \/>\nMemcached containers for caching. Let\u2019s launch our MySQL container first<br \/>\non one of the compute nodes. We do this by clicking the <em>+ Add<br \/>\nContainer<\/em> on the DataNode host. In the pop up menu we need to specify a<br \/>\nname for our container and <em>mysql<\/em> as the source image. Select <em>Advanced<br \/>\nOptions &gt; Command<\/em> &gt; <em>Environment Vars +<\/em> to add the four required<br \/>\nvariables: mysql root password, mysql user, mysql password, and mysql<br \/>\ndatabase. You may choose any values for these the root password and user<br \/>\npassword, however, the mysql user and database must be magento. After<br \/>\nadding all of these environment variables, hit create to create the<br \/>\ncontainer. Note that mysql is official Docker mysql image and details<br \/>\nof what is inside this container can be found on<br \/>\nits <a href=\"https:\/\/registry.hub.docker.com\/_\/mysql\/\">dockerhub<\/a> page.<\/p>\n<p><a href=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/envvars.png.png\"><img decoding=\"async\" src=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/envvars.png.png\" alt=\"envvars.png\" \/><\/a><\/p>\n<p>Next we will create the Memcached containers on the two magento compute<br \/>\nnodes, one on each of the Magento nodes. We again give the containers a<br \/>\nname (memcached1 and memcached2) and specify their source images<br \/>\nas <em>memcached.<\/em> The Memcached containers do not require any further<br \/>\nconfiguration and therefore we can just click create to setup the<br \/>\ncontainers. Details of the memcached official container we use can be<br \/>\nfound on<br \/>\nits <a href=\"https:\/\/registry.hub.docker.com\/_\/memcached\/\">dockerhub<\/a> page.<\/p>\n<p>Now we are ready to create the magento containers, On the Magento1 host<br \/>\ncreate a container named <em>magento1<\/em> using the image<br \/>\n<a href=\"https:\/\/github.com\/usmanismail\/docker-magento\/tree\/multi-node\">usman\/magento:multinode<\/a>.<br \/>\nYou need to specify the <em>MYSQL_HOST<\/em> and <em>MEMCACHED_HOST<\/em> environment<br \/>\nvariables using the container IPs that are listed in the Rancher UI.<br \/>\nNote that for Magento1 you should specify the IP of Memcached1.<br \/>\nSimilarly launch a second container called magento2 on the Magento2 host<br \/>\nand specify the mysql host and memcached host environment variables. In<br \/>\na few moments both your magento hosts should be up and ready. Note that<br \/>\nunlike before we did not have to link the mysql and memcached containers<br \/>\nto our magento containers. This is because Rancher now gives all<br \/>\ncontainers access to each other over a Virtual Private Network (VPN)<br \/>\nwithout the need for exposing ports or linking containers. Furthermore<br \/>\nwe will not need to expose ports on the Magento containers as we will<br \/>\nuse the same VPN to allow the load balancers to communicate with our<br \/>\nnodes.<\/p>\n<h3>Load balancer Setup<\/h3>\n<p>Now that your containers are up we can setup load balancers to split<br \/>\ntraffic onto the Magento containers. Select the <em>Balancing<\/em> tab in the<br \/>\nleft side menu then click <em>Balancers<\/em> and <em>+ Add Load Balancer<\/em>. In the<br \/>\nsubsequent screen you can specify a name and description for your load<br \/>\nbalancer. Next you can select the hosts on which to run balancer<br \/>\ncontainers run. in our case we can select both LB1 and LB2. We then need<br \/>\nto select the two Magento containers as targets. In the <em>Listening<br \/>\nPorts<\/em> section we need to specify that our Magento containers are<br \/>\nlistening for HTTP traffic on port 80 and that we want load balancers to<br \/>\nalso listen to http traffic on port 80.<\/p>\n<h3><a href=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/Screen-Shot-2015-04-29-at-9.22.12-PM.png\"><img decoding=\"async\" src=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/Screen-Shot-2015-04-29-at-9.22.12-PM.png\" alt=\"Screen Shot 2015-04-29 at 9.22.12 PM\" \/><\/a><\/h3>\n<p>Lastly, click on the <em>Health Check<\/em> tab and specify that the load<br \/>\nbalancers should send a <em>GET<\/em> request to the root URI every 2000 ms to<br \/>\ncheck that the container is still healthy. If three consecutive health<br \/>\nchecks fail then the container will be marked as unhealthy and no<br \/>\nfurther traffic will be routed to it until it can respond successfully<br \/>\nto two consecutive health checks. In a few moments your load balancers<br \/>\nwill be ready and you can load Magento on the public IP of either load<br \/>\nbalancer host. You will need to look for the IP in the Amazon EC2<br \/>\nconsole as the Rancher UI only shows the private IP of the nodes. Once<br \/>\nyou load the Magento UI follow the steps outlined in the previous<br \/>\ntutorial to setup your connection the MySQL and to setup a magento<br \/>\naccount.<\/p>\n<p><a href=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/Screen-Shot-2015-04-28-at-10.22.36-PM.png\"><img decoding=\"async\" src=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/Screen-Shot-2015-04-28-at-10.22.36-PM.png\" alt=\"Screen Shot 2015-04-28 at 10.22.36\nPM\" \/><\/a><\/p>\n<p>###<\/p>\n<h3>DNS Round-robin Setup using Amazon Route 53<\/h3>\n<p>Now that we have our load balancers up and running we can split traffic<br \/>\nonto our two Magento contianers but we still must send our requests to<br \/>\none balancer or the other. To enable routing to both load balancers<br \/>\ntransparently we need to setup DNS round-robin. For this you may use any<br \/>\nDNS provider of your choice but since we are using Amazon EC2 we will<br \/>\nuse Amazon\u2019s Route 53 service. Use the Top menu to select the <em>Route<br \/>\n53<\/em> service and select <em>Hosted Zones<\/em> from the left menu. If you don\u2019t<br \/>\nalready have a registered domain and hosted zone you may have to create<br \/>\none. We are using the <em>rancher-magento.com<\/em> domain and hosted zone. In<br \/>\nyour hosted zone click the <em>Create Record Set<\/em> button and specify a<br \/>\nsubdomain such as <em>lb.rancher-magento.com<\/em> in the form which loads to<br \/>\nthe right of the screen*. S*elect type <em>A &#8211; IPv4 address<\/em> and specify<br \/>\nthe public IP address of one of your load balancer hosts. In the<br \/>\n<em>Routing Policy<\/em> section select <em>Weighted<\/em>, and enter 10 as the weight.<br \/>\nEnter 1 as the Set ID and click <em>Save Record Set.<\/em> Repeat exactly the<br \/>\nsame process once more but use the public IP of the second load-balancer<br \/>\nhost. This pair of DNS entries is specifying that we want to route<br \/>\nclients who ask for <em>lb.rancher-magento.com<\/em> to the two specified IPs.<br \/>\nSince the IPs records have the same weight the traffic will be split<br \/>\nevenly between the two load balancers. We can now load up our Magento UI<br \/>\nusing http:\/\/<em>lb.rancher-magento.com<\/em> instead of having to specify the<br \/>\nIP.<\/p>\n<p><a href=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/Screen-Shot-2015-04-29-at-9.47.28-PM.png\"><img decoding=\"async\" src=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/04\/04211806\/Screen-Shot-2015-04-29-at-9.47.28-PM.png\" alt=\"Screen Shot 2015-04-29 at 9.47.28\nPM\" \/><\/a><\/p>\n<h3>Wrapping up<\/h3>\n<h3><a href=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/05\/04211806\/rancher-machine.png\"><img decoding=\"async\" src=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2015\/05\/04211806\/rancher-machine.png\" alt=\"rancher-machine\" \/><\/a><\/h3>\n<p>Putting it all together we get a cluster setup as shown above. Using the<br \/>\nDNS entries our web browsers are directed to one of the load balancers<br \/>\nLB1, or LB2. By having two load balancers we have split traffic and<br \/>\nhence reduced the load on each of our load balancer instances. The load<br \/>\nbalancers will then proxy traffic to either Magento1 or Magento2. This<br \/>\nagain allows us to spread the load to the separate containers running on<br \/>\ntheir own hosts. We have setup only two Magento containers but your<br \/>\ncould setup as many as you need. Furthermore, the health check setup<br \/>\nensures that if one of the Magento containers fails the traffic will<br \/>\nquickly be diverted to the remaining container without human<br \/>\nintervention. Each of the Magento containers has a Memcached server<br \/>\nrunning on its own host to provide fast access to frequently used data.<br \/>\nHowever, both magento containers use the same MySQL container to ensure<br \/>\nconsistency between the two containers. By using Rancher\u2019s docker<br \/>\nmachine support we were able to launch all hosts (other than Rancher<br \/>\nServer) directly from the Rancher UI. In addition, due to Rancher\u2019s VPN<br \/>\nwe did not have to expose ports on any of our containers nor did we have<br \/>\nto link containers. This greatly simplifies the Magento container setup<br \/>\nlogic. With support for load balancers and machine (as well as docker<br \/>\ncompose coming soon), Rancher is becoming a much more viable option for<br \/>\nrunning large scale user facing deployments.<\/p>\n<p>To learn more about Rancher, please join us for one of our monthly<br \/>\nonline meetups. You can register for an upcoming meetup by following the<br \/>\nlink below.<\/p>\n<p><a href=\"https:\/\/rancher.com\/magento-with-docker-updated-using-load-balancers-for-scalability-and-availability\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A little over a month ago I wrote about setting up a Magento cluster on Docker using Rancher. At the I identified some short comings of Rancher such as its lack of support fot load-balancing. Rancher released support for load balancing and docker machine with 0.16, and I would like to revisit our Magento deployment &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw93\/index.php\/2019\/02\/17\/magento-and-docker-magento-deployment\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Magento and Docker | Magento Deployment&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1340","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/1340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/comments?post=1340"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/1340\/revisions"}],"predecessor-version":[{"id":1411,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/1340\/revisions\/1411"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/media?parent=1340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/categories?post=1340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/tags?post=1340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}