Merge pull request #274 from khs1994/master

pull/277/head
Baohua Yang 2017-12-03 23:02:12 +08:00 committed by GitHub
commit ce051cd1a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 807 additions and 360 deletions

View File

@ -102,6 +102,7 @@
* [Etcd 项目](etcd/README.md)
* [简介](etcd/intro.md)
* [安装](etcd/install.md)
* [集群](etcd/cluster.md)
* [使用 etcdctl](etcd/etcdctl.md)
* [CoreOS 项目](coreos/README.md)
* [简介](coreos/intro.md)

View File

@ -1,8 +1,7 @@
# 基本概念
Docker 包括三个基本概念
* 镜像Image
* 容器Container
* 仓库Repository
* 镜像(`Image`
* 容器(`Container`
* 仓库(`Repository`
理解了这三个概念,就理解了 Docker 的整个生命周期。

View File

@ -1,6 +1,6 @@
## Docker 容器
镜像Image和容器Container的关系就像是面向对象程序设计中的`类`和`实例`一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
镜像(`Image`)和容器(`Container`)的关系,就像是面向对象程序设计中的 `类` `实例` 一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
容器的实质是进程,但与直接在宿主执行的进程不同,容器进程运行于属于自己的独立的 [命名空间](https://en.wikipedia.org/wiki/Linux_namespaces)。因此容器可以拥有自己的 `root` 文件系统、自己的网络配置、自己的进程空间,甚至自己的用户 ID 空间。容器内的进程是运行在一个隔离的环境里,使用起来,就好像是在一个独立于宿主的系统下操作一样。这种特性使得容器封装的应用比直接在宿主运行更加安全。也因为这种隔离的特性,很多人初学 Docker 时常常会混淆容器和虚拟机。
@ -8,6 +8,6 @@
容器存储层的生存周期和容器一样,容器消亡时,容器存储层也随之消亡。因此,任何保存于容器存储层的信息都会随容器删除而丢失。
按照 Docker 最佳实践的要求,容器不应该向其存储层内写入任何数据,容器存储层要保持无状态化。所有的文件写入操作,都应该使用 [数据卷Volume](../data_management/volume.md)、或者绑定宿主目录,在这些位置的读写会跳过容器存储层,直接对宿主(或网络存储)发生读写,其性能和稳定性更高。
按照 Docker 最佳实践的要求,容器不应该向其存储层内写入任何数据,容器存储层要保持无状态化。所有的文件写入操作,都应该使用 [数据卷Volume](../data_management/volume.md)、或者绑定宿主目录,在这些位置的读写会跳过容器存储层,直接对宿主(或网络存储)发生读写,其性能和稳定性更高。
数据卷的生存周期独立于容器,容器消亡,数据卷不会消亡。因此,使用数据卷后,容器可以随意删除、重新运行,数据却不会丢失。
数据卷的生存周期独立于容器,容器消亡,数据卷不会消亡。因此,使用数据卷后,容器删除或者重新运行之后,数据却不会丢失。

View File

@ -1,6 +1,6 @@
## Docker 镜像
我们都知道,操作系统分为内核和用户空间。对于 Linux 而言,内核启动后,会挂载 `root` 文件系统为其提供用户空间支持。而 Docker 镜像Image就相当于是一个 `root` 文件系统。比如官方镜像 `ubuntu:17.10` 就包含了完整的一套 Ubuntu 17.10 最小系统的 `root` 文件系统。
我们都知道,操作系统分为内核和用户空间。对于 Linux 而言,内核启动后,会挂载 `root` 文件系统为其提供用户空间支持。而 Docker 镜像Image就相当于是一个 `root` 文件系统。比如官方镜像 `ubuntu:16.04` 就包含了完整的一套 Ubuntu 16.04 最小系统的 `root` 文件系统。
Docker 镜像是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷、环境变量、用户等)。镜像不包含任何动态数据,其内容在构建之后也不会被改变。

View File

@ -1,8 +1,8 @@
## Docker Registry
镜像构建完成后,可以很容易的在当前宿主上运行,但是,如果需要在其它服务器上使用这个镜像,我们就需要一个集中的存储、分发镜像的服务,[Docker Registry](../repository/registry.md) 就是这样的服务。
镜像构建完成后,可以很容易的在当前宿主上运行,但是,如果需要在其它服务器上使用这个镜像,我们就需要一个集中的存储、分发镜像的服务,[Docker Registry](../repository/registry.md) 就是这样的服务。
一个 **Docker Registry** 中可以包含多个**仓库**Repository每个仓库可以包含多个**标签**Tag每个标签对应一个镜像。
一个 **Docker Registry** 中可以包含多个**仓库**`Repository`);每个仓库可以包含多个**标签**`Tag`);每个标签对应一个镜像。
通常,一个仓库会包含同一个软件不同版本的镜像,而标签就常用于对应该软件的各个版本。我们可以通过 `<仓库名>:<标签>` 的格式来指定具体是这个软件哪个版本的镜像。如果不给出标签,将以 `latest` 作为默认标签。
@ -16,7 +16,7 @@ Docker Registry 公开服务是开放给用户使用、允许用户管理镜像
最常使用的 Registry 公开服务是官方的 [Docker Hub](https://hub.docker.com/),这也是默认的 Registry并拥有大量的高质量的官方镜像。除此以外还有 [CoreOS](https://coreos.com/) 的 [Quay.io](https://quay.io/repository/)CoreOS 相关的镜像存储在这里Google 的 [Google Container Registry](https://cloud.google.com/container-registry/)[Kubernetes](http://kubernetes.io/) 的镜像使用的就是这个服务。
由于某些原因,在国内访问这些服务可能会比较慢。国内的一些云服务商提供了针对 Docker Hub 的镜像服务Registry Mirror这些镜像服务被称为**加速器**。常见的有 [阿里云加速器](https://cr.console.aliyun.com/#/accelerator)、[DaoCloud 加速器](https://www.daocloud.io/mirror#accelerator-doc) 等。使用加速器会直接从国内的地址下载 Docker Hub 的镜像,比直接从 Docker Hub 下载速度会提高很多。在 [安装 Dcoekr](../install/mirror.md) 一节中有详细的配置方法。
由于某些原因,在国内访问这些服务可能会比较慢。国内的一些云服务商提供了针对 Docker Hub 的镜像服务(`Registry Mirror`),这些镜像服务被称为**加速器**。常见的有 [阿里云加速器](https://cr.console.aliyun.com/#/accelerator)、[DaoCloud 加速器](https://www.daocloud.io/mirror#accelerator-doc) 等。使用加速器会直接从国内的地址下载 Docker Hub 的镜像,比直接从 Docker Hub 下载速度会提高很多。在 [安装 Dcoekr](../install/mirror.md) 一节中有详细的配置方法。
国内也有一些云服务商提供类似于 Docker Hub 的公开服务。比如 [时速云镜像仓库](https://hub.tenxcloud.com/)、[网易云镜像服务](https://c.163.com/hub#/m/library/)、[DaoCloud 镜像市场](https://hub.daocloud.io/)、[阿里云镜像库](https://cr.console.aliyun.com) 等。
@ -26,4 +26,4 @@ Docker Registry 公开服务是开放给用户使用、允许用户管理镜像
开源的 Docker Registry 镜像只提供了 [Docker Registry API](https://docs.docker.com/registry/spec/api/) 的服务端实现,足以支持 `docker` 命令,不影响使用。但不包含图形界面,以及镜像维护、用户管理、访问控制等高级功能。在官方的商业化版本 [Docker Trusted Registry](https://docs.docker.com/datacenter/dtr/2.0/) 中,提供了这些高级功能。
除了官方的 Docker Registry 外,还有第三方软件实现了 Docker Registry API甚至提供了用户界面以及一些高级功能。比如[VMWare Harbor](http://vmware.github.io/harbor/index_cn.html) 和 [Sonatype Nexus](https://www.sonatype.com/docker)。
除了官方的 Docker Registry 外,还有第三方软件实现了 Docker Registry API甚至提供了用户界面以及一些高级功能。比如[VMWare Harbor](https://github.com/vmware/harbor) 和 [Sonatype Nexus](https://www.sonatype.com/docker)。

View File

@ -272,24 +272,7 @@ labels:
### `links`
链接到其它服务中的容器。使用服务名称(同时作为别名)或服务名称:服务别名 `SERVICE:ALIAS` 格式都可以。
```bash
links:
- db
- db:database
- redis
```
使用的别名将会自动在服务容器中的 `/etc/hosts` 里创建。例如:
```bash
172.17.2.186 db
172.17.2.186 database
172.17.2.187 redis
```
被链接容器中相应的环境变量也将被创建。
不推荐使用该指令。
### `logging`

View File

@ -4,28 +4,16 @@
## 服务发现
`CoreOS` 的第一个重要组件就是使用 `etcd` 来实现的服务发现。
`CoreOS` 的第一个重要组件就是使用 `etcd` 来实现的服务发现。`CoreOS``etcd` 默认以 `rkt` 容器方式运行。
如果你使用默认的样例 `cloud-config` 文件,那么 `etcd` 会在启动时自动运行。
```bash
$ rkt list
```yml
#cloud-config
hostname: coreos0
ssh_authorized_keys:
- ssh-rsa AAAA...
coreos:
units:
- name: etcd.service
command: start
- name: fleet.service
command: start
etcd:
name: coreos0
discovery: https://discovery.etcd.io/<token>
UUID APP IMAGE NAME STATE CREATED STARTED NETWORKS
57581644 etcd quay.io/coreos/etcd:v3.2.10 running 1 minute ago 1 minute ago
```
配置文件里有一个 `token`,你可以通过访问 https://discovery.etcd.io/new 来获取一个包含你 `teoken` 的 URL。
`etcd` 使用方法请查看 [etcd 章节](../etcd/)。
## 容器管理

View File

@ -17,7 +17,7 @@ $ docker run -d -P \
python app.py
```
上面的命令加载主机的 `/src/webapp` 目录到容器的 `/opt/webapp`目录。这个功能在进行测试的时候十分方便,比如用户可以放置一些程序到本地目录中,来查看容器是否正常工作。本地目录的路径必须是绝对路径,如果目录不存在 Docker 会自动为你创建它
上面的命令加载主机的 `/src/webapp` 目录到容器的 `/opt/webapp`目录。这个功能在进行测试的时候十分方便,比如用户可以放置一些程序到本地目录中,来查看容器是否正常工作。本地目录的路径必须是绝对路径,以前使用 `-v` 参数时如果本地目录不存在 Docker 会自动为你创建一个文件夹,现在使用 `--mount` 参数时如果本地目录不存在Docker 会报错
Docker 挂载主机目录的默认权限是 `读写`,用户也可以通过增加 `readonly` 指定为 `只读`

View File

@ -1,3 +1,3 @@
# etcd
etcd 是 CoreOS 团队发起的一个管理配置信息和服务发现(service discovery的项目在这一章里面我们将介绍该项目的目标,安装和使用,以及实现的技术。
`etcd``CoreOS` 团队发起的一个管理配置信息和服务发现(`Service Discovery`)的项目,在这一章里面,我们将基于 `etcd 3.x` 版本介绍该项目的目标,安装和使用,以及实现的技术。

129
etcd/cluster.md Normal file
View File

@ -0,0 +1,129 @@
## etcd 集群
下面我们使用 [Docker Compose](../compose/) 模拟启动一个 3 节点的 `etcd` 集群。
编辑 `docker-compose.yml` 文件
```yaml
version: "3"
services:
node1:
image: quay.io/coreos/etcd
volumes:
- node1-data:/etcd-data
expose:
- 2379
- 2380
networks:
cluster_net:
ipv4_address: 172.16.238.100
environment:
- ETCDCTL_API=3
command:
- /usr/local/bin/etcd
- --data-dir=/etcd-data
- --name
- node1
- --initial-advertise-peer-urls
- http://172.16.238.100:2380
- --listen-peer-urls
- http://0.0.0.0:2380
- --advertise-client-urls
- http://172.16.238.100:2379
- --listen-client-urls
- http://0.0.0.0:2379
- --initial-cluster
- node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
- --initial-cluster-state
- new
- --initial-cluster-token
- docker-etcd
node2:
image: quay.io/coreos/etcd
volumes:
- node2-data:/etcd-data
networks:
cluster_net:
ipv4_address: 172.16.238.101
environment:
- ETCDCTL_API=3
expose:
- 2379
- 2380
command:
- /usr/local/bin/etcd
- --data-dir=/etcd-data
- --name
- node2
- --initial-advertise-peer-urls
- http://172.16.238.101:2380
- --listen-peer-urls
- http://0.0.0.0:2380
- --advertise-client-urls
- http://172.16.238.101:2379
- --listen-client-urls
- http://0.0.0.0:2379
- --initial-cluster
- node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
- --initial-cluster-state
- new
- --initial-cluster-token
- docker-etcd
node3:
image: quay.io/coreos/etcd
volumes:
- node3-data:/etcd-data
networks:
cluster_net:
ipv4_address: 172.16.238.102
environment:
- ETCDCTL_API=3
expose:
- 2379
- 2380
command:
- /usr/local/bin/etcd
- --data-dir=/etcd-data
- --name
- node3
- --initial-advertise-peer-urls
- http://172.16.238.102:2380
- --listen-peer-urls
- http://0.0.0.0:2380
- --advertise-client-urls
- http://172.16.238.102:2379
- --listen-client-urls
- http://0.0.0.0:2379
- --initial-cluster
- node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
- --initial-cluster-state
- new
- --initial-cluster-token
- docker-etcd
volumes:
node1-data:
node2-data:
node3-data:
networks:
cluster_net:
driver: bridge
ipam:
driver: default
config:
-
subnet: 172.16.238.0/24
```
使用 `docker-compose up` 启动集群之后使用 `docker exec` 命令登录到任一节点测试 `etcd` 集群。
```bash
/ # etcdctl member list
daf3fd52e3583ff, started, node3, http://172.16.238.102:2380, http://172.16.238.102:2379
422a74f03b622fef, started, node1, http://172.16.238.100:2380, http://172.16.238.100:2379
ed635d2a2dbef43d, started, node2, http://172.16.238.101:2380, http://172.16.238.101:2379
```

View File

@ -0,0 +1,112 @@
version: "3"
services:
node1:
image: quay.io/coreos/etcd
volumes:
- node1-data:/etcd-data
expose:
- 2379
- 2380
networks:
cluster_net:
ipv4_address: 172.16.238.100
environment:
- ETCDCTL_API=3
command:
- /usr/local/bin/etcd
- --data-dir=/etcd-data
- --name
- node1
- --initial-advertise-peer-urls
- http://172.16.238.100:2380
- --listen-peer-urls
- http://0.0.0.0:2380
- --advertise-client-urls
- http://172.16.238.100:2379
- --listen-client-urls
- http://0.0.0.0:2379
- --initial-cluster
- node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
- --initial-cluster-state
- new
- --initial-cluster-token
- docker-etcd
node2:
image: quay.io/coreos/etcd
volumes:
- node2-data:/etcd-data
networks:
cluster_net:
ipv4_address: 172.16.238.101
environment:
- ETCDCTL_API=3
expose:
- 2379
- 2380
command:
- /usr/local/bin/etcd
- --data-dir=/etcd-data
- --name
- node2
- --initial-advertise-peer-urls
- http://172.16.238.101:2380
- --listen-peer-urls
- http://0.0.0.0:2380
- --advertise-client-urls
- http://172.16.238.101:2379
- --listen-client-urls
- http://0.0.0.0:2379
- --initial-cluster
- node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
- --initial-cluster-state
- new
- --initial-cluster-token
- docker-etcd
node3:
image: quay.io/coreos/etcd
volumes:
- node3-data:/etcd-data
networks:
cluster_net:
ipv4_address: 172.16.238.102
environment:
- ETCDCTL_API=3
expose:
- 2379
- 2380
command:
- /usr/local/bin/etcd
- --data-dir=/etcd-data
- --name
- node3
- --initial-advertise-peer-urls
- http://172.16.238.102:2380
- --listen-peer-urls
- http://0.0.0.0:2380
- --advertise-client-urls
- http://172.16.238.102:2379
- --listen-client-urls
- http://0.0.0.0:2379
- --initial-cluster
- node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
- --initial-cluster-state
- new
- --initial-cluster-token
- docker-etcd
volumes:
node1-data:
node2-data:
node3-data:
networks:
cluster_net:
driver: bridge
ipam:
driver: default
config:
-
subnet: 172.16.238.0/24

281
etcd/etcdctl-v2.md Normal file
View File

@ -0,0 +1,281 @@
## 使用 etcdctl v2
`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
指定某个键的值。例如
```bash
$ etcdctl set /testdir/testkey "Hello world"
Hello world
```
支持的选项包括:
```bash
--ttl '0' 该键值的超时时间(单位为秒),不配置(默认为 0则永不超时
--swap-with-value value 若该键现在的值是 value则进行设置操作
--swap-with-index '0' 若该键现在的索引值是指定索引,则进行设置操作
```
#### get
获取指定键的值。例如
```bash
$ etcdctl set testkey hello
hello
$ etcdctl update testkey world
world
```
当键不存在时,则会报错。例如
```bash
$ etcdctl get testkey2
Error: 100: Key not found (/testkey2) [1]
```
支持的选项为
```bash
--sort 对结果进行排序
--consistent 将请求发给主节点,保证获取内容的一致性
```
#### update
当键存在时,更新值内容。例如
```bash
$ etcdctl set testkey hello
hello
$ etcdctl update testkey world
world
```
当键不存在时,则会报错。例如
```bash
$ etcdctl update testkey2 world
Error: 100: Key not found (/testkey2) [1]
```
支持的选项为
```bash
--ttl '0' 超时时间(单位为秒),不配置(默认为 0则永不超时
```
#### rm
删除某个键值。例如
```bash
$ etcdctl rm testkey
```
当键不存在时,则会报错。例如
```bash
$ etcdctl rm testkey2
Error: 100: Key not found (/testkey2) [8]
```
支持的选项为
```bash
--dir 如果键是个空目录或者键值对则删除
--recursive 删除目录和所有子键
--with-value 检查现有的值是否匹配
--with-index '0' 检查现有的 index 是否匹配
```
#### mk
如果给定的键不存在,则创建一个新的键值。例如
```bash
$ etcdctl mk /testdir/testkey "Hello world"
Hello world
```
当键存在的时候,执行该命令会报错,例如
```bash
$ etcdctl set testkey "Hello world"
Hello world
$ ./etcdctl mk testkey "Hello world"
Error: 105: Key already exists (/testkey) [2]
```
支持的选项为
```bash
--ttl '0' 超时时间(单位为秒),不配置(默认为 0则永不超时
```
#### mkdir
如果给定的键目录不存在,则创建一个新的键目录。例如
```bash
$ etcdctl mkdir testdir
```
当键目录存在的时候,执行该命令会报错,例如
```bash
$ etcdctl mkdir testdir
$ etcdctl mkdir testdir
Error: 105: Key already exists (/testdir) [7]
```
支持的选项为
```bash
--ttl '0' 超时时间(单位为秒),不配置(默认为 0则永不超时
```
#### setdir
创建一个键目录,无论存在与否。
支持的选项为
```bash
--ttl '0' 超时时间(单位为秒),不配置(默认为 0则永不超时
```
#### updatedir
更新一个已经存在的目录。
支持的选项为
```bash
--ttl '0' 超时时间(单位为秒),不配置(默认为 0则永不超时
```
#### rmdir
删除一个空目录,或者键值对。
若目录不空,会报错
```bash
$ etcdctl set /dir/testkey hi
hi
$ etcdctl rmdir /dir
Error: 108: Directory not empty (/dir) [13]
```
#### ls
列出目录(默认为根目录)下的键或者子目录,默认不显示子目录中内容。
例如
```bash
$ ./etcdctl set testkey 'hi'
hi
$ ./etcdctl set dir/test 'hello'
hello
$ ./etcdctl ls
/testkey
/dir
$ ./etcdctl ls dir
/dir/test
```
支持的选项包括
```bash
--sort 将输出结果排序
--recursive 如果目录下有子目录,则递归输出其中的内容
-p 对于输出为目录,在最后添加 `/` 进行区分
```
### 非数据库操作
#### backup
备份 etcd 的数据。
支持的选项包括
```bash
--data-dir etcd 的数据目录
--backup-dir 备份到指定路径
```
#### watch
监测一个键值的变化,一旦键值发生更新,就会输出最新的值并退出。
例如,用户更新 testkey 键值为 Hello world。
```bash
$ etcdctl watch testkey
Hello world
```
支持的选项包括
```bash
--forever 一直监测,直到用户按 `CTRL+C` 退出
--after-index '0' 在指定 index 之前一直监测
--recursive 返回所有的键值和子键值
```
#### exec-watch
监测一个键值的变化,一旦键值发生更新,就执行给定命令。
例如,用户更新 testkey 键值。
```bash
$ etcdctl exec-watch testkey -- sh -c 'ls'
default.etcd
Documentation
etcd
etcdctl
etcd-migrate
README-etcdctl.md
README.md
```
支持的选项包括
```bash
--after-index '0' 在指定 index 之前一直监测
--recursive 返回所有的键值和子键值
```
#### member
通过 list、add、remove 命令列出、添加、删除 etcd 实例到 etcd 集群中。
例如本地启动一个 etcd 服务实例后,可以用如下命令进行查看。
```bash
$ 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` 打印版本信息

View File

@ -1,279 +1,150 @@
## 使用 etcdctl
etcdctl 是一个命令行客户端,它能提供一些简洁的命令,供用户直接跟 etcd 服务打交道,而无需基于 HTTP API 方式。这在某些情况下将很方便,例如用户对服务进行测试或者手动修改数据库内容。我们也推荐在刚接触 etcd 时通过 etcdctl 命令来熟悉相关的操作,这些操作跟 HTTP API 实际上是对应的。
`etcdctl` 是一个命令行客户端,它能提供一些简洁的命令,供用户直接跟 `etcd` 服务打交道,而无需基于 `HTTP API` 方式。这在某些情况下将很方便,例如用户对服务进行测试或者手动修改数据库内容。我们也推荐在刚接触 `etcd` 时通过 `etcdctl` 命令来熟悉相关的操作,这些操作跟 `HTTP API` 实际上是对应的。
etcd 项目二进制发行包中已经包含了 etcdctl 工具,没有的话,可以从 [github.com/coreos/etcd/releases](https://github.com/coreos/etcd/releases) 下载。
`etcd` 项目二进制发行包中已经包含了 `etcdctl` 工具,没有的话,可以从 [github.com/coreos/etcd/releases](https://github.com/coreos/etcd/releases) 下载。
etcdctl 支持如下的命令,大体上分为数据库操作和非数据库操作两类,后面将分别进行解释。
`etcdctl` 支持如下的命令,大体上分为数据库操作和非数据库操作两类,后面将分别进行解释。
```bash
$ etcdctl -h
```
NAME:
etcdctl - A simple command line client for etcd.
etcdctl - A simple command line client for etcd3.
USAGE:
etcdctl [global options] command [command options] [arguments...]
etcdctl
VERSION:
2.0.0-rc.1
3.2.10
API VERSION:
3.2
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
get Gets the key or a range of keys
put Puts the given key into the store
del Removes the specified key or range of keys [key, range_end)
txn Txn processes all the requests in one transaction
compaction Compacts the event history in etcd
alarm disarm Disarms all alarms
alarm list Lists all alarms
defrag Defragments the storage of the etcd members with given endpoints
endpoint health Checks the healthiness of endpoints specified in `--endpoints` flag
endpoint status Prints out the status of endpoints specified in `--endpoints` flag
watch Watches events stream on keys or prefixes
version Prints the version of etcdctl
lease grant Creates leases
lease revoke Revokes leases
lease timetolive Get lease information
lease keep-alive Keeps leases alive (renew)
member add Adds a member into the cluster
member remove Removes a member from the cluster
member update Updates a member in the cluster
member list Lists all members in the cluster
snapshot save Stores an etcd node backend snapshot to a given file
snapshot restore Restores an etcd member snapshot to an etcd directory
snapshot status Gets backend snapshot status of a given file
make-mirror Makes a mirror at the destination etcd cluster
migrate Migrates keys in a v2 store to a mvcc store
lock Acquires a named lock
elect Observes and participates in leader election
auth enable Enables authentication
auth disable Disables authentication
user add Adds a new user
user delete Deletes a user
user get Gets detailed information of a user
user list Lists all users
user passwd Changes password of user
user grant-role Grants a role to a user
user revoke-role Revokes a role from a user
role add Adds a new role
role delete Deletes a role
role get Gets detailed information of a role
role list Lists all roles
role grant-permission Grants a key to a role
role revoke-permission Revokes a key from a role
check perf Check the performance of the etcd cluster
help Help about any 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
OPTIONS:
--cacert="" verify certificates of TLS-enabled secure servers using this CA bundle
--cert="" identify secure client using this TLS certificate file
--command-timeout=5s timeout for short running command (excluding dial timeout)
--debug[=false] enable client-side debug logging
--dial-timeout=2s dial timeout for client connections
--endpoints=[127.0.0.1:2379] gRPC endpoints
--hex[=false] print byte strings as hex encoded strings
--insecure-skip-tls-verify[=false] skip server certificate verification
--insecure-transport[=true] disable transport security for client connections
--key="" identify secure client using this TLS key file
--user="" username[:password] for authentication (prompt if password is not supplied)
-w, --write-out="simple" set the output format (fields, json, protobuf, simple, table)
```
### 数据库操作
数据库操作围绕对键值和目录的 CRUD (符合 REST 风格的一套操作Create完整生命周期的管理。
etcd 在键的组织上采用了层次化的空间结构(类似于文件系统中目录的概念),用户指定的键可以为单独的名字,如 `testkey`,此时实际上放在根目录 `/` 下面,也可以为指定目录结构,如 `cluster1/node2/testkey`,则将创建相应的目录结构。
*注CRUD 即 Create, Read, Update, Delete是符合 REST 风格的一套 API 操作。*
>注CRUD 即 Create, Read, Update, Delete是符合 REST 风格的一套 API 操作。
#### put
#### set
指定某个键的值。例如
```bash
$ etcdctl set /testdir/testkey "Hello world"
Hello world
```
支持的选项包括:
```bash
--ttl '0' 该键值的超时时间(单位为秒),不配置(默认为 0则永不超时
--swap-with-value value 若该键现在的值是 value则进行设置操作
--swap-with-index '0' 若该键现在的索引值是指定索引,则进行设置操作
$ etcdctl put /testdir/testkey "Hello world"
OK
```
#### get
获取指定键的值。例如
```bash
$ etcdctl set testkey hello
hello
$ etcdctl update testkey world
world
```
当键不存在时,则会报错。例如
```bash
$ etcdctl get testkey2
Error: 100: Key not found (/testkey2) [1]
$ etcdctl put testkey hello
OK
$ etcdctl get testkey
testkey
hello
```
支持的选项为
```bash
--sort 对结果进行排序
--consistent 将请求发给主节点,保证获取内容的一致性
```
#### update
当键存在时,更新值内容。例如
```bash
$ etcdctl set testkey hello
hello
$ etcdctl update testkey world
world
```
`--sort` 对结果进行排序
当键不存在时,则会报错。例如
```bash
$ etcdctl update testkey2 world
Error: 100: Key not found (/testkey2) [1]
```
`--consistent` 将请求发给主节点,保证获取内容的一致性
支持的选项为
```bash
--ttl '0' 超时时间(单位为秒),不配置(默认为 0则永不超时
```
#### del
#### rm
删除某个键值。例如
```bash
$ etcdctl rm testkey
```
当键不存在时,则会报错。例如
```bash
$ etcdctl rm testkey2
Error: 100: Key not found (/testkey2) [8]
```
支持的选项为
```bash
--dir 如果键是个空目录或者键值对则删除
--recursive 删除目录和所有子键
--with-value 检查现有的值是否匹配
--with-index '0' 检查现有的 index 是否匹配
```
#### mk
如果给定的键不存在,则创建一个新的键值。例如
```bash
$ etcdctl mk /testdir/testkey "Hello world"
Hello world
```
当键存在的时候,执行该命令会报错,例如
```bash
$ etcdctl set testkey "Hello world"
Hello world
$ ./etcdctl mk testkey "Hello world"
Error: 105: Key already exists (/testkey) [2]
```
支持的选项为
```bash
--ttl '0' 超时时间(单位为秒),不配置(默认为 0则永不超时
```
#### mkdir
如果给定的键目录不存在,则创建一个新的键目录。例如
```bash
$ etcdctl mkdir testdir
```
当键目录存在的时候,执行该命令会报错,例如
```bash
$ etcdctl mkdir testdir
$ etcdctl mkdir testdir
Error: 105: Key already exists (/testdir) [7]
```
支持的选项为
```bash
--ttl '0' 超时时间(单位为秒),不配置(默认为 0则永不超时
```
#### setdir
创建一个键目录,无论存在与否。
支持的选项为
```bash
--ttl '0' 超时时间(单位为秒),不配置(默认为 0则永不超时
```
#### updatedir
更新一个已经存在的目录。
支持的选项为
```bash
--ttl '0' 超时时间(单位为秒),不配置(默认为 0则永不超时
```
#### rmdir
删除一个空目录,或者键值对。
若目录不空,会报错
```bash
$ etcdctl set /dir/testkey hi
hi
$ etcdctl rmdir /dir
Error: 108: Directory not empty (/dir) [13]
```
#### ls
列出目录(默认为根目录)下的键或者子目录,默认不显示子目录中内容。
例如
```bash
$ ./etcdctl set testkey 'hi'
hi
$ ./etcdctl set dir/test 'hello'
hello
$ ./etcdctl ls
/testkey
/dir
$ ./etcdctl ls dir
/dir/test
```
支持的选项包括
```bash
--sort 将输出结果排序
--recursive 如果目录下有子目录,则递归输出其中的内容
-p 对于输出为目录,在最后添加 `/` 进行区分
$ etcdctl del testkey
1
```
### 非数据库操作
#### backup
备份 etcd 的数据。
支持的选项包括
```bash
--data-dir etcd 的数据目录
--backup-dir 备份到指定路径
```
#### watch
监测一个键值的变化,一旦键值发生更新,就会输出最新的值并退出。
例如,用户更新 testkey 键值为 Hello world。
监测一个键值的变化,一旦键值发生更新,就会输出最新的值。
例如,用户更新 `testkey` 键值为 `Hello world`
```bash
$ etcdctl watch testkey
Hello world
```
支持的选项包括
```bash
--forever 一直监测,直到用户按 `CTRL+C` 退出
--after-index '0' 在指定 index 之前一直监测
--recursive 返回所有的键值和子键值
```
#### exec-watch
监测一个键值的变化,一旦键值发生更新,就执行给定命令。
例如,用户更新 testkey 键值。
```bash
$etcdctl exec-watch testkey -- sh -c 'ls'
default.etcd
Documentation
etcd
etcdctl
etcd-migrate
README-etcdctl.md
README.md
```
支持的选项包括
```bash
--after-index '0' 在指定 index 之前一直监测
--recursive 返回所有的键值和子键值
PUT
testkey
2
```
#### member
通过 list、add、remove 命令列出、添加、删除 etcd 实例到 etcd 集群中。
例如本地启动一个 etcd 服务实例后,可以用如下命令进行查看。
通过 `list`、`add`、`update`、`remove` 命令列出、添加、更新、删除 etcd 实例到 etcd 集群中。
例如本地启动一个 `etcd` 服务实例后,可以用如下命令进行查看。
```bash
$ etcdctl member list
ce2a822cea30bfca: name=default peerURLs=http://localhost:2380,http://localhost:7001 clientURLs=http://localhost:2379,http://localhost:4001
422a74f03b622fef, started, node1, http://172.16.238.100:2380, http://172.16.238.100:23
```
### 命令选项
* `--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` 打印版本信息

View File

@ -1,78 +1,97 @@
## 安装
etcd 基于 Go 语言实现,因此,用户可以从 [项目主页](https://github.com/coreos/etcd) 下载源代码自行编译,也可以下载编译好的二进制文件,甚至直接使用制作好的 Docker 镜像文件来体验。
`etcd` 基于 `Go` 语言实现,因此,用户可以从 [项目主页](https://github.com/coreos/etcd) 下载源代码自行编译,也可以下载编译好的二进制文件,甚至直接使用制作好的 `Docker` 镜像文件来体验。
>注意:本章节内容基于 etcd `3.x` 版本
### 二进制文件方式下载
编译好的二进制文件都在 [github.com/coreos/etcd/releases](https://github.com/coreos/etcd/releases/) 页面,用户可以选择需要的版本,或通过下载工具下载。
例如,下面的命令使用 curl 工具下载压缩包,并解压。
例如,使用 `curl` 工具下载压缩包,并解压。
```bash
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
$ curl -L https://github.com/coreos/etcd/releases/download/v3.2.10/etcd-v3.2.10-linux-amd64.tar.gz -o etcd-v3.2.10-linux-amd64.tar.gz
$ tar xzvf etcd-v3.2.10-linux-amd64.tar.gz
$ cd etcd-v3.2.10-linux-amd64
```
解压后,可以看到文件包括
```bash
$ ls
etcd etcdctl etcd-migrate README-etcdctl.md README.md
Documentation README-etcdctl.md README.md READMEv2-etcdctl.md etcd etcdctl
```
其中 etcd 是服务主文件etcdctl 是提供给用户的命令客户端etcd-migrate 负责进行迁移
其中 `etcd` 是服务主文件,`etcdctl` 是提供给用户的命令客户端,其他文件是支持文档
推荐通过下面的命令将三个文件都放到系统可执行目录 `/usr/local/bin/``/usr/bin/`
下面将 `etcd` `etcdctl` 文件放到系统可执行目录(例如 `/usr/local/bin/`
```bash
$ sudo cp etcd* /usr/local/bin/
```
运行 etcd将默认组建一个两个节点的集群。数据库服务端默认监听在 2379 和 4001 端口etcd 实例监听在 2380 和 7001 端口。显示类似如下的信息:
默认 `2379` 端口处理客户端的请求,`2380` 端口用于集群各成员间的通信。启动 `etcd` 显示类似如下的信息:
```bash
$ ./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
$ etcd
2017-12-03 11:18:34.406082 I | etcdmain: etcd Version: 3.2.10
2017-12-03 11:18:34.406226 I | etcdmain: Git SHA: GitNotFound
2017-12-03 11:18:34.406235 I | etcdmain: Go Version: go1.9.2
2017-12-03 11:18:34.406242 I | etcdmain: Go OS/Arch: darwin/amd64
2017-12-03 11:18:34.406250 I | etcdmain: setting maximum number of CPUs to 4, total number of available CPUs is 4
2017-12-03 11:18:34.406265 N | etcdmain: failed to detect default host (default host not supported on darwin_amd64)
2017-12-03 11:18:34.406279 W | etcdmain: no data-dir provided, using default data-dir ./default.etcd
2017-12-03 11:18:34.406457 N | etcdmain: the server is already initialized as member before, starting as etcd member...
2017-12-03 11:18:34.411579 I | embed: listening for peers on http://localhost:2380
2017-12-03 11:18:34.411938 I | embed: listening for client requests on localhost:2379
```
此时,可以使用 etcdctl 命令进行测试,设置和获取键值 `testkey: "hello world"`,检查 etcd 服务是否启动成功:
此时,可以使用 `etcdctl` 命令进行测试,设置和获取键值 `testkey: "hello world"`,检查 `etcd` 服务是否启动成功:
```bash
$ ./etcdctl set testkey "hello world"
hello world
$ ./etcdctl get testkey
$ etcdctl member list
8e9e05c52164694d, started, default, http://localhost:2380, http://localhost:2379
$ etcdctl put testkey "hello world"
OK
$ etcdctl get testkey
testkey
hello world
```
说明 etcd 服务已经成功启动了。
当然,也可以通过 HTTP 访问本地 2379 或 4001 端口的方式来进行操作,例如查看 `testkey` 的值:
### Docker 镜像方式运行
镜像名称为 `quay.io/coreos/etcd`,可以通过下面的命令启动 `etcd` 服务监听到 `2379``2380` 端口。
```bash
$ curl -L http://localhost:4001/v2/keys/testkey
{"action":"get","node":{"key":"/testkey","value":"hello world","modifiedIndex":3,"createdIndex":3}}
$ export NODE1=192.168.1.21
$ docker run --name etcd \
-p 2379:2379 \
-p 2380:2380 \
--volume=etcd-data:/etcd-data \
quay.io/coreos/etcd:latest \
/usr/local/bin/etcd \
--data-dir=/etcd-data --name node1 \
--initial-advertise-peer-urls http://${NODE1}:2380 --listen-peer-urls http://0.0.0.0:2380 \
--advertise-client-urls http://${NODE1}:2379 --listen-client-urls http://0.0.0.0:2379 \
--initial-cluster node1=http://${NODE1}:2380
```
### Docker 镜像方式下载
>注意etcd 官方标注 `quay.io/coreos/etcd` 即将废弃,启用新的 `gcr.io/etcd-development/etcd` 镜像,但后者由于网络原因,国内不能下载到该镜像,这里仍然使用前者作为演示。
打开新的终端按照上一步的方法测试 `etcd` 是否成功启动。
### macOS 中运行
镜像名称为 quay.io/coreos/etcd:v2.0.0_rc.1,可以通过下面的命令启动 etcd 服务监听到 4001 端口。
```bash
$ docker run -p 4001:4001 -v /etc/ssl/certs/:/etc/ssl/certs/ quay.io/coreos/etcd:v2.0.0_rc.1
$ brew install etcd
$ etcd
$ etcdctl member list
```

View File

@ -2,18 +2,24 @@
![](_images/etcd_logo.png)
etcd 是 CoreOS 团队于 2013 年 6 月发起的开源项目它的目标是构建一个高可用的分布式键值key-value数据库基于 Go 语言实现。我们知道在分布式系统中各种服务的配置信息的管理分享服务的发现是一个很基本同时也是很重要的问题。CoreOS 项目就希望基于 etcd 来解决这一问题。
`etcd``CoreOS` 团队于 2013 年 6 月发起的开源项目,它的目标是构建一个高可用的分布式键值(`key-value`)数据库,基于 `Go` 语言实现。我们知道,在分布式系统中,各种服务的配置信息的管理分享,服务的发现是一个很基本同时也是很重要的问题。`CoreOS` 项目就希望基于 `etcd` 来解决这一问题。
etcd 目前在 [github.com/coreos/etcd](https://github.com/coreos/etcd) 进行维护。
`etcd` 目前在 [github.com/coreos/etcd](https://github.com/coreos/etcd) 进行维护。
受到 [Apache ZooKeeper](http://zookeeper.apache.org/) 项目和 [doozer](https://github.com/ha/doozerd) 项目的启发etcd 在设计的时候重点考虑了下面四个要素:
* 简单:支持 REST 风格的 HTTP+JSON API
* 安全:支持 HTTPS 方式的访问
* 快速:支持并发 1k/s 的写操作
* 可靠:支持分布式结构,基于 Raft 的一致性算法
受到 [Apache ZooKeeper](http://zookeeper.apache.org/) 项目和 [doozer](https://github.com/ha/doozerd) 项目的启发,`etcd` 在设计的时候重点考虑了下面四个要素:
*注Apache ZooKeeper 是一套知名的分布式系统中进行同步和一致性管理的工具。*
*注doozer 则是一个一致性分布式数据库。*
*注Raft 是一套通过选举主节点来实现分布式系统一致性的算法,相比于大名鼎鼎的 Paxos 算法,它的过程更容易被人理解,由 Stanford 大学的 Diego Ongaro 和 John Ousterhout 提出。更多细节可以参考 [raftconsensus.github.io](http://raftconsensus.github.io)。*
* 简单:具有定义良好、面向用户的 `API` ([gRPC](https://github.com/grpc/grpc))
一般情况下,用户使用 etcd 可以在多个节点上启动多个实例,并添加它们为一个集群。同一个集群中的 etcd 实例将会保持彼此信息的一致性。
* 安全:支持 `HTTPS` 方式的访问
* 快速:支持并发 `10 k/s` 的写操作
* 可靠:支持分布式结构,基于 `Raft` 的一致性算法
*Apache ZooKeeper 是一套知名的分布式系统中进行同步和一致性管理的工具。*
*doozer 是一个一致性分布式数据库。*
*[Raft](https://raft.github.io/) 是一套通过选举主节点来实现分布式系统一致性的算法,相比于大名鼎鼎的 Paxos 算法,它的过程更容易被人理解,由 Stanford 大学的 Diego Ongaro 和 John Ousterhout 提出。更多细节可以参考 [raftconsensus.github.io](http://raftconsensus.github.io)。*
一般情况下,用户使用 `etcd` 可以在多个节点上启动多个实例,并添加它们为一个集群。同一个集群中的 `etcd` 实例将会保持彼此信息的一致性。

View File

@ -2,11 +2,14 @@
在之前的介绍中,我们知道镜像是 Docker 的三大组件之一。
Docker 运行容器前需要本地存在对应的镜像,如果镜像不存在本地Docker 会从镜像仓库下载(默认是 Docker Hub
Docker 运行容器前需要本地存在对应的镜像,如果本地不存在该镜像Docker 会从镜像仓库下载该镜像
本章将介绍更多关于镜像的内容,包括:
* 从仓库获取镜像;
* 管理本地主机上的镜像;
* 介绍镜像实现的基本原理。
Docker 在 1.13 版本引进了新的管理命令management commands在 Docker 1.13+ 推荐使用 `docker image` 子命令来管理 Docker 镜像。

View File

@ -120,9 +120,9 @@ $ ./build.sh
```bash
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
go/helloworld 2 f7cf3465432c 22 seconds ago 6.47MB
go/helloworld 1 f55d3e16affc 2 minutes ago 295MB
REPOSITORY TAG IMAGE ID CREATED SIZE
go/helloworld 2 f7cf3465432c 22 seconds ago 6.47MB
go/helloworld 1 f55d3e16affc 2 minutes ago 295MB
```
### 使用多阶段构建
@ -168,10 +168,10 @@ $ docker build -t go/helloworld:3 .
```bash
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
go/helloworld 3 d6911ed9c846 7 seconds ago 6.47MB
go/helloworld 2 f7cf3465432c 22 seconds ago 6.47MB
go/helloworld 1 f55d3e16affc 2 minutes ago 295MB
REPOSITORY TAG IMAGE ID CREATED SIZE
go/helloworld 3 d6911ed9c846 7 seconds ago 6.47MB
go/helloworld 2 f7cf3465432c 22 seconds ago 6.47MB
go/helloworld 1 f55d3e16affc 2 minutes ago 295MB
```
很明显使用多阶段构建的镜像体积小,同时也完美解决了上边提到的问题。

View File

@ -16,18 +16,18 @@ docker pull [选项] [Docker Registry地址]<仓库名>:<标签>
比如:
```bash
$ docker pull ubuntu:14.04
14.04: Pulling from library/ubuntu
$ docker pull ubuntu:16.04
16.04: Pulling from library/ubuntu
bf5d46315322: Pull complete
9f13e0ac480c: Pull complete
e8988b5b3097: Pull complete
40af181810e7: Pull complete
e6f7c7e5c03e: Pull complete
Digest: sha256:147913621d9cdea08853f6ba9116c2e27a3ceffecf3b492983ae97c3d643fbbe
Status: Downloaded newer image for ubuntu:14.04
Status: Downloaded newer image for ubuntu:16.04
```
上面的命令中没有给出 Docker 镜像仓库地址,因此将会从 Docker Hub 获取镜像。而镜像名称是 `ubuntu:14.04`,因此将会获取官方镜像 `library/ubuntu` 仓库中标签为 `14.04` 的镜像。
上面的命令中没有给出 Docker 镜像仓库地址,因此将会从 Docker Hub 获取镜像。而镜像名称是 `ubuntu:16.04`,因此将会获取官方镜像 `library/ubuntu` 仓库中标签为 `16.04` 的镜像。
从下载过程中可以看到我们之前提及的分层存储的概念,镜像是由多层存储所构成。下载也是一层层的去下载,并非单一文件。下载过程中给出了每一层的 ID 的前 12 位。并且下载结束后,给出该镜像完整的 `sha256` 的摘要,以确保下载一致性。
@ -37,36 +37,33 @@ Status: Downloaded newer image for ubuntu:14.04
### 运行
有了镜像后,我们就可以以这个镜像为基础启动一个容器来运行。以上面的 `ubuntu:14.04` 为例,如果我们打算启动里面的 `bash` 并且进行交互式操作的话,可以执行下面的命令。
有了镜像后,我们就可以以这个镜像为基础启动一个容器来运行。以上面的 `ubuntu:16.04` 为例,如果我们打算启动里面的 `bash` 并且进行交互式操作的话,可以执行下面的命令。
```bash
$ docker run -it --rm \
ubuntu:14.04 \
ubuntu:16.04 \
bash
root@e7009c6ce357:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04.5 LTS, Trusty Tahr"
VERSION="16.04.4 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.5 LTS"
VERSION_ID="14.04"
PRETTY_NAME="Ubuntu 16.04.4 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
root@e7009c6ce357:/# exit
exit
$
```
`docker run` 就是运行容器的命令,具体格式我们会在后面的章节讲解,我们这里简要的说明一下上面用到的参数。
* `-it`:这是两个参数,一个是 `-i`:交互式操作,一个是 `-t` 终端。我们这里打算进入 `bash` 执行一些命令并查看返回结果,因此我们需要交互式终端。
* `--rm`:这个参数是说容器退出后随之将其删除。默认情况下,为了排障需求,退出的容器并不会立即删除,除非手动 `docker rm`。我们这里只是随便执行个命令,看看结果,不需要排障和保留结果,因此使用 `--rm` 可以避免浪费空间。
* `ubuntu:14.04`:这是指用 `ubuntu:14.04` 镜像为基础来启动容器。
* `ubuntu:16.04`:这是指用 `ubuntu:16.04` 镜像为基础来启动容器。
* `bash`:放在镜像名后的是**命令**,这里我们希望有个交互式 Shell因此用的是 `bash`
进入容器后,我们可以在 Shell 下操作,执行任何所需的命令。这里,我们执行了 `cat /etc/os-release`,这是 Linux 常用的查看当前系统版本的命令,从返回的结果可以看到容器内是 `Ubuntu 14.04.5 LTS` 系统。
进入容器后,我们可以在 Shell 下操作,执行任何所需的命令。这里,我们执行了 `cat /etc/os-release`,这是 Linux 常用的查看当前系统版本的命令,从返回的结果可以看到容器内是 `Ubuntu 16.04.4 LTS` 系统。
最后我们通过 `exit` 退出了这个容器。

View File

@ -34,6 +34,8 @@ $ sudo service docker restart
}
```
> 注意,一定要保证该文件符合 json 规范,否则 Docker 将不能启动。
之后重新启动服务。
```bash
@ -41,7 +43,7 @@ $ sudo systemctl daemon-reload
$ sudo systemctl restart docker
```
注意:如果您之前查看旧教程,修改了 `docker.service` 文件内容,请去掉您添加的内容(`--registry-mirror=https://registry.docker-cn.com`),这里不再赘述。
>注意:如果您之前查看旧教程,修改了 `docker.service` 文件内容,请去掉您添加的内容(`--registry-mirror=https://registry.docker-cn.com`),这里不再赘述。
### Windows 10
对于使用 Windows 10 的系统,在系统右下角托盘 Docker 图标内右键菜单选择 `Settings`,打开配置窗口后左侧导航菜单选择 `Docker Daemon`。编辑窗口内的 JSON 串,填写加速器地址,如:

View File

@ -2,7 +2,9 @@
Docker Machine 支持多种后端驱动,包括虚拟机、本地主机和云平台等。
### 本地主机实例
### 创建本地主机实例
#### Virtualbox 驱动
使用 `virtualbox` 类型的驱动,创建一台 Docker 主机,命名为 test。
@ -10,7 +12,55 @@ Docker Machine 支持多种后端驱动,包括虚拟机、本地主机和云
$ docker-machine create -d virtualbox test
```
查看主机
你也可以在创建时加上如下参数,来配置主机或者主机上的 Docker。
`--engine-opt dns=114.114.114.114` 配置 Docker 的默认 DNS
`--engine-registry-mirror https://registry.docker-cn.com` 配置 Docker 的仓库镜像
`--virtualbox-memory 2048` 配置主机内存
`--virtualbox-cpu-count 2` 配置主机 CPU
更多参数请使用 `docker-machine create --driver virtualbox --help` 命令查看。
#### macOS xhyve 驱动
`xhyve` 驱动 GitHub: https://github.com/zchee/docker-machine-driver-xhyve
[`xhyve`](https://github.com/mist64/xhyve) 是 macOS 上轻量化的虚拟引擎,使用其创建的 Docker Machine 较 `VirtualBox` 驱动创建的运行效率要高。
```bash
$ brew install docker-machine-driver-xhyve
$ docker-machine create \
-d xhyve \
# --xhyve-boot2docker-url ~/.docker/machine/cache/boot2docker.iso \
--engine-opt dns=114.114.114.114 \
--engine-registry-mirror https://registry.docker-cn.com \
--xhyve-memory-size 2048 \
--xhyve-rawdisk \
--xhyve-cpu-count 2 \
xhyve
```
>注意:非首次创建时建议加上 `--xhyve-boot2docker-url ~/.docker/machine/cache/boot2docker.iso` 参数,避免每次创建时都从 GitHub 下载 ISO 镜像。
更多参数请使用 `docker-machine create --driver xhyve --help` 命令查看。
#### Windows 10
Windows 10 安装 Docker for Windows 之后不能再安装 VirtualBox也就不能使用 `virtualbox` 驱动来创建 Docker Machine我们可以选择使用 `hyperv` 驱动。
```bash
$ docker-machine create --driver hyperv vm
```
更多参数请使用 `docker-machine create --driver hyperv --help` 命令查看。
### 使用介绍
创建好主机之后,查看主机
```bash
$ docker-machine ls
@ -36,6 +86,8 @@ docker@test:~$ docker --version
Docker version 17.10.0-ce, build f4ffd25
```
连接到主机之后你就可以在其上使用 Docker 了。
### 官方支持驱动
通过 `-d` 选项可以选择支持的驱动类型。
@ -46,12 +98,14 @@ Docker version 17.10.0-ce, build f4ffd25
* exoscale
* generic
* google
* hyperv
* none
* openstack
* rackspace
* softlayer
* virtualbox
* vmwarevcloudair
* vmwarefusion
* vmwarevsphere
### 第三方驱动
@ -89,4 +143,5 @@ Docker version 17.10.0-ce, build f4ffd25
```bash
$ docker-machine COMMAND --help
```
来查看具体的用法。

View File

@ -1,10 +1,7 @@
# 访问仓库
仓库Repository是集中存放镜像的地方。
仓库(`Repository`)是集中存放镜像的地方。
一个容易混淆的概念是注册服务器Registry。实际上注册服务器是管理仓库的具体服务器每个服务器上可以有多个仓库而每个仓库下面有多个镜像。从这方面来说仓库可以被认为是一个具体的项目或目录。例如对于仓库地址 `dl.dockerpool.com/ubuntu` 来说,`dl.dockerpool.com` 是注册服务器地址,`ubuntu` 是仓库名。
一个容易混淆的概念是注册服务器(`Registry`)。实际上注册服务器是管理仓库的具体服务器,每个服务器上可以有多个仓库,而每个仓库下面有多个镜像。从这方面来说,仓库可以被认为是一个具体的项目或目录。例如对于仓库地址 `dl.dockerpool.com/ubuntu` 来说,`dl.dockerpool.com` 是注册服务器地址,`ubuntu` 是仓库名。
大部分时候,并不需要严格区分这两者的概念。

View File

@ -80,7 +80,7 @@ username/ubuntu
有时候,用户创建了镜像,安装了某个软件,如果软件发布新版本则需要手动更新镜像。
而自动创建允许用户通过 Docker Hub 指定跟踪一个目标网站(目前支持 [GitHub](https://github.com) 或 [BitBucket](https://bitbucket.org)上的项目一旦项目发生新的提交或者创建新的标签tag则自动执行创建
而自动创建允许用户通过 Docker Hub 指定跟踪一个目标网站(目前支持 [GitHub](https://github.com) 或 [BitBucket](https://bitbucket.org)上的项目一旦项目发生新的提交或者创建新的标签tagDocker Hub 会自动构建镜像并推送到 Docker Hub 中
要配置自动创建,包括如下的步骤:

View File

@ -8,10 +8,14 @@
* 更新 `CoreOS` 章节
* 更新 `etcd` 章节,基于 3.x 版本
* 增加 Docker 中文资源链接
* 增加介绍基于 Docker 的 CI/CD 工具 Drone
* 替换 `docker daemon` 命令为 `dockerd`
* 0.9-rc1: 2017-11-29
* 根据最新版本v17.09)修订内容