4.蜂群(Swarms)

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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市槽卫,隨后出現(xiàn)的幾起案子跟压,更是在濱河造成了極大的恐慌,老刑警劉巖歼培,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件震蒋,死亡現(xiàn)場離奇詭異茸塞,居然都是意外死亡,警方通過查閱死者的電腦和手機喷好,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進店門翔横,熙熙樓的掌柜王于貴愁眉苦臉地迎上來读跷,“玉大人梗搅,你說我怎么就攤上這事⌒Ю溃” “怎么了无切?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長丐枉。 經(jīng)常有香客問我哆键,道長,這世上最難降的妖魔是什么瘦锹? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任籍嘹,我火速辦了婚禮,結果婚禮上弯院,老公的妹妹穿的比我還像新娘辱士。我一直安慰自己,他們只是感情好听绳,可當我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布颂碘。 她就那樣靜靜地躺著,像睡著了一般椅挣。 火紅的嫁衣襯著肌膚如雪头岔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天鼠证,我揣著相機與錄音峡竣,去河邊找鬼。 笑死量九,一個胖子當著我的面吹牛适掰,可吹牛的內容都是我干的。 我是一名探鬼主播娩鹉,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼攻谁,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了弯予?” 一聲冷哼從身側響起戚宦,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎锈嫩,沒想到半個月后受楼,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體垦搬,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年艳汽,在試婚紗的時候發(fā)現(xiàn)自己被綠了猴贰。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡河狐,死狀恐怖米绕,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情馋艺,我是刑警寧澤栅干,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站捐祠,受9級特大地震影響碱鳞,放射性物質發(fā)生泄漏。R本人自食惡果不足惜踱蛀,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一窿给、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧率拒,春花似錦崩泡、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至寥掐,卻和暖如春靴寂,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背召耘。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工百炬, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人污它。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓剖踊,卻偏偏與公主長得像,于是被迫代替她去往敵國和親衫贬。 傳聞我的和親對象是個殘疾皇子德澈,可洞房花燭夜當晚...
    茶點故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內容