轉載請註明出處,原文連接http://tailnode.tk/2017/01/%E...node
這是一篇很是入門的文章,讓你大概瞭解一下etcd。寫這篇文章時使用etcd的版本是3.1.0。
etcd是以實現共享配置和服務發現爲目的,提供一致性的鍵值存儲的分佈式數據庫。kubernetes等項目使用了etcd。linux
去這裏下載release包,解壓後是一些文檔和兩個二進制文件etcd和etcdctl。etcd是server端,etcdctl是客戶端。將etcd和etcdctl加入PATH路徑方便咱們執行命令。git
執行命令etcd,便可啓動servergithub
ming@ming:/tmp$ etcd 2017-02-14 14:04:40.164639 I | etcdmain: etcd Version: 3.1.0 2017-02-14 14:04:40.164725 I | etcdmain: Git SHA: 8ba2897 2017-02-14 14:04:40.164736 I | etcdmain: Go Version: go1.7.4 2017-02-14 14:04:40.164776 I | etcdmain: Go OS/Arch: linux/amd64 2017-02-14 14:04:40.164784 I | etcdmain: setting maximum number of CPUs to 4, total number of available CPUs is 4 2017-02-14 14:04:40.164850 W | etcdmain: no data-dir provided, using default data-dir ./default.etcd 2017-02-14 14:04:40.164934 I | etcdmain: advertising using detected default host "192.168.1.124" 2017-02-14 14:04:40.165855 I | embed: listening for peers on http://localhost:2380 2017-02-14 14:04:40.167090 I | embed: listening for client requests on localhost:2379 ......
說明:etcd最新的API版本是v3。與v2相比,v3更高效更清晰。設置環境變量ETCDCTL_API=3。redis
ming@ming:/tmp$ export ETCDCTL_API=3 ming@ming:/tmp$ etcdctl version etcdctl version: 3.1.0 API version: 3.1
put
設置key,get
取得key數據庫
ming@ming:/tmp$ etcdctl put msg "Hello TenxCloud" OK ming@ming:/tmp$ etcdctl get msg msg Hello TenxCloud
del
刪除key併發
ming@ming:/tmp$ etcdctl get msg msg Hello TenxCloud ming@ming:/tmp$ etcdctl del msg 1 ming@ming:/tmp$ etcdctl get msg ming@ming:/tmp$
txn
事務
txn從標準輸入中讀取多個請求,將它們看作一個原子性的事務執行。事務是由條件列表,條件判斷成功時的執行列表(條件列表中所有條件爲真表示成功)和條件判斷失敗時的執行列表(條件列表中有一個爲假即爲失敗)組成的。
看文字解釋容易暈,來看實例吧分佈式
ming@ming:/tmp$ etcdctl put flag 1 OK ming@ming:/tmp$ etcdctl txn -i compares: value("flag") = "1" success requests (get, put, delete): put result true failure requests (get, put, delete): put result false SUCCESS OK ming@ming:/tmp$ etcdctl get result result true
解釋一下:ide
etcdctl put flag 1設置flag爲1學習
etcdctl txn -i開啓事務(-i表示交互模式)
第2步輸入命令後回車,終端顯示出compares:
輸入value("flag") = "1",此命令是比較flag的值與1是否相等
第4步完成後輸入回車,終端會換行顯示,此時能夠繼續輸入判斷條件(前面說過事務由條件列表組成),再次輸入回車表示判斷條件輸入完畢
第5步連續輸入兩個回車後,終端顯示出success requests (get, put, delete):,表示下面輸入判斷條件爲真時要執行的命令
與輸入判斷條件相同,連續兩個回車表示成功時的執行列表輸入完成
終端顯示failure requests (get, put, delete):後輸入條件判斷失敗時的執行列表
爲了看起來簡潔,此實例中條件列表和執行列表只寫了一行命令,實際能夠輸入多行
總結上面的事務,要作的事情就是flag爲1時設置result爲true,不然設置result爲false
事務執行完成後查看result值爲true
watch
監聽
watch後etcdctl阻塞,在另外一個終端中執行etcdctl put flag 2後,watch會打印出相關信息
ming@ming:/tmp$ etcdctl watch flag PUT flag 2
lease
租約
etcd也能爲key設置超時時間,但與redis不一樣,etcd須要先建立lease,而後使用put命令加上參數–lease=<lease ID>來設置
ming@ming:/tmp$ etcdctl lease grant 100 lease 38015a3c00490513 granted with TTL(100s) ming@ming:/tmp$ etcdctl put k1 v1 --lease=38015a3c00490513 OK ming@ming:/tmp$ etcdctl lease timetolive 38015a3c00490513 lease 38015a3c00490513 granted with TTL(100s), remaining(67s) ming@ming:/tmp$ etcdctl lease timetolive 38015a3c00490513 lease 38015a3c00490513 granted with TTL(100s), remaining(64s) ming@ming:/tmp$ etcdctl lease timetolive 38015a3c00490513 --keys lease 38015a3c00490513 granted with TTL(100s), remaining(59s), attached keys([k1]) ming@ming:/tmp$ etcdctl put k2 v2 --lease=38015a3c00490513 OK ming@ming:/tmp$ etcdctl lease timetolive 38015a3c00490513 --keys lease 38015a3c00490513 granted with TTL(100s), remaining(46s), attached keys([k1 k2]) ming@ming:/tmp$ etcdctl lease revoke 38015a3c00490513 lease 38015a3c00490513 revoked ming@ming:/tmp$ etcdctl get k1 ming@ming:/tmp$ etcdctl get k2 ming@ming:/tmp$ ming@ming:/tmp$ etcdctl lease grant 10 lease 38015a3c0049051d granted with TTL(10s) ming@ming:/tmp$ etcdctl lease keep-alive 38015a3c0049051d lease 38015a3c0049051d keepalived with TTL(10) lease 38015a3c0049051d keepalived with TTL(10) lease 38015a3c0049051d keepalived with TTL(10)
lease grant <ttl>
建立lease,返回lease ID。建立的lease生存時間大於或等於ttl秒(TODO:爲何可能大於?) lease revoke <lease ID>
刪除lease,並刪除全部關聯的key lease timetolive <lease ID>
取得lease的總時間和剩餘時間 lease keep-alive <lease ID>
此命令不會只更新一次lease時間,而是週期性地刷新,保證它不會過時。
TODO
lock <lock name>
經過指定的名字加鎖。注意,只有當正常退出且釋放鎖後,lock命令的退出碼是0,不然這個鎖會一直被佔用直到過時(默認60秒)
使用Ctrl+C正常退出lock命令,退出碼爲0,第二次能正常lock: ming@ming:/tmp$ etcdctl lock test test/38015a3fd6795e04 ^Cming@ming:/tmp$ echo $? 0 ming@ming:/tmp$ etcdctl lock test test/38015a3fd6795e0a kill掉lock命令,退出碼不爲0,第二次lock被阻塞: 終端1,第一次正常鎖住test: ming@ming:/tmp$ etcdctl lock test test/38015a3fd6795e11 終端2,kill掉lock命令: ming@ming:~$ ps aux|grep 'etcdctl lock' ming 44546 0.5 0.5 19876 11436 pts/5 Sl+ 11:42 0:00 etcdctl lock test ming 44560 0.0 0.0 14224 1084 pts/6 S+ 11:43 0:00 grep --color=auto etcdctl lock ming@ming:~$ kill -9 44546 終端1,退出碼不爲0,第二次鎖test被阻塞 ming@ming:/tmp$ etcdctl lock test test/38015a3fd6795e1e Killed ming@ming:/tmp$ echo $? 137 ming@ming:/tmp$ etcdctl lock test
elect
TODO
user
能夠爲etcd建立多個用戶並設置密碼,子命令有:
add 添加用戶
delete 刪除用戶
get 取得用戶詳情
list 列出全部用戶
passwd 修改用戶密碼
grant-role 給用戶分配角色
revoke-role 給用戶移除角色
role
能夠爲etcd建立多個角色並設置權限,子命令有:
add 添加角色
delete 刪除角色
get 取得角色信息
list 列出全部角色
grant-permission 爲角色設置某個key的權限
revoke-permission 爲角色移除某個key的權限
auth
開啓/關閉權限控制
示例
下面以示例來學習這三個命令
root用戶存在時才能開啓權限控制 ming@ming:/tmp$ etcdctl auth enable Error: etcdserver: root user does not exist ming@ming:/tmp$ etcdctl user add root Password of root: Type password of root again for confirmation: User root created ming@ming:/tmp$ etcdctl auth enable Authentication Enabled 開啓權限控制後須要用--user指定用戶 ming@ming:/tmp$ etcdctl user list Error: etcdserver: user name not found ming@ming:/tmp$ etcdctl user list --user=root Password: root ming@ming:/tmp$ etcdctl user get root --user=root Password: User: root Roles: root 添加用戶,前兩個密碼是新用戶的,後一個密碼是root的 ming@ming:/tmp$ etcdctl user add mengyuan --user=root Password of mengyuan: Type password of mengyuan again for confirmation: Password: User mengyuan created 使用新用戶執行put命令,提示沒有權限 ming@ming:/tmp$ etcdctl put key1 v1 --user=mengyuan Password: Error: etcdserver: permission denied 建立名爲rw_key_的role,添加對字符串"key"作爲前綴的key的讀寫權限,爲mengyuan添加角色 ming@ming:/tmp$ etcdctl role add rw_key_ --user=root Password: Role rw_key_ created ming@ming:/tmp$ etcdctl --user=root role grant-permission rw_key_ readwrite key --prefix=true Password: Role rw_key_ updated ming@ming:/tmp$ etcdctl --user=root user grant-role mengyuan rw_key_ Password: Role rw_key_ is granted to user mengyuan 添加權限成功後執行put key1成功,執行put k1失敗(由於上面只給前綴爲"key"的key添加了權限) ming@ming:/tmp$ etcdctl put key1 v1 --user=mengyuan Password: OK ming@ming:/tmp$ etcdctl put k1 v1 --user=mengyuan Password: Error: etcdserver: permission denied 執行user list命令失敗,沒有權限 ming@ming:/tmp$ etcdctl user list --user=mengyuan Password: Error: etcdserver: permission denied 爲新用戶添加root的角色後就能執行user list命令了,注意命令中第一個root是角色,第二個root是用戶 ming@ming:/tmp$ etcdctl user grant-role mengyuan root --user=root Password: Role root is granted to user mengyuan ming@ming:/tmp$ etcdctl user list --user=mengyuan Password: mengyuan root
etcdctl <command> -h查看子命令的幫助(例:etcdctl watch -h)
http://play.etcd.io/play 是網頁版集羣環境
etcdctl可以設置--prefix=true來操做多個指定前綴的key
https://github.com/coreos/etcd
https://github.com/coreos/etc...