Getting Started with Docker six section--swarm

Source: Internet
Author: User
Tags ssh docker compose docker swarm docker toolbox docker machine

Original link: ready To install Docker (version 1.13 minimum). Install Docker Compose, as described in the previous article. Install Docker machine, as mentioned in the previous article, Mac and Windows are pre-installed, but Linux needs to be installed on your own. If it is WINDOWS10, then use Docker Toolbox. Read the six basics of Docker entry-basic guidance. Read the six section of the Docker starter-container. Read the six basics of Docker-service. Make sure you've moved your image to the remote repository as described in the previous few. Make sure that your images are deployed properly. Follow the Docker introductory six section-the service description, and write out your docker-compose.yml file. Introduction

Getting started with Docker six-service we've already done. Define the service and scale the capacity of the service.

In this article, you can see how to deploy the application to a cluster and run it on multiple machines. Swarm can help us deploy services on multi-container, multi-machine. Understanding swarm Clusters

The swarm is a cluster running Docker. You can also use the Docker name to control the cluster, but you will only be able to issue commands to swarm manager . A machine in a cluster can be a real physical machine, or it can be a virtual machine. After joining Swarm, they are all a node.

Swarm Manager can use different policies to run containers, such as the "most empty node" policy--Select the node with the lowest utilization to run the container, or the "global" policy--each node has at least one mirrored container. You can specify the policy in the compose file.

Swarm Manager is in this cluster, you can execute commands, or authorize other work nodes to join the management node. A work node is a node that provides only resources but cannot authorize other nodes to join the cluster. Configure your Swarm

A swarm cluster consists of a number of machines, whether virtual or physical. The simplest way is to execute Docker swarm init to open a swarm node, and use the node that currently executes the command as the Management node, and then execute the Docker swarm join on the other machine to join the swarm cluster. On -Premises virtual machine (MAC,LINUX,WINDOWS7,WINDOWS8)

For simplicity, we use virtual machines to complete the swarm cluster configuration. You need to install VirtualBox.

Then use Docker-machine to create several virtual machines:

$ docker-machine Create--driver VirtualBox myvm1
$ docker-machine Create--driver VirtualBox myvm2

This needs to download the image, if the network is not good, please wait patiently.

Now you have two VMS locally, MYVM1 and myvm2 (can be viewed with docker-machine ls). We are going to use MYVM1 as the Management node, myvm2 as the working node.

We can use Docker-machine ssh to log in to the virtual machine or send commands. Let's first initialize the MYVM1 into a management node:

$ docker-machine ssh myvm1 "docker swarm init"
swarm initialized:current node <node id> is now a manager.

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

  docker swarm join \
  --token <token> \
  <IP&G T;:<port>

execution failed, prompting to add--advertise-addr.
Use Docker-machine ls to view the virtual machine, and then copy the MYVM1 IP, specifying port 2377, for example:

Docker-machine ssh myvm1 "docker swarm init--advertise-addr"

After the initialization is successful, a prompt will be returned to tell you how to join the cluster. Copy this command, and then go to myvm2 to execute:

$ docker-machine ssh myvm2 "docker swarm join \
--token <token> \
<ip>:<port>" This

node Jo Ined a swarm as a worker.

Note: backslashes cannot be lost.

With Docker-machine ssh myvm1 you can log on to MYVM1 and use Docker node ls to view all nodes in the current swarm cluster:

docker@myvm1:~$ docker node ls
ID                            HOSTNAME            status              availability        MANAGER status
BRTU9URXWFD5J0ZRMKUBHPKBD     myvm2               ready               Active              
Rihwohkh3ph38fhillhhb84sk *   myvm1               Ready               Active              Leader

Execution exit exits the login and returns to the original machine. Of course, you can also send commands directly:

Docker-machine ssh myvm1 "Docker node ls"
Deploying Apps

You must have never thought that the hardest part had been passed by you. Now we just need to repeat the steps on the previous article. Remember, however, that we can only execute the dokcer command on MYVM1 because he is the management node.

Copy the previous docker-compose.yml. Upload to MYVM1 using Docker-machine SCP:

Docker-machine SCP docker-compose.yml myvm1:~

We're going to start using swarm to deploy. Same as Docker stack deploy:

Docker-machine ssh myvm1 "Docker stack deploy-c docker-compose.yml Getstartedlab"

It's over.

Let's take a look at the container:

$ docker-machine ssh myvm1 "Docker stack ps getstartedlab"

ID            NAME        IMAGE              NODE   desired
State JQ2G3QP8NZWX  test_web.1  username/repo:tag  myvm1  Running
88wgshobzoxl  test_web.2  username/repo:tag  myvm2  Running
vbb1qbkb0o2z  test_web.3  username/repo:tag  MYVM2  Running
ghii74p9budx  test_web.4  username/repo:tag  myvm1  Running
0prmarhavs87  test_web.5  username/repo:tag  myvm2  Running

Note: desired and state, the first time you execute a command deployment, you need to download the image from the remote repository, so if the network is not good, it will take a while to be running. Accessing the cluster

You can access this cluster using MYVM1 and myvm2 any IP. The network inside the cluster is shared, and is load balanced. You can use Docker-machine ls to view the IP. Then open in the browser, constantly refreshed, you will see the ID of 5 containers because it is load balanced.

Network Diagram:
Telescopic Applications

As in the previous article, you only need to modify the DOCKER-COMPOSE.YML and then re-execute the Docker stack deploy, and swarm will automatically adjust it for you.

You can also create several virtual machines and then join the cluster. Using Docker stack deploy again, Swarm will take advantage of the new node. Sweep the battlefield

If you want to drop the app:

Docker-machine ssh myvm1 "Docker stack rm getstartedlab"

If you want to get the work node out of the Swarm cluster: docker-machine ssh myvm2 "Docker swarm leave"

If you close the Management node: docker-machine ssh myvm1 "docker swarm leave--force"

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: 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.