learning docker steps(4) ----- docker swarm 初次体验

参考:https://docs.docker.com/get-started/part4/

了解 swarm 集群

swarm 是一组运行 Docker 并且已加入集群中的机器。执行此操作后,您可以继续运行已使用的 Docker 命令,但现在它们在集群上由 swarm 管理节点执行。 swarm 中的机器可以为物理或虚拟机。加入 swarm 后,可以将它们称为节点。

swarm 管理节点可以使用多项策略来运行容器,例如“最空的节点”– 这将使用容器填充使用最少的机器。或“全局”,这将确保每台机器恰好获得指定容器的一个实例。您可以指示 swarm 管理节点使用 Compose 文件中的这些策略,就像您已使用的策略一样。

swarm 管理节点是 swarm 中可以执行命令或授权其他机器加入 swarm 作为工作节点的唯一机器。工作节点仅用于提供功能,并且无权告知任何其他机器它可以做什么和不能做什么。

在前面我们学习了docker services 在本地机器创建5个副本,这次我们可以docker swarm来体验一下在两台机器内,创建5个副本,这5个副本不在同一台机器上,实现真正的集群并且有负载均衡的功能。

 

首先我们得先来安培装一个工具docker-machine,

参考 :https://docs.docker.com/machine/install-machine/

 

$ base=https://github.com/docker/machine/releases/download/v0.14.0 &&   \

  curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&  \

  sudo install /tmp/docker-machine /usr/local/bin/docker-machine

 

先创建两台机器:

root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine create –driver virtualbox myvm1
Running pre-create checks…
(myvm1) No default Boot2Docker ISO found locally, downloading the latest release…
(myvm1) Latest release for github.com/boot2docker/boot2docker is v18.06.1-ce
(myvm1) Downloading /root/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v18.06.1-ce/boot2docker.iso…
(myvm1) 0%….10%….20%….30%….40%….50%….60%….70%….80%….90%….100%
Creating machine…
(myvm1) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.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: “vboxnet0”
(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
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine create –driver virtualbox myvm2
Running pre-create checks…
Creating machine…
(myvm2) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/myvm2/boot2docker.iso…
(myvm2) Creating VirtualBox VM…
(myvm2) Creating SSH key…
(myvm2) Starting the VM…
(myvm2) Check network to re-create if needed…
(myvm2) 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 myvm2

查看创建的docker-machine:

root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
myvm1 – virtualbox Running tcp://192.168.99.100:2376 v18.06.1-ce
myvm2 – virtualbox Running tcp://192.168.99.101:2376 v18.06.1-ce

myvm1集群初始化:
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ssh myvm1 “docker swarm init –advertise-addr 192.168.99.100:2376”
Swarm initialized: current node (mwy9dpd6ea1bqzvhqpi4j2ov3) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join –token SWMTKN-1-1n6a2nkkkutdrij7hdg71hih8x5c2l7oanxa08dmvm9u20kli4-3okzvyjr4hpzu8j4q7ldhfchb 192.168.99.100:2377

To add a manager to this swarm, run ‘docker swarm join-token manager’ and follow the instructions.

myvm2加入集群当中:

root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ssh myvm2 “docker swarm join –token SWMTKN-1-1n6a2nkkkutdrij7hdg71hih8x5c2l7oanxa08dmvm9u20kli4-3okzvyjr4hpzu8j4q7ldhfchb 192.168.99.100:2377”
This node joined a swarm as a worker.
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ssh myvm1 “docker node ls”
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
mwy9dpd6ea1bqzvhqpi4j2ov3 * myvm1 Ready Active Leader 18.06.1-ce
ecvpnan32s3oug4h28bf66rxl myvm2 Ready Active 18.06.1-ce

root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine env myvm1
export DOCKER_TLS_VERIFY=”1″
export DOCKER_HOST=”tcp://192.168.99.100:2376″
export DOCKER_CERT_PATH=”/root/.docker/machine/machines/myvm1″
export DOCKER_MACHINE_NAME=”myvm1″
# Run this command to configure your shell:
# eval $(docker-machine env myvm1)

eval指令是为了登录myvm1做次备:

root@vmuser-virtual-machine:/home/vmuser/workdir/services# eval $(docker-machine env myvm1)
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
myvm1 * virtualbox Running tcp://192.168.99.100:2376 v18.06.1-ce
myvm2 – virtualbox Running tcp://192.168.99.101:2376 v18.06.1-ce

root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ssh myvm1
## .
## ## ## ==
## ## ## ## ## ===
/”””””””””””””””””\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| ‘_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ ‘__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.06.1-ce, build HEAD : c7e5c3e – Wed Aug 22 16:27:42 UTC 2018
Docker version 18.06.1-ce, build e68fc7a
docker@myvm1:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
mwy9dpd6ea1bqzvhqpi4j2ov3 * myvm1 Ready Active Leader 18.06.1-ce
ecvpnan32s3oug4h28bf66rxl myvm2 Ready Active 18.06.1-ce
docker@myvm1:~$ exit

将docker-compose.yml传到myvm1当中:
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine scp docker-compose.yml myvm1:~
docker-compose.yml 100% 371 72.8KB/s 00:00

 在myvm1上面创建服务:
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ssh myvm1 “docker stack deploy -c docker-compose.yml getstartedlab”
Creating network getstartedlab_webnet
Creating service getstartedlab_web

通过如下指令我们可以看到5个副本当中2个副本在myvm1当中,3个副本在myvm2当中,从而所现本地化集群:
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ssh myvm1 “docker stack ps getstartedlab”
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
y597za9yvdcb getstartedlab_web.1 pan19881018/get-start:part2 myvm2 Running Preparing 20 seconds ago
0x8gp8t2r8v2 getstartedlab_web.2 pan19881018/get-start:part2 myvm1 Running Preparing 20 seconds ago
qwkkru77pyib getstartedlab_web.3 pan19881018/get-start:part2 myvm2 Running Preparing 20 seconds ago
v7jry6xie03j getstartedlab_web.4 pan19881018/get-start:part2 myvm2 Running Preparing 20 seconds ago
cucyq1fdgzbh getstartedlab_web.5 pan19881018/get-start:part2 myvm1 Running Preparing 20 seconds ago

 

posted @ 2018-12-25 16:18  嵌入式实操  阅读(211)  评论(0编辑  收藏  举报