Use Docker swarm to create clusters and validate effects __ Cluster

Source: Internet
Author: User
Tags docker ps docker run docker swarm

The previous lecture explained how to install the Docker and how to get and start the container under Docker. Here's how to create a cluster using Docker. Docker as a new container technology, compared to traditional virtual machines, the biggest advantage is lightweight. The main purpose of using a virtual machine was to use a virtual machine to start some isolated applications. But in order to start these applications, you have to create huge virtual machines. So why don't we just start the quarantined program without having to start the virtual machine. This does not save a lot of hard disk and memory storage space.

Not only that, in today's Bs+ms mode popular global situation, we need to provide online services to others, and once the increase in the number of concurrent visits, our servers are often difficult to load. Therefore, it is very difficult to meet the application requirements by using a single server to provide services externally. This time requires multiple machines to provide services in parallel, but no matter how many in the background of the machine running for the user is not known, only to follow a unified address to access. This is the cluster. Cluster technology has been applied to the Internet or government cloud environment for many years. In the past, the model of virtual machine was basically run through the mode of "load balancer + logical Host", that is, the external multiple requests are distributed by the load balancer, the request is distributed to a logical host for processing, and the processing result is returned to the client. There are two kinds of load balancer, hardware and software. There are many types of hardware, and prices are often expensive. Software load balancer has Apache, Nginx and so on several kinds, most free, basically can meet the daily needs. However, either way, dynamic expansion is not very easy, not to have downtime is to write complex modules to mount to Apache or Nginx.

When the Docker container appears, everything becomes so simple, the configuration becomes simple, dynamic expansion or contraction becomes easy.

If you want to use Docker to create a container cluster, the lower version of the Docker is not easy to get, the operation is more complex, it is recommended to upgrade the Docker to the latest version (version 1.13). Here take 1.13.1 as an example to introduce how to use swarm to build Docker cluster.

The first step: first in the host terminal to execute the following command:

$ sudo docker run--RM swarm create

The generated token 6cce80f59f4292ba8f4e085950c6f480 must be saved and then used to create a clustered environment.

Next, we use the following command to start multiple containers on the host:

$docker run-i-t-d Tomcat:latest/bin/bash


As shown in the figure above, we ran two commands, so we created two container instances. The parameter "-D" is added to the command to allow the container to run in daemon mode so that the container shuts down automatically after the terminal enters "exit" to disconnect the container.

Next look at the IP address of each container and enter the following command in the host to view all the running container instances:

$sudo Docker PS


You can see from the illustration above that two Tomcat container instances are running, and then run the following command into the first container:

$docker exec-it E7bc718f8429/bin/bash
The IP address of the container can be viewed by entering the container with ID e7bc718f8429 and then running the IP Addr command in the container:


As you can see, the container has an IP address of 172.17.0.3, and similar, you can see that the IP address of another container is 172.17.0.2 (Note: If no additional configuration is made, and no IP address is specified when the container is started, Then the IP address of the container usually starts from 172.17.0.1 or 172.17.0.2, if the host's IP address is 127.17.0.1, the container's IP address is allocated from the 172.17.0.2, or it is started from 172.17.0.1.

Step two: Run the following command in the host to add two containers to the cluster (if you are in the container as shown above, enter the Exit command in the terminal to return to the host):

$sudo Docker run–-rm Swarm join–addr=172.17.0.2:2375 


The hint in the previous illustration indicates that the addition was successful, but the CTRL + C key combination must be pressed to exit. Similarly, we can add another IP address 172.17.0.3 to the swarm cluster.
Step three: Run the following command in the host terminal to start Swarm Manager:

$sudo Docker run–d–p 2376:2375 Swarm manage token://6cce80f59f4292ba8f4e085950c6f480


As shown above, after executing the command, a new ID indicates that the startup was successful, and the Swarm cluster was completed. You can run the following command to view the nodes contained in the cluster:

$docker run–-rm Swarm list token://6cce80f59f4292ba8f4e085950c6f480

As you can see, there are currently two nodes in the cluster: 172.17.0.3 and 172.17.0.2. Let's verify the effect of the cluster. Let's go into each container and start Tomcat:


Then use the host's external IP address to access tomcat:

As you can see, Tomcat can already be accessed through the cluster.

To verify the effect of the cluster, we can go into the container again and close the Tomcat in turn. First go into the 172.17.0.2 to turn off Tomcat:

Root@647d583c2f14:/usr/local/tomcat/bin#sh shutdown.sh
When you refresh the page in the browser, you will find that you can still access the Tomcat home:

And then enter 172.17.0.3 to turn off Tomcat:

Root@e7bc718f8429:/usr/local/tomcat/bin#sh shutdown.sh

When you refresh the page in the browser, you will receive the following questions:

The page cannot be accessed through the cluster at this time. And then enter 172.17.0.3 to start Tomcat:


And then refresh the page:


Ok,swarm cluster is done.

Come back and we'll analyze it. Many of the online tutorials on the Swarm join command in the IP address is not very clear, in the end is which machine IP. Host or container. After testing, I found that swarm join can only write the IP address of the container initiated by the machine, otherwise, although it can add success, the cluster mechanism does not work.

We hope to learn Docker cluster help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.