Docker swarm cluster construction practice



Docker swarm is a native cluster management tool of docker. It can organize multiple docker hosts (nodes) into a cluster, so that containers can form a cross host subnet network. In addition, docker swarm also has built-in functions such as API, service discovery and high availability for deploying and managing clusters.

Here, in the windows 10 environment, first use the docker machine command tool to create multiple virtual machines (3 managers and 2 workers), and use docker swarm to build a cluster.

Add a virtual switch

About how to install docker on windows10, Download docker for window from the official website and install it according to the instructions (note to open Hyper-V), which is omitted here. Docker for window has built-in docker machine tool, so there is no need to install it separately.

After docker for window is installed. Open the window menu, search ‘hyper’, and select hyper manager to open:

Click the on the rightVirtual switch manager, the window opens:

clickNew virtual network switch, select in the window on the rightexternal, and clickCreate a virtual switch Button. Next, fill in the name in the new window on the right, such as here:Primary Virtual Switch, select the connection type asExternal network, and finally clickdetermine。 A virtual switch is created.

Create virtual host

reachboot2dockerDownload on pageboot2docker.isoFiles, placing inC:\Users\<your-name>\.docker\machine\cache(note that \ < your name > is replaced by the user name on your PC. of course, you can directly execute the virtual machine creation command without this step, but it is slow).

Next, create a virtual machine:

docker-machine create -d hyperv --hyperv-virtual-switch "Primary Virtual Switch" manager1

The command line output is as follows:

C:\WINDOWS\system32>docker-machine create -d hyperv --hyperv-virtual-switch "Primary Virtual Switch" manager1
Running pre-create checks...
Creating machine...
(manager1) Unable to get the latest Boot2Docker ISO release version:  Get
docker/releases/latest: EOF
(manager1) Copying C:\Users\hugh\.docker\machine\cache\boot2docker.iso to C:\Users\<your-name>\.docker\machine\machines\manager1\boot2docker.iso...
(manager1) Creating SSH key...
(manager1) Creating VM...
(manager1) Using switch "Primary Virtual Switch"
(manager1) Creating VHD
(manager1) Starting VM...
(manager1) Waiting for host to start...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env manager1

It will be created in about 1-2 minutes. Then, create other virtual machines in the same way, and execute the command line in turn:

docker-machine create -d hyperv --hyperv-virtual-switch "Primary Virtual Switch" manager2
docker-machine create -d hyperv --hyperv-virtual-switch "Primary Virtual Switch" manager3
docker-machine create -d hyperv --hyperv-virtual-switch "Primary Virtual Switch" worker1
docker-machine create -d hyperv --hyperv-virtual-switch "Primary Virtual Switch" worker2

In this way, we created five virtual hosts. implementdocker-machine lsView the created virtual machine, and the results are as follows:

Remember the IP address of each node, which will be useful later.

To view the Manager1 virtual host environment:

docker-machine env manager1

The output results are as follows:

SET DOCKER_CERT_PATH=C:\Users\hugh\.docker\machine\machines\manager1
REM Run this command to configure your shell:
REM     @FOR /f "tokens=*" %i IN ('docker-machine env manager1') DO @%i

Create swarm cluster

Initialize a swarm cluster

We can use docker machine’s SSH tool to connect to the virtual machine. Which virtual machine needs to be logged in to execute the command can be added in front of the command linedocker-machine ssh <host-name>Indicates which host to connect to. So you can initialize a swarm by executing the command:

docker-machine ssh manager1 docker swarm init --advertise-addr --listen-addr

--advertise-addrSpecifies the IP and port that other nodes connect to the current node,--listen-addrSpecify the listening address. The default is TCP port 2377 ( The output results after execution are as follows:

Swarm initialized: current node (yxsk9zdj5n5u1k73rts861r03) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-574foig445ceah4d47ceh7gh6hd5676ivexmfuth8878q0sj4i-cixvg4yoz9h7u4m8jgq5178il

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

To view the node you just created:

docker-machine ssh manager1 docker node ls

The results are as follows:

This is a leader manager node.

Add worker node

  • function:docker-machine ssh manager1 docker swarm join-token worker, will output:

        To add a worker to this swarm, run the following command:
        docker swarm join --token SWMTKN-1-574foig445ceah4d47ceh7gh6hd5676ivexmfuth8878q0sj4i-cixvg4yoz9h7u4m8jgq5178il

    The result contains a token, which will be used when adding a worker node later.

  • Add the Worker1 node to the cluster and execute the following command:

    docker-machine ssh worker1 docker swarm join --token SWMTKN-1-574foig445ceah4d47ceh7gh6hd5676ivexmfuth8878q0sj4i-cixvg4yoz9h7u4m8jgq5178il --advertise-addr --listen-addr

    --advertise-addrand--listen-addrOptions are optional, but it is better to add them.
    Similarly, add the worker2 node to the cluster (note that the IP address of the worker2 node is

Add manager node

The process is basically the same as that of adding worker nodes.

  • function:docker-machine ssh manager1 docker swarm join-token managerGenerate a token. The output results are as follows:

    To add a manager to this swarm, run the following command:
        docker swarm join --token SWMTKN-1-574foig445ceah4d47ceh7gh6hd5676ivexmfuth8878q0sj4i-66hrkcz0y8k8c5bpy8if6zzm6
  • Add the remaining two manager nodes and execute them respectively:

    #Add manager2
    docker-machine ssh manager2 docker swarm join --token SWMTKN-1-574foig445ceah4d47ceh7gh6hd5676ivexmfuth8878q0sj4i-66hrkcz0y8k8c5bpy8if6zzm6 --advertise-addr --listen-addr
    #Add manager3
    docker-machine ssh manager3 docker swarm join --token SWMTKN-1-574foig445ceah4d47ceh7gh6hd5676ivexmfuth8878q0sj4i-66hrkcz0y8k8c5bpy8if6zzm6 --advertise-addr --listen-addr

    At this point, all nodes are created. functiondocker-machine ssh manager1 docker node lsView creation results:

reference material

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Was mich nicht umbringt, macht mich stärker