diff --git a/README.md b/README.md index ac2c739..c8763e9 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,28 @@ Docker —— 从入门到实践 =============== -v0.3.0 +v0.3.1 -[Docker](docker.com) 是个伟大的项目,它彻底释放了虚拟化的威力,让应用的分发、部署和管理都变得前所未有的高效和轻松! +[Docker](docker.com) 是个伟大的项目,它彻底释放了虚拟化的威力,极大降低了云计算资源供应的成本,同时让应用的分发、测试、部署和分发都变得前所未有的高效和轻松! -本书既适用于具备基础 Linux 知识的 Docker 初学者,也可供希望理解原理和实现的高级用户参考。同时,书中给出的实践案例,可供在进行实际部署时借鉴。 +本书既适用于具备基础 Linux 知识的 Docker 初学者,也希望可供理解原理和实现的高级用户参考。同时,书中给出的实践案例,可供在进行实际部署时借鉴。前六章为基础内容,供用户理解 Docker 的基本概念和操作;7 ~ 9 章介绍一些高级操作;第 10 章给出典型的应用场景和实践案例;11 ~ 13 章介绍关于 Docker 实现的相关技术。14 ~ 章介绍相关的一些开源项目。 -本书前六章为基础内容,供用户理解 Docker 的基本概念和操作;7 ~ 9 章介绍一些高级操作;第 10 章给出典型的应用场景和实践案例;11 ~ 13 章介绍关于 Docker 实现的相关技术。 +在线阅读:[GitBook](https://www.gitbook.io/book/yeasy/docker_practice) 或 [DockerPool](http://dockerpool.com/static/books/docker_practice/index.html)。 -最新版本在线阅读:[GitBook](https://www.gitbook.io/book/yeasy/docker_practice) 或 [DockerPool](http://dockerpool.com/static/books/docker_practice/index.html)。欢迎加入 DockerPool QQ 群(341410255),分享 Docker 资源,交流 Docker 技术。 +欢迎关注 DockerPool 社区微博 [@dockerpool](http://weibo.com/u/5345404432),或加入 DockerPool QQ 群(341410255),分享 Docker 资源,交流 Docker 技术。 ![Docker 技术入门与实战](docker_primer.png) 《[Docker 技术入门与实战](http://item.jd.com/11598400.html)》一书已经正式出版,包含大量第一手实战案例,欢迎大家阅读使用。 -[京东图书](http://item.jd.com/11598400.html) [当当图书](http://product.dangdang.com/23620853.html) [China-Pub](http://product.china-pub.com/3770833) +* [China-Pub](http://product.china-pub.com/3770833) +* [京东图书](http://item.jd.com/11598400.html) +* [当当图书](http://product.dangdang.com/23620853.html) +* [亚马逊图书](http://www.amazon.cn/%E5%9B%BE%E4%B9%A6/dp/B00R5MYI7C/ref=lh_ni_t?ie=UTF8&psc=1&smid=A1AJ19PSB66TGU) ## 主要版本历史 +* 0.4: 2015-01-TBD + * 添加 Etcd 项目 * 0.3: 2014-11-25 * 完成仓库章节; * 重写安全章节; @@ -37,17 +42,15 @@ v0.3.0 * 修正错别字和表达不通顺的地方。 -本书源码在 Github 上维护,欢迎参与:[https://github.com/yeasy/docker_practice](https://github.com/yeasy/docker_practice)。 - -感谢所有的 [贡献者](https://github.com/yeasy/docker_practice/graphs/contributors)。 +本书源码在 Github 上维护,欢迎参与:[https://github.com/yeasy/docker_practice](https://github.com/yeasy/docker_practice)。贡献者 [名单](https://github.com/yeasy/docker_practice/graphs/contributors)。 ## 参加步骤 * 在 GitHub 上 `fork` 到自己的仓库,如 `docker_user/docker_practice`,然后 `clone` 到本地,并设置用户信息。 ``` $ git clone git@github.com:docker_user/docker_practice.git $ cd docker_practice -$ git config user.name "Docker User" -$ git config user.email docker_user@dockcer.com +$ git config user.name "yourname" +$ git config user.email "your email" ``` * 修改代码后提交,并推送到自己的仓库。 ``` diff --git a/SUMMARY.md b/SUMMARY.md index d28498d..49f00a3 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -69,6 +69,10 @@ * [联合文件系统](underly/ufs.md) * [容器格式](underly/container_format.md) * [网络](underly/network.md) +* [相关项目-Etcd](etcd/README.md) + * [项目简介](etcd/intro.md) + * [安装](etcd/install.md) + * [使用 etcdctl](etcd/etcdctl.md) * [附录一:命令查询](appendix_command/README.md) * [附录二:常见仓库介绍](appendix_repo/README.md) * [Ubuntu](appendix_repo/ubuntu.md) diff --git a/_images/etcd_logo.png b/_images/etcd_logo.png new file mode 100644 index 0000000..2638ff3 Binary files /dev/null and b/_images/etcd_logo.png differ diff --git a/etcd/README.md b/etcd/README.md new file mode 100644 index 0000000..bacd48a --- /dev/null +++ b/etcd/README.md @@ -0,0 +1,3 @@ +# etcd + +etcd 是 CoreOS 团队发起的一个管理配置信息和服务发现(service discovery)的项目,在这一章里面,我们将介绍该项目的目标,安装和使用,以及实现的技术。 diff --git a/etcd/etcdctl.md b/etcd/etcdctl.md new file mode 100644 index 0000000..8d83874 --- /dev/null +++ b/etcd/etcdctl.md @@ -0,0 +1,282 @@ +## 使用 etcdctl + +etcdctl 是一个命令行客户端,它能提供一些简洁的命令,供用户直接跟 etcd 服务打交道,而无需基于 HTTP API 方式。这在某些情况下将很方便,例如用户对服务进行测试或者手动修改数据库内容。我们也推荐在刚接触 etcd 时通过 etcdctl 命令来熟悉相关的操作,这些操作跟 HTTP API 实际上是对应的。 + +etcd 项目二进制发行包中已经包含了 etcdctl 工具,没有的话,可以从 (github.com/coreos/etcd/releases)[https://github.com/coreos/etcd/releases] 下载。 + +etcdctl 支持如下的命令,大体上分为数据库操作和非数据库操作两类,后面将分别进行解释。 + +``` +$ etcdctl -h +NAME: + etcdctl - A simple command line client for etcd. + +USAGE: + etcdctl [global options] command [command options] [arguments...] + +VERSION: + 2.0.0-rc.1 + +COMMANDS: + backup backup an etcd directory + mk make a new key with a given value + mkdir make a new directory + rm remove a key + rmdir removes the key if it is an empty directory or a key-value pair + get retrieve the value of a key + ls retrieve a directory + set set the value of a key + setdir create a new or existing directory + update update an existing key with a given value + updatedir update an existing directory + watch watch a key for changes + exec-watch watch a key for changes and exec an executable + member member add, remove and list subcommands + help, h Shows a list of commands or help for one command + +GLOBAL OPTIONS: + --debug output cURL commands which can be used to reproduce the request + --no-sync don't synchronize cluster information before sending request + --output, -o 'simple' output response in the given format (`simple` or `json`) + --peers, -C a comma-delimited list of machine addresses in the cluster (default: "127.0.0.1:4001") + --cert-file identify HTTPS client using this SSL certificate file + --key-file identify HTTPS client using this SSL key file + --ca-file verify certificates of HTTPS-enabled servers using this CA bundle + --help, -h show help + --version, -v print the version +``` + +### 数据库操作 +数据库操作围绕对键值和目录的 CRUD (符合 REST 风格的一套操作:Create)完整生命周期的管理。 + +etcd 在键的组织上采用了层次化的空间结构(类似于文件系统中目录的概念),用户指定的键可以为单独的名字,如 `testkey`,此时实际上放在根目录 `/` 下面,也可以为指定目录结构,如 `cluster1/node2/testkey`,则将创建相应的目录结构。 + +*注:CRUD 即 Create, Read, Update, Delete,是符合 REST 风格的一套 API 操作。* + +#### set +指定某个键的值。例如 +``` +$ etcdctl set /testdir/testkey "Hello world" +Hello world +``` +支持的选项包括: +``` +--ttl '0' 该键值的超时时间(单位为秒),不配置(默认为 0)则永不超时 +--swap-with-value value 若该键现在的值是 value,则进行设置操作 +--swap-with-index '0' 若该键现在的索引值是指定索引,则进行设置操作 +``` + +#### get +获取指定键的值。例如 +``` +$ etcdctl set testkey hello +hello +$ etcdctl update testkey world +world +``` + +当键不存在时,则会报错。例如 +``` +$ etcdctl get testkey2 +Error: 100: Key not found (/testkey2) [1] +``` + +支持的选项为 +``` +--sort 对结果进行排序 +--consistent 将请求发给主节点,保证获取内容的一致性 +``` + +#### update +当键存在时,更新值内容。例如 +``` +$ etcdctl set testkey hello +hello +$ etcdctl update testkey world +world +``` + +当键不存在时,则会报错。例如 +``` +$ etcdctl update testkey2 world +Error: 100: Key not found (/testkey2) [1] +``` + +支持的选项为 +``` +--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时 +``` + +#### rm +删除某个键值。例如 +``` +$ etcdctl rm testkey + +``` + +当键不存在时,则会报错。例如 +``` +$ etcdctl rm testkey2 +Error: 100: Key not found (/testkey2) [8] +``` + +支持的选项为 +``` +--dir 如果键是个空目录或者键值对则删除 +--recursive 删除目录和所有子键 +--with-value 检查现有的值是否匹配 +--with-index '0' 检查现有的 index 是否匹配 + +``` + +#### mk +如果给定的键不存在,则创建一个新的键值。例如 +``` +$ etcdctl mk /testdir/testkey "Hello world" +Hello world +``` +当键存在的时候,执行该命令会报错,例如 +``` +$ etcdctl set testkey "Hello world" +Hello world +$ ./etcdctl mk testkey "Hello world" +Error: 105: Key already exists (/testkey) [2] +``` + +支持的选项为 +``` +--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时 +``` + + +#### mkdir +如果给定的键目录不存在,则创建一个新的键目录。例如 +``` +$ etcdctl mkdir testdir +``` +当键目录存在的时候,执行该命令会报错,例如 +``` +$ etcdctl mkdir testdir +$ etcdctl mkdir testdir +Error: 105: Key already exists (/testdir) [7] +``` +支持的选项为 +``` +--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时 +``` + +#### setdir + +创建一个键目录,无论存在与否。 + +支持的选项为 +``` +--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时 +``` + +#### updatedir +更新一个已经存在的目录。 +支持的选项为 +``` +--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时 +``` + +#### rmdir +删除一个空目录,或者键值对。 + +若目录不空,会报错 +``` +$ etcdctl set /dir/testkey hi +hi +$ etcdctl rmdir /dir +Error: 108: Directory not empty (/dir) [13] +``` + +#### ls +列出目录(默认为根目录)下的键或者子目录,默认不显示子目录中内容。 + +例如 +``` +$ ./etcdctl set testkey 'hi' +hi +$ ./etcdctl set dir/test 'hello' +hello +$ ./etcdctl ls +/testkey +/dir +$ ./etcdctl ls dir +/dir/test +``` + +支持的选项包括 +``` +--sort 将输出结果排序 +--recursive 如果目录下有子目录,则递归输出其中的内容 +-p 对于输出为目录,在最后添加 `/` 进行区分 +``` + +### 非数据库操作 + +#### backup +备份 etcd 的数据。 + +支持的选项包括 +``` +--data-dir etcd 的数据目录 +--backup-dir 备份到指定路径 +``` +#### watch +监测一个键值的变化,一旦键值发生更新,就会输出最新的值并退出。 + +例如,用户更新 testkey 键值为 Hello world。 +``` +$ etcdctl watch testkey +Hello world +``` + +支持的选项包括 +``` +--forever 一直监测,直到用户按 `CTRL+C` 退出 +--after-index '0' 在指定 index 之前一直监测 +--recursive 返回所有的键值和子键值 +``` +#### exec-watch +监测一个键值的变化,一旦键值发生更新,就执行给定命令。 + +例如,用户更新 testkey 键值。 +``` +$etcdctl exec-watch testkey -- sh -c 'ls' +default.etcd +Documentation +etcd +etcdctl +etcd-migrate +README-etcdctl.md +README.md +``` + +支持的选项包括 +``` +--after-index '0' 在指定 index 之前一直监测 +--recursive 返回所有的键值和子键值 +``` + +#### member +通过 list、add、remove 命令列出、添加、删除 etcd 实例到 etcd 集群中。 + +例如本地启动一个 etcd 服务实例后,可以用如下命令进行查看。 +``` +$ etcdctl member list +ce2a822cea30bfca: name=default peerURLs=http://localhost:2380,http://localhost:7001 clientURLs=http://localhost:2379,http://localhost:4001 + +``` +### 命令选项 +* `--debug` 输出 cURL 命令,显示执行命令的时候发起的请求 +* `--no-sync` 发出请求之前不同步集群信息 +* `--output, -o 'simple'` 输出内容的格式 (`simple` 为原始信息,`json` 为进行json格式解码,易读性好一些) +* `--peers, -C` 指定集群中的同伴信息,用逗号隔开 (默认为: "127.0.0.1:4001") +* `--cert-file` HTTPS 下客户端使用的 SSL 证书文件 +* `--key-file` HTTPS 下客户端使用的 SSL 密钥文件 +* `--ca-file` 服务端使用 HTTPS 时,使用 CA 文件进行验证 +* `--help, -h` 显示帮助命令信息 +* `--version, -v` 打印版本信息 diff --git a/etcd/install.md b/etcd/install.md new file mode 100644 index 0000000..6e935f3 --- /dev/null +++ b/etcd/install.md @@ -0,0 +1,78 @@ +## 安装 + +etcd 基于 Go 语言实现,因此,用户可以从 [项目主页](https://github.com/coreos/etcd) 下载源代码自行编译,也可以下载编译好的二进制文件,甚至直接使用制作好的 Docker 镜像文件来体验。 + +### 二进制文件方式下载 + +编译好的二进制文件都在 [github.com/coreos/etcd/releases](https://github.com/coreos/etcd/releases/) 页面,用户可以选择需要的版本,或通过下载工具下载。 + +例如,下面的命令使用 curl 工具下载压缩包,并解压。 + +``` +curl -L https://github.com/coreos/etcd/releases/download/v2.0.0-rc.1/etcd-v2.0.0-rc.1-linux-amd64.tar.gz -o etcd-v2.0.0-rc.1-linux-amd64.tar.gz +tar xzvf etcd-v2.0.0-rc.1-linux-amd64.tar.gz +cd etcd-v2.0.0-rc.1-linux-amd64 +``` + +解压后,可以看到文件包括 +``` +$ ls +etcd etcdctl etcd-migrate README-etcdctl.md README.md +``` + +其中 etcd 是服务主文件,etcdctl 是提供给用户的命令客户端,etcd-migrate 负责进行迁移。 + +推荐通过下面的命令将三个文件都放到系统可执行目录 `/usr/local/bin/` 或 `/usr/bin/`。 + +``` +$ sudo cp etcd* /usr/local/bin/ +``` + +运行 etcd,将默认组件一个两个节点的集群。数据库服务端默认监听在 2379 和 4001 端口,etcd 实例监听在 2380 和 7001 端口。显示类似如下的信息: +``` +$ ./etcd +2014/12/31 14:52:09 no data-dir provided, using default data-dir ./default.etcd +2014/12/31 14:52:09 etcd: listening for peers on http://localhost:2380 +2014/12/31 14:52:09 etcd: listening for peers on http://localhost:7001 +2014/12/31 14:52:09 etcd: listening for client requests on http://localhost:2379 +2014/12/31 14:52:09 etcd: listening for client requests on http://localhost:4001 +2014/12/31 14:52:09 etcdserver: name = default +2014/12/31 14:52:09 etcdserver: data dir = default.etcd +2014/12/31 14:52:09 etcdserver: snapshot count = 10000 +2014/12/31 14:52:09 etcdserver: advertise client URLs = http://localhost:2379,http://localhost:4001 +2014/12/31 14:52:09 etcdserver: initial advertise peer URLs = http://localhost:2380,http://localhost:7001 +2014/12/31 14:52:09 etcdserver: initial cluster = default=http://localhost:2380,default=http://localhost:7001 +2014/12/31 14:52:10 etcdserver: start member ce2a822cea30bfca in cluster 7e27652122e8b2ae +2014/12/31 14:52:10 raft: ce2a822cea30bfca became follower at term 0 +2014/12/31 14:52:10 raft: newRaft ce2a822cea30bfca [peers: [], term: 0, commit: 0, lastindex: 0, lastterm: 0] +2014/12/31 14:52:10 raft: ce2a822cea30bfca became follower at term 1 +2014/12/31 14:52:10 etcdserver: added local member ce2a822cea30bfca [http://localhost:2380 http://localhost:7001] to cluster 7e27652122e8b2ae +2014/12/31 14:52:11 raft: ce2a822cea30bfca is starting a new election at term 1 +2014/12/31 14:52:11 raft: ce2a822cea30bfca became candidate at term 2 +2014/12/31 14:52:11 raft: ce2a822cea30bfca received vote from ce2a822cea30bfca at term 2 +2014/12/31 14:52:11 raft: ce2a822cea30bfca became leader at term 2 +2014/12/31 14:52:11 raft.node: ce2a822cea30bfca elected leader ce2a822cea30bfca at term 2 +2014/12/31 14:52:11 etcdserver: published {Name:default ClientURLs:[http://localhost:2379 http://localhost:4001]} to cluster 7e27652122e8b2ae +``` + +此时,可以使用 etcdctl 命令进行测试,设置和获取键值 `testkey: "hello world"`,检查 etcd 服务是否启动成功: +``` +$ ./etcdctl set testkey "hello world" +hello world +$ ./etcdctl get testkey +hello world +``` +说明 etcd 服务已经成功启动了。 + +当然,也可以通过 HTTP 访问本地 2379 或 4001 端口的方式来进行操作,例如查看 `testkey` 的值: +``` +$ curl -L http://localhost:4001/v2/keys/testkey +{"action":"get","node":{"key":"/testkey","value":"hello world","modifiedIndex":3,"createdIndex":3}} +``` + +### Docker 镜像方式下载 + +镜像名称为 quay.io/coreos/etcd:v2.0.0_rc.1,可以通过下面的命令启动 etcd 服务监听到 4001 端口。 +``` +$ sudo docker run -p 4001:4001 -v /etc/ssl/certs/:/etc/ssl/certs/ quay.io/coreos/etcd:v2.0.0_rc.1 +``` diff --git a/etcd/intro.md b/etcd/intro.md new file mode 100644 index 0000000..7531a7b --- /dev/null +++ b/etcd/intro.md @@ -0,0 +1,19 @@ +## 什么是 etcd + +![](../_images/etcd_logo.png) + +etcd 是 CoreOS 团队于 2013 年 6 月发起的开源项目,它的目标是构建一个高可用的分布式键值(key-value)数据库,基于 Go 语言实现。我们知道,在分布式系统中,各种服务的配置信息的管理分享,服务的发现是一个很基本同时也是很重要的问题。CoreOS 项目就希望基于 etcd 来解决这一问题。 + +etcd 目前在 [github.com/coreos/etcd](https://github.com/coreos/etcd) 进行维护,即将发布 2.0.0 版本。 + +受到 [Apache ZooKeeper](http://zookeeper.apache.org/) 项目和 [doozer](https://github.com/ha/doozerd) 项目的启发,etcd 在设计的时候重点考虑了下面四个要素: +* 简单:支持 REST 风格的 HTTP+JSON API +* 安全:支持 HTTPS 方式的访问 +* 快速:支持并发 1k/s 的写操作 +* 可靠:支持分布式结构,基于 Raft 的一致性算法 + +*注:Apache ZooKeeper 是一套知名的分布式系统中进行同步和一致性管理的工具。* +*注:doozer 则是一个一致性分布式数据库。* +*注:Raft 是一套通过选举主节点来实现分布式系统一致性的算法,相比于大名鼎鼎的 Paxos 算法,它的过程更容易被人理解,由 Stanford 大学的 Diego Ongaro 和 John Ousterhout 提出。更多细节可以参考 (raftconsensus.github.io)[http://raftconsensus.github.io]。* + +一般情况下,用户使用 etcd 可以在多个节点上启动多个实例,并添加它们为一个集群。同一个集群中的 etcd 实例将会保持彼此信息的一致性。