閱讀目錄:
- 主機安裝
- 集群搭建
- API 操作
- API 說明和 etcdctl 命令說明
etcd 是 CoreOS 團隊發起的一個開源項目(Go 語言,其實很多這類項目都是 Go 語言實現的,只能說很強大),實現了分布式鍵值存儲和服務發現,etcd 和 ZooKeeper/Consul 非常相似,都提供了類似的功能,以及 REST API 的訪問操作,具有以下特點:
- 簡單:安裝和使用簡單,提供了 REST API 進行操作交互
- 安全:支持 HTTPS SSL 證書
- 快速:支持并發 10 k/s 的讀寫操作
- 可靠:采用 raft 算法,實現分布式系統數據的可用性和一致性
etcd 可以單個實例使用,也可以進行集群配置,因為很多項目都是以 etcd 作為服務發現,比如 CoreOS 和 Kubernetes,所以,下面我們使用 Docker 簡單搭建一下 etcd 集群。
1. 主機安裝
如果不使用 Docker 的話,etcd 在主機上安裝,也非常簡單。
Linux 安裝命令:
$ curl -L https://github.com/coreos/etcd/releases/download/v3.3.0-rc.0/etcd-v3.3.0-rc.0-linux-amd64.tar.gz -o etcd-v3.3.0-rc.0-linux-amd64.tar.gz &&
sudo tar xzvf etcd-v3.3.0-rc.0-linux-amd64.tar.gz &&
cd etcd-v3.3.0-rc.0-linux-amd64 &&
sudo cp etcd* /usr/local/bin/
其實就是將編譯后的二進制文件,拷貝到/usr/local/bin/
目錄,各個版本的二進制文件,可以從 https://github.com/coreos/etcd/releases/ 中查找下載。
Mac OS 安裝命令:
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
$ brew install etcd
執行下面命令,查看 etcd 是否安裝成功:
$ etcd --version
etcd Version: 3.2.12
Git SHA: GitNotFound
Go Version: go1.9.2
Go OS/Arch: darwin/amd64
2. 集群搭建
搭建 etcd 集群,需要借助下 Docker Machine 創建三個 Docker 主機,命令:
$ docker-machine create -d virtualbox manager1 &&
docker-machine create -d virtualbox worker1 &&
docker-machine create -d virtualbox worker2
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
manager1 - virtualbox Running tcp://192.168.99.100:2376 v17.11.0-ce
worker1 - virtualbox Running tcp://192.168.99.101:2376 v17.11.0-ce
worker2 - virtualbox Running tcp://192.168.99.102:2376 v17.11.0-ce
為防止 Docker 主機中垃取官方鏡像,速度慢的問題,我們還需要將 etcd 鏡像打包推送到私有倉庫中,命令:
$ docker tag quay.io/coreos/etcd 192.168.99.1:5000/quay.io/coreos/etcd:latest &&
docker push 192.168.99.1:5000/quay.io/coreos/etcd:latest &&
docker pull 192.168.99.1:5000/quay.io/coreos/etcd:latest
另外,還需要將私有倉庫地址配置在 Docker 主機中,并重啟三個 Docker 主機,具體配置參考:Docker 三劍客之 Docker Swarm
Docker 主機配置好之后,我們需要使用docker-machine ssh
命令,分別進入三個 Docker 主機中,執行 Docker etcd 配置命令。
manager1 主機(node1 192.168.99.100
):
$ docker run -d --name etcd \
-p 2379:2379 \
-p 2380:2380 \
--volume=etcd-data:/etcd-data \
192.168.99.1:5000/quay.io/coreos/etcd \
/usr/local/bin/etcd \
--data-dir=/etcd-data --name node1 \
--initial-advertise-peer-urls http://192.168.99.100:2380 --listen-peer-urls http://0.0.0.0:2380 \
--advertise-client-urls http://192.168.99.100:2379 --listen-client-urls http://0.0.0.0:2379 \
--initial-cluster-state new \
--initial-cluster-token docker-etcd \
--initial-cluster node1=http://192.168.99.100:2380,node2=http://192.168.99.101:2380,node3=http://192.168.99.102:2380
worker1 主機(node2 192.168.99.101
):
$ docker run -d --name etcd \
-p 2379:2379 \
-p 2380:2380 \
--volume=etcd-data:/etcd-data \
192.168.99.1:5000/quay.io/coreos/etcd \
/usr/local/bin/etcd \
--data-dir=/etcd-data --name node2 \
--initial-advertise-peer-urls http://192.168.99.101:2380 --listen-peer-urls http://0.0.0.0:2380 \
--advertise-client-urls http://192.168.99.101:2379 --listen-client-urls http://0.0.0.0:2379 \
--initial-cluster-state new \
--initial-cluster-token docker-etcd \
--initial-cluster node1=http://192.168.99.100:2380,node2=http://192.168.99.101:2380,node3=http://192.168.99.102:2380
worker2 主機(node1 192.168.99.102
):
$ docker run -d --name etcd \
-p 2379:2379 \
-p 2380:2380 \
--volume=etcd-data:/etcd-data \
192.168.99.1:5000/quay.io/coreos/etcd \
/usr/local/bin/etcd \
--data-dir=/etcd-data --name node3 \
--initial-advertise-peer-urls http://192.168.99.102:2380 --listen-peer-urls http://0.0.0.0:2380 \
--advertise-client-urls http://192.168.99.102:2379 --listen-client-urls http://0.0.0.0:2379 \
--initial-cluster-state existing \
--initial-cluster-token docker-etcd \
--initial-cluster node1=http://192.168.99.100:2380,node2=http://192.168.99.101:2380,node3=http://192.168.99.102:2380
先來說明下 etcd 各個配置參數的意思(參考自 etcd 使用入門):
--name
:節點名稱,默認為 default。--data-dir
:服務運行數據保存的路徑,默認為${name}.etcd
。--snapshot-count
:指定有多少事務(transaction)被提交時,觸發截取快照保存到磁盤。--heartbeat-interval
:leader 多久發送一次心跳到 followers。默認值是 100ms。--eletion-timeout
:重新投票的超時時間,如果 follow 在該時間間隔沒有收到心跳包,會觸發重新投票,默認為 1000 ms。--listen-peer-urls
:和同伴通信的地址,比如http://ip:2380
,如果有多個,使用逗號分隔。需要所有節點都能夠訪問,所以不要使用 localhost!--listen-client-urls
:對外提供服務的地址:比如http://ip:2379,http://127.0.0.1:2379
,客戶端會連接到這里和 etcd 交互。--advertise-client-urls
:對外公告的該節點客戶端監聽地址,這個值會告訴集群中其他節點。--initial-advertise-peer-urls
:該節點同伴監聽地址,這個值會告訴集群中其他節點。--initial-cluster
:集群中所有節點的信息,格式為node1=http://ip1:2380,node2=http://ip2:2380,…
,注意:這里的 node1 是節點的 --name 指定的名字;后面的 ip1:2380 是 --initial-advertise-peer-urls 指定的值。--initial-cluster-state
:新建集群的時候,這個值為 new;假如已經存在的集群,這個值為 existing。--initial-cluster-token
:創建集群的 token,這個值每個集群保持唯一。這樣的話,如果你要重新創建集群,即使配置和之前一樣,也會再次生成新的集群和節點 uuid;否則會導致多個集群之間的沖突,造成未知的錯誤。
上述配置也可以設置配置文件,默認為/etc/etcd/etcd.conf
。
我們可以使用docker ps
,查看 Docker etcd 是否配置成功:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
463380d23dfe 192.168.99.1:5000/quay.io/coreos/etcd "/usr/local/bin/et..." 2 hours ago Up 2 hours 0.0.0.0:2379-2380->2379-2380/tcp etcd
然后進入其中一個 Docker 主機:
$ docker exec -it etcd bin/sh
執行下面命令(查看集群成員):
$ etcdctl member list
773d30c9fc6640b4: name=node2 peerURLs=http://192.168.99.101:2380 clientURLs=http://192.168.99.101:2379 isLeader=true
b2b0bca2e0cfcc19: name=node3 peerURLs=http://192.168.99.102:2380 clientURLs=http://192.168.99.102:2379 isLeader=false
c88e2cccbb287a01: name=node1 peerURLs=http://192.168.99.100:2380 clientURLs=http://192.168.99.100:2379 isLeader=false
可以看到,集群里面有三個成員,并且node2
為管理員,node1
和node3
為普通成員。
etcdctl 是 ectd 的客戶端命令工具(也是 go 語言實現),里面封裝了 etcd 的 REST API 執行命令,方便我們進行操作 etcd,后面再列出 etcdctl 的命令詳細說明。
上面命令的 etcd API 版本為 2.0,我們可以手動設置版本為 3.0,命令:
$ export ETCDCTL_API=3 && /usr/local/bin/etcdctl put foo bar
OK
部分命令和執行結果還是和 2.0 版本,有很多不同的,比如同是查看集群成員,3.0 版本的執行結果:
$ etcdctl member list
773d30c9fc6640b4, started, node2, http://192.168.99.101:2380, http://192.168.99.101:2379
b2b0bca2e0cfcc19, started, node3, http://192.168.99.102:2380, http://192.168.99.102:2379
c88e2cccbb287a01, started, node1, http://192.168.99.100:2380, http://192.168.99.100:2379
好了,我們現在再演示一種情況,就是從集群中移除一個節點,然后再把它添加到集群中,為演示 etcd 中使用 Raft 算法,我們將node2
管理節點,作為操作對象。
我們在隨便一個主機 etcd 容器中(node2
除外),執行成員移除集群命令(必須使用 ID,使用別名會報錯):
$ etcdctl member remove 773d30c9fc6640b4
Member 773d30c9fc6640b4 removed from cluster f84185fa5f91bdf6
我們再執行下查看集群成員命令(v2 版本):
$ etcdctl member list
b2b0bca2e0cfcc19: name=node3 peerURLs=http://192.168.99.102:2380 clientURLs=http://192.168.99.102:2379 isLeader=true
c88e2cccbb287a01: name=node1 peerURLs=http://192.168.99.100:2380 clientURLs=http://192.168.99.100:2379 isLeader=false
會發現node2
管理節點被移除集群了,并且通過 Raft 算法,node3
被推舉為管理節點。
在將node2
節點重新加入集群之前,我們需要執行下面命令:
$ etcdctl member add node2 --peer-urls="http://192.168.99.101:2380"
Member 22b0de6ffcd98f00 added to cluster f84185fa5f91bdf6
ETCD_NAME="node2"
ETCD_INITIAL_CLUSTER="node2=http://192.168.99.101:2380,node3=http://192.168.99.102:2380,node1=http://192.168.99.100:2380"
ETCD_INITIAL_CLUSTER_STATE="existing"
可以看到,ETCD_INITIAL_CLUSTER_STATE 值為existing
,也就是我們配置的--initial-cluster-state
參數。
我們再執行下查看集群成員命令(v2 版本):
$ etcdctl member list
22b0de6ffcd98f00[unstarted]: peerURLs=http://192.168.99.101:2380
b2b0bca2e0cfcc19: name=node3 peerURLs=http://192.168.99.102:2380 clientURLs=http://192.168.99.102:2379 isLeader=true
c88e2cccbb287a01: name=node1 peerURLs=http://192.168.99.100:2380 clientURLs=http://192.168.99.100:2379 isLeader=false
會發現22b0de6ffcd98f00
成員狀態變為了unstarted
。
我們在node2
節點,執行 Docker etcd 集群配置命令:
$ docker run -d --name etcd \
-p 2379:2379 \
-p 2380:2380 \
--volume=etcd-data:/etcd-data \
192.168.99.1:5000/quay.io/coreos/etcd \
/usr/local/bin/etcd \
--data-dir=/etcd-data --name node2 \
--initial-advertise-peer-urls http://192.168.99.101:2380 --listen-peer-urls http://0.0.0.0:2380 \
--advertise-client-urls http://192.168.99.101:2379 --listen-client-urls http://0.0.0.0:2379 \
--initial-cluster-state existing \
--initial-cluster-token docker-etcd \
--initial-cluster node1=http://192.168.99.100:2380,node2=http://192.168.99.101:2380,node3=http://192.168.99.102:2380
結果并不像我們想要的那樣成功,執行查看日志:
$ docker logs etcd
2017-12-25 08:19:30.160967 I | etcdmain: etcd Version: 3.2.12
2017-12-25 08:19:30.161062 I | etcdmain: Git SHA: b19dae0
2017-12-25 08:19:30.161082 I | etcdmain: Go Version: go1.8.5
2017-12-25 08:19:30.161092 I | etcdmain: Go OS/Arch: linux/amd64
2017-12-25 08:19:30.161105 I | etcdmain: setting maximum number of CPUs to 1, total number of available CPUs is 1
2017-12-25 08:19:30.161144 N | etcdmain: the server is already initialized as member before, starting as etcd member...
2017-12-25 08:19:30.161195 I | embed: listening for peers on http://0.0.0.0:2380
2017-12-25 08:19:30.161232 I | embed: listening for client requests on 0.0.0.0:2379
2017-12-25 08:19:30.165269 I | etcdserver: name = node2
2017-12-25 08:19:30.165317 I | etcdserver: data dir = /etcd-data
2017-12-25 08:19:30.165335 I | etcdserver: member dir = /etcd-data/member
2017-12-25 08:19:30.165347 I | etcdserver: heartbeat = 100ms
2017-12-25 08:19:30.165358 I | etcdserver: election = 1000ms
2017-12-25 08:19:30.165369 I | etcdserver: snapshot count = 100000
2017-12-25 08:19:30.165385 I | etcdserver: advertise client URLs = http://192.168.99.101:2379
2017-12-25 08:19:30.165593 I | etcdserver: restarting member 773d30c9fc6640b4 in cluster f84185fa5f91bdf6 at commit index 14
2017-12-25 08:19:30.165627 I | raft: 773d30c9fc6640b4 became follower at term 11
2017-12-25 08:19:30.165647 I | raft: newRaft 773d30c9fc6640b4 [peers: [], term: 11, commit: 14, applied: 0, lastindex: 14, lastterm: 11]
2017-12-25 08:19:30.169277 W | auth: simple token is not cryptographically signed
2017-12-25 08:19:30.170424 I | etcdserver: starting server... [version: 3.2.12, cluster version: to_be_decided]
2017-12-25 08:19:30.171732 I | etcdserver/membership: added member 773d30c9fc6640b4 [http://192.168.99.101:2380] to cluster f84185fa5f91bdf6
2017-12-25 08:19:30.171845 I | etcdserver/membership: added member c88e2cccbb287a01 [http://192.168.99.100:2380] to cluster f84185fa5f91bdf6
2017-12-25 08:19:30.171877 I | rafthttp: starting peer c88e2cccbb287a01...
2017-12-25 08:19:30.171902 I | rafthttp: started HTTP pipelining with peer c88e2cccbb287a01
2017-12-25 08:19:30.175264 I | rafthttp: started peer c88e2cccbb287a01
2017-12-25 08:19:30.175339 I | rafthttp: added peer c88e2cccbb287a01
2017-12-25 08:19:30.178326 I | etcdserver/membership: added member cbd7fa8d01297113 [http://192.168.99.102:2380] to cluster f84185fa5f91bdf6
2017-12-25 08:19:30.178383 I | rafthttp: starting peer cbd7fa8d01297113...
2017-12-25 08:19:30.178410 I | rafthttp: started HTTP pipelining with peer cbd7fa8d01297113
2017-12-25 08:19:30.179794 I | rafthttp: started peer cbd7fa8d01297113
2017-12-25 08:19:30.179835 I | rafthttp: added peer cbd7fa8d01297113
2017-12-25 08:19:30.180062 N | etcdserver/membership: set the initial cluster version to 3.0
2017-12-25 08:19:30.180132 I | etcdserver/api: enabled capabilities for version 3.0
2017-12-25 08:19:30.180255 N | etcdserver/membership: updated the cluster version from 3.0 to 3.2
2017-12-25 08:19:30.180430 I | etcdserver/api: enabled capabilities for version 3.2
2017-12-25 08:19:30.183979 I | rafthttp: started streaming with peer c88e2cccbb287a01 (writer)
2017-12-25 08:19:30.184139 I | rafthttp: started streaming with peer c88e2cccbb287a01 (writer)
2017-12-25 08:19:30.184232 I | rafthttp: started streaming with peer c88e2cccbb287a01 (stream MsgApp v2 reader)
2017-12-25 08:19:30.185142 I | rafthttp: started streaming with peer c88e2cccbb287a01 (stream Message reader)
2017-12-25 08:19:30.186518 I | etcdserver/membership: removed member cbd7fa8d01297113 from cluster f84185fa5f91bdf6
2017-12-25 08:19:30.186573 I | rafthttp: stopping peer cbd7fa8d01297113...
2017-12-25 08:19:30.186614 I | rafthttp: started streaming with peer cbd7fa8d01297113 (writer)
2017-12-25 08:19:30.186786 I | rafthttp: stopped streaming with peer cbd7fa8d01297113 (writer)
2017-12-25 08:19:30.186815 I | rafthttp: started streaming with peer cbd7fa8d01297113 (writer)
2017-12-25 08:19:30.186831 I | rafthttp: stopped streaming with peer cbd7fa8d01297113 (writer)
2017-12-25 08:19:30.186876 I | rafthttp: started streaming with peer cbd7fa8d01297113 (stream MsgApp v2 reader)
2017-12-25 08:19:30.187224 I | rafthttp: started streaming with peer cbd7fa8d01297113 (stream Message reader)
2017-12-25 08:19:30.187647 I | rafthttp: stopped HTTP pipelining with peer cbd7fa8d01297113
2017-12-25 08:19:30.187682 I | rafthttp: stopped streaming with peer cbd7fa8d01297113 (stream MsgApp v2 reader)
2017-12-25 08:19:30.187873 I | rafthttp: stopped streaming with peer cbd7fa8d01297113 (stream Message reader)
2017-12-25 08:19:30.187895 I | rafthttp: stopped peer cbd7fa8d01297113
2017-12-25 08:19:30.187911 I | rafthttp: removed peer cbd7fa8d01297113
2017-12-25 08:19:30.188034 I | etcdserver/membership: added member b2b0bca2e0cfcc19 [http://192.168.99.102:2380] to cluster f84185fa5f91bdf6
2017-12-25 08:19:30.188059 I | rafthttp: starting peer b2b0bca2e0cfcc19...
2017-12-25 08:19:30.188075 I | rafthttp: started HTTP pipelining with peer b2b0bca2e0cfcc19
2017-12-25 08:19:30.188510 I | rafthttp: started peer b2b0bca2e0cfcc19
2017-12-25 08:19:30.188533 I | rafthttp: added peer b2b0bca2e0cfcc19
2017-12-25 08:19:30.188795 I | etcdserver/membership: removed member 773d30c9fc6640b4 from cluster f84185fa5f91bdf6
2017-12-25 08:19:30.193643 I | rafthttp: started streaming with peer b2b0bca2e0cfcc19 (writer)
2017-12-25 08:19:30.193730 I | rafthttp: started streaming with peer b2b0bca2e0cfcc19 (writer)
2017-12-25 08:19:30.193797 I | rafthttp: started streaming with peer b2b0bca2e0cfcc19 (stream MsgApp v2 reader)
2017-12-25 08:19:30.194782 I | rafthttp: started streaming with peer b2b0bca2e0cfcc19 (stream Message reader)
2017-12-25 08:19:30.195663 I | raft: 773d30c9fc6640b4 [term: 11] received a MsgHeartbeat message with higher term from b2b0bca2e0cfcc19 [term: 12]
2017-12-25 08:19:30.195716 I | raft: 773d30c9fc6640b4 became follower at term 12
2017-12-25 08:19:30.195736 I | raft: raft.node: 773d30c9fc6640b4 elected leader b2b0bca2e0cfcc19 at term 12
2017-12-25 08:19:30.196617 E | rafthttp: streaming request ignored (ID mismatch got 22b0de6ffcd98f00 want 773d30c9fc6640b4)
2017-12-25 08:19:30.197064 E | rafthttp: streaming request ignored (ID mismatch got 22b0de6ffcd98f00 want 773d30c9fc6640b4)
2017-12-25 08:19:30.197846 E | rafthttp: streaming request ignored (ID mismatch got 22b0de6ffcd98f00 want 773d30c9fc6640b4)
2017-12-25 08:19:30.198242 E | rafthttp: streaming request ignored (ID mismatch got 22b0de6ffcd98f00 want 773d30c9fc6640b4)
2017-12-25 08:19:30.201771 E | etcdserver: the member has been permanently removed from the cluster
2017-12-25 08:19:30.202060 I | etcdserver: the data-dir used by this member must be removed.
2017-12-25 08:19:30.202307 E | etcdserver: publish error: etcdserver: request cancelled
2017-12-25 08:19:30.202338 I | etcdserver: aborting publish because server is stopped
2017-12-25 08:19:30.202364 I | rafthttp: stopping peer b2b0bca2e0cfcc19...
2017-12-25 08:19:30.202482 I | rafthttp: stopped streaming with peer b2b0bca2e0cfcc19 (writer)
2017-12-25 08:19:30.202504 I | rafthttp: stopped streaming with peer b2b0bca2e0cfcc19 (writer)
2017-12-25 08:19:30.204143 I | rafthttp: stopped HTTP pipelining with peer b2b0bca2e0cfcc19
2017-12-25 08:19:30.204186 I | rafthttp: stopped streaming with peer b2b0bca2e0cfcc19 (stream MsgApp v2 reader)
2017-12-25 08:19:30.204205 I | rafthttp: stopped streaming with peer b2b0bca2e0cfcc19 (stream Message reader)
2017-12-25 08:19:30.204217 I | rafthttp: stopped peer b2b0bca2e0cfcc19
2017-12-25 08:19:30.204228 I | rafthttp: stopping peer c88e2cccbb287a01...
2017-12-25 08:19:30.204241 I | rafthttp: stopped streaming with peer c88e2cccbb287a01 (writer)
2017-12-25 08:19:30.204255 I | rafthttp: stopped streaming with peer c88e2cccbb287a01 (writer)
2017-12-25 08:19:30.204824 I | rafthttp: stopped HTTP pipelining with peer c88e2cccbb287a01
2017-12-25 08:19:30.204860 I | rafthttp: stopped streaming with peer c88e2cccbb287a01 (stream MsgApp v2 reader)
2017-12-25 08:19:30.204878 I | rafthttp: stopped streaming with peer c88e2cccbb287a01 (stream Message reader)
2017-12-25 08:19:30.204891 I | rafthttp: stopped peer c88e2cccbb287a01
這么長的日志,說明啥問題呢,就是說我們雖然重新執行的 etcd 創建命令,但因為讀取之前配置文件的關系,etcd 會恢復之前的集群成員,但之前的集群節點已經被移除了,所以集群節點就一直處于停止狀態。
怎么解決呢?很簡單,就是將我們之前創建的etcd-data
數據卷軸刪掉,命令:
$ docker volume ls
DRIVER VOLUME NAME
local etcd-data
$ docker volume rm etcd-data
etcd-data
然后,再在node2
節點,重新執行 Docker etcd 集群配置命令(上面),會發現執行是成功的。
我們再執行下查看集群成員命令(v2 版本):
$ etcdctl member list
22b0de6ffcd98f00: name=node2 peerURLs=http://192.168.99.101:2380 clientURLs=http://192.168.99.101:2379 isLeader=false
b2b0bca2e0cfcc19: name=node3 peerURLs=http://192.168.99.102:2380 clientURLs=http://192.168.99.102:2379 isLeader=true
c88e2cccbb287a01: name=node1 peerURLs=http://192.168.99.100:2380 clientURLs=http://192.168.99.100:2379 isLeader=false
3. API 操作
etcd REST API 被用于鍵值操作和集群成員操作,這邊就簡單說幾個,詳細的 API 查看附錄說明。
1. 鍵值管理
設置鍵值命令:
$ curl http://127.0.0.1:2379/v2/keys/hello -XPUT -d value="hello world"
{"action":"set","node":{"key":"/hello","value":"hello world","modifiedIndex":17,"createdIndex":17}}
查看鍵值命令:
$ curl http://127.0.0.1:2379/v2/keys/hello
{"action":"get","node":{"key":"/hello","value":"hello world","modifiedIndex":17,"createdIndex":17}}
刪除鍵值命令:
$ curl http://127.0.0.1:2379/v2/keys/hello -XDELETE
{"action":"delete","node":{"key":"/hello","modifiedIndex":19,"createdIndex":17},"prevNode":{"key":"/hello","value":"hello world","modifiedIndex":17,"createdIndex":17}}
2. 成員管理
列出集群中的所有成員:
$ curl http://127.0.0.1:2379/v2/members
{"members":[{"id":"22b0de6ffcd98f00","name":"node2","peerURLs":["http://192.168.99.101:2380"],"clientURLs":["http://192.168.99.101:2379"]},{"id":"b2b0bca2e0cfcc19","name":"node3","peerURLs":["http://192.168.99.102:2380"],"clientURLs":["http://192.168.99.102:2379"]},{"id":"c88e2cccbb287a01","name":"node1","peerURLs":["http://192.168.99.100:2380"],"clientURLs":["http://192.168.99.100:2379"]}]}
查看當前節點是否為管理節點:
$ curl http://127.0.0.1:2379/v2/stats/leader
{"leader":"b2b0bca2e0cfcc19","followers":{"22b0de6ffcd98f00":{"latency":{"current":0.001051,"average":0.0029195000000000002,"standardDeviation":0.001646769458667484,"minimum":0.001051,"maximum":0.006367},"counts":{"fail":0,"success":10}},"c88e2cccbb287a01":{"latency":{"current":0.000868,"average":0.0022389999999999997,"standardDeviation":0.0011402923601720172,"minimum":0.000868,"maximum":0.004725},"counts":{"fail":0,"success":12}}}}
查看當前節點信息:
$ curl http://127.0.0.1:2379/v2/stats/self
{"name":"node3","id":"b2b0bca2e0cfcc19","state":"StateLeader","startTime":"2017-12-25T06:00:28.803429523Z","leaderInfo":{"leader":"b2b0bca2e0cfcc19","uptime":"36m45.45263851s","startTime":"2017-12-25T08:13:02.103896843Z"},"recvAppendRequestCnt":6,"sendAppendRequestCnt":22}
查看集群狀態:
$ curl http://127.0.0.1:2379/v2/stats/store
{"getsSuccess":9,"getsFail":4,"setsSuccess":9,"setsFail":0,"deleteSuccess":3,"deleteFail":0,"updateSuccess":0,"updateFail":0,"createSuccess":7,"createFail":0,"compareAndSwapSuccess":0,"compareAndSwapFail":0,"compareAndDeleteSuccess":0,"compareAndDeleteFail":0,"expireCount":0,"watchers":0}
當然也可以通過 API 添加和刪除集群成員。
4. API 說明和 etcdctl 命令說明
etcd REST API 說明(v2 版本):
更多 API 請查看:etcd API 和 Members API
etcdctl 命令說明:
命令 | 說明 |
---|---|
etcdctl set key value | 添加鍵值 |
etcdctl get key | 獲取鍵值 |
etcdctl update key value | 更新鍵值 |
etcdctl rm key | 刪除鍵值 |
etcdctl mkdir dirname | 添加目錄(不存在的話創建) |
etcdctl setdir | 添加目錄(都創建) |
etcdctl updatedir | 更新目錄 |
etcdctl rmdir | 刪除目錄 |
etcdctl ls | 列出目錄 |
etcdctl watch | 監控鍵值 |
etcdctl exec-watch | 監控鍵值(執行命令) |
etcdctl list | 查看集群成員 |
etcdctl member add | 添加集群成員 |
etcdctl remove | 移除集群成員 |
參考資料:
文章列表
留言列表