重慶高端網(wǎng)站開(kāi)發(fā)百度精準(zhǔn)獲客平臺(tái)
作者主頁(yè): 正函數(shù)的個(gè)人主頁(yè)
文章收錄專(zhuān)欄: Docker
歡迎大家點(diǎn)贊 👍 收藏 ? 加關(guān)注哦!
etcd
etcd
是 CoreOS
團(tuán)隊(duì)發(fā)起的一個(gè)管理配置信息和服務(wù)發(fā)現(xiàn)(Service Discovery
)的項(xiàng)目,在這一章里面,我們將基于 etcd 3.x
版本介紹該項(xiàng)目的目標(biāo),安裝和使用,以及實(shí)現(xiàn)的技術(shù)。
一、簡(jiǎn)介:什么是 etcd
etcd
是 CoreOS
團(tuán)隊(duì)于 2013 年 6 月發(fā)起的開(kāi)源項(xiàng)目,它的目標(biāo)是構(gòu)建一個(gè)高可用的分布式鍵值(key-value
)數(shù)據(jù)庫(kù),基于 Go
語(yǔ)言實(shí)現(xiàn)。我們知道,在分布式系統(tǒng)中,各種服務(wù)的配置信息的管理分享,服務(wù)的發(fā)現(xiàn)是一個(gè)很基本同時(shí)也是很重要的問(wèn)題。CoreOS
項(xiàng)目就希望基于 etcd
來(lái)解決這一問(wèn)題。
etcd
目前在 github.com/etcd-io/etcd 進(jìn)行維護(hù)。
受到 Apache ZooKeeper 項(xiàng)目和 doozer 項(xiàng)目的啟發(fā),etcd
在設(shè)計(jì)的時(shí)候重點(diǎn)考慮了下面四個(gè)要素:
-
簡(jiǎn)單:具有定義良好、面向用戶的
API
(gRPC) -
安全:支持
HTTPS
方式的訪問(wèn) -
快速:支持并發(fā)
10 k/s
的寫(xiě)操作 -
可靠:支持分布式結(jié)構(gòu),基于
Raft
的一致性算法
Apache ZooKeeper 是一套知名的分布式系統(tǒng)中進(jìn)行同步和一致性管理的工具。
doozer 是一個(gè)一致性分布式數(shù)據(jù)庫(kù)。
Raft 是一套通過(guò)選舉主節(jié)點(diǎn)來(lái)實(shí)現(xiàn)分布式系統(tǒng)一致性的算法,相比于大名鼎鼎的 Paxos 算法,它的過(guò)程更容易被人理解,由 Stanford 大學(xué)的 Diego Ongaro 和 John Ousterhout 提出。更多細(xì)節(jié)可以參考 raftconsensus.github.io。
一般情況下,用戶使用 etcd
可以在多個(gè)節(jié)點(diǎn)上啟動(dòng)多個(gè)實(shí)例,并添加它們?yōu)橐粋€(gè)集群。同一個(gè)集群中的 etcd
實(shí)例將會(huì)保持彼此信息的一致性。
二、安裝
etcd
基于 Go
語(yǔ)言實(shí)現(xiàn),因此,用戶可以從 項(xiàng)目主頁(yè) 下載源代碼自行編譯,也可以下載編譯好的二進(jìn)制文件,甚至直接使用制作好的 Docker
鏡像文件來(lái)體驗(yàn)。
注意:本章節(jié)內(nèi)容基于 etcd
3.4.x
版本
二進(jìn)制文件方式下載
編譯好的二進(jìn)制文件都在 github.com/etcd-io/etcd/releases 頁(yè)面,用戶可以選擇需要的版本,或通過(guò)下載工具下載。
例如,使用 curl
工具下載壓縮包,并解壓。
$ curl -L https://github.com/etcd-io/etcd/releases/download/v3.4.0/etcd-v3.4.0-linux-amd64.tar.gz -o etcd-v3.4.0-linux-amd64.tar.gz# 國(guó)內(nèi)用戶可以使用以下方式加快下載
$ curl -L https://download.fastgit.org/etcd-io/etcd/releases/download/v3.4.0/etcd-v3.4.0-linux-amd64.tar.gz -o etcd-v3.4.0-linux-amd64.tar.gz$ tar xzvf etcd-v3.4.0-linux-amd64.tar.gz
$ cd etcd-v3.4.0-linux-amd64
解壓后,可以看到文件包括
$ ls
Documentation README-etcdctl.md README.md READMEv2-etcdctl.md etcd etcdctl
其中 etcd
是服務(wù)主文件,etcdctl
是提供給用戶的命令客戶端,其他文件是支持文檔。
下面將 etcd
etcdctl
文件放到系統(tǒng)可執(zhí)行目錄(例如 /usr/local/bin/
)。
$ sudo cp etcd* /usr/local/bin/
默認(rèn) 2379
端口處理客戶端的請(qǐng)求,2380
端口用于集群各成員間的通信。啟動(dòng) etcd
顯示類(lèi)似如下的信息:
$ etcd
...
2017-12-03 11:18:34.411579 I | embed: listening for peers on http://localhost:2380
2017-12-03 11:18:34.411938 I | embed: listening for client requests on localhost:2379
此時(shí),可以使用 etcdctl
命令進(jìn)行測(cè)試,設(shè)置和獲取鍵值 testkey: "hello world"
,檢查 etcd
服務(wù)是否啟動(dòng)成功:
$ ETCDCTL_API=3 etcdctl member list
8e9e05c52164694d, started, default, http://localhost:2380, http://localhost:2379$ ETCDCTL_API=3 etcdctl put testkey "hello world"
OK$ etcdctl get testkey
testkey
hello world
說(shuō)明 etcd 服務(wù)已經(jīng)成功啟動(dòng)了。
Docker 鏡像方式運(yùn)行
鏡像名稱(chēng)為 quay.io/coreos/etcd
,可以通過(guò)下面的命令啟動(dòng) etcd
服務(wù)監(jiān)聽(tīng)到 2379
和 2380
端口。
$ docker run \
-p 2379:2379 \
-p 2380:2380 \
--mount type=bind,source=/tmp/etcd-data.tmp,destination=/etcd-data \
--name etcd-gcr-v3.4.0 \
quay.io/coreos/etcd:v3.4.0 \
/usr/local/bin/etcd \
--name s1 \
--data-dir /etcd-data \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://0.0.0.0:2379 \
--listen-peer-urls http://0.0.0.0:2380 \
--initial-advertise-peer-urls http://0.0.0.0:2380 \
--initial-cluster s1=http://0.0.0.0:2380 \
--initial-cluster-token tkn \
--initial-cluster-state new \
--log-level info \
--logger zap \
--log-outputs stderr
打開(kāi)新的終端按照上一步的方法測(cè)試 etcd
是否成功啟動(dòng)。
macOS 中運(yùn)行
$ brew install etcd$ etcd$ etcdctl member list
三、etcd 集群
下面我們使用 Docker Compose 模擬啟動(dòng)一個(gè) 3 節(jié)點(diǎn)的 etcd
集群。
編輯 docker-compose.yml
文件
version: "3.6"
services:node1:image: quay.io/coreos/etcd:v3.4.0volumes:- node1-data:/etcd-dataexpose:- 2379- 2380networks:cluster_net:ipv4_address: 172.16.238.100environment:- ETCDCTL_API=3command:- /usr/local/bin/etcd- --data-dir=/etcd-data- --name- node1- --initial-advertise-peer-urls- http://172.16.238.100:2380- --listen-peer-urls- http://0.0.0.0:2380- --advertise-client-urls- http://172.16.238.100:2379- --listen-client-urls- http://0.0.0.0:2379- --initial-cluster- node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380- --initial-cluster-state- new- --initial-cluster-token- docker-etcdnode2:image: quay.io/coreos/etcd:v3.4.0volumes:- node2-data:/etcd-datanetworks:cluster_net:ipv4_address: 172.16.238.101environment:- ETCDCTL_API=3expose:- 2379- 2380command:- /usr/local/bin/etcd- --data-dir=/etcd-data- --name- node2- --initial-advertise-peer-urls- http://172.16.238.101:2380- --listen-peer-urls- http://0.0.0.0:2380- --advertise-client-urls- http://172.16.238.101:2379- --listen-client-urls- http://0.0.0.0:2379- --initial-cluster- node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380- --initial-cluster-state- new- --initial-cluster-token- docker-etcdnode3:image: quay.io/coreos/etcd:v3.4.0volumes:- node3-data:/etcd-datanetworks:cluster_net:ipv4_address: 172.16.238.102environment:- ETCDCTL_API=3expose:- 2379- 2380command:- /usr/local/bin/etcd- --data-dir=/etcd-data- --name- node3- --initial-advertise-peer-urls- http://172.16.238.102:2380- --listen-peer-urls- http://0.0.0.0:2380- --advertise-client-urls- http://172.16.238.102:2379- --listen-client-urls- http://0.0.0.0:2379- --initial-cluster- node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380- --initial-cluster-state- new- --initial-cluster-token- docker-etcdvolumes:node1-data:node2-data:node3-data:networks:cluster_net:driver: bridgeipam:driver: defaultconfig:-subnet: 172.16.238.0/24
使用 docker-compose up
啟動(dòng)集群之后使用 docker exec
命令登錄到任一節(jié)點(diǎn)測(cè)試 etcd
集群。
/ # etcdctl member list
daf3fd52e3583ff, started, node3, http://172.16.238.102:2380, http://172.16.238.102:2379
422a74f03b622fef, started, node1, http://172.16.238.100:2380, http://172.16.238.100:2379
ed635d2a2dbef43d, started, node2, http://172.16.238.101:2380, http://172.16.238.101:2379
四、使用 etcdctl
etcdctl
是一個(gè)命令行客戶端,它能提供一些簡(jiǎn)潔的命令,供用戶直接跟 etcd
服務(wù)打交道,而無(wú)需基于 HTTP API
方式。這在某些情況下將很方便,例如用戶對(duì)服務(wù)進(jìn)行測(cè)試或者手動(dòng)修改數(shù)據(jù)庫(kù)內(nèi)容。我們也推薦在剛接觸 etcd
時(shí)通過(guò) etcdctl
命令來(lái)熟悉相關(guān)的操作,這些操作跟 HTTP API
實(shí)際上是對(duì)應(yīng)的。
etcd
項(xiàng)目二進(jìn)制發(fā)行包中已經(jīng)包含了 etcdctl
工具,沒(méi)有的話,可以從 github.com/etcd-io/etcd/releases 下載。
etcdctl
支持如下的命令,大體上分為數(shù)據(jù)庫(kù)操作和非數(shù)據(jù)庫(kù)操作兩類(lèi),后面將分別進(jìn)行解釋。
NAME:etcdctl - A simple command line client for etcd3.USAGE:etcdctlVERSION:3.4.0API VERSION:3.4COMMANDS:get Gets the key or a range of keysput Puts the given key into the storedel Removes the specified key or range of keys [key, range_end)txn Txn processes all the requests in one transactioncompaction Compacts the event history in etcdalarm disarm Disarms all alarmsalarm list Lists all alarmsdefrag Defragments the storage of the etcd members with given endpointsendpoint health Checks the healthiness of endpoints specified in `--endpoints` flagendpoint status Prints out the status of endpoints specified in `--endpoints` flagwatch Watches events stream on keys or prefixesversion Prints the version of etcdctllease grant Creates leaseslease revoke Revokes leaseslease timetolive Get lease informationlease keep-alive Keeps leases alive (renew)member add Adds a member into the clustermember remove Removes a member from the clustermember update Updates a member in the clustermember list Lists all members in the clustersnapshot save Stores an etcd node backend snapshot to a given filesnapshot restore Restores an etcd member snapshot to an etcd directorysnapshot status Gets backend snapshot status of a given filemake-mirror Makes a mirror at the destination etcd clustermigrate Migrates keys in a v2 store to a mvcc storelock Acquires a named lockelect Observes and participates in leader electionauth enable Enables authenticationauth disable Disables authenticationuser add Adds a new useruser delete Deletes a useruser get Gets detailed information of a useruser list Lists all usersuser passwd Changes password of useruser grant-role Grants a role to a useruser revoke-role Revokes a role from a userrole add Adds a new rolerole delete Deletes a rolerole get Gets detailed information of a rolerole list Lists all rolesrole grant-permission Grants a key to a rolerole revoke-permission Revokes a key from a rolecheck perf Check the performance of the etcd clusterhelp Help about any commandOPTIONS:--cacert="" verify certificates of TLS-enabled secure servers using this CA bundle--cert="" identify secure client using this TLS certificate file--command-timeout=5s timeout for short running command (excluding dial timeout)--debug[=false] enable client-side debug logging--dial-timeout=2s dial timeout for client connections--endpoints=[127.0.0.1:2379] gRPC endpoints--hex[=false] print byte strings as hex encoded strings--insecure-skip-tls-verify[=false] skip server certificate verification--insecure-transport[=true] disable transport security for client connections--key="" identify secure client using this TLS key file--user="" username[:password] for authentication (prompt if password is not supplied)-w, --write-out="simple" set the output format (fields, json, protobuf, simple, table)
數(shù)據(jù)庫(kù)操作
數(shù)據(jù)庫(kù)操作圍繞對(duì)鍵值和目錄的 CRUD (符合 REST 風(fēng)格的一套操作:Create)完整生命周期的管理。
etcd 在鍵的組織上采用了層次化的空間結(jié)構(gòu)(類(lèi)似于文件系統(tǒng)中目錄的概念),用戶指定的鍵可以為單獨(dú)的名字,如 testkey
,此時(shí)實(shí)際上放在根目錄 /
下面,也可以為指定目錄結(jié)構(gòu),如 cluster1/node2/testkey
,則將創(chuàng)建相應(yīng)的目錄結(jié)構(gòu)。
注:CRUD 即 Create, Read, Update, Delete,是符合 REST 風(fēng)格的一套 API 操作。
put
$ etcdctl put /testdir/testkey "Hello world"
OK
get
獲取指定鍵的值。例如
$ etcdctl put testkey hello
OK
$ etcdctl get testkey
testkey
hello
支持的選項(xiàng)為
--sort
對(duì)結(jié)果進(jìn)行排序
--consistent
將請(qǐng)求發(fā)給主節(jié)點(diǎn),保證獲取內(nèi)容的一致性
del
刪除某個(gè)鍵值。例如
$ etcdctl del testkey
1
非數(shù)據(jù)庫(kù)操作
watch
監(jiān)測(cè)一個(gè)鍵值的變化,一旦鍵值發(fā)生更新,就會(huì)輸出最新的值。
例如,用戶更新 testkey
鍵值為 Hello world
。
$ etcdctl watch testkey
PUT
testkey
2
member
通過(guò) list
、add
、update
、remove
命令列出、添加、更新、刪除 etcd 實(shí)例到 etcd 集群中。
例如本地啟動(dòng)一個(gè) etcd
服務(wù)實(shí)例后,可以用如下命令進(jìn)行查看。
$ etcdctl member list
422a74f03b622fef, started, node1, http://172.16.238.100:2380, http://172.16.238.100:23
作者主頁(yè): 正函數(shù)的個(gè)人主頁(yè)
文章收錄專(zhuān)欄: Docker
歡迎大家點(diǎn)贊 👍 收藏 ? 加關(guān)注哦!
如果你認(rèn)為這篇文章對(duì)你有幫助,請(qǐng)給正函數(shù)點(diǎn)個(gè)贊吧,如果發(fā)現(xiàn)什么問(wèn)題,歡迎評(píng)論區(qū)留言!!