在集群上部署应用,运行在多个机器上。多个容器、多个机器应用通过将多个机器加入到docker集群(Dockerized Cluster)被称为Swarm
什么是群集群(Swarm Cluster)?
一个swarm是一组运行docker并参与集群的机器。你可以在这之上执行docker相关命令,但是需要在集群中的swarm manager中执行。在swarm中的机器可以是物理机也可以是虚拟机。在加入一个群后,它们都将成为一个结点。
Swarm Manager可以使用多个策略来运行容器。如:空结点-哪个用容器填充利用率最低的机器。或者全局,它确保了每一个机器都会确切地得到一个实例。可以只是swarm manager在 compose file 去使用这些策略.
Swarm manager是唯一的机器可以执行你的命令或者授权其它机器作为worker参与这个群。Worker仅增大访问容量。
之前都在使用单节点模式,但是也可以转化为swarm 模式。开启swarm mode要使当前的机器成为一个swarm manager。that's ok!
开启swarm mode,并使当前机器成为manager
docker swarm init (docker swarm init --advertise-addr <myvm1 ip>)
在其它机器上执行docker swarm join 加入swarm cluster
docker swarm join (docker swarm join \--token <token> \<ip>:2377)
查看当前结点(不是container)
docker node ls
离开swarm
docker swarm leave
在Swarm Manager机器上部署运行应用程序
docker stack deply
(docker stack deploy-cdocker-compose.yml getstartedlab)
查看当前群中运行容器
docker stack ps getstartedlab
访问集群,可通过任何一个集群中的ip:port进行访问
路由示意Iterating and scaling应用程序
通过更改docker-compose.yml文件来伸缩应用,可通过docker stack deploy再次部署这些更改。
关闭集群
docker stack rm getstartedlab
以下是本机主要命令的使用(使用Docker Toolbox )
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 <node ID>" # 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 node ls # View nodes in swarm (while logged on to manager)
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 ls # list VMs, asterisk shows which VM this shell is talking to
docker-machine start myvm1 # Start a VM that is currently not running
docker-machine env myvm1 # show environment variables and command for myvm1
eval $(docker-machine env myvm1) # Mac command to connect shell to myvm1
& "C:\Program Files\Docker\Docker\Resources\bin\docker-machine.exe" env myvm1 | Invoke-Expression # Windows command to connect shell to myvm1
docker stack deploy -c <file> <app> # Deploy an app; command shell must be set to talk to manager (myvm1), uses local Compose file
docker-machine scp docker-compose.yml myvm1:~ # Copy file to node's home dir (only required if you use ssh to connect to manager and deploy the app)
docker-machine ssh myvm1 "docker stack deploy -c <file> <app>" # Deploy an app using ssh (you must have first copied the Compose file to myvm1)
eval $(docker-machine env -u) # Disconnect shell from VMs, use native docker
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
网友评论