Etcd 使用

Etcd 簡(jiǎn)介

etcd 是一個(gè)高可用的鍵值存儲(chǔ)系統(tǒng),主要用于共享配置和服務(wù)發(fā)現(xiàn)。etcd是由CoreOS開發(fā)并維護(hù)的,靈感來自于 ZooKeeper 和 Doozer,它使用Go語言編寫,并通過Raft一致性算法處理日志復(fù)制以保證強(qiáng)一致性。Raft是一個(gè)來自Stanford的新的一致性算法,適用于分布式系統(tǒng)的日志復(fù)制,Raft通過選舉的方式來實(shí)現(xiàn)一致性,在Raft中,任何一個(gè)節(jié)點(diǎn)都可能成為Leader。Google的容器集群管理系統(tǒng)Kubernetes、開源PaaS平臺(tái)Cloud Foundry和CoreOS的Fleet都廣泛使用了etcd。

etcd 集群的工作原理基于 raft 共識(shí)算法 (The Raft Consensus Algorithm)。
etcd 在 0.5.0 版本中重新實(shí)現(xiàn)了 raft 算法,而非像之前那樣依賴于第三方庫 go-raft 。
raft 共識(shí)算法的優(yōu)點(diǎn)在于可以在高效的解決分布式系統(tǒng)中各個(gè)節(jié)點(diǎn)日志內(nèi)容一致性問題的同時(shí),也使得集群具備一定的容錯(cuò)能力。即使集群中出現(xiàn)部分節(jié)點(diǎn)故障、網(wǎng)絡(luò)故障等問題,仍可保證其余大多數(shù)節(jié)點(diǎn)正確的步進(jìn)。甚至當(dāng)更多的節(jié)點(diǎn)(一般來說超過集群節(jié)點(diǎn)總數(shù)的一半)出現(xiàn)故障而導(dǎo)致集群不可用時(shí),依然可以保證節(jié)點(diǎn)中的數(shù)據(jù)不會(huì)出現(xiàn)錯(cuò)誤的結(jié)果。

1. Getting etcd

從 Release 頁面(https://github.com/coreos/etcd/releases/ )下載二進(jìn)制,或者拉最新 master 代碼進(jìn)行編譯。

$ git clone https://github.com/coreos/etcd.git
$ cd etcd
$ ./build

還有一種辦法,直接獲取一個(gè) vendor 編譯好的 etcd,如下:

$ go get github.com/coreos/etcd/cmd/etcd

2. Starting etcd

直接執(zhí)行

$ etcd

或者指定 data-dir

$ etcd -data-dir=/var/edata

更加詳細(xì)的配置選項(xiàng),請(qǐng)查看:https://coreos.com/etcd/docs/latest/op-guide/configuration.html

一個(gè)簡(jiǎn)單例子 human readable:

# This config is meant to be consumed by the config transpiler, which will
# generate the corresponding Ignition config. Do not pass this config directly
# to instances of Container Linux.

etcd:
  name:                        my-etcd-1
  listen_client_urls:          https://10.240.0.1:2379
  advertise_client_urls:       https://10.240.0.1:2379
  listen_peer_urls:            https://10.240.0.1:2380
  initial_advertise_peer_urls: https://10.240.0.1:2380
  initial_cluster:             my-1=https://10.240.0.1:2380,my-2=https://10.240.0.2:2380,my-3=https://10.240.0.3:2380
  initial_cluster_token:       my-etcd-token
  initial_cluster_state:       new

參考:
https://coreos.com/etcd/docs/latest/getting-started-with-etcd.html
https://docs.portworx.com/run-etcd.html

3. etcd cluster

etcd --name infra0 --initial-advertise-peer-urls http://10.0.1.10:2380 \
  --listen-peer-urls http://10.0.1.10:2380 \
  --listen-client-urls http://10.0.1.10:2379,http://127.0.0.1:2379 \
  --advertise-client-urls http://10.0.1.10:2379 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380,infra2=http://10.0.1.12:2380 \
  --initial-cluster-state new

參考:
https://coreos.com/etcd/docs/latest/v2/docker_guide.html#running-etcd-in-standalone-mode
https://coreos.com/etcd/docs/latest/op-guide/clustering.html

4. etcdctl 查看幫助

上述啟動(dòng)的 etcd 是服務(wù)器,官方還提供了一個(gè) commandline 客戶端,即 etcdctl,所有的命令都通過這個(gè)客戶端去請(qǐng)求。
執(zhí)行下列命令來查看幫助信息:

$ ETCDCTL_API=3 etcdctl help

想要查看某個(gè)具體 command 的幫助,比如查看 get 命令的幫助:

$ ETCDCTL_API=3 etcdctl help get

NAME:
        get - Gets the key or a range of keys
USAGE:
        etcdctl get [options] <key> [range_end]
OPTIONS:
      --consistency="l"                 Linearizable(l) or Serializable(s)
      --from-key[=false]                Get keys that are greater than or equal to the given key using byte compare
      --keys-only[=false]               Get only the keys
      --limit=0                         Maximum number of results
      --order=""                        Order of results; ASCEND or DESCEND (ASCEND by default)
      --prefix[=false]                  Get keys with matching prefix
      --print-value-only[=false]        Only write values when using the "simple" output format
      --rev=0                           Specify the kv revision
      --sort-by=""                      Sort target; CREATE, KEY, MODIFY, VALUE, or VERSION
GLOBAL OPTIONS:
      --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)

5. Command examples

假設(shè)已有如下 keys

foo = bar
foo1 = bar1
foo2 = bar2
foo3 = bar3

默認(rèn) etcdctl 會(huì)key 和 value 都打印,想要只打印 value 的值:

$ etcdctl get foo --print-value-only
bar

獲取所有 prefix 為 foo 的 key 所對(duì)應(yīng)的 value

$ etcdctl get --prefix foo
foo
bar
foo1
bar1
foo2
bar2
foo3
bar3

更多命令:
https://coreos.com/etcd/docs/latest/dev-guide/interacting_v3.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容