etcd的學習心得和使用
1 etcd key-value 存儲結構
“A highly-available key value store for shared configuration and service discovery.”
Etcd是coreos開發的分布式服務系統,內部采用raft協議作為一致性算法
raft通過選舉的方式來實現一致性,在Raft中,任何一個節點都可能成為Leader
Etcd是一個高可用的 Key/Value 存儲系統,主要用於分享配置和服務發現。
● 簡單:支持 curl 方式的用戶 API (HTTP+JSON)
Etcd構建自身高可用集群主要有三種形式:
1)靜態發現: 預先已知 Etcd 集群中有哪些節點,在啟動時直接指定好Etcd的各個node節點地址
2)Etcd動態發現: 通過已有的Etcd集群作為數據交互點,然後在擴展新的集群時實現通過已有集群進行服務發現的機制
3)DNS動態發現: 通過DNS查詢方式獲取其他節點地址信息
2 下載地址
https://github.com/coreos/etcd/releases/
################### 演示etcd單機的使用
3. 安裝(解壓即可使用)
cd /usr/local/src/
wget https://github.com/coreos/etcd/releases/download/v3.2.5/etcd-v3.2.5-linux-amd64.tar.gz
tar xvf etcd-v3.2.5-linux-amd64.tar.gz
cp -r etcd-v3.2.5-linux-amd64 /usr/local/etcd #將軟件放置到常用目錄下
4.創建配置文件和數據目錄
cd /usr/local/etcd/
touch etcd.conf
mkdir data
## 將啟動文件和命令管理文件拷貝到 PATH找到的路徑中
cp etcd /usr/local/bin
cp etcdctl /usr/local/bin
5. 編輯配置文件
name: ops-cuidehua001 data-dir: "/usr/local/etcd/data" #監聽URL,用於與其他節點通訊 listen-peer-urls: "http://10.59.87.121:2380" # list of URLs to listen on for client traffic listen-client-urls: "http://10.59.87.121:2379,http://127.0.0.1:2379" #list of this member‘s client URLs to advertise to the public advertise-client-urls: "http://10.59.87.121:2379"
listen-peer-urls、listen-client-urls、advertise-client-urls的區別?
6、啟動
etcd --config-file=/usr/local/etcd/etcd.conf &
7、檢查是否啟動
ps -ef |grep etcd
或者
[[email protected] etcd]# etcdctl cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://10.59.87.121:2379
cluster is healthy
生成的了數據目錄
[[email protected] etcd]# ls data/member/
snap wal
8、常用命令操作
#查看版本
etcdctl -version 或者 etcd -version
[[email protected] etcd]# etcdctl -v
etcdctl version: 3.2.5
API version: 2
(可以查看到命令的版本和api接口的版本)
#查看檢查狀況
etcdctl cluster-health
##etcd主要是做存儲k-v的作用,所以經常對其數據進行操作(即etcd的操作 增刪改查)
數據庫操作圍繞對鍵值和目錄的 CRUD
(符合 REST 風格的一套操作:Create
)完整生命周期的管理。
etcd 在鍵的組織上采用了層次化的空間結構(類似於文件系統中目錄的概念),用戶指定的鍵可以為單獨的名字,如 testkey
,此時實際上放在根目錄 /
下面,也可以為指定目錄結構,如 cluster1/node2/testkey
,則將創建相應的目錄結構。
註:CRUD 即 Create, Read, Update, Delete,是符合 REST 風格的一套 API 操作。
#################### ##數據庫操作 #################
##set 指定某個鍵的值
etcdctl set /testdir/testkey "Hello world"
會創建目錄/testdir 和設定這個目錄下的testkey的value值為"Hello world"
##查看目錄結構,默認是查看根目錄下
[[email protected] etcd]# etcdctl ls
/testdir
[[email protected] etcd]# etcdctl ls /testdir
/testdir/testkey
##get 獲取某個key的value
[[email protected] etcd]# etcdctl get /testdir/testkey
Hello world
#不支持則會報錯,且key不支持通配符
[[email protected] etcd]# etcdctl get /testdir/testkey2
Error: 100: Key not found (/testdir/testkey2) [5]
##update更新建的值,key不存在時會報錯
etcdctl update /testdir/testkey "hello"
##rm刪除某個鍵
etcdctl rm /testdir/testkey
[[email protected] etcd]# etcdctl rm /testdir/testkey
PrevNode.Value: hello
[[email protected] etcd]# etcdctl get /testdir/testkey
Error: 100: Key not found (/testdir/testkey) [7]
當刪除某個目錄時
etcdctl rm -h 查看命令幫助
etcdctl rm -r
[[email protected] etcd]# etcdctl rm -r /testdir
## 創建目錄mkdir setdir
etcdctl setdir /testdir
(mkdir 如果目錄存在則會報錯 setdir 也會報錯,但是不一樣? 待研究兩者不同)
[[email protected] etcd]# etcdctl setdir /testdir
Error: 102: Not a file (/testdir) [11]
###################### 非數據庫操作 #################
# 備份etcd數據目錄
[[email protected] etcd]# du -sh data/
123Mdata/
[[email protected] etcd]# etcdctl backup --data-dir=/usr/local/etcd/data/ --backup-dir=/tmp/data
[[email protected] etcd]# du -sh /tmp/data/
62M/tmp/data/
(臨時目錄文件不會備份)
#watch 觀察一個值的變化(只是針對key 不針對目錄)
觀察到變化後,打印值並watch退出
可以用選項: -f
forever watch a key until CTRL+C
#exce-wathc 監聽到值有變化,就執行指定的命令(且不退出執行的可以是shell命令)
etcdctl exec-watch /testdir/testkey -- sh -c ‘pwd‘
#member 集群用途將其他成員添加到cluster中或者從cluster中刪除
[[email protected] etcd]# etcdctl member list
8e9e05c52164694d: name=ops-cuidehua001 peerURLs=http://localhost:2380 clientURLs=http://10.59.87.121:2379 isLeader=true
(因為是單點操作所以就一個節點)
#################### API 接口 ####
可以通過瀏覽器訪問,或者通過curl命令增刪改查
基本接口:
http://10.59.87.121:2379/v2/keys/
[[email protected] etcd]# curl -s http://10.59.87.121:2379/v2/keys/ | jq "."
{ "node": { "nodes": [ { "createdIndex": 4, "modifiedIndex": 4, "dir": true, "key": "/testdir" } ], "dir": true }, "action": "get" }
可獲取目錄下的所有key-value和
[[email protected] etcd]# curl -s http://10.59.87.121:2379/v2/keys/testdir | jq "."
{ "node": { "createdIndex": 4, "modifiedIndex": 4, "nodes": [ { "createdIndex": 4, "modifiedIndex": 12, "value": "yes3", "key": "/testdir/testkey" }, { "createdIndex": 10, "modifiedIndex": 10, "value": "yes3", "key": "/testdir/testkey2" } ], "dir": true, "key": "/testdir" }, "action": "get" }
## 刪除指定的key,返回被刪掉的內容(目錄是不能刪除的)
curl -s http://10.59.87.121:2379/v2/keys/testdir/testkey2 -XDELETE |jq "."
## set指定的值
curl -s http://10.59.87.121:2379/v2/keys/testdir/testkey3 -XPUT -d value="hello world" |jq "."
################### 配置集群 ###############
http://www.linuxidc.com/Linux/2017-01/139665.htm
可以在初始知道的時候就加入,還可在後面在加入
目前支持三種發現方式:Static,etcd Discovery,DNS Discovery。
● Static適用於有固定IP的主機節點
● etcd Discovery適用於DHCP環境
● DNS Discovery依賴DNS SRV記錄
這裏我們采用Static方式,創建etcd0腳本,方便配置etcd啟動參數
方法1: (參考)
可以在知道有哪些作為集群的時候,使用
● –initial-cluster-token 集群的ID
● –initial-cluster 集群中所有節點
● –initial-cluster-state 集群狀態,new為新創建集群,existing為已存在的集群
(以下 只是作為知識說明,在此文章中並未使用到)
#初始化名稱
INITIAL_CLUSTER_TOKEN=etcd_cluster_1
#初始化群集列表
INITIAL_CLUSTER="node1=http://10.59.72.221:2380,node2=http://10.59.72.191:2380,node3=http://10.59.72.192:2380"
#初始化狀態
INITIAL_CLUSTER_STATE=new
方法2:(這裏舉例使用了這種方法 演示)
後面add的節點(比如從一臺機器的集群 變成多臺的集群)
和第一個節點一樣搭建了如下第二個節點。
這裏要將 這個第二個節點加入到 上面的第一個節點中去。
[[email protected] etcd]# etcdctl member list
8e9e05c52164694d: name=etcd-node-002 peerURLs=http://localhost:2380 clientURLs=http://10.59.87.11:2379 isLeader=true
(記住 一定要在配置中補充) 補充腳本
#!/bin/bash # #初始化Etcd # #ETCD 名稱 ETCD_NAME=ops-cuidehua001 #ETCD存儲目錄 ETCD_DATA_DIR=/usr/local/etcd/data #本機IP地址 LOCAL_IP=10.59.87.121 #初始化名稱 INITIAL_CLUSTER_TOKEN=etcd_cluster_cuidehua #初始化群集列表 INITIAL_CLUSTER="ops-cuizhiliang001=http://10.59.87.121:2380,etcd-node-002=http://10.59.87.11:2380" #初始化狀態 INITIAL_CLUSTER_STATE=new #初始化 M1(){ # [ -d ${ETCD_DATA_DIR} ] && /bin/rm -rf ${ETCD_DATA_DIR} >/dev/null 2>&1 # /bin/mkdir ${ETCD_DATA_DIR} >/dev/null 2>&1 /usr/local/etcd/etcd --name ${ETCD_NAME} --data-dir ${ETCD_DATA_DIR} --initial-advertise-peer-urls http://${LOCAL_IP}:2380 --listen-peer-urls http://${LOCAL_IP}:2380 --listen-client-urls http://${LOCAL_IP}:2379,http://127.0.0.1:2379 --advertise-client-urls http://${LOCAL_IP}:2379 --initial-cluster-token ${INITIAL_CLUSTER_TOKEN} --initial-cluster ${INITIAL_CLUSTER} --initial-cluster-state ${INITIAL_CLUSTER_STATE} } M2(){ /usr/local/etcd/etcd --config-file /usr/local/etcd/etcd.conf } M${1}
每臺機器上執行
sh init.sh 1
sh init.sh 2 &>/dev/null &
查看:
# etcdctl member list
bfb265066811202: name=ops-cuidehua001 peerURLs=http://10.59.87.121:2380 clientURLs=http://10.59.87.121:2379 isLeader=false
4ee240ac8ec54efb: name=etcd-node-002 peerURLs=http://10.59.87.11:2380 clientURLs=http://10.59.87.11:2379 isLeader=true
## 集群成員操作(不管否是leader 都可以執行這個操作,執行完後,對方就從集群中踢出去了,在從新選舉Leader)
etcdctl member remove bfb265066811202
增加成員時候,INITTAL_CLUSTER_STATE 一定要標記為
#初始化狀態 INITIAL_CLUSTER_STATE=existing
本文出自 “殘劍” 博客,請務必保留此出處http://cuidehua.blog.51cto.com/5449828/1957659
etcd的學習心得和使用