swarm
- create a couple of VMs using docker-machine, using the VirtualBox driver:
$ docker-machine create --driver virtualbox myvm1
$ docker-machine create --driver virtualbox myvm2
- Instruct myvm1 to become a swarm manager with
docker swarm init
and you’ll see output like this:
[root@localhost centos]# docker-machine ssh myvm2 "docker swarm init"
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
Got an error about needing to use --advertise-addr?
Copy the IP address for myvm1 by running docker-machine ls, then run the docker swarm init command again, using that IP and specifying port 2377 (the port for swarm joins) with --advertise-addr.
[root@localhost centos]# docker-machine ssh myvm1 "docker swarm init --advertise-addr 192.168.99.101:2376"
Swarm initialized: current node (y3rtxupqphgls4m5f4ez0vvgw) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-5zbv1tyrm4jc2zrczemlqowjsadmnicvg115mmfvoadkiw16k2-42esjdacg0ogqtggdy3y5r853 192.168.99.101:2376
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
- Copy this command, and send it to myvm2 via docker-machine ssh to have myvm2 join your new swarm as a worker:
[root@localhost centos]# docker-machine ssh myvm2 "docker swarm join --token SWMTKN-1-1u4flv6jd4yj9eikq4hnrmge1alggfk9g8r9vi90763hq82i3k-961t050euz6f566t4gxgl2i95 192.168.99.101"
This node joined a swarm as a worker.
(注意并不需要加入端口号,虽然会提示让你加入端口号)
-
进入一个有docker-compose.yml 的 目录,利用scp指令,把文件复制到node中。
[root@localhost docker_fffff]# docker-machine scp docker-compose.yml myvm1:~
docker-compose.yml -
运行
[root@localhost docker_fffff]# docker-machine ssh myvm1 "docker stack deploy -c docker-compose.yml getstartedlab"
Creating network getstartedlab_webnet
Creating service getstartedlab_web
*可以观察到部署在集群中的服务,会自动执行负载均衡,分布来运行的
[root@localhost docker_fffff]# docker-machine ssh myvm1 "docker stack ps getstartedlab"
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
sa9rrz6rko0i getstartedlab_web.1 ethan2lee/get-started:part1 myvm1 Running Preparing about a minute ago
mi989eqde8l6 getstartedlab_web.2 ethan2lee/get-started:part1 myvm2 Running Running 31 seconds ago
*实验结束后,删除servies,并将两节点退出集群
[root@localhost docker_fffff]# docker-machine ssh myvm1 "docker stack rm getstartedlab"
Removing service getstartedlab_web
Removing network getstartedlab_webnet
[root@localhost docker_fffff]# docker-machine ssh myvm2 "docker swarm leave"
Node left the swarm.
[root@localhost docker_fffff]# docker-machine ssh myvm1 "docker swarm leave --force"
Node left the swarm.