前提
- dockers 1.13以上
Docker Compose
Docker Machine
本节内容
将app部署到一个集群中,在几个机器上运行。Multi-container, multi-machine applications are made possible by joining multiple machines into a “Dockerized” cluster called a swarm
.
理解Swarm cluster
swarm
是一组运行了docker、组织到一个集群中的机器。swarm manager
在一个集群中执行我们的指令。swarm
中的机器可以是真是的或者虚拟的。一个节点加入swarm
后被成为node
。
swarm managers
运行container
可以使用多种策略。empties node
,global
等。
swarm managers
可以执行命令,授权别的机器作为workers
加入swarm
。
Up until now, you have been using Docker in a single-host mode on your local machine. But Docker also can be switched into swarm mode
, and that’s what enables the use of swarms. Enabling swarm mode instantly makes the current machine a swarm manager. From then on, Docker will run the commands you execute on the swarm you’re managing, rather than just on the current machine.
建立swarm
run docker swarm init
to enable swarm mode and make your current machine a swarm manager, then run docker swarm join
on other machines to have them join the swarm as workers.
$ docker-machine create --driver virtualbox myvm1
$ docker-machine create --driver virtualbox myvm2
$ docker-machine ssh myvm1 "docker swarm init"
Swarm initialized: current node is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token \
:
$ docker-machine ssh myvm2 "docker swarm join \
--token \
:"
This node joined a swarm as a worker.
$ docker-machine ssh myvm1 "docker node ls"
部署app到cluster上
The hard part is over. Now you just repeat the process you used in part 3
to deploy on your new swarm. Just remember that only swarm managers like myvm1
execute Docker commands; workers are just for capacity.
Copy the file docker-compose.yml
you created in part 3 to the swarm manager myvm1’s home directory (alias: ~) by using the docker-machine scp
command:
docker-machine scp docker-compose.yml myvm1:~
Now have myvm1
use its powers as a swarm manager to deploy your app, by sending the same docker stack deploy
command you used in part 3 to myvm1
using docker-machine ssh
:
docker-machine ssh myvm1 "docker stack deploy -c docker-compose.yml getstartedlab"
And that’s it, the app is deployed on a cluster.
Wrap all the commands you used in part 3 in a call to docker-machine ssh
, and they’ll all work as you’d expect. Only this time, you’ll see that the containers have been distributed between both myvm1 and myvm2.
$ docker-machine ssh myvm1 "docker stack ps getstartedlab"
ID NAME IMAGE NODE DESIRED STATE
jq2g3qp8nzwx test_web.1 username/repo:tag myvm1 Running
88wgshobzoxl test_web.2 username/repo:tag myvm2 Running
vbb1qbkb0o2z test_web.3 username/repo:tag myvm2 Running
ghii74p9budx test_web.4 username/repo:tag myvm1 Running
0prmarhavs87 test_web.5 username/repo:tag myvm2 Running
Accessing your cluster
You can access your app from the IP address of either myvm1
or myvm2
. The network you created is shared between them and load-balancing. Run docker-machine ls
to get your VMs’ IP addresses and visit either of them on a browser, hitting refresh (or just curl
them). You’ll see five possible container IDs all cycling by randomly, demonstrating the load-balancing.
The reason both IP addresses work is that nodes in a swarm participate in an ingress routing mesh
. This ensures that a service deployed at a certain port within your swarm always has that port reserved to itself, no matter what node is actually running the container.
Cleanup
You can tear down the stack with docker stack rm. For example:
docker-machine ssh myvm1 "docker stack rm getstartedlab"
docker-machine ssh myvm2 "docker swarm leave"
docker-machine ssh myvm1 "docker swarm leave --force"
docker-machine create --driver virtualbox myvm1 # Create a VM (Mac, Win7, Linux)
docker-machine create -d hyperv --hyperv-virtual-switch "myswitch" myvm1 # Win10
docker-machine env myvm1 # View basic information about your node
docker-machine ssh myvm1 "docker node ls" # List the nodes in your swarm
docker-machine ssh myvm1 "docker node inspect " # Inspect a node
docker-machine ssh myvm1 "docker swarm join-token -q worker" # View join token
docker-machine ssh myvm1 # Open an SSH session with the VM; type "exit" to end
docker-machine ssh myvm2 "docker swarm leave" # Make the worker leave the swarm
docker-machine ssh myvm1 "docker swarm leave -f" # Make master leave, kill swarm
docker-machine start myvm1 # Start a VM that is currently not running
docker-machine stop $(docker-machine ls -q) # Stop all running VMs
docker-machine rm $(docker-machine ls -q) # Delete all VMs and their disk images
docker-machine scp docker-compose.yml myvm1:~ # Copy file to node's home dir
docker-machine ssh myvm1 "docker stack deploy -c " # Deploy an app