etcd 一個(gè)分布式一致性鍵值存儲(chǔ)系統(tǒng)
etcd是一個(gè)分布式一致性鍵值存儲(chǔ)系統(tǒng),用于共享配置和服務(wù)發(fā)現(xiàn),專注于:
*簡(jiǎn)單:良好定義的面向用戶的API
*安全:帶有可選客戶端證書(shū)認(rèn)證的自動(dòng)TLS
*快速:測(cè)試驗(yàn)證,每秒10000寫(xiě)入
*可靠:使用Raft適當(dāng)分布
etcd 是Go編寫(xiě),并使用Raft一致性算法來(lái)管理高可用復(fù)制日志,架構(gòu)圖如下圖所示

image.png
1.安裝etcd庫(kù)
1.cd go
2.cd src
3.mkdir github.com (要是存在的話直接進(jìn)入到目錄cd github.com)
4.mkdir -p $GOPATH/src/github.com/coreos
5.$ cd !$
6.$ git clone https://github.com/coreos/etcd.git
7.$ cd etcd
8.$ ./build
9.$ ./bin/etcd
測(cè)試
$ cd $GOPATH
$ ./bin/etcd
$ cd $GOPATH
$ ETCDCTL_API=3 ./bin/etcdctl put foo bar
# 輸出結(jié)果顯示OK,表示安裝成功
OK
搭建本地集群
$ go get github.com/mattn/goreman
$ cd $GOPATH/src/github.com/coreos/etcd
$ goreman -f Procfile start
查看本地集群的服務(wù)器列表
$ cd $GOPATH/src/github.com/coreos/etcd
$ ./bin/etcdctl member list
# 顯示結(jié)果
8211f1d0f64f3269: name=infra1 peerURLs=http://127.0.0.1:12380 clientURLs=http://127.0.0.1:2379 isLeader=false
91bc3c398fb3c146: name=infra2 peerURLs=http://127.0.0.1:22380 clientURLs=http://127.0.0.1:22379 isLeader=true
fd422379fda50e48: name=infra3 peerURLs=http://127.0.0.1:32380 clientURLs=http://127.0.0.1:32379 isLeader=false
存儲(chǔ)數(shù)據(jù)
export ETCDCTL_API=3
$ ./bin/etcdctl put foo "Hello World!"
OK
$ ./bin/etcdctl get foo
foo
Hello World!
$ ./bin/etcdctl --write-out="json" get foo
{"header":{"cluster_id":17237436991929493444,"member_id":9372538179322589801,"revision":2,"raft_term":2},"kvs":[{"key":"Zm9v","create_revision":2,"mod_revision":2,"version":1,"value":"SGVsbG8gV29ybGQh"}],"count":1}
根據(jù)前綴查詢
$ ./bin/etcdctl put web1 value1
$ ./bin/etcdctl put web2 value2
$ ./bin/etcdctl put web3 value3
$ ./bin/etcdctl get web --prefix
web1
value1
web2
value2
web3
value3
刪除數(shù)據(jù)
$ ./bin/etcdctl put key myvalue
$ ./bin/etcdctl del key
1
$ ./bin/etcdctl get key
// 查詢結(jié)果為空
$ ./bin/etcdctl put k1 value1
$ ./bin/etcdctl put k2 value2
$ ./bin/etcdctl del k --prefix
2
$ ./bin/etcdctl get k --prefix
// 查詢結(jié)果為空
事務(wù)寫(xiě)入
$ ./bin/etcdctl put user1 bad
OK
$ ./bin/etcdctl txn --interactive
compares:
// 輸入以下內(nèi)容,輸入結(jié)束按 兩次回車
value("user1") = "bad"
//如果 user1 = bad,則執(zhí)行 get user1
success requests (get, put, del):
get user1
//如果 user1 != bad,則執(zhí)行 put user1 good
failure requests (get, put, del):
put user1 good
// 運(yùn)行結(jié)果,執(zhí)行 success
SUCCESS
user1
bad
$ ./bin/etcdctl txn --interactive
compares:
value("user1") = "111"
// 如果 user1 = 111,則執(zhí)行 get user1
success requests (get, put, del):
get user1
//如果 user1 != 111,則執(zhí)行 put user1 2222
failure requests (get, put, del):
put user1 2222
// 運(yùn)行結(jié)果,執(zhí)行 failure
FAILURE
OK
$ ./bin/etcdctl get user1
user1
2222
watch
// 當(dāng) stock1 的數(shù)值改變( put 方法)的時(shí)候,watch 會(huì)收到通知
$ ./bin/etcdctl watch stock1
// 新打開(kāi)終端
$ export ETCDCTL_API=3
$ ./bin/etcdctl put stock1 1000
//在watch 終端顯示
PUT
stock1
1000
$ ./bin/etcdctl watch stock --prefix
$ ./bin/etcdctl put stock1 10
$ ./bin/etcdctl put stock2 20
lease
$ ./bin/etcdctl lease grant 300
# lease 326963a02758b527 granted with TTL(300s)
$ ./bin/etcdctl put sample value --lease=326963a02758b527
OK
$ ./bin/etcdctl get sample
$ ./bin/etcdctl lease keep-alive 326963a02758b520
$ ./bin/etcdctl lease revoke 326963a02758b527
lease 326963a02758b527 revoked
# or after 300 seconds
$ ./bin/etcdctl get sample
Distributed locks
//第一終端
$ ./bin/etcdctl lock mutex1
mutex1/326963a02758b52d
# 第二終端
$ ./bin/etcdctl lock mutex1
// 當(dāng)?shù)谝粋€(gè)終端結(jié)束了,第二個(gè)終端會(huì)顯示
mutex1/326963a02758b531
Elections
$ ./bin/etcdctl elect one p1
one/326963a02758b539
p1
# another client with the same name blocks
$ ./bin/etcdctl elect one p2
//結(jié)束第一終端,第二終端顯示
one/326963a02758b53e
p2
Cluster status
集群狀態(tài)
$ ./bin/etcdctl --write-out=table endpoint status
$ ./bin/etcdctl endpoint health
Snapshot
./bin/etcdctl snapshot save my.db
Snapshot saved at my.db
./bin/etcdctl --write-out=table snapshot status my.db
Member
./bin/etcdctl member list -w table