搭建本地集群

对于测试和开发部署,最快最简单的方式是搭建本地集群。对于产品部署,参考 集群 章节。

本地独立集群

注: 独立集群 指只有一台服务器的集群。

部署 etcd 集群作为独立集群是直截了当的。仅用一个命令就可以启动它:

  1. $ ./etcd
  2. ...

启动的 etcd 成员在 localhost:2379 监听客户端请求。

通过使用 etcdctl 来和已经启动的集群交互:

  1. # 使用 API 版本 3
  2. $ export ETCDCTL_API=3
  3. $ ./etcdctl put foo bar
  4. OK
  5. $ ./etcdctl get foo
  6. bar

本地多成员集群

注: 多成员集群 指有多台台服务器的集群。

提供 Procfile 用于简化搭建本地多成员集群。通过少量命令就可以启动多成员集群:

  1. # 安装 goreman 程序来控制基于 Profile 的应用程序.
  2. $ go get github.com/mattn/goreman
  3. $ goreman -f Procfile start
  4. ...

注1: 必须先安装 go,请见章节 Go语言安装

注2: 这里所说的 Procfile 文件是来自 etcd 的 gitub 项目的根目录下的 Procfile 文件,但是需要修改一下,将里面的 bin/etcd 修改为 etcd

启动的成员各自在 localhost:12379, localhost:22379, 和 localhost:32379 上监听客户端请求。

注: 英文原文中是 localhost:12379 用的是 12379 端口,但是实际上述 Procfile 文件中启动的是 2379 端口,如果连接时发现无法访问,请自行修改。下面的 12379 也是如此,请自行修改为 2379.

通过使用 etcdctl 来和已经启动的集群交互:

  1. # 使用 API 版本 3
  2. $ export ETCDCTL_API=3
  3. $ etcdctl --write-out=table --endpoints=localhost:12379 member list
  4. +------------------+---------+--------+------------------------+------------------------+
  5. | ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS |
  6. +------------------+---------+--------+------------------------+------------------------+
  7. | 8211f1d0f64f3269 | started | infra1 | http://127.0.0.1:12380 | http://127.0.0.1:12379 |
  8. | 91bc3c398fb3c146 | started | infra2 | http://127.0.0.1:22380 | http://127.0.0.1:22379 |
  9. | fd422379fda50e48 | started | infra3 | http://127.0.0.1:32380 | http://127.0.0.1:32379 |
  10. +------------------+---------+--------+------------------------+------------------------+
  11. $ etcdctl --endpoints=localhost:12379 put foo bar
  12. OK

为了体验 etcd 的容错性,杀掉一个成员:

  1. # 杀掉 etcd2
  2. $ goreman run stop etcd2
  3. # 注:实测这个命令无法停止etcd,最后还是用ps命令找出pid,然后kill
  4. # ps -ef | grep etcd | grep 127.0.0.1:22379
  5. # kill ****
  6. $ etcdctl --endpoints=localhost:12379 put key hello
  7. OK
  8. $ etcdctl --endpoints=localhost:12379 get key
  9. hello
  10. # 试图从被杀掉的成员获取key
  11. $ etcdctl --endpoints=localhost:22379 get key
  12. 2016/04/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"
  13. Error: grpc: timed out trying to connect
  14. # 重启被杀掉的成员
  15. # 注:实测这个restart命令可用
  16. $ goreman run restart etcd2
  17. # 从重启的成员获取key
  18. $ etcdctl --endpoints=localhost:22379 get key
  19. hello

了解更多和 etcd 的交互,请阅读 和 etcd 交互