A swarm is a group of machines that are running Docker and joined into a cluster. After that has happened, you continue to run the Docker commands you’re used to, but now they are executed on a cluster by a swarm manager. The machines in a swarm can be physical or virtual. After joining a swarm, they are referred to as nodes.
Swarm managers can use several strategies to run containers, such as “emptiest node” – which fills the least utilized machines with containers. Or “global”, which ensures that each machine gets exactly one instance of the specified container. You instruct the swarm manager to use these strategies in the Compose file, just like the one you have already been using.
Swarm managers are the only machines in a swarm that can execute your commands, or authorize other machines to join the swarm as workers. Workers are just there to provide capacity and do not have the authority to tell any other machine what it can and cannot do.
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.
蜂群是一組安裝了Docker的機器組成的集群抒线,我們可以像之前一樣執(zhí)行Docker命令,但是只能在蜂群管理機上才能執(zhí)行截型。蜂群中的機器可以是真實機器或者虛擬機利诺,當接入蜂群后枢纠,他們被視為節(jié)點车吹。
蜂群管理機有幾種運行容器的策略育苟,比如“空閑節(jié)點”:用容器填滿最少使用的機器较鼓,“整體”:確保每個機器都能準確地獲得指定容器的一個實例。我們可以在Compose文件中定義這些违柏。
只有在蜂群管理機上才能執(zhí)行命令博烂,或者授權其他機器作為工作機加入蜂群,工作機只提勞動力漱竖,無權告訴其他機器自己能做什么禽篱。
目前為止,我們只在單機使用Docker馍惹,但是Docker可以切換到蜂群模式躺率。切換到蜂群模式玛界,會立即使當前操作的機器變?yōu)楣芾頇C。然后Docker會將命令執(zhí)行在蜂群上悼吱,而不僅是在當前機器上慎框。
1.創(chuàng)建多臺機器的集群,目前是單機測試所以需要使用虛擬機后添,以下幾種方式可供選擇笨枯,推薦安裝docker toolbox
1.1 docker toolbox
https://docs.docker.com/toolbox/overview/
1.2 oracle VirtualBox
https://www.virtualbox.org/wiki/Downloads
1.3 如果是win10可直接使用 Hyper-V Manager
利用docker toolbox自帶虛擬機,創(chuàng)建一對VM 通過docker-machine help查看相關命令
docker-machine create --driver virtualbox myvm1
docker-machine create --driver virtualbox myvm2
myvm1的控制臺輸出
Running pre-create checks...
(myvm1) Default Boot2Docker ISO is out-of-date, downloading the latest release...
(myvm1) Latest release for github.com/boot2docker/boot2docker is v17.10.0-ce
(myvm1) Downloading /Users/jionglu/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v17.10.0-ce/boot2docker.iso...
(myvm1) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(myvm1) Copying /Users/jionglu/.docker/machine/cache/boot2docker.iso to /Users/jionglu/.docker/machine/machines/myvm1/boot2docker.iso...
(myvm1) Creating VirtualBox VM...
(myvm1) Creating SSH key...
(myvm1) Starting the VM...
(myvm1) Check network to re-create if needed...
(myvm1) Found a new host-only adapter: "vboxnet1"
(myvm1) 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 myvm1
創(chuàng)建完2個VM后查看VM列表
docker-machine ls
控制臺輸出
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
myvm1 - virtualbox Running tcp://192.168.99.100:2376 v17.10.0-ce
myvm2 - virtualbox Running tcp://192.168.99.101:2376 v17.10.0-ce
2.下面設置第一臺機器A作為管理機遇西,執(zhí)行管理命令馅精、驗證工作機加入蜂群,第二臺機器B作為工作機粱檀,先通過docker-machine ssh命令操作VM
2.1 管理機初始化蜂群
docker-machine ssh myvm1 "docker swarm init --advertise-addr 192.168.99.100"
控制臺輸出
Swarm initialized: current node (i9pgdm5g061yombmj8b4tqvfi) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-4fv4d3nbbqy1kwbl4zbqu033qxati9075ohsat9892u8mln0zd-5o1ncmtydr3c1krvbuvjznulf 192.168.99.100:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
2.2 根據(jù)管理機的提示洲敢,在工作機上執(zhí)行加入蜂群的命令,注意端口要是2377梧税,2366是守護進程的端口
docker-machine ssh myvm2 "docker swarm join --token SWMTKN-1-4fv4d3nbbqy1kwbl4zbqu033qxati9075ohsat9892u8mln0zd-5o1ncmtydr3c1krvbuvjznulf 192.168.99.100:2377"
控制臺輸出
This node joined a swarm as a worker.
注:客戶端離開蜂群命令 docker swarm leave
管理端離開蜂群命令 docker swarm leave --force
2.3 通過docker-machine ssh在管理機查看節(jié)點
docker-machine ssh myvm1 "docker node ls"
控制臺輸出
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
i9pgdm5g061yombmj8b4tqvfi * myvm1 Ready Active Leader
bckre41y6jrovnmuz7djwbiej myvm2 Ready Active
3.通過docker-machine env命令操作docker-machine
之前執(zhí)行命令都是用docker-machine ssh包裹命令沦疾,使用非常不方便,更好選擇是通過執(zhí)行docker-machine env myvm1來獲取并執(zhí)行一個命令第队。通過env的方式可以使用本地的docker-compose.yml文件遠程部署應用哮塞,不需要將它拷貝到管理機或者工作機上。
注:如果一定要拷貝凳谦,也可以使用scp命令拷貝文件
docker-machine scp docker-compose.yml myvm1:~
3.1 執(zhí)行
docker-machine env myvm1
控制臺輸出
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/jionglu/.docker/machine/machines/myvm1"
export DOCKER_MACHINE_NAME="myvm1"
# Run this command to configure your shell:
# eval $(docker-machine env myvm1)
3.2 根據(jù)提示執(zhí)行
eval $(docker-machine env myvm1)
注:類似于ssh遠程登錄到mymv1忆畅,但是目錄還是顯示本地的目錄,可以把本地文件用于遠程執(zhí)行尸执。
docker-machine ls 查看myvm1是否是當前激活的機器
控制臺輸出
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
myvm1 * virtualbox Running tcp://192.168.99.100:2376 v17.10.0-ce
myvm2 - virtualbox Running tcp://192.168.99.101:2376 v17.10.0-ce
ACTIVIE列為*號表示當前激活的虛擬機家凯,如果已經(jīng)激活,可以像文章3中用相同的命令操作myvm1
注意:
a.mac使用Item如失,只有在執(zhí)行eval $(docker-machine env myvm1)的窗口才會顯示*號绊诲,所以可以打開多個窗口對應多個VM
b.如果docker-machine不是Ruuning狀態(tài),執(zhí)行開啟命令docker-machine start myvm1
c.退出控制虛擬機eval $(docker-machine env -u)
3.3 像之前一樣在本地執(zhí)行命令
docker stack deploy -c docker-compose.yml getstartedlab
查看進程
docker stack ps getstartedlab
控制臺輸出
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
eomp0knftsxd getstartedlab_web.1 gaojingyuan/testrepo:v1 myvm2 Running Preparing 24 seconds ago
xzfb2fbtc78h getstartedlab_web.2 gaojingyuan/testrepo:v1 myvm1 Running Preparing 24 seconds ago
pismd6w405in getstartedlab_web.3 gaojingyuan/testrepo:v1 myvm2 Running Preparing 24 seconds ago
4bg66tpcjejf getstartedlab_web.4 gaojingyuan/testrepo:v1 myvm2 Running Preparing 24 seconds ago
f85u2q958tn9 getstartedlab_web.5 gaojingyuan/testrepo:v1 myvm1 Running Preparing 24 seconds ago
可見service啟動時褪贵,利用了myvm1和myvm2兩個節(jié)點掂之。
總結:多個container組成了一個service對外提供服務,使用swarm脆丁,用多個節(jié)點分擔這個service的壓力世舰。
備注:
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