How to build a Docker Swarm cluster from scratch

[Editor's Note] This article describes how to build Docker Swarm cluster from scratch, with reference to the Youtube video Demo of Machine + Swarm + compose integration and Demo of Docker Swarm Beta and the official Docker Swarm document , to provide readers with more intuitive Swarm Demonstration example.

demand

1. Docker version 1.4.0+
2. Two node hosts:

  • A: 192.168.20.1
  • B: 192.168.20.2

Check the node Docker configuration

1. Open the Docker configuration file (example is centos 7)

  Vim / etc / sysconfig / docker 

2. Add -H tcp://0.0.0.0:2375 to OPTIONS

  OPTIONS = '- g / cutome-path / docker -H tcp: //0.0.0.0: 2375' 

3. CentOS6.6 need to add- -H unix:///var/run/docker.sock

  OPTIONS = '- g / mnt / docker -H tcp: //0.0.0.0: 2375 -H unix: ///var/run/docker.sock' 

Install swarm for A, B nodes, respectively

  $ Docker pull swarm 

Generate cluster token (once)

  $ Docker run --rm swarm create 
6856663cdefdec325839a4b7e1de38e8

Which 6856663cdefdec325839a4b7e1de38e8 is that we will create a cluster of token

Add nodes A, B to the cluster

  $ Docker run -d swarm join --addr = 192.168.20.1: 2375 token: // 6856663cdefdec325839a4b7e1de38e8 

$ Docker run -d swarm join --addr = 192.168.20.2: 2375 token: // 6856663cdefdec325839a4b7e1de38e8

List cluster A, B nodes

  $ Docker run --rm swarm list token: // 6856663cdefdec325839a4b7e1de38e8 

192.168.20.1:2375
192.168.20.2:2375

Cluster management:

Open the hypervisor on any host A, B, or C (C: 192.168.20.3). For example, in the C host open:

  $ Docker run -d -p 8888: 2375 swarm manage token: // 6856663cdefdec325839a4b7e1de38e8 

Now you can manage cluster A on host C, B:

  $ Docker-h 192.168.20.3:8888 info 
$ Docker -H 192.168.20.3:8888 ps
$ Docker-h 192.168.20.3:8888 logs ...

Run the container on the cluster

  $ Docker -H 192.168.20.3:8888 run -d --name web1 nginx 
$ Docker -H 192.168.20.3:8888 run -d --name web2 nginx
$ Docker -H 192.168.20.3:8888 run -d --name web3 nginx
$ Docker -H 192.168.20.3:8888 run -d --name web4 nginx
$ Docker -H 192.168.20.3:8888 run -d --name web5 nginx

View containers in cluster A, B

  $ Docker -H 192.168.20.3:8888 ps-a 

The results are as follows:

  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
4cc1f232fb18 nginx: latest "nginx -g 'daemon of 16 hours ago Up 16 hours 80 / tcp, 443 / tcp Host-A / web5
E8327939721a nginx: latest "nginx -g 'daemon of 16 hours ago Up 16 hours 443 / tcp, 80 / tcp Host-A / web3
35g08c4a1b43 nginx: 1 "nginx -g 'daemon of 23 hours ago Up 16 hours 443 / tcp, 80 / tcp Host-B / web4
9bd07067620d nginx: 1 "nginx -g 'daemon of 23 hours ago Up 16 hours 443 / tcp, 80 / tcp Host-B / web2
626fe5b1dcfa nginx: 1 "nginx -g 'daemon of 23 hours ago Up 16 hours 80 / tcp, 443 / tcp Host-B / web1

Where NAMES column: / front is the node name, followed by the name of the container created within the node.

Use the Docker Machine to build a Swarm cluster

Heads up! This alert needs your attention, but it's not super important.