一、Etcd
类比于Zookeeper,开源的、分布式的键值对数据存储系统,主要用来做配置共享、服务的注册和发现。高可用,强一致性。内部采用 Raft 协议作为一致性算法选举leader,保证集群数据不丢失
每个 etcd cluster 都是有若干个 member 组成的,每个 member 是一个独立运行的 etcd 实例,单台机器上可以运行多个 member。在正常运行的状态下,集群中会有一个 leader,其余的 member 都是 followers。leader 向 followers 同步日志,保证数据在各个 member 都有副本。leader 还会定时向所有的 member 发送心跳报文,如果在规定的时间里 follower 没有收到心跳,就会重新进行选举。客户端所有的请求都会先发送给 leader,leader 向所有的 followers 同步日志,等收到超过半数的确认后就把该日志存储到磁盘,并返回响应客户端。每个 etcd 服务有三大主要部分组成:raft 实现、WAL 日志存储、数据的存储和索引。WAL 会在本地磁盘(就是之前提到的 --data-dir)上存储日志内容(wal file)和快照(snapshot)
官方文档:https://coreos.com/etcd/
etcd-browser(etcd-webui): https://github.com/henszey/etcd-browser
etckeeper:https://github.com/evildecay/etcdkeeper
参考文档:
etcd:从应用场景到实现原理的全方位解读
ETCD是什么
Etcd安装及使用详解
Java】用etcd做服务注册和发现
Etcd V3版本API
二、Mac安装
用brew安装非常方便,没安装的自行安装Homebrew
brew search etcd
可以看到候选安装包
brew install etcd
安装
#执行etcd即可启动服务
etcd
可以看到以下信息:
2018-11-26 17:35:02.659194 I | etcdmain: etcd Version: 3.3.10
2018-11-26 17:35:02.659310 I | etcdmain: Git SHA: GitNotFound
2018-11-26 17:35:02.659315 I | etcdmain: Go Version: go1.11.1
2018-11-26 17:35:02.659319 I | etcdmain: Go OS/Arch: darwin/amd64
2018-11-26 17:35:02.659324 I | etcdmain: setting maximum number of CPUs to 4, total number of available CPUs is 4
2018-11-26 17:35:02.659335 N | etcdmain: failed to detect default host (default host not supported on darwin_amd64)
2018-11-26 17:35:02.659345 W | etcdmain: no data-dir provided, using default data-dir ./default.etcd
2018-11-26 17:35:02.659758 N | etcdmain: the server is already initialized as member before, starting as etcd member...
2018-11-26 17:35:02.661503 I | embed: listening for peers on http://localhost:2380
2018-11-26 17:35:02.661918 I | embed: listening for client requests on localhost:2379
2018-11-26 17:35:02.666171 I | etcdserver: name = default
2018-11-26 17:35:02.666189 I | etcdserver: data dir = default.etcd
2018-11-26 17:35:02.666195 I | etcdserver: member dir = default.etcd/member
2018-11-26 17:35:02.666199 I | etcdserver: heartbeat = 100ms
2018-11-26 17:35:02.666203 I | etcdserver: election = 1000ms
2018-11-26 17:35:02.666207 I | etcdserver: snapshot count = 100000
2018-11-26 17:35:02.666220 I | etcdserver: advertise client URLs = http://localhost:2379
2018-11-26 17:35:02.668339 I | etcdserver: restarting member 8e9e05c52164694d in cluster cdf818194e3a8c32 at commit index 4
2018-11-26 17:35:02.668614 I | raft: 8e9e05c52164694d became follower at term 2
2018-11-26 17:35:02.668636 I | raft: newRaft 8e9e05c52164694d [peers: [], term: 2, commit: 4, applied: 0, lastindex: 4, lastterm: 2]
2018-11-26 17:35:02.671551 W | auth: simple token is not cryptographically signed
2018-11-26 17:35:02.672511 I | etcdserver: starting server... [version: 3.3.10, cluster version: to_be_decided]
2018-11-26 17:35:02.672700 E | etcdserver: cannot monitor file descriptor usage (cannot get FDUsage on darwin)
2018-11-26 17:35:02.673933 I | etcdserver/membership: added member 8e9e05c52164694d [http://localhost:2380] to cluster cdf818194e3a8c32
2018-11-26 17:35:02.674131 N | etcdserver/membership: set the initial cluster version to 3.3
2018-11-26 17:35:02.674257 I | etcdserver/api: enabled capabilities for version 3.3
2018-11-26 17:35:03.671575 I | raft: 8e9e05c52164694d is starting a new election at term 2
2018-11-26 17:35:03.671609 I | raft: 8e9e05c52164694d became candidate at term 3
2018-11-26 17:35:03.671916 I | raft: 8e9e05c52164694d received MsgVoteResp from 8e9e05c52164694d at term 3
2018-11-26 17:35:03.671942 I | raft: 8e9e05c52164694d became leader at term 3
2018-11-26 17:35:03.671951 I | raft: raft.node: 8e9e05c52164694d elected leader 8e9e05c52164694d at term 3
2018-11-26 17:35:03.672275 I | etcdserver: published {Name:default ClientURLs:[http://localhost:2379]} to cluster cdf818194e3a8c32
2018-11-26 17:35:03.672537 I | embed: ready to serve client requests
2018-11-26 17:35:03.682936 N | embed: serving insecure client requests on 127.0.0.1:2379, this is strongly discouraged!
etcdserver: name = default name表示节点名称,默认为default
data-dir保存日志和快照的目录,默认为当前工作目录default.etcd/
在http://localhost:2380和集群中其他节点通信
在http://localhost:2379提供HTTP API服务,供客户端交互。等会配置webui就是这个地址
etcdserver: heartbeat = 100ms leader发送心跳到followers的间隔时间
etcdserver: election = 1000ms 重新投票的超时时间,如果follow在该时间间隔没有收到心跳包,会触发重新投票,默认为1000ms
三、安装etcd webui
记得启动Etcd服务。
先安装node,git环境,然后clone
git clone https://github.com/henszey/etcd-browser.git
cd etcd-browser/
vim server.js
编辑server.js,修改内容如下:
var etcdHost = process.env.ETCD_HOST || '127.0.0.1'; # etcd 主机IP
var etcdPort = process.env.ETCD_PORT || 4001; # etcd 主机端口
var serverPort = process.env.SERVER_PORT || 8000; # etcd-browser 监听端口
然后启动
node server.js
访问:http://127.0.0.1:8000/