$ docker-machine create --driver virtualbox manager1 Running pre-create checks... (manager1) Unable to get the latest Boot2Docker ISO release version: Get https://api.github.com/repos/boot2docker/boot2docker/releases/latest: dial tcp: lookup api.github.com on [::1]:53: server misbehaving Creating machine... (manager1) Unable to get the latest Boot2Docker ISO release version: Get https://api.github.com/repos/boot2docker/boot2docker/releases/latest: dial tcp: lookup api.github.com on [::1]:53: server misbehaving (manager1) Copying /home/zuolan/.docker/machine/cache/boot2docker.iso to /home/zuolan/.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) Found a new host-only adapter: "vboxnet0" (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
查看虚拟机的环境变量等信息,包括虚拟机的 IP 地址:
1 2 3 4 5 6 7
$ docker-machine env manager1 export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.99.100:2376" export DOCKER_CERT_PATH="/home/zuolan/.docker/machine/machines/manager1" export DOCKER_MACHINE_NAME="manager1" # Run this command to configure your shell: # eval $(docker-machine env manager1)
$ docker-machine ssh manager1 docker swarm init --listen-addr 192.168.99.100:2377 --advertise-addr 192.168.99.100 Swarm initialized: current node (23lkbq7uovqsg550qfzup59t6) is now a manager.
To add a worker to this swarm, run the following command:
$ docker-machine ssh worker1 docker swarm join --token \ SWMTKN-1-3z5rzoey0u6onkvvm58f7vgkser5d7z8sfshlu7s4oz2gztlvj-c036gwrakjejql06klrfc585r \ 192.168.99.100:2377 This node joined a swarm as a worker.
上面 join 命令中可以添加 –listen-addr $WORKER1_IP:2377 作为监听准备,因为有时候可能会遇到把一个 work 节点提升为 manger 节点的可能,当然本例子没有这个打算就不添加这个参数了。
$ docker-machine ssh manager1 docker swarm init --listen-addr $MANAGER1_IP:2377 Error response from daemon: could not choose an IP address to advertise since this system has multiple addresses on different interfaces (10.0.2.15 on eth0 and 192.168.99.100 on eth1) - specify one with --advertise-addr exit status 1
发生错误的原因是因为有两个 IP 地址,而 Swarm 不知道用户想使用哪个,因此要指定 IP。
1 2 3 4 5 6 7 8 9 10
$ docker-machine ssh manager1 docker swarm init --advertise-addr 192.168.99.100 --listen-addr 192.168.99.100:2377 Swarm initialized: current node (ahvwxicunjd0z8g0eeosjztjx) is now a manager.
To add a worker to this swarm, run the following command:
$ docker-machine ssh manager1 docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 23lkbq7uovqsg550qfzup59t6 * manager1 Ready Active Leader dqb3fim8zvcob8sycri3hy98a worker1 Ready Active
$ docker-machine ssh worker2 docker swarm join \ --token SWMTKN-1-3z5rzoey0u6onkvvm58f7vgkser5d7z8sfshlu7s4oz2gztlvj-c036gwrakjejql06klrfc585r \ 192.168.99.100:2377 This node joined a swarm as a worker.
添加 worker3 到集群中:
1 2 3 4
$ docker-machine ssh worker3 docker swarm join \ --token SWMTKN-1-3z5rzoey0u6onkvvm58f7vgkser5d7z8sfshlu7s4oz2gztlvj-c036gwrakjejql06klrfc585r \ 192.168.99.100:2377 This node joined a swarm as a worker.
$ docker-machine ssh manager2 docker swarm join \ --token SWMTKN-1-3z5rzoey0u6onkvvm58f7vgkser5d7z8sfshlu7s4oz2gztlvj-8tn855hkjdb6usrblo9iu700o \ 192.168.99.100:2377 This node joined a swarm as a manager.
现在再来查看集群信息:
1 2 3 4 5 6 7
$ docker-machine ssh manager2 docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 16w80jnqy2k30yez4wbbaz1l8 worker1 Ready Active 2gkwhzakejj72n5xoxruet71z worker2 Ready Active 35kutfyn1ratch55fn7j3fs4x worker3 Ready Active a9r21g5iq1u6h31myprfwl8ln * manager2 Ready Active Reachable dpo7snxbz2a0dxvx6mf19p35z manager1 Ready Active Leader
$ docker swarm join \ --token SWMTKN-1-3z5rzoey0u6onkvvm58f7vgkser5d7z8sfshlu7s4oz2gztlvj-8tn855hkjdb6usrblo9iu700o \ 192.168.99.100:2377 This node joined a swarm as a manager.
现在我们有三台 manager,三台 worker。其中一台是宿主机,五台虚拟机。
1 2 3 4 5 6 7 8
$ docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 6z2rpk1t4xucffzlr2rpqb8u3 worker3 Ready Active 7qbr0xd747qena4awx8bx101s * user-pc Ready Active Reachable 9v93sav79jqrg0c7051rcxxev manager2 Ready Active Reachable a1ner3zxj3ubsiw4l3p28wrkj worker1 Ready Active a5w7h8j83i11qqi4vlu948mad worker2 Ready Active d4h7vuekklpd6189fcudpfy18 manager1 Ready Active Leader
查看网络状态:
1 2 3 4 5 6
$ docker network ls NETWORK ID NAME DRIVER SCOPE 764ff31881e5 bridge bridge local fbd9a977aa03 host host local 6p6xlousvsy2 ingress overlay swarm e81af24d643d none null local
$ docker service ls ID NAME REPLICAS IMAGE COMMAND 5gz0h2s5agh2 helloworld 0/2 nginx:alpine
查看 helloworld 服务详情(为了方便阅读,已调整输出内容):
1 2 3 4
$ docker service ps helloworld ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR ay081uome3 helloworld.1 nginx:alpine manager1 Running Preparing 2 seconds ago 16cvore0c96 helloworld.2 nginx:alpine worker2 Running Preparing 2 seconds ago
可以看到两个实例分别运行在两个节点上。
进入两个节点,查看服务状态(为了方便阅读,已调整输出内容):
1 2 3 4 5 6
$ docker-machine ssh manager1 docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 119f787622c2 nginx:alpine "nginx -g ..." 4 minutes ago Up 4 minutes 80/tcp, 443/tcp hello ... $ docker-machine ssh worker2 docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5db707401a06 nginx:alpine "nginx -g ..." 4 minutes ago Up 4 minutes 80/tcp, 443/tcp hello ...
$ docker-machine ssh worker2 docker exec -i helloworld.2.16cvore0c96rby1vp0sny3mvt \ ping helloworld.1.ay081uome3eejeg4mspa8pdlx PING helloworld.1.ay081uome3eejeg4mspa8pdlx (10.0.0.3): 56 data bytes 64 bytes from 10.0.0.3: seq=0 ttl=64 time=0.466 ms 64 bytes from 10.0.0.3: seq=1 ttl=64 time=0.465 ms 64 bytes from 10.0.0.3: seq=2 ttl=64 time=0.548 ms 64 bytes from 10.0.0.3: seq=3 ttl=64 time=0.689 ms ^C
可以看到这两个跨主机的服务集群里面各个容器是可以互相连接的。
为了体现 Swarm 集群的优势,我们可以使用虚拟机的 ping 命令来测试对方虚拟机内的容器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$ docker-machine ssh worker2 ping helloworld.1.ay081uome3eejeg4mspa8pdlx PING helloworld.1.ay081uome3eejeg4mspa8pdlx (221.179.46.190): 56 data bytes 64 bytes from 221.179.46.190: seq=0 ttl=63 time=48.651 ms 64 bytes from 221.179.46.190: seq=1 ttl=63 time=63.239 ms 64 bytes from 221.179.46.190: seq=2 ttl=63 time=47.686 ms 64 bytes from 221.179.46.190: seq=3 ttl=63 time=61.232 ms ^C $ docker-machine ssh manager1 ping helloworld.2.16cvore0c96rby1vp0sny3mvt PING helloworld.2.16cvore0c96rby1vp0sny3mvt (221.179.46.194): 56 data bytes 64 bytes from 221.179.46.194: seq=0 ttl=63 time=30.150 ms 64 bytes from 221.179.46.194: seq=1 ttl=63 time=54.455 ms 64 bytes from 221.179.46.194: seq=2 ttl=63 time=73.862 ms 64 bytes from 221.179.46.194: seq=3 ttl=63 time=53.171 ms ^C
$ docker service ls ID NAME REPLICAS IMAGE COMMAND 9gfziifbii7a helloworld 2/2 nginx:alpine
不知你有没有发现,虽然我们使用 –replicas 参数的值都是一样的,但是上一节中获取服务状态时,REPLICAS 返回的是 0/2,现在的 REPLICAS 返回的是 2/2。 同样使用 docker service ps 查看服务详细状态时(下面输出已经手动调整为更易读的格式),可以看到实例的 CURRENT STATE 中是 Running 状态的,而上一节中的 CURRENT STATE 中全部是处于 Preparing 状态。
1 2 3 4
$ docker service ps helloworld ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR 9ikr3agyi... helloworld.1 nginx:alpine user-pc Running Running 13 seconds ago 7acmhj0u... helloworld.2 nginx:alpine worker2 Running Running 6 seconds ago
$ docker service ps helloworld ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR 9ikr3agyi... helloworld.1 nginx:alpine zuolan-pc Running Running 19 minutes ago 8f866igpl... helloworld.2 nginx:alpine manager1 Running Running 4 seconds ago 7acmhj0u... \_ helloworld.2 nginx:alpine worker2 Shutdown Failed 11 seconds ago ...exit... $ docker service ls ID NAME REPLICAS IMAGE COMMAND 9gfziifbii7a helloworld 2/2 nginx:alpine