本文摘自:http://blog.csdn.net/tzm1921995/article/details/52212168
對于通過apt或yum方式安裝etcd的痰滋,可以修改etcd.conf配置文件來配置各個參數(shù)。
Etcd集群簡介
etcd組件作為一個高可用強一致性的服務(wù)發(fā)現(xiàn)存儲倉庫续崖。我們此次在三個主機上搭建了一個包含三個Etcd節(jié)點的集群敲街,實現(xiàn)了集群的動態(tài)擴展和收縮,并測試和驗證了Etcd集群鍵——值存儲的一致性和高
一严望、環(huán)境準(zhǔn)備
OS: Ubuntu 14.04
user: root
ipaddress:?
etcd01: 192.168.200.24
etcd02: 192.168.200.25
etcd03: 192.168.200.26
下載etcd 源碼包:https://github.com/coreos/etcd/releases/
這里用的是etcd-v2.3.7-Linux-amd64.tar.gz
二多艇、安裝配置etcd
etcd01上安裝
tar xf etcd-v2.3.7-linux-amd64.tar.gz
cd?etcd-v2.3.7-linux-amd64
cp etcd* /usr/local/bin/
創(chuàng)建etcd01腳本,可方便配置etcd啟動參數(shù)
#cat etcd01
/usr/local/bin/etcd??-name?etcd01?\
-data-dir?/data/etcd01?\
-advertise-client-urls?http://192.168.200.24:2379,http://192.168.200.24:4001??\
-listen-client-urls??http://0.0.0.0:2379,http://192.168.200.24:4001?\
-initial-advertise-peer-urls?http://192.168.200.24:2380 \
-listen-peer-urls?http://0.0.0.0:2380?\
-initial-cluster-token?etcd-cluster-1?\
-initial-cluster?etcd01=http://192.168.200.24:2380,etcd02=http://192.168.200.25:2380,etcd03=http://192.168.200.26:2380?\
-initial-cluster-state?new
參數(shù)說明:-name 指定名字
-data-dir 指定數(shù)據(jù)保存目錄像吻,默認(rèn)是當(dāng)前目錄
-initial-cluster-state 集群狀態(tài) new為新創(chuàng)建集群 existing為已存在(可不指定)
在etcd02 etcd03上分別做相似操作
腳本中-advertise-client-urls 和 -initial-advertis-peer-urls 參數(shù)修改一下即可
然后分別運行腳本:nohup ./etcd01 &
三峻黍、測試
在任一臺主機上執(zhí)行etcdctl member list
#etcdctl member list
6a223770249e927d:?name=etcd02?peerURLs=http://192.168.200.25:2380?clientURLs=http://192.168.200.25:2379,http://192.168.200.25:4001?isLeader=false
7e0ce16121dfea24:?name=etcd01?peerURLs=http://192.168.200.24:2380?clientURLs=http://192.168.200.24:2379,http://192.168.200.24:4001?isLeader=true
bfc28be8765b503e: name=etcd03 peerURLs=http://192.168.200.26:2380 clientURLs=http://192.168.200.26:2379,http://192.168.200.26:4001 isLeader=false
可以看到集群的節(jié)點情況,并能看出哪個是leader節(jié)點
我們在etcd01上設(shè)置一個key/value
root@etcd1:~# etcdctl set api_server http://192.168.5.44:8080
http://192.168.5.44:8080
這時就可以在任意一臺主機上獲取這個key/value
root@etcd2:~# etcdctl get api_server
root@etcd3:~#?etcdctl?get?api_server
http://192.168.5.44:8080
在member list上看到etcd01是leader 拨匆,這時把etcd01停掉(kill)
用etcdctl cluster-health查看
root@etcd2:~# etcdctl cluster-health
member?6a223770249e927d?is?healthy:?got?healthy?result?from?http://192.168.200.25:2379
failed?to?check?the?health?of?member?7e0ce16121dfea24?on?http://192.168.200.24:2379:?Get?http://192.168.200.24:2379/health:?dial?tcp?192.168.200.24:2379:?getsockopt:?connection?refused
failed?to?check?the?health?of?member?7e0ce16121dfea24?on?http://192.168.200.24:4001:?Get?http://192.168.200.24:4001/health:?dial?tcp?192.168.200.24:4001:?getsockopt:?connection?refused
member?7e0ce16121dfea24?is?unreachable:?[http://192.168.200.24:2379?http://192.168.200.24:4001]?are?all?unreachable
member?bfc28be8765b503e?is?healthy:?got?healthy?result?from?http://192.168.200.26:2379
cluster?is?healthy
并且集群leader進(jìn)行了重新選舉
root@etcd2:~# etcdctl member list
6a223770249e927d:?name=etcd02?peerURLs=http://192.168.200.25:2380?clientURLs=http://192.168.200.25:2379,http://192.168.200.25:4001?isLeader=true
7e0ce16121dfea24:?name=etcd01?peerURLs=http://192.168.200.24:2380?clientURLs=http://192.168.200.24:2379,http://192.168.200.24:4001?isLeader=false
bfc28be8765b503e: name=etcd03 peerURLs=http://192.168.200.26:2380 clientURLs=http://192.168.200.26:2379,http://192.168.200.26:4001 isLeader=false
現(xiàn)在etcd02是leader了姆涩,這時我們在群集中設(shè)置兩個key/value
root@etcd2:~# etcdctl set test01 123456
123456
root@etcd2:~#?etcdctl?set?test02?abcdefg
abcdefg
重新啟動etcd01
root@etcd1:~# etcdctl cluster-health
member?6a223770249e927d?is?healthy:?got?healthy?result?from?http://192.168.200.25:2379
member?7e0ce16121dfea24?is?healthy:?got?healthy?result?from?http://192.168.200.24:2379
member?bfc28be8765b503e?is?healthy:?got?healthy?result?from?http://192.168.200.26:2379
cluster?is?healthy
root@etcd1:~# etcdctl get test01
123456
root@etcd1:~#?etcdctl?get?test02
abcdefg
但這時在etcd01重新加入集群,并保持了key/value的全局一致性惭每,由此可見 etcd 搭建的集群是可以實現(xiàn)高可用的骨饿。
Etcd集群的擴展與收縮
etcd集群如果收縮很簡單,直接在命令行輸入
etcdctl member remove {$memberID}
$memberID是你即將要刪除節(jié)點的etcd的ID,etcd的擴展有一些地方需要注意一下宏赘,我在這里操作的時候遇到了不少坑绒北。從上文寫到現(xiàn)在,有一個文件夾很重要察署,幾乎每個坑都與它有關(guān)闷游,那就是-data-dir所聲明的文件夾,注意要擴展一個etcd集群時贴汪,首先在集群內(nèi)的任一臺機器上輸入
etcdctl member add $etcd_name $peer_url
$etcd_name:新加入的etcd節(jié)點的名字
$peer_url:一般為新加入的節(jié)點?IP:2380
下面為通過yum方式安裝后脐往,/etc/etcd/etcd.conf配置文件的參考實例:
# 節(jié)點名稱
ETCD_NAME=etcd0
# # 數(shù)據(jù)存放位置
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
# # 監(jiān)聽其他 Etcd 實例的地址
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
# # 監(jiān)聽客戶端地址
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
# # 通知其他 Etcd 實例地址
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.212.140:2380"
# # 初始化集群內(nèi)節(jié)點地址
ETCD_INITIAL_CLUSTER="etcd0=http://192.168.212.140:2380,etcd1=http://192.168.212.139:2380,etcd2=http://192.168.212.138:2380"
# # 初始化集群狀態(tài),new 表示新建
ETCD_INITIAL_CLUSTER_STATE="new"
# # 初始化集群 token
ETCD_INITIAL_CLUSTER_TOKEN="mritd-etcd-cluster"
# # 通知 客戶端地址
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.212.140:2379,http://192.168.212.140:4001"