etcd

etcd

分布式一致性key-value存儲

版本

v2

使用場景

  1. 注冊中心,服務(wù)注冊與發(fā)現(xiàn)
  2. 配置中心,配置推送
  3. 分布式鎖
  4. 消息發(fā)布與訂閱
  5. 分布式cas
  6. 分布式隊(duì)列

特點(diǎn)

  1. 簡單,api友好(gPRC)
  2. 安全,tls支持
  3. 性能,10000/s寫入速度
  4. 可靠,使用Raft協(xié)議

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
監(jiān)聽 curl http://127.0.0.1:2379/v2/keys/foo?wait=true
創(chuàng)建文件夾 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
節(jié)點(diǎn)信息 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

集群搭建

  1. 靜態(tài),在集群啟動之前就知道集群機(jī)器情況

示例:準(zhǔn)備三臺機(jī)器,安裝完成etcd

192.168.1.100
192.168.1.101
192.168.1.102

每個機(jī)器上分別執(zhí)行命令

$ 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
  1. etcd發(fā)現(xiàn)
  • 使用存在的etcd集群做自動發(fā)現(xiàn)

    指定集群數(shù)目

   $ 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
  • 使用公共服務(wù)
   $ 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發(fā)現(xiàn)

節(jié)點(diǎn)監(jiān)控

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

參考

  1. https://github.com/coreos/etcd
  2. https://raft.github.io/
  3. http://www.ruanyifeng.com/blog/2011/09/curl.html
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容