Docker Swarm入门(四)Demo
【编者的话】本文作者
Matt Bajor热衷Docker及相关产品的研究,本文是他写的Docker Swarm入门系列的第四篇,通过一个Demo让读者更好地了解Docker Swarm。
我的hackweek努力的主要方向是Vagrant环境下的Docker Swarm集群。这篇文章将看看如何使它运行起来以及如何与它进行交互。
它是什么?
这是一个在Vagrant环境中运行的Docker Swarm集群,包括4个节点:
- dockerhost01
- dockerhost02
- dockerhost03
- dockerswarm01
Docker节点(dockerhost01到dockerhost03)运行了Docker守护进程以及一些配套服务,如下:
- Docker daemon:配置了一系列参数后运行的进程。
- Registrator daemon:这个守护进程可以连接到Consul,以注册和注销有端口公开的容器。相关的端口配置信息可以在
/services
路径下的Consul的键/值存储中看到。 - Swarm客户端:Swarm客户端维护在Consul中的Swarm节点列表。此列表保存在
/swarm
下,包含集群中参与的Swarm节点<ip>:<ports>
的列表。
Docker Swarm节点(dockerswarm01)同样也运行了几个服务。由于这仅仅是一个例子,所以我们只使用了一台机器。如果是生产环境,你可以使用多台机器来运行Docker Swarm节点。
- Swarm daemon:作为master以及监听Docker的命令而代理他们到Docker主机。
- Consul:单个节点的Consul实例。它的用户界面在这里。
- Nginx:代理Consul的UI。
如何搭建集群
1.安装前提
- GitHub Repo:docker-swarm-demo
- Vagrant(最新):Vagrant下载
- Vagrant主机插件:
vagrant plugin install vagrant-hosts
- VirtualBox:VirtualBox下载
- Ansible:
brew install ansible
- 主机信息:添加以下行到
/etc/hosts
文件:
10.100.199.200 dockerswarm01
10.100.199.201 dockerhost01
10.100.199.202 dockerhost02
10.100.199.203 dockerhost03
2a. Clone && Vagrant up(无TLS)
这个过程可能会耗费一些时间,因为要下载几G的数据。这种情况是不使用TLS的。如果你想要Swarm使用TLS,去步骤2b。
# Clone our repo
git clone https://github.com/technolo-g/docker-swarm-demo.git
cd docker-swarm-demo
# Bring up the cluster with Vagrant
vagrant up
# Provision the host files on the vagrant hosts
vagrant provision --provision-with hosts
# Activate your enviornment
source bin/env
2b. Clone && Vagrant up(有TLS)
这种情况会生成证书而且集群会启用TLS。
# Clone our repo
git clone https://github.com/technolo-g/docker-swarm-demo.git
cd docker-swarm-demo
# Generate Certs
./bin/gen_ssl.sh
# Enable TLS for the cluster
echo -e "use_tls: True\ndocker_port: 2376" > ansible/group_vars/all.yml
# Bring up the cluster with Vagrant
vagrant up
# Provision the host files on the vagrant hosts
vagrant provision --provision-with hosts
# Activate your TLS enabled enviornment
source bin/env_tls
3. 确认正常工作
现在,集群已经配置完毕并运行,你需要确认它是否正常工作。首先看一下Docker客户端:
$ docker version
Client version: 1.4.1
Client API version: 1.16
Go version (client): go1.4
Git commit (client): 5bc2ff8
OS/Arch (client): darwin/amd64
Server version: swarm/0.0.1
Server API version: 1.16
Go version (server): go1.2.1
Git commit (server): n/a
$ docker info
Containers: 0
Nodes: 3
dockerhost02: 10.100.199.202:2376
dockerhost01: 10.100.199.201:2376
dockerhost03: 10.100.199.203:2376
然后在浏览器中输入
http://dockerswarm01/ui/#/test/kv/swarm/来访问Consul,并确认Docker主机以及它的端口信息:
集群好像可以正常工作了,现在让我们在上边配置一个应用吧。
如何使用它
现在,你可以使用Swarm集群来配置节点。在Vagrant配置期间相关的镜像已经下载完毕,所以运行时速度会非常快。关于命令有两件事情需要注意:
- 启动Docker后,约束需要与相应的标签匹配,Swarm的过滤器需要知道当前哪些主机是可供调度的。
- SERVICE_NAME变量是为Registrator设置的。由于我们使用的是通用的容器,所以以这种方式来代替指定服务的名称。
# Primary load balancer
docker run -d \
-e constraint:zone==external \
-e constraint:status==master \
-e SERVICE_NAME=proxy \
-p 80:80 \
nginx:latest
# Secondary load balancer
docker run -d \
-e constraint:zone==external \
-e constraint:status==non-master \
-e SERVICE_NAME=proxy \
-p 80:80 \
nginx:latest
# 3 Instances of the webapp
docker run -d \
-e constraint:zone==internal \
-e SERVICE_NAME=webapp \
-p 80 \
nginx:latest
docker run -d \
-e constraint:zone==internal \
-e SERVICE_NAME=webapp \
-p 80 \
nginx:latest
docker run -d \
-e constraint:zone==internal \
-e SERVICE_NAME=webapp \
-p 80 \
nginx:latest
现在你如果运行
docker ps
或者输入
http://dockerswarm01/ui/#/test/kv/services来浏览Consul会得到如下页面:
你可以看到有两个已经注册的服务!由于路由和服务发现部分是额外的,这个程序实际上不会工作,但我觉得你已经有了思路。
我希望你喜欢有关Docker Swarm的这个系列。我发现Docker Swarm是由一个伟大的且非常敏捷的团队开发的一个前景广阔的应用。我相信它将改变我们使用Docker主机的方式,并大大简化使用复杂应用程序的流程。
===========================
译者介绍
田浩浩,