Etcd 入門

Etcd 入門

Etcd3 是 CoreOS 於 2016年7月發佈的 etcd新版本

  • 分佈式 key-value 存儲
  • Go語言編寫,Raft一致性算法來管理高可用複製日誌

Linux 單機部署

安裝etcd

curl -L https://github.com/coreos/etcd/releases/download/v3.2.1/etcd-v3.2.1-linux-amd64.tar.gz -o etcd-v3.2.1-linux-amd64.tar.gz
tar xzvf etcd-v3.2.1-linux-amd64.tar.gz
mv etcd-v3.2.1-linux-amd64 etcd
cd etcd
./etcd --version
etcd Version: 3.2.1
Git SHA: 61fc123
Go Version: go1.8.3
Go OS/Arch: linux/amd64

配置環境變量 /etc/profile

# etcd
export PATH=/root/etcd:$PATH
export ETCDCTL_API=3

ETCDCTL_API API版本,記得source /etc/profile,默認版本2

etcd 目錄加入到環境變量,主要是爲了 etcd etcdctl 可以方便的執行這兩個命令

啓動程序,默認爲 127.0.0.1:2379

etcd

使用客戶端存/取,

因爲剛剛啓動的etcd 不是後臺運行的,我們需要維持shell,另外開一個窗口執行

# 存
$ etcdctl put name tian
OK

# 取
$ etcdctl get name
name
tian

單機多實例集羣

多實例集羣 我們可以使用 github上開源的構建工具,但是需要安裝Go語言環境。

Go語言環境安裝

https://blog.csdn.net/sunt2018/article/details/105416526

安裝完成後

go get github.com/mattn/goreman
goreman 
# 查看安裝是否成功

vim Procfile

https://github.com/etcd-io/etcd/blob/master/Procfile

bin/etcd 改成 etcd , 需要注意的是,去分支中,找到對應etcd的版本,在去複製Procfile,本次實驗用的3.2

etcd1: etcd --name infra1 --listen-client-urls http://127.0.0.1:12379 --advertise-client-urls http://127.0.0.1:12379 --listen-peer-urls http://127.0.0.1:12380 --initial-advertise-peer-urls http://127.0.0.1:12380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof

etcd2: etcd --name infra2 --listen-client-urls http://127.0.0.1:22379 --advertise-client-urls http://127.0.0.1:22379 --listen-peer-urls http://127.0.0.1:22380 --initial-advertise-peer-urls http://127.0.0.1:22380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof

etcd3: etcd --name infra3 --listen-client-urls http://127.0.0.1:32379 --advertise-client-urls http://127.0.0.1:32379 --listen-peer-urls http://127.0.0.1:32380 --initial-advertise-peer-urls http://127.0.0.1:32380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof

啓動

goreman -f Procfile start
# 查看成員
etcdctl --write-out=table --endpoints=localhost:12379 member list

# 添加數據
etcdctl --endpoints=localhost:12379 put foo bar

# 停止 或者用kill -9 殺掉
goreman run stop etcd2

# 向etcd1中 存入和獲取key
etcdctl --endpoints=localhost:12379 put key hello
etcdctl --endpoints=localhost:12379 get key

# 向etcd2 獲取key,超時,因爲我們關閉了
etcdctl --endpoints=localhost:22379 get key

# 重啓etcd2,再次獲取,發現數據已經同步了。
# 重啓 向本地8555 tcp 發送消息,推測啓動命令的master爲 8555,子進程爲etcd1/2/3
goreman run restart etcd2  
etcdctl --endpoints=localhost:22379 get key

etcd 交互

設置API版本

# 首先要設置我們的API版本
export ETCDCTL_API=3

寫入key

$ etcdctl put name zhangsaner
OK

讀取key

假設我們有key name1/2/3/4

(base) [root@10-9-127-85 ~]# etcdctl --endpoints=localhost:12379 put name1 liuda
OK
(base) [root@10-9-127-85 ~]# etcdctl --endpoints=localhost:12379 put name2 wanger
OK
(base) [root@10-9-127-85 ~]# etcdctl --endpoints=localhost:12379 put name3 zhangsan
OK
(base) [root@10-9-127-85 ~]# etcdctl --endpoints=localhost:12379 put name4 lisi
OK
# 獲取單個
etcdctl --endpoints=localhost:22379 get name1
etcdctl --endpoints=localhost:22379 get name

# 獲取區間, [name1,name4),不包含name4
etcdctl --endpoints=localhost:22379 get name name4

我們重啓啓動一下程序,修改Profile,將12379改爲2379,這樣就不需要指定–endpoints=localhost:22379了。

刪除key

etcdctl del name

補充

可以使用 /a/b/c/ 來模擬目錄結構
租期續約
https://www.jianshu.com/p/b788c3271846
# 讀取歷史版本 沒有搞懂,實驗不成功

讀取 key 過往版本的值
應用可能想讀取 key 的被替代的值。例如,應用可能想通過訪問 key 的過往版本來回滾到舊的配置。或者,應用可能想通過訪問 key 歷史記錄的多個請求來得到一個覆蓋多個 key 上的統一視圖。

因爲 etcd 集羣上鍵值存儲的每個修改都會增加 etcd 集羣的全局修訂版本,應用可以通過提供舊有的 etcd 版本來讀取被替代的 key。

假設 etcd 集羣已經有下列 key:

$ etcdctl put foo bar         # revision = 2
$ etcdctl put foo1 bar1       # revision = 3
$ etcdctl put foo bar_new     # revision = 4
$ etcdctl put foo1 bar1_new   # revision = 5


$ etcdctl get foo foo9 # 訪問 key 的最新版本
foo
bar_new
foo1
bar1_new
$ etcdctl get --rev=4 foo foo9 # 訪問 key 的修訂版本4
foo
bar_new
foo1
bar1
$ etcdctl get --rev=3 foo foo9 # 訪問 key 的修訂版本3
foo
bar
foo1
bar1
$ etcdctl get --rev=2 foo foo9 # 訪問 key 的修訂版本2
foo
bar
$ etcdctl get --rev=1 foo foo9 # 訪問 key 的修訂版本1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章