1. 程式人生 > >服務發現系統etcd介紹

服務發現系統etcd介紹

etcd的特性如下:

  • 簡單: 支援curl方式的使用者API(HTTP+JSON)
  • 安全: 可選的SSL客戶端證書認證
  • 快速: 單例項每秒 1000 次寫操作
  • 可靠: 使用Raft保證一致性

二、安裝和使用

etcd的安裝非常簡單,可以直接下載編譯後的可執行檔案。

wget https://github.com/coreos/etcd/releases/download/v3.0.12/etcd-v3.0.12-linux-amd64.tar.gz
tar xzvf etcd-v3.0.12-linux-amd64.tar.gz
./etcd --version

啟動方式:./etcd 

duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcd
2019-07-06 04:05:14.961448 I | etcdmain: etcd Version: 3.0.12
2019-07-06 04:05:14.961795 I | etcdmain: Git SHA: 2d1e2e8
2019-07-06 04:05:14.962112 I | etcdmain: Go Version: go1.6.3
2019-07-06 04:05:14.962427 I | etcdmain: Go OS/Arch: linux/amd64
2019-07-06 04:05:14.962808 I | etcdmain: setting maximum number of CPUs to 1, total number of available CPUs is 1
2019-07-06 04:05:14.963079 W | etcdmain: no data-dir provided, using default data-dir ./default.etcd
2019-07-06 04:05:14.964292 I | etcdmain: listening for peers on http://localhost:2380
2019-07-06 04:05:14.964866 I | etcdmain: listening for client requests on localhost:2379
2019-07-06 04:05:14.972043 I | etcdserver: name = default
2019-07-06 04:05:14.972560 I | etcdserver: data dir = default.etcd
2019-07-06 04:05:14.973497 I | etcdserver: member dir = default.etcd/member
2019-07-06 04:05:14.974055 I | etcdserver: heartbeat = 100ms
2019-07-06 04:05:14.974406 I | etcdserver: election = 1000ms
2019-07-06 04:05:14.974802 I | etcdserver: snapshot count = 10000
2019-07-06 04:05:14.975163 I | etcdserver: advertise client URLs = http://localhost:2379
2019-07-06 04:05:14.975658 I | etcdserver: initial advertise peer URLs = http://localhost:2380
2019-07-06 04:05:14.975985 I | etcdserver: initial cluster = default=http://localhost:2380
2019-07-06 04:05:14.987525 I | etcdserver: starting member 8e9e05c52164694d in cluster cdf818194e3a8c32
2019-07-06 04:05:14.988349 I | raft: 8e9e05c52164694d became follower at term 0
2019-07-06 04:05:14.988796 I | raft: newRaft 8e9e05c52164694d [peers: [], term: 0, commit: 0, applied: 0, lastindex: 0, lastterm: 0]
2019-07-06 04:05:14.989133 I | raft: 8e9e05c52164694d became follower at term 1
2019-07-06 04:05:14.993798 I | etcdserver: starting server... [version: 3.0.12, cluster version: to_be_decided]
2019-07-06 04:05:15.018325 I | membership: added member 8e9e05c52164694d [http://localhost:2380] to cluster cdf818194e3a8c32
2019-07-06 04:05:15.191979 I | raft: 8e9e05c52164694d is starting a new election at term 1
2019-07-06 04:05:15.192994 I | raft: 8e9e05c52164694d became candidate at term 2
2019-07-06 04:05:15.193820 I | raft: 8e9e05c52164694d received vote from 8e9e05c52164694d at term 2
2019-07-06 04:05:15.194712 I | raft: 8e9e05c52164694d became leader at term 2
2019-07-06 04:05:15.195595 I | raft: raft.node: 8e9e05c52164694d elected leader 8e9e05c52164694d at term 2
2019-07-06 04:05:15.197315 I | etcdserver: setting up the initial cluster version to 3.0
2019-07-06 04:05:15.199185 N | membership: set the initial cluster version to 3.0
2019-07-06 04:05:15.199995 I | api: enabled capabilities for version 3.0
2019-07-06 04:05:15.200486 I | etcdmain: ready to serve client requests
2019-07-06 04:05:15.205889 N | etcdmain: serving insecure client requests on localhost:2379, this is strongly discouraged!
2019-07-06 04:05:15.212655 I | etcdserver: published {Name:default ClientURLs:[http://localhost:2379]} to cluster cdf818194e3a8c32
2019-07-06 04:05:15.214893 E | etcdmain: forgot to set Type=notify in systemd service file?

從上面的輸出中,我們可以看到很多資訊。以下是幾個比較重要的資訊:

2019-07-06 04:05:14.972043 I | etcdserver: name = default

name表示節點名稱,預設為default。

2019-07-06 04:05:14.972560 I | etcdserver: data dir = default.etcd

data-dir 儲存日誌和快照的目錄,預設為當前工作目錄default.etcd/目錄下。

2019-07-06 04:05:14.974055 I | etcdserver: heartbeat = 100ms

heartbeat為100ms,該引數的作用是leader多久傳送一次心跳到followers,預設值是100ms。

2019-07-06 04:05:14.974406 I | etcdserver: election = 1000ms

election為1000ms,該引數的作用是重新投票的超時時間,如果follow在該時間間隔沒有收到心跳包,會觸發重新投票,預設為1000ms。

2019-07-06 04:05:14.974802 I | etcdserver: snapshot count = 10000

snapshot count為10000,該引數的作用是指定有多少事務被提交時,觸發擷取快照儲存到磁碟。 

2019-07-06 04:05:14.975163 I | etcdserver: advertise client URLs = http://localhost:2379

http://localhost:2379提供HTTP API服務,供客戶端互動。 

2019-07-06 04:05:14.975985 I | etcdserver: initial cluster = default=http://localhost:2380

http://localhost:2380和叢集中其他節點通訊。

叢集和每個節點都會生成一個uuid。 
啟動的時候會執行raft,選舉出leader。

採用這種方式啟動的etcd只是一個程式,如果啟動etcd的視窗被關閉的話則etcd便會被關閉 
,所以如果要長期使用的話最好是為etcd開啟一個服務,此處便不提供開啟服務的方法,如果有需要讀者可以自行百度。

etcd命令列介面使用

./etcdctl -h 可以檢視用法

duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl -h

etcd在鍵的組織上採用了層次化的空間結構(類似於檔案系統中目錄的概念),使用者指定的鍵可以為單獨的名字,如:testkey,此時實際上放在根目錄/下面,也可以為指定目錄結構,如/cluster1/node2/testkey,則將建立相應的目錄結構。

set get

set 
指定某個鍵的值。 
-ttl ‘0’ 該鍵值的超時時間(單位為秒),不配置(預設為0)則永不超時 
–swap-with-value value 若該鍵現在的值是value,則進行設定操作 
–swap-with-index ‘0’ 若該鍵現在的索引值是指定索引,則進行設定操作

get 
獲取指定鍵的值
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl set --ttl '5' key_1 "hello world"
hello world
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl get key_1
hello world
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl get key_1
Error:  100: Key not found (/key_1) [6]

上面第二個get方法在是在5秒之後請求的,已經請求不到key了,因為已經超時。

update

update 
對指定鍵進行修改 
–ttl ‘0’ 超時時間(單位為秒),不配置(預設為 0)則永不超時。
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl set --ttl '5' key_1 "hello world"
hello world
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl update key_1 "hello world 2"
hello world 2
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl get key_1
hello world 2

rm

rm 
刪除某個鍵值。 
–dir 如果鍵是個空目錄或者鍵值對則刪除 
–recursive 刪除目錄和所有子鍵 
–with-value 檢查現有的值是否匹配 
–with-index ‘0’檢查現有的index是否匹配
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl set --ttl '5' key_1 "hello world"
hello world
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl rm key_1
PrevNode.Value: hello world
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl get key_1
Error:  100: Key not found (/key_1) [18]

mk

mk 
如果給定的鍵不存在,則建立一個新的鍵值。 
–ttl ‘0’ 超時時間(單位為秒),不配置(預設為 0)。則永不超時
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl mk /testdir/testkey "hello world"
hello world
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl mk /testdir/testkey "hello world"
Error:  105: Key already exists (/testdir/testkey) [20]

mkdir

mkdir 
–ttl ‘0’ 超時時間(單位為秒),不配置(預設為0)則永不超時。 
如果給定的鍵目錄不存在,則建立一個新的鍵目錄。
如果給定的鍵目錄存在,則報錯
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl mkdir dir2
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl mkdir dir2
Error:  105: Key already exists (/dir2) [21]

setdir

setdir 
建立一個鍵目錄。如果目錄不存在就建立,如果目錄存在更新目錄TTL。 
–ttl ‘0’ 超時時間(單位為秒),不配置(預設為0)則永不超時。
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl setdir dir3

rmdir

rmdir 
刪除一個空目錄,或者鍵值對。

 

duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl rmdir dir3

ls

ls 
列出目錄(預設為根目錄)下的鍵或者子目錄,預設不顯示子目錄中內容。 
–sort 將輸出結果排序 
–recursive 如果目錄下有子目錄,則遞迴輸出其中的內容 
-p 對於輸出為目錄,在最後新增/進行區分
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl ls
/message
/test
/testdir
/dir2

非資料庫操作,非資料庫操作包括:備份、監測、節點管理等

backup

backup 
備份etcd的資料。 
–data-dir etcd的資料目錄 
–backup-dir 備份到指定路徑
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl backup --data-dir default.etcd/ --backup-dir ~/

watch

watch 
監測一個鍵值的變化,一旦鍵值發生更新,就會輸出最新的值並退出。 
–forever 一直監測直到使用者按CTRL+C退出 
–after-index ‘0’ 在指定index之前一直監測 
–recursive 返回所有的鍵值和子鍵值

先設定mykey="hello world",然後監測mykey

duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl set mykey "Hello world"
Hello world
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl watch mykey

在另一個視窗中修改mykey的值:

duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl update mykey "test"
test

之前的監控介面便打印出test,然後退出:

duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl watch mykey
test

exec-watch 

exec-watch 
監測一個鍵值的變化,一旦鍵值發生更新,就執行給定命令。 
–after-index ‘0’ 在指定 index 之前一直監測 
–recursive 返回所有的鍵值和子鍵值
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl exec-watch  mykey -- sh -c "ls"

當我在另一個視窗更新mykey時,輸出:

Documentation	   README.md		default.etcd	   etcd     nohup.out
README-etcdctl.md  READMEv2-etcdctl.md	docker-node1.etcd  etcdctl

member

member 
list 列出etcd例項 
add 新增etcd例項 
remove 刪除etcd例項

檢視列表: 

duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl member list
8e9e05c52164694d: name=default peerURLs=http://localhost:2380 clientURLs=http://localhost:2379 isLeader=true

刪除節點:

duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl member remove 8e9e05c52164694d
Removed member 8e9e05c52164694d from cluster
duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl member list
Error:  client: etcd cluster is unavailable or misconfigured
error #0: dial tcp 127.0.0.1:2379: getsockopt: connection refused
error #1: dial tcp 127.0.0.1:4001: getsockopt: connection refused

向叢集中新加節點,例如:

duandingyang@ubuntu:~/etcd-v3.0.12-linux-amd64$ ./etcdctl member add etcd2  http://localhost:2380