2014-12-31 09:11:09 +00:00
|
|
|
|
## 安装
|
|
|
|
|
|
2019-08-31 14:57:43 +00:00
|
|
|
|
`etcd` 基于 `Go` 语言实现,因此,用户可以从 [项目主页](https://github.com/etcd-io/etcd) 下载源代码自行编译,也可以下载编译好的二进制文件,甚至直接使用制作好的 `Docker` 镜像文件来体验。
|
2017-12-03 05:57:02 +00:00
|
|
|
|
|
2019-08-31 14:57:43 +00:00
|
|
|
|
>注意:本章节内容基于 etcd `3.4.x` 版本
|
2014-12-31 09:11:09 +00:00
|
|
|
|
|
|
|
|
|
### 二进制文件方式下载
|
|
|
|
|
|
2019-08-31 14:57:43 +00:00
|
|
|
|
编译好的二进制文件都在 [github.com/etcd-io/etcd/releases](https://github.com/etcd-io/etcd/releases/) 页面,用户可以选择需要的版本,或通过下载工具下载。
|
2014-12-31 09:11:09 +00:00
|
|
|
|
|
2017-12-03 05:57:02 +00:00
|
|
|
|
例如,使用 `curl` 工具下载压缩包,并解压。
|
2014-12-31 09:11:09 +00:00
|
|
|
|
|
2017-11-13 10:54:38 +00:00
|
|
|
|
```bash
|
2019-08-31 14:57:43 +00:00
|
|
|
|
$ curl -L https://github.com/etcd-io/etcd/releases/download/v3.4.0/etcd-v3.4.0-linux-amd64.tar.gz -o etcd-v3.4.0-linux-amd64.tar.gz
|
|
|
|
|
$ tar xzvf etcd-v3.4.0-linux-amd64.tar.gz
|
|
|
|
|
$ cd etcd-v3.4.0-linux-amd64
|
2014-12-31 09:11:09 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
解压后,可以看到文件包括
|
2017-12-03 05:57:02 +00:00
|
|
|
|
|
2017-11-13 10:54:38 +00:00
|
|
|
|
```bash
|
2014-12-31 09:11:09 +00:00
|
|
|
|
$ ls
|
2017-12-03 05:57:02 +00:00
|
|
|
|
Documentation README-etcdctl.md README.md READMEv2-etcdctl.md etcd etcdctl
|
2014-12-31 09:11:09 +00:00
|
|
|
|
```
|
|
|
|
|
|
2017-12-03 05:57:02 +00:00
|
|
|
|
其中 `etcd` 是服务主文件,`etcdctl` 是提供给用户的命令客户端,其他文件是支持文档。
|
2014-12-31 09:11:09 +00:00
|
|
|
|
|
2017-12-03 05:57:02 +00:00
|
|
|
|
下面将 `etcd` `etcdctl` 文件放到系统可执行目录(例如 `/usr/local/bin/`)。
|
2014-12-31 09:11:09 +00:00
|
|
|
|
|
2017-11-13 10:54:38 +00:00
|
|
|
|
```bash
|
2014-12-31 09:11:09 +00:00
|
|
|
|
$ sudo cp etcd* /usr/local/bin/
|
|
|
|
|
```
|
|
|
|
|
|
2017-12-03 10:53:35 +00:00
|
|
|
|
默认 `2379` 端口处理客户端的请求,`2380` 端口用于集群各成员间的通信。启动 `etcd` 显示类似如下的信息:
|
2017-12-03 05:57:02 +00:00
|
|
|
|
|
2017-11-13 10:54:38 +00:00
|
|
|
|
```bash
|
2017-12-03 05:57:02 +00:00
|
|
|
|
$ etcd
|
2019-08-31 14:57:43 +00:00
|
|
|
|
...
|
2017-12-03 05:57:02 +00:00
|
|
|
|
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
|
2014-12-31 09:11:09 +00:00
|
|
|
|
```
|
|
|
|
|
|
2017-12-03 10:53:35 +00:00
|
|
|
|
此时,可以使用 `etcdctl` 命令进行测试,设置和获取键值 `testkey: "hello world"`,检查 `etcd` 服务是否启动成功:
|
2017-12-03 05:57:02 +00:00
|
|
|
|
|
2017-11-13 10:54:38 +00:00
|
|
|
|
```bash
|
2018-09-14 08:42:45 +00:00
|
|
|
|
$ ETCDCTL_API=3 etcdctl member list
|
2017-12-03 05:57:02 +00:00
|
|
|
|
8e9e05c52164694d, started, default, http://localhost:2380, http://localhost:2379
|
|
|
|
|
|
2018-09-14 08:42:45 +00:00
|
|
|
|
$ ETCDCTL_API=3 etcdctl put testkey "hello world"
|
2017-12-03 05:57:02 +00:00
|
|
|
|
OK
|
|
|
|
|
|
|
|
|
|
$ etcdctl get testkey
|
|
|
|
|
testkey
|
2014-12-31 09:11:09 +00:00
|
|
|
|
hello world
|
|
|
|
|
```
|
2017-12-03 05:57:02 +00:00
|
|
|
|
|
2014-12-31 09:11:09 +00:00
|
|
|
|
说明 etcd 服务已经成功启动了。
|
|
|
|
|
|
2017-12-03 05:57:02 +00:00
|
|
|
|
### Docker 镜像方式运行
|
|
|
|
|
|
|
|
|
|
镜像名称为 `quay.io/coreos/etcd`,可以通过下面的命令启动 `etcd` 服务监听到 `2379` 和 `2380` 端口。
|
|
|
|
|
|
2017-11-13 10:54:38 +00:00
|
|
|
|
```bash
|
2019-08-31 14:57:43 +00:00
|
|
|
|
$ docker run \
|
|
|
|
|
-p 2379:2379 \
|
|
|
|
|
-p 2380:2380 \
|
|
|
|
|
--mount type=bind,source=/tmp/etcd-data.tmp,destination=/etcd-data \
|
|
|
|
|
--name etcd-gcr-v3.4.0 \
|
|
|
|
|
quay.io/coreos/etcd:v3.4.0 \
|
|
|
|
|
/usr/local/bin/etcd \
|
|
|
|
|
--name s1 \
|
|
|
|
|
--data-dir /etcd-data \
|
|
|
|
|
--listen-client-urls http://0.0.0.0:2379 \
|
|
|
|
|
--advertise-client-urls http://0.0.0.0:2379 \
|
|
|
|
|
--listen-peer-urls http://0.0.0.0:2380 \
|
|
|
|
|
--initial-advertise-peer-urls http://0.0.0.0:2380 \
|
|
|
|
|
--initial-cluster s1=http://0.0.0.0:2380 \
|
|
|
|
|
--initial-cluster-token tkn \
|
|
|
|
|
--initial-cluster-state new \
|
|
|
|
|
--log-level info \
|
|
|
|
|
--logger zap \
|
|
|
|
|
--log-outputs stderr
|
2014-12-31 09:11:09 +00:00
|
|
|
|
```
|
|
|
|
|
|
2017-12-03 10:53:35 +00:00
|
|
|
|
打开新的终端按照上一步的方法测试 `etcd` 是否成功启动。
|
2017-12-03 05:57:02 +00:00
|
|
|
|
|
|
|
|
|
### macOS 中运行
|
2014-12-31 09:11:09 +00:00
|
|
|
|
|
2017-11-13 10:54:38 +00:00
|
|
|
|
```bash
|
2017-12-03 05:57:02 +00:00
|
|
|
|
$ brew install etcd
|
|
|
|
|
|
|
|
|
|
$ etcd
|
|
|
|
|
|
|
|
|
|
$ etcdctl member list
|
2014-12-31 09:11:09 +00:00
|
|
|
|
```
|