etcd
分布式一致性key-value存储
版本
v2
使用场景
- 注册中心,服务注册与发现
- 配置中心,配置推送
- 分布式锁
- 消息发布与订阅
- 分布式cas
- 分布式队列
特点
- 简单,api友好(gPRC)
- 安全,tls支持
- 性能,10000/s写入速度
- 可靠,使用Raft协议
REST命令
查看版本 curl -L http://127.0.0.1:2379/version
获得值 curl http://127.0.0.1:2379/v2/keys/message
插入值 curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world"
修改值 curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello etcd"
删除值 curl http://127.0.0.1:2379/v2/keys/message -XDELETE
使用ttl curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=bar -d ttl=5
监听 curl http://127.0.0.1:2379/v2/keys/foo?wait=true
创建文件夹 curl http://127.0.0.1:2379/v2/keys/dir -XPUT -d dir=true
删除文件夹 curl http://127.0.0.1:2379/v2/keys/foo_dir?dir=true -XDELETE
上传文件 curl http://127.0.0.1:2379/v2/keys/afile -XPUT --data-urlencode value@afile.txt
主信息 curl http://127.0.0.1:2379/v2/stats/leader
节点信息 curl http://127.0.0.1:2379/v2/stats/self
存储信息 curl http://127.0.0.1:2379/v2/stats/store
cas curl http://127.0.0.1:2379/v2/keys/foo?prevValue=one -XPUT -d value=two
集群搭建
- 静态,在集群启动之前就知道集群机器情况
示例:准备三台机器,安装完成etcd
192.168.1.100
192.168.1.101
192.168.1.102
每个机器上分别执行命令
$ etcd --name infra0 --initial-advertise-peer-urls http://192.168.1.100:2380 \
--listen-peer-urls http://192.168.1.100:2380 \
--listen-client-urls http://192.168.1.100:2379 \
--advertise-client-urls http://192.168.1.100:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra0=http://192.168.1.100:2380,infra1=http://192.168.1.101:2380,infra2=http://192.168.1.102:2380 \
--initial-cluster-state new
$ etcd --name infra1 --initial-advertise-peer-urls http://192.168.1.101:2380 \
--listen-peer-urls http://192.168.1.101:2380 \
--listen-client-urls http://192.168.1.101:2379 \
--advertise-client-urls http://192.168.1.101:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra0=http://192.168.1.100:2380,infra1=http://192.168.1.101:2380,infra2=http://192.168.1.102:2380 \
--initial-cluster-state new
$ etcd --name infra2 --initial-advertise-peer-urls http://192.168.1.102:2380 \
--listen-peer-urls http://192.168.1.102:2380 \
--listen-client-urls http://192.168.1.102:2379 \
--advertise-client-urls http://192.168.1.102:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra0=http://192.168.1.100:2380,infra1=http://192.168.1.101:2380,infra2=http://192.168.1.102:2380 \
--initial-cluster-state new
- etcd发现
-
使用存在的etcd集群做自动发现
指定集群数目
$ curl -X PUT https://myetcd.local/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83/_config/size -d value=3
启动集群
$ etcd --name infra0 --initial-advertise-peer-urls http://10.0.1.10:2380 \
--listen-peer-urls http://10.0.1.10:2380 \
--listen-client-urls http://10.0.1.10:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://10.0.1.10:2379 \
--discovery https://myetcd.local/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83
$ etcd --name infra1 --initial-advertise-peer-urls http://10.0.1.11:2380 \
--listen-peer-urls http://10.0.1.11:2380 \
--listen-client-urls http://10.0.1.11:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://10.0.1.11:2379 \
--discovery https://myetcd.local/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83
$ etcd --name infra2 --initial-advertise-peer-urls http://10.0.1.12:2380 \
--listen-peer-urls http://10.0.1.12:2380 \
--listen-client-urls http://10.0.1.12:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://10.0.1.12:2379 \
--discovery https://myetcd.local/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83
- 使用公共服务
$ curl https://discovery.etcd.io/new?size=3
https://discovery.etcd.io/3e86b59982e49066c5d813af1c2e2579cbf573de
$ etcd --name infra0 --initial-advertise-peer-urls http://10.0.1.10:2380 \
--listen-peer-urls http://10.0.1.10:2380 \
--listen-client-urls http://10.0.1.10:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://10.0.1.10:2379 \
--discovery https://discovery.etcd.io/3e86b59982e49066c5d813af1c2e2579cbf573de
$ etcd --name infra1 --initial-advertise-peer-urls http://10.0.1.11:2380 \
--listen-peer-urls http://10.0.1.11:2380 \
--listen-client-urls http://10.0.1.11:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://10.0.1.11:2379 \
--discovery https://discovery.etcd.io/3e86b59982e49066c5d813af1c2e2579cbf573de
$ etcd --name infra2 --initial-advertise-peer-urls http://10.0.1.12:2380 \
--listen-peer-urls http://10.0.1.12:2380 \
--listen-client-urls http://10.0.1.12:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://10.0.1.12:2379 \
--discovery https://discovery.etcd.io/3e86b59982e49066c5d813af1c2e2579cbf573de
3.DNA发现
节点监控
curl http://127.0.0.1:2379/v2/stats/leader
id: the unique identifier for the member
leaderInfo.leader: id of the current leader member
leaderInfo.uptime: amount of time the leader has been leader
name: this member's name
recvAppendRequestCnt: number of append requests this node has processed
recvBandwidthRate: number of bytes per second this node is receiving (follower only)
recvPkgRate: number of requests per second this node is receiving (follower only)
sendAppendRequestCnt: number of requests that this node has sent
sendBandwidthRate: number of bytes per second this node is sending (leader only). This value is undefined on single member clusters.
sendPkgRate: number of requests per second this node is sending (leader only). This value is undefined on single member clusters.
state: either leader or follower
startTime: the time when this node was started
curl http://127.0.0.1:2379/v2/stats/self
curl http://127.0.0.1:2379/v2/stats/store
网友评论