http://www.dockone.io/article/227
need
1. Docker version 1.4.0+
2. Two node hosts:
- A:192.168.20.1
- B:192.168.20.2
Check 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
toOPTIONS
OPTIONS='-g /cutome-path/docker -H tcp://0.0.0.0:2375'
3. CentOS6.6 needs to be added-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 and B nodes respectively
$ docker pull swarm
Generate cluster token (once)
$ docker run --rm swarm create 6856663cdefdec325839a4b7e1de38e8
Among them 6856663cdefdec325839a4b7e1de38e8
is the token that we will create the cluster
Add nodes A and 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 and B nodes
$ docker run --rm swarm list token://6856663cdefdec325839a4b7e1de38e8 192.168.20.1:2375 192.168.20.2:2375
Cluster management:
Start the hypervisor on any host A, B, or C (C:192.168.20.3). For example, open on the C host:
$ docker run -d -p 8888:2375 swarm manage token://6856663cdefdec325839a4b7e1de38e8
Now you can manage clusters A, B on host C:
$ 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 containers 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 clusters A and B
$ docker -H 192.168.20.3:8888 ps -a
The result is 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 35e08c4a1b43 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
In the NAMES
column: /
the front is the node name, and the back is the name of the container created in the node.