Docker 搭建 etcd 集群

来源:https://www.cnblogs.com/xishuai/archive/2017/12/26/docker-etcd.html
-Advertisement-
Play Games

閱讀目錄: 主機安裝 集群搭建 API 操作 API 說明和 etcdctl 命令說明 "etcd" 是 CoreOS 團隊發起的一個開源項目(Go 語言,其實很多這類項目都是 Go 語言實現的,只能說很強大),實現了 分散式鍵值存儲 和 服務發現 ,etcd 和 ZooKeeper/Consul ...


閱讀目錄:

  • 主機安裝
  • 集群搭建
  • 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為管理員,node1node3為普通成員。

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 版本):

命令 說明
curl -L http://127.0.0.1:2379/version 查看版本
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 獲取鍵值
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 刪除鍵值
curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=bar -d ttl=5 添加 TTL 鍵值(過期時間)
curl http://127.0.0.1:2379/v2/keys/foo 獲取 TTL 鍵值
curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=bar -d ttl= -d prevExist=true 更新 TTL 鍵值
curl http://127.0.0.1:2379/v2/keys/foo?wait=true
curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=bar
curl 'http://127.0.0.1:2379/v2/keys/foo?wait=true&waitIndex=7'
curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job1
curl -s 'http://127.0.0.1:2379/v2/keys/queue?recursive=true&sorted=true'
curl http://127.0.0.1:2379/v2/keys/dir -XPUT -d ttl=30 -d dir=true
curl http://127.0.0.1:2379/v2/keys/dir -XPUT -d ttl=30 -d dir=true -d prevExist=true
curl 'http://127.0.0.1:2379/v2/keys/dir?wait=true'
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/foo -XPUT -d value=bar 在目錄下添加鍵值
curl http://127.0.0.1:2379/v2/keys/?recursive=true 獲取目錄下的鍵值
curl 'http://127.0.0.1:2379/v2/keys/foo_dir?dir=true' -XDELETE 刪除目錄
curl http://10.0.0.10:2379/v2/members 查看集群成員
curl http://10.0.0.10:2379/v2/members -XPOST -H "Content-Type: application/json" -d '{"peerURLs":["http://10.0.0.10:2380"]}' 添加集群成員
curl http://10.0.0.10:2379/v2/members/272e204152 -XDELETE 刪除集群成員
curl http://10.0.0.10:2379/v2/members/272e204152 -XPUT -H "Content-Type: application/json" -d '{"peerURLs":["http://10.0.0.10:2380"]}' 更新集群成員

更多 API 請查看:etcd APIMembers 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 移除集群成員

參考資料:


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 代碼: 方案一: div絕對定位水平垂直居中【margin:auto實現絕對定位元素的居中】, 相容性:,IE7及之前版本不支持 .father{ width:400px; height:400px; background: red; position:relative; /* 或者position ...
  • 最近剛剛用vue寫了個公司項目,使用vue-cli構建的,算是中大型項目吧,然後這裡想記錄並且分享一下其中的知識點,希望對大家有幫助,後期會逐漸分享;話不多說,直接上代碼!! app.vue main.js router文件夾裡面的index.js home.vue pagevue.vue next ...
  • 一、前端MVC概要 1.1、庫與框架的區別 框架是一個軟體的半成品,在全局範圍內給了大的約束。庫是工具,在單點上給我們提供功能。框架是依賴庫的。Vue是框架而jQuery則是庫。 1.2、AMD與CMD 在傳統的非模塊化JavaScript開發中有許多問題:命名衝突、文件依賴、跨環境共用模塊、性能優 ...
  • 閑話不多說,開篇擼代碼,你可以會看到類似如下的結構: See the Pen react props by 糊一笑 (@rynxiao) on CodePen. 當一個組件嵌套了若幹層子組件時,而想要在特定的組件中取得父組件的屬性,就不得不將 一層一層地往下傳,我這裡只是簡單的列舉了3個子組件,而當 ...
  • 1、示例代碼 (註:寫到vue單文件中了) 2、說明 (1)需要transition 標簽包裹。 (2)6個class狀態 (3)效果: ...
  • 本節流程如圖: 現在正式進入打包流程,起步方法為run: 為什麼不介紹compiler對象?因為構造函數中並沒有一個初始化的方法,只是普通的變數聲明,沒啥好講的。 在run方法中,首先是調用了tapable的applyPluginsAsync執行了before-run事件流,該事件流的定義地點如下: ...
  • 例如: !function(){alert('ok')}(); 常見格式:(function() { /* code */ })(); //也等同於此 匿名函數 解釋:包圍函數(function(){})的第一對括弧向腳本返回未命名的函數,隨後一對空括弧立即執行返回的未命名函數,括弧內為匿名函數的參 ...
  • 需求說明:簡單演示ajax提交fromData類型數據。 代碼說明:ajax在傳輸數據的時候基本格式大都是固定的,只需要修改傳輸類型即可。下麵介紹基本的參數提交。 步驟一:建立html或者jsp頁面,引入jquery-3.2.1.min.js(其他版本亦可)。 步驟二:建立文件選擇輸入框,上傳按鈕並 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...