开始使用Docker Swarm: 让我们开始使用Docker Swam
让我们开始使用Docker Swarm吧
以下是原文的中文释义:https://docs.docker.com/swarm/install-w-machine/
本页面将介绍如何使用Docker Machine和VirtualBox在本地机器上创建Docker Swarm集群。一旦创建并使用了swarm,您就可以在云提供商或自己的网络上进行测试性的部署。
请您记住,Docker Swarm 目前是一个 beta 版本,所以内容可能会发生变化。在生产环境中使用它目前还不被推荐。
必须的要素
检查本地系统是否安装了VirtualBox。如果您在Mac OS X或Windows上使用Docker,则VirtualBox已经安装完成。
创建 Docker Swarm
Docker Machine可以在主机上运行Docker容器。Docker Swarm的每个节点都需要访问Docker以获取映像并执行容器。Docker Machine将管理Swarm集群的所有配置。
在创建集群之前,首先要创建一个发现令牌(discovery token)。Docker Swarm 使用令牌来进行发现节点和代理注册。使用这个令牌可以安全地创建 swarm 集群。
- 展示系统内的机器清单。
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM
dev * virtualbox Running tcp://192.168.99.100:2376
这个例子是在安装了boot2docker的Mac OS X系统上。它显示了在boot2docker机器上创建的名为dev的环境。
在系统上创建一个名为”local”的VirtualBox虚拟机。
$ docker-machine create -d virtualbox local
INFO[0000] Creating SSH key...
INFO[0000] Creating VirtualBox VM...
INFO[0005] Starting VirtualBox VM...
INFO[0005] Waiting for VM to start...
INFO[0050] "local" has been created and is now the active machine.
INFO[0050] To point your Docker client at it, run this in your shell: eval "$(docker-machine env local)"
3. 将连接到本地机器的环境设置加载到shell中。
$ eval "$(docker-machine env local)"
使用Docker Swarm镜像来生成发现标记的令牌。
下面的swarm create命令将在容器上执行。如果本地机器上不存在swarm:latest镜像,Docker将自动获取它。
$ docker run swarm create
Unable to find image 'swarm:latest' locally
latest: Pulling from swarm
de939d6ed512: Pull complete
79195899a8a4: Pull complete
79ad4f2cc8e0: Pull complete
0db1696be81b: Pull complete
ae3b6728155e: Pull complete
57ec2f5f3e06: Pull complete
73504b2882a3: Already exists
swarm:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:aaaf6c18b8be01a75099cc554b4fb372b8ec677ae81764dcdf85470279a61d6f
Status: Downloaded newer image for swarm:latest
fe0cc96a72cf04dba8c1c4aa79536ec3
这个 swarm crate 命令返回了令牌 fe0cc96a72cf04dba8c1c4aa79536ec3。
5. 将令牌存放在一个安全的地方。
使用生成的令牌,继续下面的步骤来创建Docker Swarm。
启动Swarm manager。
在您的网络上有一个单一的系统,被称为Docker Swarm管理器。Swarm Manager负责在整个集群中的编排和容器的调度。Swarm Manager指挥代理群(也称为节点或Docker节点)。
游泳代理是负责容器托管的组件。它们通常是普通的Docker守护程序,并且可以通过Docker远程API进行通信。
在这一章中,我们将创建Swam Manager和两个节点。
- 创建虚拟机中的Swarm管理器。
docker-machine create \
-d virtualbox \
--swarm \
--swarm-master \
--swarm-discovery token://<TOKEN-FROM-ABOVE> \
swarm-master
执行示例:
$ docker-machine create -d virtualbox --swarm --swarm-master --swarm-discovery token://fe0cc96a72cf04dba8c1c4aa79536ec3 swarm-master
INFO[0000] Creating SSH key...
INFO[0000] Creating VirtualBox VM...
INFO[0005] Starting VirtualBox VM...
INFO[0005] Waiting for VM to start...
INFO[0060] "swarm-master" has been created and is now the active machine.
INFO[0060] To point your Docker client at it, run this in your shell: eval "$(docker-machine env swarm-master)"
打开VirtualBox管理器(在电脑上),您会看到一个本地机器和一个新的swarm-master机器。
3. 创建 Swarm 节点。
docker-machine create \
-d virtualbox \
--swarm \
--swarm-discovery token://<TOKEN-FROM-ABOVE> \
swarm-agent-00
Example of execution:
$ docker-machine create -d virtualbox --swarm --swarm-discovery token://fe0cc96a72cf04dba8c1c4aa79536ec3 swarm-agent-00
INFO[0000] Creating SSH key...
INFO[0000] Creating VirtualBox VM...
INFO[0005] Starting VirtualBox VM...
INFO[0006] Waiting for VM to start...
INFO[0066] "swarm-agent-00" has been created and is now the active machine.
INFO[0066] To point your Docker client at it, run this in your shell: eval "$(docker-machine env swarm-agent-00)"
我們將新增一位名為swam-agent-01的其他代理人。
$ docker-machine create -d virtualbox --swarm --swarm-discovery token://fe0cc96a72cf04dba8c1c4aa79536ec3 swarm-agent-01
在VirtualBox管理器中,会显示两个代理。
直接连接到Swam
在这个步骤中,连接到Swarm机器并显示与Swarm集群相关的信息,然后在Swarm集群上启动镜像。
- 指定 Swarm 管理的机器上正在运行的 Docker 环境变量。
$ eval $(docker-machine env --swarm swarm-master)
使用Docker命令,获取新创建的Swarm集群的信息。
$ docker info
Containers: 4
Strategy: spread
Filters: affinity, health, constraint, port, dependency
Nodes: 3
swarm-agent-00: 192.168.99.105:2376
└ Containers: 1
└ Reserved CPUs: 0 / 8
└ Reserved Memory: 0 B / 1.023 GiB
swarm-agent-01: 192.168.99.106:2376
└ Containers: 1
└ Reserved CPUs: 0 / 8
└ Reserved Memory: 0 B / 1.023 GiB
swarm-master: 192.168.99.104:2376
└ Containers: 2
└ Reserved CPUs: 0 / 8
我们可以看到每个代理和主节点都公开了端口2376。在创建Swarm集群时,您可以使用任意端口,也可以在每个节点上使用不同的端口。每个Swarm节点都运行Swarm代理容器。
マスタはSwarm ManagerおよびSwarmエージェントコンテナの両方で動作していますが、エージェントのフェイルオーバーに関する問題があるため、本番環境での利用は推奨されません。ただし、このような状況は、動作環境を学ぶためには非常に役立ちます。
- 我们会在Swam集群上检查正在运行的镜像。
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78be991b58d1 swarm:latest "/swarm join --addr 3 minutes ago Up 2 minutes 2375/tcp swarm-agent-01/swarm-agent
da5127e4f0f9 swarm:latest "/swarm join --addr 6 minutes ago Up 6 minutes 2375/tcp swarm-agent-00/swarm-agent
ef395f316c59 swarm:latest "/swarm join --addr 16 minutes ago Up 16 minutes 2375/tcp swarm-master/swarm-agent
45821ca5208e swarm:latest "/swarm manage --tls 16 minutes ago Up 16 minutes 2375/tcp, 192.168.99.104:3376->3376/tcp swarm-master/swarm-agent-master
在 Swam 集群上执行 Docker 的 hello-world 测试镜像。
$ docker run hello-world
Hello from Docker.
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(Assuming it was not already locally available.)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
For more examples and ideas, visit:
http://docs.docker.com/userguide/
使用docker ps命令,确认在哪个节点上运行着哪个容器。
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
54a8690043dd hello-world:latest "/hello" 22 seconds ago Exited (0) 3 seconds ago swarm-agent-00/modest_goodall
78be991b58d1 swarm:latest "/swarm join --addr 5 minutes ago Up 4 minutes 2375/tcp swarm-agent-01/swarm-agent
da5127e4f0f9 swarm:latest "/swarm join --addr 8 minutes ago Up 8 minutes 2375/tcp swarm-agent-00/swarm-agent
ef395f316c59 swarm:latest "/swarm join --addr 18 minutes ago Up 18 minutes 2375/tcp swarm-master/swarm-agent
45821ca5208e swarm:latest "/swarm manage --tls 18 minutes ago Up 18 minutes 2375/tcp, 192.168.99.104:3376->3376/tcp swarm-master/swarm-agent-master