在集群上部署應(yīng)用连锯,運(yùn)行在多個(gè)機(jī)器上与学。多個(gè)容器丰泊、多個(gè)機(jī)器應(yīng)用通過將多個(gè)機(jī)器加入到docker集群(Dockerized Cluster)被稱為Swarm
什么是群集群(Swarm Cluster)喷户?
一個(gè)swarm是一組運(yùn)行docker并參與集群的機(jī)器汇恤。你可以在這之上執(zhí)行docker相關(guān)命令庞钢,但是需要在集群中的swarm manager中執(zhí)行。在swarm中的機(jī)器可以是物理機(jī)也可以是虛擬機(jī)因谎。在加入一個(gè)群后基括,它們都將成為一個(gè)結(jié)點(diǎn)。
Swarm Manager可以使用多個(gè)策略來運(yùn)行容器财岔。如:空結(jié)點(diǎn)-哪個(gè)用容器填充利用率最低的機(jī)器风皿。或者全局匠璧,它確保了每一個(gè)機(jī)器都會(huì)確切地得到一個(gè)實(shí)例桐款。可以只是swarm manager在 compose file 去使用這些策略.
Swarm manager是唯一的機(jī)器可以執(zhí)行你的命令或者授權(quán)其它機(jī)器作為worker參與這個(gè)群夷恍。Worker僅增大訪問容量魔眨。
之前都在使用單節(jié)點(diǎn)模式,但是也可以轉(zhuǎn)化為swarm 模式酿雪。開啟swarm mode要使當(dāng)前的機(jī)器成為一個(gè)swarm manager遏暴。that's ok!
開啟swarm mode,并使當(dāng)前機(jī)器成為manager
docker swarm init (docker swarm init --advertise-addr <myvm1 ip>)
在其它機(jī)器上執(zhí)行docker swarm join 加入swarm cluster
docker swarm join (docker swarm join \--token <token> \<ip>:2377)
查看當(dāng)前結(jié)點(diǎn)(不是container)
docker node ls
離開swarm
docker swarm leave
在Swarm Manager機(jī)器上部署運(yùn)行應(yīng)用程序
docker stack deply
(docker stack deploy-cdocker-compose.yml getstartedlab)
查看當(dāng)前群中運(yùn)行容器
docker stack ps getstartedlab
訪問集群指黎,可通過任何一個(gè)集群中的ip:port進(jìn)行訪問
Iterating and scaling應(yīng)用程序
通過更改docker-compose.yml文件來伸縮應(yīng)用朋凉,可通過docker stack deploy再次部署這些更改。
關(guān)閉集群
docker stack rm getstartedlab
以下是本機(jī)主要命令的使用(使用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