{"id":7277,"date":"2018-03-21T11:07:38","date_gmt":"2018-03-21T03:07:38","guid":{"rendered":"http:\/\/rmohan.com\/?p=7277"},"modified":"2018-03-21T12:05:34","modified_gmt":"2018-03-21T04:05:34","slug":"7277","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7277","title":{"rendered":"Docker Swarm on Centos 7"},"content":{"rendered":"<p><strong>Install of docker<\/strong><\/p>\n<p>Lets remove all the unnecessary software on the machine<\/p>\n<p>yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine<\/p>\n<p>now we have to install dependancy software for Docker<\/p>\n<p>yum install -y yum-utils device-mapper-persistent-data lvm2<\/p>\n<p><strong>Let add a docker repo<\/strong><\/p>\n<p>yum-config-manager &#8211;add-repo https:\/\/download.docker.com\/linux\/centos\/docker-ce.repo<\/p>\n<p>Enable the yum docker repo on the centos 7.4 server<\/p>\n<p>yum-config-manager &#8211;enable docker-ce-edge<\/p>\n<p>List current docker<\/p>\n<p>yum list docker-ce &#8211;showduplicates | sort -r<\/p>\n<p><strong>Install latest docker community edition on the server<\/strong><\/p>\n<p>yum install docker-ce -y<\/p>\n<p>After install ensure we have enable the services and start the docker<\/p>\n<p>systemctl start docker<\/p>\n<p>systemctl enable docker<\/p>\n<p>systemctl status docker<\/p>\n<p>download https:\/\/github.com\/MohanRamadoss\/docker\/tree\/master\/centos7-nginx<\/p>\n<p>docker build -t=&#8221;nginx\/centos-nginx&#8221; .<\/p>\n<p><strong>Initialize a swarm. The docker engine targeted by this command becomes a manager in the newly created single-node swarm.<\/strong><\/p>\n<p>[root@clusterserver1 centos7-nginx]# docker swarm init<br \/>\nSwarm initialized: current node (d58oju29e07c4u59tnf5e1ysn) is now a manager.<\/p>\n<p>To add a worker to this swarm, run the following command:<\/p>\n<p>docker swarm join &#8211;token SWMTKN-1-0uppeopy2mg0a8cblugyv6gje9gssnhxtl5qk0sa5nqubojap8-b1rzpxnxxzu0ubt7q9mhtn0dr 192.168.1.20:2377<\/p>\n<p>To add a manager to this swarm, run &#8216;docker swarm join-token manager&#8217; and follow the instructions.<\/p>\n<p>In the output from the initialization command (the swarm join token is provided), use the token for joining new worker nodes into the cluster.<\/p>\n<p>If you accidentally close your terminal and can&#8217;t remember the token, not to worry, use the following command to retrieve the command with the join-token for either joining a new manager or a new worker.<\/p>\n<p>OK, now we have a swarm cluster with only one manager node:<\/p>\n<p>[root@clusterserver3 centos7-nginx]# docker swarm join &#8211;token SWMTKN-1-0uppeopy2mg0a8cblugyv6gje9gssnhxtl5qk0sa5nqubojap8-b1rzpxnxxzu0ubt7q9mhtn0dr 192.168.1.20:2377<\/p>\n<p>[root@clusterserver4 centos7-nginx]# docker swarm join &#8211;token SWMTKN-1-0uppeopy2mg0a8cblugyv6gje9gssnhxtl5qk0sa5nqubojap8-b1rzpxnxxzu0ubt7q9mhtn0dr 192.168.1.20:2377<\/p>\n<p>root@clusterserver1 centos7-nginx]# docker node ls<br \/>\nID HOSTNAME STATUS AVAILABILITY MANAGER STATUS<br \/>\nd58oju29e07c4u59tnf5e1ysn * clusterserver1.rmohan.com Ready Active Leader<br \/>\ns0vxqeav20pzsnuegh6re5oc7 clusterserver3.rmohan.com Ready Active<br \/>\nk9eaqn6279iozux3edmpu92jx clusterserver4.rmohan.com Ready Active<br \/>\n[root@clusterserver1 centos7-nginx]#<\/p>\n<p><strong>Get the Service Up<\/strong><\/p>\n<p>Hope you&#8217;re rubbing your hands together like I am at the moment. Yes, finally, we get to the stage of getting the customized Apache image onto the Docker Swarm cluster.<\/p>\n<p>The following steps need to be executed on the Swarm manager node, so let&#8217;s jump on clusterserver1.<\/p>\n<p>docker service create &#8211;name swarm_cluster &#8211;replicas=2 -p 80:80 nginx\/centos-nginx:latest<\/p>\n<p>[root@clusterserver1 centos7-nginx]# docker service update &#8211;publish-add 80:80 swarm_cluster<br \/>\nswarm_cluster<br \/>\noverall progress: 3 out of 3 tasks<br \/>\n1\/3: running [==================================================&gt;]<br \/>\n2\/3: running [==================================================&gt;]<br \/>\n3\/3: running [==================================================&gt;]<br \/>\nverify: Service converged<br \/>\n[root@clusterserver1 centos7-nginx]#<\/p>\n<p>[root@clusterserver1 centos7-nginx]# docker service inspect swarm_cluster<\/p>\n<p>[root@clusterserver1 centos7-nginx]# docker service ps swarm_cluster<\/p>\n<p>[root@clusterserver1 centos7-nginx]# docker ps &#8211;format &#8216;table {{.ID}} {{.Image}} {{.Ports}}&#8217;<\/p>\n<p>&nbsp;<\/p>\n<p>[root@clusterserver1 centos7-nginx]# docker service ls<br \/>\nID NAME MODE REPLICAS IMAGE PORTS<br \/>\nt6vd2agk6wdg swarm_cluster replicated 2\/2 nginx\/centos-nginxv1:latest *:80-&gt;80\/tcp<\/p>\n<p><strong>Let kill the docker instance in one of the node we can see the docker container is spin up<\/strong><\/p>\n<p>root@clusterserver1 centos7-nginx]# docker service ps swarm_cluster<br \/>\nID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS<br \/>\npjmpcau7uylu swarm_cluster.1 nginx\/centos-nginx:latest clusterserver1.rmohan.com Ready Ready less than a second ago<br \/>\ns7s802sc0dpu \\_ swarm_cluster.1 nginx\/centos-nginx:latest clusterserver3.rmohan.com Shutdown Failed 4 seconds ago &#8220;task: non-zero exit (137)&#8221;<br \/>\n47542368ylsw swarm_cluster.2 nginx\/centos-nginx:latest clusterserver4.rmohan.com Running Running 15 minutes ago<br \/>\n[root@clusterserver1 centos7-nginx]#<\/p>\n<p><strong>Horizontal Scaling<\/strong><\/p>\n<p>One of the coolest things ab0ut cluster orchestration is auto-scaling, which is also a great feature of Docker Swarm.<\/p>\n<p>At the moment, I&#8217;ve got 2 replicas hosting &#8220;swarm_cluster&#8221; service, and I want to add one more, which can be simply done by running the following command on the swarm manager<\/p>\n<p>[root@clusterserver1 centos7-nginx]# docker service scale swarm_cluster=6<br \/>\nswarm_cluster scaled to 6<br \/>\noverall progress: 6 out of 6 tasks<br \/>\n1\/6: running [==================================================&gt;]<br \/>\n2\/6: running [==================================================&gt;]<br \/>\n3\/6: running [==================================================&gt;]<br \/>\n4\/6: running [==================================================&gt;]<br \/>\n5\/6: running [==================================================&gt;]<br \/>\n6\/6: running [==================================================&gt;]<br \/>\nverify: Service converged<br \/>\n[root@clusterserver1 centos7-nginx]#<\/p>\n<p>[root@clusterserver1 centos7-nginx]# docker service ps swarm_cluster<br \/>\nID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS<br \/>\ns7s802sc0dpu swarm_cluster.1 nginx\/centos-nginx:latest clusterserver3.rmohan.com Running Running 13 minutes ago<br \/>\n47542368ylsw swarm_cluster.2 nginx\/centos-nginx:latest clusterserver4.rmohan.com Running Running 13 minutes ago<br \/>\nz6c82pw2nd6o swarm_cluster.3 nginx\/centos-nginx:latest clusterserver1.rmohan.com Running Running 13 minutes ago<br \/>\nwu7i6bdbqfxs swarm_cluster.4 nginx\/centos-nginx:latest clusterserver1.rmohan.com Running Running 8 minutes ago<br \/>\nvax5efeo3ay1 swarm_cluster.5 nginx\/centos-nginx:latest clusterserver3.rmohan.com Running Running 8 minutes ago<br \/>\nfzoy80qhv219 swarm_cluster.6 nginx\/centos-nginx:latest clusterserver4.rmohan.com Running Running 8 minutes ago<\/p>\n<p><strong>Rolling Update<\/strong><\/p>\n<p>The last thing I want to demonstrate is a rolling update of the service by making a minor change on the index.html file.<\/p>\n<p>Then I need to rebuild my Docker image and push it to my public DockerHub repository; please refer to the previous sections for the details.<\/p>\n<p>Once I&#8217;ve got my new image available in the remote repository, I run the following command on the swarm manager node &#8211; clusterserver1:<\/p>\n<p>docker build -t=&#8221;nginx\/centos-nginxv1&#8243; .<\/p>\n<p>docker service update &#8211;image nginx\/centos-nginxv1:latest swarm_cluster<\/p>\n<p>[root@clusterserver1 centos7-nginx]# docker service update &#8211;image nginx\/centos-nginxv1:latest swarm_cluster<br \/>\nimage nginx\/centos-nginxv1:latest could not be accessed on a registry to record<br \/>\nits digest. Each node will access nginx\/centos-nginxv1:latest independently,<br \/>\npossibly leading to different nodes running different<br \/>\nversions of the image.<\/p>\n<p>swarm_cluster<br \/>\noverall progress: 2 out of 2 tasks<br \/>\n1\/2: running [==================================================&gt;]<br \/>\n2\/2: running [==================================================&gt;]<br \/>\nverify: Service converged<br \/>\n[root@clusterserver1 centos7-nginx]#<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Cleaning up<\/strong><\/p>\n<p>[root@clusterserver1 centos7-nginx]# docker service ls<br \/>\nID NAME MODE REPLICAS IMAGE PORTS<br \/>\nt6vd2agk6wdg swarm_cluster replicated 2\/2 nginx\/centos-nginxv1:latest *:80-&gt;80\/tcp<\/p>\n<p>$ docker service rm swarm_cluster<br \/>\nswarm_cluster<\/p>\n<p>$ docker service ls<br \/>\nID NAME MODE REPLICAS IMAGE PORTS<\/p>\n<p>$ docker swarm leave &#8211;force<\/p>\n<p>Node left the swarm.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Install of docker<\/p>\n<p>Lets remove all the unnecessary software on the machine<\/p>\n<p>yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine<\/p>\n<p>now we have to install dependancy software for Docker<\/p>\n<p>yum install -y yum-utils device-mapper-persistent-data lvm2<\/p>\n<p>Let add a docker repo<\/p>\n<p>yum-config-manager &#8211;add-repo https:\/\/download.docker.com\/linux\/centos\/docker-ce.repo<\/p>\n<p>Enable the yum docker repo on the centos [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7277"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7277"}],"version-history":[{"count":6,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7277\/revisions"}],"predecessor-version":[{"id":7283,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7277\/revisions\/7283"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}