Docker学习笔记(七)-如何创建docker swarm

创建docker swarm

此处总结官网的文档,但是官网的这一小节,写得不完善,官网没有介绍如何创建节点的machine,需要查找以前的文档才方便学习,官网最好在文档中给出相关的链接,但没有,此博文进行补充一下。

Prerequisite

  1. 3个能够联网的主机(manager1, worker1, worker2)可以是virtual machine, data center, a cloud service provider等
  2. 主机中的docker 版本1.12 or later.
  3. manager machine的IP地址
  4. 主机间的open ports

步骤

STEP1. 建立manager1节点

(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上进行的。

STEP2. 建立/添加worker1节点

(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:~$ 

STEP3. 建立/添加worker2节点

与STEP2基本一样。

STEP4. 在manager1的终端查看节点信息

输入docker node ls

docker@manager1:~$ docker node ls

这里写图片描述

在MANAGER的纵列leader为manager1
运用类似的办法可以添加更多的节点。

你可能感兴趣的:(Docker,Ubuntu)