1.Linux安裝
curl -L https://github.com/coreos/etcd/releases/download/v3.3.2/etcd-v3.3.2-linux-amd64.tar.gz -o etcd-v3.3.2-linux-amd64.tar.gz
tar zxf etcd-v3.3.2-linux-amd64.tar.gz
etcd源碼安裝
git clone https://github.com/etcd-io/etcd.git
cd etcd
./build
cd bin
cp etcd etcdctl /usr/local/bin
#或者
brew install etcd
#啟動etcd
etcd
2.查看版本
./etcd -version
3.后台啟動
nohup ./etcd >/tmp/etcd.log 2>&1 & // 日志文件輸出到/tmp/etcd.log目錄
3.1 設置環境變量
root@localhost etcd]# echo 'export ETCDCTL_API=3' >> /etc/profile ## 環境變量添加 ETCDCTL_API=3
[root@localhost etcd]# source /etc/profile # 是profile中修改的文件生效
[root@localhost etcd]# ./etcdctl get mykey # 可以直接使用./etcdctl get key 命令了
mykey
this is awesome # 剛添加的內容
4.添加key
./etcdctl put mykey "this is awesome"
5.其他操作
./etcdctl –version –api版本
./etcdctl ls -p / –列出目錄下所有目錄或節點,-p參數會自動用/標識出節點還是目錄
./etcdctl set /p1/p2/v1 hello –創建一個節點,並給他一個value
./etcdctl get /p1/p2/v1 –獲取一個節點的value
# 刪除某個 key
➜ ./etcdctl mk /foo bar
bar
➜ ./etcdctl rm /foo
PrevNode.Value: bar
➜ ./etcdctl get /foo
Error: 100: Key not found (/foo) [1062]
# 只有當 key 的值匹配的時候,才進行刪除
➜ ./etcdctl mk /foo bar
bar
➜ ./etcdctl rm --with-value wrong /foo
Error: 101: Compare failed ([wrong != bar]) [1063]
➜ ./etcdctl rm --with-value bar /foo
# 創建一個目錄
➜ ./etcdctl mkdir /dir
# 刪除空目錄
➜ ./etcdctl mkdir /dir/subdir/
➜ ./etcdctl rmdir /dir/subdir/
# 刪除非空目錄
➜ ./etcdctl rmdir /dir
Error: 108: Directory not empty (/dir) [1071]
➜ ./etcdctl rm --recursive /dir
# 列出目錄的內容
➜ ./etcdctl ls /
/queue
/anotherdir
/message
# 遞歸列出目錄的內容
➜ ./etcdctl ls --recursive /
/anotherdir
/message
/queue
/queue/00000000000000001053
/queue/00000000000000001054
# 監聽某個 key,當 key 改變的時候會打印出變化
➜ ./etcdctl watch /message
changed
# 監聽某個目錄,當目錄中任何 node 改變的時候,都會打印出來
➜ ./etcdctl watch --recursive /
[set] /message
changed
# 一直監聽,除非 `CTL + C` 導致退出監聽
➜ ./etcdctl watch --forever /message
new value
chaned again
Wola
# 監聽目錄,並在發生變化的時候執行一個命令
➜ ./etcdctl exec-watch --recursive / -- sh -c "echo change detected."
change detected.
簡單:易於部署,易使用。基於 HTTP+JSON 的 API 讓你用 curl 就可以輕松使用。
安全:可選 SSL 客戶認證機制。
快速:每個實例每秒支持一千次寫操作。
可信:使用一致性 Raft 算法充分實現了分布式。
6.使用etcd作為服務發現
# 啟動 srv 注冊到etcd
cd user/srv
./user-srv --registry etcd
#啟動 api 注冊到etcd
cd /user/api
./user-api --registry etcd
#啟動 micro-api api網關,注冊到etcd
cd micro-examples/
micro --registry etcd api --namespace=io.github.entere.api --handler=api
#查看服務是否正常啟動
micro --registry etcd list services
#獲取指定名的服務
micro --registry etcd get service io.github.entere.srv.user
micro --registry etcd get service io.github.entere.api.user
#測試
curl -H "Content-Type:application/x-www-form-urlencoded" -X POST -d "user_id=f546e78a46284765" http://localhost:8080/user/info
etcdctl --debug member list
etcdctl --debug --endpoints http://127.0.0.1:2379 ls
etcdctl cluster-health
7.etcdkeeper安裝
docker run -it -d --name etcdkeeper \
-p 8080:8080 \
deltaprojects/etcdkeeper
訪問http://IP:8080/etcdkeeper/
// etcdkeeper二進制下載
https://github.com/evildecay/etcdkeeper/releases
./etcdkeeper -p 12349 //啟動etcd
//瀏覽器查看:
http://127.0.0.1:12379/etcdkeeper/
8.etcd-browser安裝
docker run --rm -d --name etcd-browser \
-p 8000:8000 \
--env ETCD_HOST=IP \
--env ETCD_PORT=2379 \
buddho/etcd-browser
運行后訪問http://IP:8000/
9.實踐案例
// 設置 etcdctl的API版本
export ETCDCTL_API=3
// 查詢所有注冊的微服務
etcdctl --endpoints=127.0.0.1:2379 get / --prefix --keys-only
// 查看前綴為xx到key
etcdctl --endpoints=$ENDPOINTS get xxx --prefix
// 查詢對應的值
etcdctl --write-out="json" get /micro/registry/xxx | python -m json.tool
// 證書的訪問
ETCDCTL_API=3 etcdctl --endpoints=$ENDPOINTS --cacert=<ca-file> --cert=<cert-file> --key=<key-file> <command>
backup[備份 etcd 的數據]
etcdctl backup
watch[監測一個鍵值的變化,一旦鍵值發生更新,就會輸出最新的值並退出]
etcdctl watch key
exec-watch[監測一個鍵值的變化,一旦鍵值發生更新,就執行給定命令]
etcdctl exec-watch key --sh -c "ls"
member[通過 list、add、remove、update 命令列出、添加、刪除 、更新etcd 實例到 etcd 集群中]
etcdctl member list;etcdctl member add 實例;etcdctl member remove 實例;etcdctl member update 實例。
etcdctl cluster-health[檢查集群健康狀態]
https://www.huweihuang.com/kubernetes-notes/etcd/etcdctl-v2.html
相關鏈接
https://www.jianshu.com/p/2966b6ef5d10
https://juejin.im/post/5dabc50ef265da5b591b761a