Docker 生态系统工具的使用与实践
1. 使用 Ansible 部署 WordPress 和 MySQL 容器
1.1 任务配置
可以使用 Ansible 来部署 WordPress 和 MySQL 容器,具体的任务配置如下:
- hosts: wordpress
tasks:
- name: Docker pull mysql
command: docker pull mysql:latest
- name: Docker pull wordpress
command: docker pull wordpress:latest
- name: Run mysql container
docker:
name=mysql
image=mysql
detach=true
env="MYSQL_ROOT_PASSWORD=wordpressdocker,MYSQL_DATABASE=wordpress, \
MYSQL_USER=wordpress,MYSQL_PASSWORD=wordpresspwd"
- name: Run wordpress container
docker:
image=wordpress
env="WORDPRESS_DB_NAME=wordpress,WORDPRESS_DB_USER=wordpress, \
WORDPRESS_DB_PASSWORD=wordpresspwd"
ports="80:80"
detach=true
links="mysql:mysql"
1.2 使用 Vagrant 运行 Playbook
可以使用 Vagrant 的 Ansible 预配器在虚拟机启动时运行 playbook。操作步骤如下:
1. 进入
ch07/ansible/solo
目录。
2. 执行
vagrant up
命令,Nginx 容器将自动启动。
2. 使用 Deis
2.1 问题描述
需要一个平台来简化应用程序的部署和管理。
2.2 解决方案
安装和配置 Deis,具体步骤如下:
1. 安装
deisctl
:
$ curl -sSL http://deis.io/deisctl/install.sh | sh -s 1.4.1
- 克隆 Deis 仓库:
$ git clone https://github.com/deis/deis.git
- 进入 Deis 目录:
$ cd deis
- 生成发现 URL:
$ make discovery-url
- 启动 Vagrant 虚拟机:
$ vagrant up
- 添加 SSH 密钥:
$ ssh-add ~/.vagrant.d/insecure_private_key
- 设置环境变量:
$ export DEISCTL_TUNNEL=172.17.8.100
- 配置平台:
$ deisctl config platform set sshPrivateKey=$HOME/.vagrant.d/insecure_private_key
$ deisctl config platform set domain=local3.deisapp.com
- 安装平台:
$ deisctl install platform
3. 使用 Rancher 管理 Docker 主机集群中的容器
3.1 问题描述
需要一个系统来管理生产环境中的容器,该系统支持多主机、覆盖网络、组管理和强大的仪表盘。
3.2 解决方案
使用 Rancher 来管理容器,操作步骤如下:
1. 克隆 Rancher 项目仓库:
$ git clone https://github.com/rancherio/rancher.git
- 进入 Rancher 目录:
$ cd rancher
- 启动 Vagrant 虚拟机:
$ vagrant up
- 启动管理服务器和工作代理容器:
$ docker run -d -p 8080:8080 rancher/server:latest
$ docker run -e CATTLE_AGENT_IP=172.17.8.100 --privileged -e WAIT=true \
-v /var/run/docker.sock:/var/run/docker.sock \
rancher/agent:latest http://localhost:8080
-
访问 Rancher 仪表盘:
如果本地机器的 8080 端口没有被占用,可以通过http://localhost:8080访问仪表盘;如果被占用,Vagrant 会选择一个不同的端口,可以通过http://172.17.8.100:8080访问。
3.3 操作流程
graph LR
A[克隆 Rancher 仓库] --> B[进入 Rancher 目录]
B --> C[启动 Vagrant 虚拟机]
C --> D[启动管理服务器容器]
C --> E[启动工作代理容器]
D --> F[访问 Rancher 仪表盘]
E --> F
4. 通过 Apache Mesos 和 Marathon 运行容器
4.1 问题描述
需要一个集群调度器来在数据中心的 Docker 主机上启动容器,并且可能已经在运行 Apache Mesos 来调度长期运行的作业、cron 作业或 Hadoop 等并行计算工作负载,希望使用它来运行容器。
4.2 解决方案
使用 Apache Mesos 和 Docker 容器化器,结合 Marathon 框架来运行容器,操作步骤如下:
1. 克隆
playa-mesos
仓库:
$ git clone https://github.com/mesosphere/playa-mesos.git
- 启动 Vagrant 虚拟机:
$ vagrant up
- SSH 连接到虚拟机:
$ vagrant ssh
-
访问 Mesos 和 Marathon 网页界面:
Mesos 界面:http://10.141.141.10:5050
Marathon 界面:http://10.141.141.10:8080
4.3 启动 HTTP 应用程序
-
创建
test.json文件,内容如下:
{
"id": "http",
"cmd": "python -m SimpleHTTPServer $PORT0",
"mem": 50,
"cpus": 0.1,
"instances": 1,
"constraints": [
["hostname", "UNIQUE"]
],
"ports": [0]
}
-
使用
curl提交应用程序:
$ curl -is -H "Content-Type: application/json" -d @test.json 10.141.141.10:8080/v2/apps
4.4 启动 Nginx 容器应用程序
- 配置 Mesos 从节点使用 Docker 容器化器:
vagrant@mesos:~$ sudo su
root@mesos:/home/vagrant# cd /etc/mesos-slave
root@mesos:/etc/mesos-slave# echo 'docker,mesos' > containerizers
root@mesos:/etc/mesos-salve# echo '5mins' > executor_registration_timeout
root@mesos:/etc/mesos-slave# service mesos-slave restart
-
创建
docker.json文件,内容如下:
{
"container": {
"type": "DOCKER",
"docker": {
"image": "nginx",
"network": "BRIDGE",
"portMappings": [
{ "containerPort": 80, "hostPort": 0 }
]
}
},
"id": "nginx",
"instances": 1,
"cpus": 0.5,
"mem": 512
}
-
使用
curl创建应用程序并检查运行的应用程序列表:
$ curl -si -H 'Content-Type: application/json' -d @docker.json 10.141.141.10:8080/v2/apps
$ curl -sX GET -H "Content-Type: application/json" 10.141.141.10:8080/v2/tasks | python -m json.tool
4.5 操作表格
| 操作步骤 | 命令 |
|---|---|
| 克隆仓库 |
git clone https://github.com/mesosphere/playa-mesos.git
|
| 启动虚拟机 |
vagrant up
|
| SSH 连接 |
vagrant ssh
|
| 提交 HTTP 应用 |
curl -is -H "Content-Type: application/json" -d @test.json 10.141.141.10:8080/v2/apps
|
| 配置从节点 |
echo 'docker,mesos' > containerizers; echo '5mins' > executor_registration_timeout; service mesos-slave restart
|
| 提交 Nginx 应用 |
curl -si -H 'Content-Type: application/json' -d @docker.json 10.141.141.10:8080/v2/apps
|
| 检查任务列表 |
curl -sX GET -H "Content-Type: application/json" 10.141.141.10:8080/v2/tasks | python -m json.tool
|
5. 在 Mesos 集群上使用 Mesos Docker 容器化器
5.1 解决方案
可以构建一个使用容器和 Mesosphere 在 Docker Hub 上准备的镜像的 Mesos 集群,并配置 Mesos 从节点使用 Docker 容器化器。为了便于测试,可以使用相关的 Vagrantfile 来设置一个本地三节点的 Mesos 集群,并使用 Ansible 启动运行 Mesos 软件组件的容器。具体操作步骤如下:
1. 克隆仓库:
$ git clone https://github.com/how2dock/docbook.git
- 进入目录:
$ cd dockbook/ch07/mesos
- 启动集群:
$ vagrant up
- 查看状态:
$ vagrant status
- 运行 Ansible playbook 启动容器:
$ ansible-playbook -u vagrant mesos.yml
-
访问界面:
-
Mesos UI:
http://192.168.33.10:5050 -
Marathon UI:
http://192.168.33.10:8080
-
Mesos UI:
- 启动 Nginx 容器应用程序:
$ curl -si -H 'Content-Type: application/json' -d @docker.json 192.168.33.10:8080/v2/apps
5.2 注意事项
- 确保从节点有足够的内存,否则应用程序可能会卡在部署阶段。
- 如果更改了节点的 IP 地址或添加了更多节点,需要更新 Ansible 的 inventory 文件。
- 目前使用 Ansible Docker 模块的 play 可能无法获得可用的集群,如果发现问题可以提交相关请求。
5.3 操作流程图
graph LR
A[克隆仓库] --> B[进入目录]
B --> C[启动集群]
C --> D[查看状态]
D --> E[运行 Ansible playbook]
E --> F[访问界面]
F --> G[启动 Nginx 应用]
6. 使用 Registrator 发现 Docker 服务
6.1 问题描述
在构建基于容器的分布式应用程序时,需要自动发现多个主机上启动的服务,以便配置应用程序。
6.2 解决方案
使用 Registrator 来实现服务发现,它作为一个容器运行在系统的主机上,通过挂载 Docker 套接字
/var/run/docker.sock
监听容器的启动和停止事件,并在数据存储后端上注册或注销这些服务。目前有多种后端数据存储可供选择,如 etcd、consul、skyDNS2 等。具体操作步骤如下:
6.2.1 设置 etcd 后端
- 下载 etcd:
$ curl -L https://github.com/coreos/etcd/releases/download/v0.4.6/etcd-v0.4.6-linux-amd64.tar.gz -o etcd-v0.4.6-linux-amd64.tar.gz
- 解压文件:
$ tar xzvf etcd-v0.4.6-linux-amd64.tar.gz
- 进入目录:
$ cd etcd-v0.4.6-linux-amd64
- 启动 etcd:
$ sudo ./etcd
- 创建目录:
$ ./etcdctl mkdir cookbook
- 查看目录:
$ ./etcdctl ls
6.2.2 运行 Registrator
- 拉取 Registrator 镜像:
$ docker pull gliderlabs/registrator
- 运行 Registrator 容器:
$ docker run -d -v /var/run/docker.sock:/tmp/docker.sock -h 192.168.33.10 gliderlabs/registrator -ip 192.168.33.10 etcd://192.168.33.10:4001/cookbook
6.2.3 启动容器并验证注册
- 启动 Nginx 容器:
$ docker run -d -p 80:80 nginx
- 查看 etcd 中的注册信息:
$ ./etcdctl ls /cookbook
$ ./etcdctl ls /cookbook/nginx-80
$ ./etcdctl get /cookbook/nginx-80/192.168.33.10:pensive_franklin:80
- 查看 Registrator 容器日志:
$ docker logs <CONTAINER_ID>
6.2.4 操作表格
| 操作步骤 | 命令 |
|---|---|
| 下载 etcd |
curl -L https://github.com/coreos/etcd/releases/download/v0.4.6/etcd-v0.4.6-linux-amd64.tar.gz -o etcd-v0.4.6-linux-amd64.tar.gz
|
| 解压 etcd |
tar xzvf etcd-v0.4.6-linux-amd64.tar.gz
|
| 进入 etcd 目录 |
cd etcd-v0.4.6-linux-amd64
|
| 启动 etcd |
sudo ./etcd
|
| 创建目录 |
./etcdctl mkdir cookbook
|
| 拉取 Registrator 镜像 |
docker pull gliderlabs/registrator
|
| 运行 Registrator 容器 |
docker run -d -v /var/run/docker.sock:/tmp/docker.sock -h 192.168.33.10 gliderlabs/registrator -ip 192.168.33.10 etcd://192.168.33.10:4001/cookbook
|
| 启动 Nginx 容器 |
docker run -d -p 80:80 nginx
|
| 查看 etcd 注册信息 |
./etcdctl ls /cookbook; ./etcdctl ls /cookbook/nginx-80; ./etcdctl get /cookbook/nginx-80/192.168.33.10:pensive_franklin:80
|
| 查看 Registrator 日志 |
docker logs <CONTAINER_ID>
|
通过以上这些工具和方法,可以更高效地管理和部署 Docker 容器,构建分布式应用程序。不同的工具适用于不同的场景和需求,可以根据实际情况选择合适的工具来满足自己的业务需求。
超级会员免费看
1222

被折叠的 条评论
为什么被折叠?



