分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)

   ETCD 是一個(gè)高可用的分布式鍵值數(shù)據(jù)庫(kù),可用于服務(wù)發(fā)現(xiàn)。ETCD 采用 raft 一致性算法,基于 Go 語(yǔ)言實(shí)現(xiàn),隨著CoreOS和Kubernetes等項(xiàng)目在開(kāi)源社區(qū)日益火熱,它們項(xiàng)目中都用到的etcd組件作為一個(gè)高可用強(qiáng)一致性的服務(wù)發(fā)現(xiàn)存儲(chǔ)倉(cāng)庫(kù),漸漸為開(kāi)發(fā)人員所關(guān)注。在云計(jì)算時(shí)代,如何讓服務(wù)快速透明地接入到計(jì)算集群中,如何讓共享配置信息快速被集群中的所有機(jī)器發(fā)現(xiàn),更為重要的是,如何構(gòu)建這樣一套高可用、安全、易于部署以及響應(yīng)快速的服務(wù)集群,已經(jīng)成為了迫切需要解決的問(wèn)題。etcd為解決這類問(wèn)題帶來(lái)了福音,本文將從etcd的應(yīng)用場(chǎng)景開(kāi)始,深入解讀etcd的實(shí)現(xiàn)方式,以供開(kāi)發(fā)者們更為充分地享用etcd所帶來(lái)的便利。

特點(diǎn)
簡(jiǎn)單:安裝配置使用簡(jiǎn)單,提供 HTTP API
安全:支持 SSL 證書(shū)
可靠:采用 raft 算法,實(shí)現(xiàn)分布式系統(tǒng)數(shù)據(jù)的可用性和一致性
下載安裝包:
[root@master ~]# wget https://github.com/coreos/etcd/releases/download/v3.3.2/etcd-v3.3.2-linux-amd64.tar.gz
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
復(fù)制安裝包到各接點(diǎn):
[root@master ~]# ansible k8s -m copy -a'src=/root/etcd-v3.3.2-linux-amd64.tar.gz dest=/root/'
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
部署環(huán)境三臺(tái)機(jī)子:IP--主機(jī)名--集群結(jié)點(diǎn)名
192.168.27.211 client1 (etcd1)
192.168.27.212 client2 (etcd2)
192.168.27.213 client3 (etcd3)

創(chuàng)新互聯(lián)是一家專業(yè)提供金臺(tái)企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為金臺(tái)眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。

  1. 解壓版本包
    tar -zxvf etcd-v3.3.2-linux-amd64.tar.gz 或 [root@master ~]# ansible k8s -m shell -a'tar -zxf etcd-v3.3.2-linux-amd64.tar.gz'
    分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
    分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
  2. 將解壓文件重命名并移到/etc/目錄下
    mv etcd-v3.3.2-linux-amd64 /etc/etcd-v3.3.2 或
    [root@master ~]# ansible k8s -m shell -a'mv etcd-v3.3.2-linux-amd64 /etc/etcd-v3.3.2'
    分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
  3. 將etcd和etcdctl加入系統(tǒng)變量PATH中(可以省略)
    [root@master ~]# ansible k8s -m shell -a'echo export PATH=/etc/etcd-v3.3.2:$PATH >> ~/.bashrc'
    [root@master ~]# ansible k8s -m shell -a'source ~/.bashrc '
    [root@master ~]# ansible k8s -m shell -a'source /root/.bashrc ' #靠普些
    分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
    分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
    4.進(jìn)入/etc/etcd-v3.3.2,并創(chuàng)建配置文件etcd.conf,內(nèi)容:
    分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
    分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
    name: etcd1
    data-dir: /etc/etcd-v3.3.2/data
    listen-client-urls: http://0.0.0.0:2379
    advertise-client-urls: http://192.168.27.211:2379
    listen-peer-urls: http://0.0.0.0:2380
    initial-advertise-peer-urls: http://192.168.27.211:2380
    initial-cluster: etcd1=http://192.168.27.211:2380,etcd2=http://192.168.27.212:2380,etcd3=http://192.168.27.213:2380
    initial-cluster-token: etcd-cluster-my
    initial-cluster-state: new
    5.設(shè)置etcd版本號(hào),默認(rèn)為2,我們安裝的版本為3.3.2,所以此處設(shè)置為3
    分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
    在 文件~/.bashrc中加入一行改為3
    export ETCDCTL_API=3
    分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
    分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
    分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
  4. 設(shè)置開(kāi)機(jī)啟動(dòng),新建文件/usr/lib/systemd/system/etcd.service,設(shè)置方便管理配置內(nèi)容:
    [root@client1 etcd-v3.3.2]# cat /usr/lib/systemd/system/etcd.service
    [Unit]
    Description=Etcd Server
    After=network.target
    After=network-online.target
    Wants=network-online.target

[Service]
Type=notify
WorkingDirectory=/etc/etcd-v3.3.2
#User=etcd
ExecStart=/etc/etcd-v3.3.2/etcd --config-file /etc/etcd-v3.3.2/etcd.conf
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

  1. 啟動(dòng)etcd,啟動(dòng)第一個(gè)member的時(shí)候,此member等待其它member初始化完畢,處于watting狀態(tài),此時(shí)緊接著啟動(dòng)第二個(gè)就行了
    systemctl daemon-reload
    systemctl enable etcd
    systemctl start etcd
    systemctl restart etcd

注意:192.168.27.212,192.168.27.213部署如同上面步驟,只需要將步驟4里面接點(diǎn)名字、ip改為自己ip即可。
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
name: etcd2
data-dir: /etc/etcd-v3.3.2/data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://192.168.27.212:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://192.168.27.212:2380
initial-cluster: etcd1=http://192.168.27.211:2380,etcd2=http://192.168.27.212:2380,etcd3=http://192.168.27.213:2380
initial-cluster-token: etcd-cluster-my
initial-cluster-state: new
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
name: etcd3
data-dir: /etc/etcd-v3.3.2/data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://192.168.27.213:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://192.168.27.213:2380
initial-cluster: etcd1=http://192.168.27.211:2380,etcd2=http://192.168.27.212:2380,etcd3=http://192.168.27.213:2380
initial-cluster-token: etcd-cluster-my
initial-cluster-state: new
6、啟動(dòng)成功后查看集群成員及狀態(tài):
[root@client3 etcd-v3.3.2]# etcdctl member list
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
etcdctl endpoint health --endpoints=192.168.27.211:2379,192.168.27.212:2379,192.168.27.213:2379
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
[root@client3 etcd-v3.3.2]# etcdctl endpoint status --endpoints=192.168.27.211:2379,192.168.27.212:2379,192.168.27.213:2379
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
[root@client3 etcd-v3.3.2]# etcdctl endpoint status --endpoints=192.168.27.211:2379
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
通過(guò)非ETCD集群成員訪問(wèn)測(cè)試:
[root@master etcd-v3.3.2]# etcdctl endpoint status --endpoints=192.168.27.211:2379
192.168.27.211:2379, f4e909b85dbd820b, 3.3.2, 25 kB, false, 69, 71
[root@master etcd-v3.3.2]# etcdctl endpoint status --endpoints=192.168.27.211:2379
192.168.27.211:2379, f4e909b85dbd820b, 3.3.2, 25 kB, false, 69, 71
[root@master etcd-v3.3.2]# etcdctl endpoint status --endpoints=192.168.27.211:2379,192.168.27.212:2379,192.168.27.213:2379
192.168.27.211:2379, f4e909b85dbd820b, 3.3.2, 25 kB, false, 69, 71
192.168.27.212:2379, 265ebeb3298fa51f, 3.3.2, 25 kB, false, 69, 71
192.168.27.213:2379, 510ff85ed64c7436, 3.3.2, 25 kB, true, 69, 71
[root@master etcd-v3.3.2]# etcdctl put name jerry --endpoints=192.168.27.211:2379,192.168.27.212:2379,192.168.27.213:2379
OK
[root@master etcd-v3.3.2]# etcdctl put topic "this is a etcd client test" --endpoints=192.168.27.211:2379,192.168.27.212:2379,192.168.27.213:2379
OK
[root@master etcd-v3.3.2]# etcdctl get topic --endpoints=192.168.27.211:2379,192.168.27.212:2379,192.168.27.213:2379
topic
this is a etcd client test
[root@master etcd-v3.3.2]# etcdctl get topic --endpoints=192.168.27.211:2379
topic
this is a etcd client test
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
Etcd使用:
讀寫(xiě)鍵值:
[root@client1 etcd-v3.3.2]# etcdctl put key1 value1
OK
[root@client1 etcd-v3.3.2]# etcdctl get key1
key1
value1
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
前綴讀(將所有前綴是key的鍵值對(duì)都讀出來(lái))
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
鍵值刪除:
[root@client1 etcd-v3.3.2]# etcdctl del key
0
[root@client1 etcd-v3.3.2]# etcdctl get key
[root@client1 etcd-v3.3.2]# etcdctl get key1
key1
value1
[root@client1 etcd-v3.3.2]# etcdctl del key1
1
[root@client1 etcd-v3.3.2]# etcdctl get key1
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
觀察已無(wú)KEY1鍵及值的存在。
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
按前綴全部刪除
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
觀察已無(wú)鍵值及數(shù)據(jù)
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
重新創(chuàng)建三個(gè)鍵值:
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
其它ETCD集群成員上訪問(wèn)鍵值測(cè)試:
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
監(jiān)聽(tīng)某個(gè)鍵,每次鍵值有變化
[root@client1 etcd-v3.3.2]# etcdctl watch key --prefix
PUT
key1
value
PUT
key2
jerry
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
設(shè)置過(guò)期時(shí)間TTL
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
[root@client2 etcd-v3.3.2]# etcdctl lease grant 30
lease 251f6cf538287411 granted with TTL(30s)
[root@client2 etcd-v3.3.2]# etcdctl put key5 value5 --lease=251f6cf538287411
Error: etcdserver: requested lease not found
[root@client2 etcd-v3.3.2]# etcdctl lease grant 30
lease 251f6cf538287414 granted with TTL(30s)
[root@client2 etcd-v3.3.2]# etcdctl put key5 value5 --lease=251f6cf538287414
OK
[root@client2 etcd-v3.3.2]# etcdctl lease keep-alive 251f6cf538287414
lease 251f6cf538287414 expired or revoked.
[root@client2 etcd-v3.3.2]# etcdctl get key5
[root@client2 etcd-v3.3.2]# etcdctl get key5
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)

網(wǎng)頁(yè)題目:分布式鍵值高可用存儲(chǔ)集群架構(gòu)搭建(ETCD)
網(wǎng)頁(yè)URL:http://muchs.cn/article14/gecide.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站排名、網(wǎng)頁(yè)設(shè)計(jì)公司、商城網(wǎng)站關(guān)鍵詞優(yōu)化、域名注冊(cè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)