etcd官方文档中文版
  • Introduction
  • 官方文档
    • 开发指南
      • 搭建本地集群
      • 和 etcd 交互
      • 核心 API 参考文档
      • 并发 API 参考文档
      • gRPC 网关
      • gRPC 命名和发现
      • 试验性的 API 和特性
      • 系统限制
    • 运维指南
      • 搭建 etcd 集群
        • 运行时重配置
        • 运行时重配置的设计
      • 搭建 etcd 网关
      • 在容器内运行 etcd 集群
      • 配置
      • gRPC代理(TBD)
      • L4 网关
      • 支持平台
      • 硬件推荐(TBD)
      • 性能评测
      • 调优(TBD)
      • 安全模式
      • 基于角色的访问控制(TBD)
      • 常见问题(TBD)
      • 监控(TBD)
      • 维护
      • 理解失败
      • 灾难恢复
      • 版本
    • 学习
      • 为什么是etcd
      • 理解数据模型
      • 理解API
      • 术语
      • API保证
      • 认证子系统(TBD)
  • 核心 API 参考文档
    • KV service
      • Range方法
      • Put方法
      • DeleteRange方法
      • Txn方法
      • Compact方法
    • Watch service
      • Watch方法
    • Lease service
      • LeaseGrant方法
      • LeaseRevoke方法
      • LeaseKeepAlive方法
      • LeaseTimeToLive方法
  • 并发 API 参考文档
    • Lock service
      • Lock方法
      • Unlock方法
    • Election service
      • Campaign方法
      • Proclaim方法
      • Leader方法
      • Observe方法
      • Resign方法
  • 全文标签总览
Powered by GitBook
On this page
  • 本地独立集群
  • 本地多成员集群
  1. 官方文档
  2. 开发指南

搭建本地集群

Previous开发指南Next和 etcd 交互

Last updated 6 years ago

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

本地独立集群

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

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

$ ./etcd
...

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

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

# 使用 API 版本 3
$ export ETCDCTL_API=3

$ ./etcdctl put foo bar
OK

$ ./etcdctl get foo
bar

本地多成员集群

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

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

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

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

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

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

# 使用 API 版本 3
$ export ETCDCTL_API=3

$ etcdctl --write-out=table --endpoints=localhost:12379 member list
+------------------+---------+--------+------------------------+------------------------+
|        ID        | STATUS  |  NAME  |       PEER ADDRS       |      CLIENT ADDRS      |
+------------------+---------+--------+------------------------+------------------------+
| 8211f1d0f64f3269 | started | infra1 | http://127.0.0.1:12380 | http://127.0.0.1:12379 |
| 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 |
+------------------+---------+--------+------------------------+------------------------+

$ etcdctl --endpoints=localhost:12379 put foo bar
OK

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

# 杀掉 etcd2
$ goreman run stop etcd2
# 注:实测这个命令无法停止etcd,最后还是用ps命令找出pid,然后kill
# ps -ef | grep etcd | grep 127.0.0.1:22379
# kill ****

$ etcdctl --endpoints=localhost:12379 put key hello
OK

$ etcdctl --endpoints=localhost:12379 get key
hello

# 试图从被杀掉的成员获取key
$ etcdctl --endpoints=localhost:22379 get key
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"
Error:  grpc: timed out trying to connect

# 重启被杀掉的成员
# 注:实测这个restart命令可用
$ goreman run restart etcd2

# 从重启的成员获取key
$ etcdctl --endpoints=localhost:22379 get key
hello

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

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

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

集群
Go语言安装
etcd 的 gitub 项目的根目录下的 Procfile 文件
和 etcd 交互