配置本地集群
配置本地集群进行测试和开发
对于测试和开发部署,最快捷简便的方式是配置本地集群。对于生产部署,请参考 clustering
章节。
本地独立集群
启动集群
运行以下命令以将 etcd 集群部署为独立集群:
如果 etcd 二进制文件不在当前工作目录中,它可能位于 $GOPATH/bin/etcd 或 /usr/local/bin/etcd。请相应地运行命令。
运行中的 etcd 成员在 localhost:2379 上监听客户端请求。
与集群交互
使用 etcdctl 与运行中的集群交互:
在集群中存储一个示例键值对:
$ ./etcdctl put foo bar
OK
如果输出 OK,表示键值对已成功存储。
获取 foo 的值:
如果返回 bar,表示可正常与 etcd 集群交互。
本地多成员集群
启动集群
在 etcd 代码仓库根目录下提供了一个 Procfile,用于便捷地配置本地多成员集群。要启动多成员集群,请进入 etcd 源码根目录并执行以下操作:
安装 goreman 以控制基于 Procfile 的应用程序:
$ go install github.com/mattn/goreman@latest
使用 goreman 和 etcd 的默认 Procfile 启动集群:
$ goreman -f Procfile start
各成员启动后,分别在 localhost:2379、localhost:22379 和 localhost:32379 上监听客户端请求。
与集群交互
使用 etcdctl 与运行中的集群交互:
打印成员列表:
$ etcdctl --write-out=table --endpoints=localhost:2379 member list
etcd 成员列表如下:
+------------------+---------+--------+------------------------+------------------------+
| ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS |
+------------------+---------+--------+------------------------+------------------------+
| 8211f1d0f64f3269 | started | infra1 | http://127.0.0.1:2380 | http://127.0.0.1:2379 |
| 91bc3c398fb3c146 | started | infra2 | http://127.0.0.1:22380 | http://127.0.0.1:22379 |
| fd422379fda50e48 | started | infra3 | http://127.0.0.1:32380 | http://127.0.0.1:32379 |
+------------------+---------+--------+------------------------+------------------------+
在集群中存储一个示例键值对:
如果输出 OK,表示键值对已成功存储。
测试容错能力
为验证 etcd 的容错能力,请终止一个成员,并尝试获取键。
确定待停止成员的进程名称。
Procfile 列出了多成员集群的属性。以进程名为 etcd2 的成员为例。
停止成员:
# kill etcd2
$ goreman run stop etcd2
存储键:
$ etcdctl put key hello
OK
检索上一步存储的键:
从已停止的成员中检索键:
$ etcdctl --endpoints=localhost:22379 get key
该命令应显示由连接失败引起的错误:
2017/06/18 23:07:35 grpc: Conn.resetTransport failed to create client transport: connection error: desc = "transport: dial tcp 127.0.0.1:22379: getsockopt: connection refused"; Reconnecting to "localhost:22379"
Error: grpc: timed out trying to connect
重启已停止的成员:
$ goreman run restart etcd2
从重启后的成员获取键:
$ etcdctl --endpoints=localhost:22379 get key
hello
重启成员后会重新建立连接,etcdctl 现在应能成功获取该键。如需详细了解如何与 etcd 交互,请参阅与 etcd 交互
。