此处总结官网的文档,但是官网的这一小节,写得不完善,官网没有介绍如何创建节点的machine,需要查找以前的文档才方便学习,官网最好在文档中给出相关的链接,但没有,此博文进行补充一下。
(1)先创建这个machine
$ sudo docker-machine create --driver virtualbox manager1
应该得到类似如下输出:
mingchen@mingchen-HP:~$ sudo docker-machine create --driver virtualbox manager1 [sudo] password for mingchen: Running pre-create checks... (manager1) You are using version 4.3.36_Ubuntur105129 of VirtualBox. If you encounter issues, you might want to upgrade to version 5 at https://www.virtualbox.org Creating machine... (manager1) Copying /home/mingchen/.docker/machine/cache/boot2docker.iso to /home/mingchen/.docker/machine/machines/manager1/boot2docker.iso... (manager1) Creating VirtualBox VM... (manager1) Creating SSH key... (manager1) Starting the VM... (manager1) Check network to re-create if needed... (manager1) Waiting for an IP... 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 mingchen@mingchen-HP:~$ sudo docker-machine ssh manager1 ## . ## ## ## == ## ## ## ## ## === /"""""""""""""""""\___/ === ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~ \______ o __/ \ \ __/ \____\_______/ _ _ ____ _ _ | |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __ | '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__| | |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ | |_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_| Boot2Docker version 1.13.0, build HEAD : 5b8d9cb - Wed Jan 18 18:50:40 UTC 2017 Docker version 1.13.0, build 49bf474 docker@manager1:~$
(2)以ssh方式登入该machine
这个步骤是下次在新窗口才需要使用的,因为第一步创建完后,已经进入了machine,docker@manager1:~$
若是在新的终端窗口,则:
$ sudo docker-machine ssh manager1
已经登入了的话,则不必进行上面的操作。
(3)添加manager1为该swarm为主节点
依据
docker swarm init --advertise-addr <MANAGER-IP>
把
换成manager1主机IP
docker@manager1:~$ docker swarm init --advertise-addr 192.168.99.101
得到如下输出
Swarm initialized: current node (qf12wwq4i3gjv4gnhiuetqiy5) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-4hggsl45lcdtn8wd8m5y5qb4yek2c1ztj6tibkrsxe8lb8nyt2-07l8c6ke7ba6n4g235vgwdgi3 \
192.168.99.101:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
docker@manager1:~$
上面的输出中,包含了怎么添加worker和manager。不过,添加的操作是在另外的终端machine上进行的。
(1)用类似于STEP1中的方法,在另外的新终端窗口建立worker1的节点machine.
mingchen@mingchen-HP:~$ sudo docker-machine creat --driver virtualbox worker1
(2)ssh登入
mingchen@mingchen-HP:~$ sudo docker-machine ssh worker1
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ === ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~ \______ o __/ \ \ __/ \____\_______/ _ _ ____ _ _ | |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __ | '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__| | |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ | |_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_| Boot2Docker version 1.13.0, build HEAD : 5b8d9cb - Wed Jan 18 18:50:40 UTC 2017 Docker version 1.13.0, build 49bf474 docker@worker1:~$
(3)把worker1节点添加进以manager1为MANAGER的swarm
docker@worker1:~$ docker swarm join \
> --token SWMTKN-1-4hggsl45lcdtn8wd8m5y5qb4yek2c1ztj6tibkrsxe8lb8nyt2-07l8c6
ke7ba6n4g235vgwdgi3 \
> 192.168.99.101:2377
This node joined a swarm as a worker.
docker@worker1:~$
与STEP2基本一样。
输入docker node ls
docker@manager1:~$ docker node ls
在MANAGER的纵列leader
为manager1
运用类似的办法可以添加更多的节点。