Docker Swarm是Docker的本机集群。 它允许您使用全套Docker工具创建和访问Docker主机池。 因为Docker Swarm提供了标准的Docker API,所以任何已经与Docker守护程序通信的工具都可以使用Swarm透明地扩展到多个主机。
Docker Swarm具有一个Manager ,一个预定义的Docker Host,并且是所有管理的单点。 Swarm Manager在整个集群上编排和调度容器,并且可以在High Availability中进行配置。 容器部署在作为其他Docker主机的节点上。
Swarm与托管的发现服务进行对话,该服务维护您群集中的IP列表。 对于开发而言,它易于使用,托管在Docker Hub上的默认发现服务。 安装和创建Docker Swarm中提供了完整的说明。 该博客将展示如何使用Consul设置Docker Swarm Cluster。
让我们开始吧!
创建领事发现服务
- 创建将托管发现服务的计算机:
docker-machine create -d=virtualbox consul-machine Running pre-create checks... Creating machine... Waiting for machine to be running, this may take a few minutes... Machine is running, waiting for SSH to be available... Detecting operating system of created instance... Provisioning created instance... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon... To see how to connect Docker to this machine, run: docker-machine env consul-machine
- 连接到此机器:
eval $(docker-machine env consul-machine)
- 使用以下Compose文件运行Consul服务:
myconsul: image: progrium/consul restart: always hostname: consul ports: - 8500:8500 command: "-server -bootstrap"
该文件也可以在github.com/arun-gupta/docker-images/tree/master/consul中找到 。 服务启动为:
docker-compose up -d Pulling myconsul (progrium/consul:latest)... latest: Pulling from progrium/consul 3b4d28ce80e4: Pull complete e5ab901dcf2d: Pull complete 30ad296c0ea0: Pull complete 3dba40dec256: Pull complete f2ef4387b95e: Pull complete 53bc8dcc4791: Pull complete 75ed0b50ba1d: Pull complete 17c3a7ed5521: Pull complete 8aca9e0ecf68: Pull complete 4d1828359d36: Pull complete 46ed7df7f742: Pull complete b5e8ce623ef8: Pull complete 049dca6ef253: Pull complete bdb608bc4555: Pull complete 8b3d489cfb73: Pull complete c74500bbce24: Pull complete 9f3e605442f6: Pull complete d9125e9e799b: Pull complete Digest: sha256:8cc8023462905929df9a79ff67ee435a36848ce7a10f18d6d0faba9306b97274 Status: Downloaded newer image for progrium/consul:latest Creating consul_myconsul_1
启动的容器可以验证为:
docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f05d8dd11e7f progrium/consul "/bin/start -server -" 30 seconds ago Up 29 seconds 53/tcp, 53/udp, 8300-8302/tcp, 8400/tcp, 0.0.0.0:8500->8500/tcp, 8301-8302/udp consul_myconsul_1
使用Consul创建Docker Swarm集群
Swarm与Machine完全集成在一起,因此是最简单的入门方法。
- 使用Consul发现服务创建Swarm Master:
docker-machine create -d virtualbox --swarm --swarm-master --swarm-discovery="consul://$(docker-machine ip consul-machine):8500" --engine-opt="cluster-store=consul://$(docker-machine ip consul-machine):8500" --engine-opt="cluster-advertise=eth1:2376" swarm-master Running pre-create checks... Creating machine... Waiting for machine to be running, this may take a few minutes... Machine is running, waiting for SSH to be available... Detecting operating system of created instance... Provisioning created instance... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon... Configuring swarm... To see how to connect Docker to this machine, run: docker-machine env swarm-master
在此处查看三个选项:
-
--swarm-discovery
定义发现服务的地址 -
--cluster-advertise
在网络上播发计算机 -
--cluster-store
指定集群的分布式k / v存储后端
此外,
--swarm
将机器配置为Swarm,--swarm-master
将创建的机器配置为Swarm主机。 -
- 连接到这个新创建的母版,并找到有关它的一些信息:
eval "$(docker-machine env swarm-master)" docker info
这将输出显示为:
docker info Containers: 2 Images: 8 Server Version: 1.9.0 Storage Driver: aufs Root Dir: /mnt/sda1/var/lib/docker/aufs Backing Filesystem: tmpfs Dirs: 12 Dirperm1 Supported: true Execution Driver: native-0.2 Logging Driver: json-file Kernel Version: 4.1.12-boot2docker Operating System: Boot2Docker 1.9.0 (TCL 6.4); master : 16e4a2a - Tue Nov 3 19:49:22 UTC 2015 CPUs: 1 Total Memory: 996.2 MiB Name: swarm-master ID: 2EDA:WPOD:YVWO:GGLZ:ZUHY:WCBU:ZERW:OWBE:6MPQ:IPXN:BS2V:QCSI Debug mode (server): true File Descriptors: 30 Goroutines: 67 System Time: 2015-11-29T02:08:10.636829121Z EventsListeners: 1 Init SHA1: Init Path: /usr/local/bin/docker Docker Root Dir: /mnt/sda1/var/lib/docker Username: arungupta Registry: https://index.docker.io/v1/ Labels: provider=virtualbox Cluster store: consul://192.168.99.109:8500 Cluster advertise: 192.168.99.111:2376
- 创建一个新的Machine作为该Swarm集群的一部分:
docker-machine create -d virtualbox --swarm --swarm-discovery="consul://$(docker-machine ip consul-machine):8500" --engine-opt="cluster-store=consul://$(docker-machine ip consul-machine):8500" --engine-opt="cluster-advertise=eth1:2376" swarm-node-01 Running pre-create checks... Creating machine... Waiting for machine to be running, this may take a few minutes... Machine is running, waiting for SSH to be available... Detecting operating system of created instance... Provisioning created instance... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon... Configuring swarm... To see how to connect Docker to this machine, run: docker-machine env swarm-node-01
机器使用
--swarm-discovery
与发现服务--swarm-discovery
。 - 在此集群中创建第二个节点:
docker-machine create -d virtualbox --swarm --swarm-discovery="consul://$(docker-machine ip consul-machine):8500" --engine-opt="cluster-store=consul://$(docker-machine ip consul-machine):8500" --engine-opt="cluster-advertise=eth1:2376" swarm-node-02 Running pre-create checks... Creating machine... Waiting for machine to be running, this may take a few minutes... Machine is running, waiting for SSH to be available... Detecting operating system of created instance... Provisioning created instance... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon... Configuring swarm... To see how to connect Docker to this machine, run: docker-machine env swarm-node-02
- 列出所有创建的计算机:
docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM consul-machine - virtualbox Running tcp://192.168.99.109:2376 default - virtualbox Running tcp://192.168.99.100:2376 swarm-master * virtualbox Running tcp://192.168.99.111:2376 swarm-master (master) swarm-node-01 - virtualbox Running tcp://192.168.99.112:2376 swarm-master swarm-node-02 - virtualbox Running tcp://192.168.99.113:2376 swarm-master
属于群集的计算机在“群集”列中具有群集的名称,否则为空白。 例如,“
default
”和“consul-machine
”是独立计算机,其中所有其他计算机都是“swarm-master
”集群的一部分。 Swarm主机也由SWARM列中的(主机)标识。 - 连接到Swarm集群并找到有关它的一些信息:
eval "$(docker-machine env --swarm swarm-master)" docker info
这里的主要区别是
--swarm
当查找有关Swarm集群的信息而不是单个Machine时。输出显示为:
docker info Containers: 4 Images: 3 Role: primary Strategy: spread Filters: health, port, dependency, affinity, constraint Nodes: 3 swarm-master: 192.168.99.111:2376 └ Containers: 2 └ Reserved CPUs: 0 / 1 └ Reserved Memory: 0 B / 1.021 GiB └ Labels: executiondriver=native-0.2, kernelversion=4.1.12-boot2docker, operatingsystem=Boot2Docker 1.9.0 (TCL 6.4); master : 16e4a2a - Tue Nov 3 19:49:22 UTC 2015, provider=virtualbox, storagedriver=aufs swarm-node-01: 192.168.99.112:2376 └ Containers: 1 └ Reserved CPUs: 0 / 1 └ Reserved Memory: 0 B / 1.021 GiB └ Labels: executiondriver=native-0.2, kernelversion=4.1.12-boot2docker, operatingsystem=Boot2Docker 1.9.0 (TCL 6.4); master : 16e4a2a - Tue Nov 3 19:49:22 UTC 2015, provider=virtualbox, storagedriver=aufs swarm-node-02: 192.168.99.113:2376 └ Containers: 1 └ Reserved CPUs: 0 / 1 └ Reserved Memory: 0 B / 1.021 GiB └ Labels: executiondriver=native-0.2, kernelversion=4.1.12-boot2docker, operatingsystem=Boot2Docker 1.9.0 (TCL 6.4); master : 16e4a2a - Tue Nov 3 19:49:22 UTC 2015, provider=virtualbox, storagedriver=aufs CPUs: 3 Total Memory: 3.064 GiB Name: 10edc606d097
有3个节点-一个Swarm主节点和2个Swarm _worker_节点。 此群集中总共有4个容器在运行–主节点和每个节点上有一个Swarm代理,主节点上还有一个额外的swarm-agent-master。 可以通过连接到主数据库并列出所有容器来验证这一点。
- 使用以下命令列出集群中的节点:
docker run swarm list consul://$(docker-machine ip consul-machine):8500 192.168.99.111:2376 192.168.99.112:2376 192.168.99.113:2376
随后的博客将解释如何将应用程序部署到此Docker Swarm集群。
请享用!
翻译自: https://www.javacodegeeks.com/2015/12/docker-swarm-cluster-using-consul.html