隨着CoreOS和Kubernetes等項目在開源社區日益火熱,它們項目中都用到的etcd組件做爲一個高可用、強一致性的服務發現存儲倉庫,漸漸爲開發人員所關注。
在雲計算時代,如何讓服務快速透明地接入到計算集羣中,如何讓共享配置信息快速被集羣中的全部機器發現,
更爲重要的是,如何構建這樣一套高可用、安全、易於部署以及響應快速的服務集羣,已經成爲了迫切須要解決的問題。
etcd爲解決這類問題帶來了福音,它是一個高可用的 Key/Value 存儲系統,內部採用raft協議做爲一致性算法。
準備好了嗎,乾貨要來了,are you ok ?node
mkdir -p /home/chenqionghe/test/etcd/ cd !$ # 下載 wget https://github.com/etcd-io/etcd/releases/download/v3.3.12/etcd-v3.3.12-linux-amd64.tar.gz # 解壓安裝 tar zxvf etcd-v3.3.12-linux-amd64.tar.gz cd etcd-v3.3.12-linux-amd64 ## 將啓動文件和命令管理文件拷貝到 PATH找到的路徑中 cp etcd /usr/local/bin cp etcdctl /usr/local/bin
--name etcd集羣中的節點名,這裏能夠隨意,可區分且不重複就行 --listen-peer-urls 監聽的用於節點之間通訊的url,可監聽多個,集羣內部將經過這些url進行數據交互(如選舉,數據同步等) --initial-advertise-peer-urls 建議用於節點之間通訊的url,節點間將以該值進行通訊。 --listen-client-urls 監聽的用於客戶端通訊的url,一樣能夠監聽多個。 --advertise-client-urls 建議使用的客戶端通訊url,該值用於etcd代理或etcd成員與etcd節點通訊。 --initial-cluster-token etcd-cluster-1 節點的token值,設置該值後集羣將生成惟一id,併爲每一個節點也生成惟一id,當使用相同配置文件再啓動一個集羣時,只要該token值不同,etcd集羣就不會相互影響。 --initial-cluster 也就是集羣中全部的initial-advertise-peer-urls 的合集 --initial-cluster-state new 新建集羣的標誌
直接啓動linux
etcd
etcd默認監聽的是localhost的2379端口,既只監聽了lo設備,這樣會致使啓動後集羣中的其餘機器沒法訪問
所以咱們能夠在啓動的時候將默認的localhost改爲0.0.0.0,確保etcd監聽了全部網卡。git
etcd -listen-client-urls="http://0.0.0.0:2379" --advertise-client-urls="http://0.0.0.0:2379"
咱們來使用curl來測試一下,是否能夠遠程訪問,這裏個人機器IP是10.211.55.25github
➜ ~ curl -L http://10.211.55.25:2379/version {"etcdserver":"3.3.12","etcdcluster":"3.3.0"}
若是不想使用2379和2380端口,也能夠web
#etcd 啓動 指定端口2280 2279 /usr/local/bin/etcd \ --data-dir=/etcd-data --name node1 \ --initial-advertise-peer-urls http://0.0.0.0:2280 --listen-peer-urls http://0.0.0.0:2280 \ --advertise-client-urls http://0.0.0.0:2279 --listen-client-urls http://0.0.0.0:2279 \ --initial-cluster node1=http://0.0.0.0:2280 #鏈接 etcdctl --endpoints=http://127.0.0.1:2279 set cqh chenqionghe etcdctl --endpoints=http://127.0.0.1:2279 get cqh
固然,咱們也能夠經過docker運行,這裏給出啓動腳本算法
#!/usr/bin/env bash ETCD_NAME="etcd" ETCD_VERSION="v3.3.1" ETCD_PORT_CLIENT=2379 ETCD_PORT_NODE=2380 docker run -d \ -p ${ETCD_PORT_CLIENT}:2379 \ -p ${ETCD_PORT_NODE}:2380 \ --name ${ETCD_NAME} quay.io/coreos/etcd:${ETCD_VERSION} \ /usr/local/bin/etcd \ --data-dir=/etcd-data --name node1 \ --initial-advertise-peer-urls http://0.0.0.0:2380 --listen-peer-urls http://0.0.0.0:2380 \ --advertise-client-urls http://0.0.0.0:2379 --listen-client-urls http://0.0.0.0:2379 \ --initial-cluster node1=http://0.0.0.0:2380
Etcd構建自身高可用集羣主要有三種形式:docker
這裏咱們採用Static方式,準備三臺機器,ip以下(都已經安裝etcd)shell
node1 10.211.55.2 node2 10.211.55.25 node3 10.211.55.26
#!/usr/bin/env bash #節點名稱 ETCD_NAME=node-1 #本機IP地址 LOCAL_IP=10.211.55.2 #ETCD存儲目錄 ETCD_DATA_DIR=/usr/local/etcd/data #初始化名稱 INITIAL_CLUSTER_TOKEN=cqh-test-cluster #初始化羣集列表 INITIAL_CLUSTER="node-1=http://10.211.55.2:2380,node-2=http://10.211.55.25:2380,node-3=http://10.211.55.26:2380" #初始化狀態 INITIAL_CLUSTER_STATE=new #開始運行 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}
#!/usr/bin/env bash #節點名稱 ETCD_NAME=node-2 #本機IP地址 LOCAL_IP=10.211.55.25 #ETCD存儲目錄 ETCD_DATA_DIR=/usr/local/etcd/data #初始化名稱 INITIAL_CLUSTER_TOKEN=cqh-test-cluster #初始化羣集列表 INITIAL_CLUSTER="node-1=http://10.211.55.2:2380,node-2=http://10.211.55.25:2380,node-3=http://10.211.55.26:2380" #初始化狀態 INITIAL_CLUSTER_STATE=new #開始運行 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}
#!/usr/bin/env bash #節點名稱 ETCD_NAME=node-3 #本機IP地址 LOCAL_IP=10.211.55.26 #ETCD存儲目錄 ETCD_DATA_DIR=/usr/local/etcd/data #初始化名稱 INITIAL_CLUSTER_TOKEN=cqh-test-cluster #初始化羣集列表 INITIAL_CLUSTER="node-1=http://10.211.55.2:2380,node-2=http://10.211.55.25:2380,node-3=http://10.211.55.26:2380" #初始化狀態 INITIAL_CLUSTER_STATE=new #開始運行 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}
咱們看到3臺都啓動成功了
ubuntu
而後咱們在node2上面執行操做api
root@ubuntu:~# etcdctl member list 2033c1336b929ca7: name=node-3 peerURLs=http://10.211.55.26:2380 clientURLs=http://10.211.55.26:2379 isLeader=true edc51d36208cfbcf: name=node-2 peerURLs=http://10.211.55.25:2380 clientURLs=http://10.211.55.25:2379 isLeader=false f09a9dba19a725e2: name=node-1 peerURLs=http://10.211.55.2:2380 clientURLs=http://10.211.55.2:2379 isLeader=false
能夠看到集羣已經生效了,咱們再來測試一下,在ndoe2上執行操做
etcdctl set /cqh muscle
看看node1和node3是否能保持數據一致
能夠看到在node1和node3中都能可以正確的獲取/cqh的值
etcdctl watch key
觀察一個值的變化,觀察到變化後,打印值並watch退出
etcdctl exec-watch key -- sh -c 'pwd'
監聽到值有變化,就執行指定的命令(且不退出執行的能夠是shell命令)
➜ ~ curl http://10.211.55.25:2379/v2/keys/cqh -XPUT -d value="陳瓊和1" {"action":"set","node":{"key":"/cqh","value":"陳瓊和","modifiedIndex":14,"createdIndex":14},"prevNode":{"key":"/cqh","value":"陳瓊和","modifiedIndex":13,"createdIndex":13}}
➜ ~ curl http://10.211.55.25:2379/v2/keys/gym -XPUT -d dir=true {"action":"set","node":{"key":"/gym","dir":true,"modifiedIndex":12,"createdIndex":12}}
➜ ~ curl http://10.211.55.25:2379/v2/keys/cqh {"action":"get","node":{"key":"/cqh","value":"陳瓊和","modifiedIndex":14,"createdIndex":14}}
➜ ~ curl http://10.211.55.25:2379/v2/keys/hero -XPUT -d value="超人" -d ttl=5
curl http://10.211.55.25:2379/v2/keys/fitness -XPOST -d value="bench_press" curl http://10.211.55.25:2379/v2/keys/fitness -XPOST -d value="dead_lift" curl http://10.211.55.25:2379/v2/keys/fitness -XPOST -d value="deep_squat"
獲取剛建立的fitness
curl http://10.211.55.25:2379/v2/keys/fitness {"action":"create","node":{"key":"/fitness/00000000000000000020","value":"bench_press","modifiedIndex":20,"createdIndex":20}} {"action":"create","node":{"key":"/fitness/00000000000000000021","value":"dead_lift","modifiedIndex":21,"createdIndex":21}} {"action":"create","node":{"key":"/fitness/00000000000000000022","value":"deep_squat","modifiedIndex":22,"createdIndex":22}} {"action":"get","node":{"key":"/fitness","dir":true,"nodes":[{"key":"/fitness/00000000000000000022","value":"deep_squat","modifiedIndex":22,"createdIndex":22},{"key":"/fitness/00000000000000000020","value":"bench_press","modifiedIndex":20,"createdIndex":20},{"key":"/fitness/00000000000000000021","value":"dead_lift","modifiedIndex":21,"createdIndex":21}],"modifiedIndex":20,"createdIndex":20}}
curl http://10.211.55.25:2379/v2/keys/cqh -XDELETE
curl http://10.211.55.25:2379/v2/members
curl http://10.211.55.25:2379/v2/stats/leader
curl http://10.211.55.26:2379/v2/stats/self
curl http://10.211.55.26:2379/v2/stats/store
這裏分別演示搭建etcd-browser和etcdkeeper,二者功能大同小異,不一樣的是etcdkeeper支持v3的api
docker run --rm -d --name etcd-browser \ -p 8000:8000 \ --env ETCD_HOST=10.211.55.25 \ --env ETCD_PORT=2379 \ buddho/etcd-browser
運行後訪問http://10.211.55.25:8000/
看到以下界面,能夠看到上面添加的全部數據,這裏我使用界面添加了漫威和DC的英雄
docker run -it -d --name etcdkeeper \ -p 8080:8080 \ deltaprojects/etcdkeeper
訪問http://10.211.55.25:8080/etcdkeeper/,輸入etcd的地址,看到以下界面
到這裏,etcd的單機版搭建、集羣版、客戶端使用、rest api、web管理界面等功能都介紹了,你還在等什麼,趕忙high起來