Docker安裝etcd

2018-08-04 18:03 更新

etcd 基于 Go 語(yǔ)言實(shí)現(xiàn),因此,用戶可以從 項(xiàng)目主頁(yè) 下載源代碼自行編譯,也可以下載編譯好的二進(jìn)制文件,甚至直接使用制作好的 Docker 鏡像文件來(lái)體驗(yàn)。

二進(jìn)制文件方式下載

編譯好的二進(jìn)制文件都在 github.com/coreos/etcd/releases 頁(yè)面,用戶可以選擇需要的版本,或通過(guò)下載工具下載。

例如,下面的命令使用 curl 工具下載壓縮包,并解壓。

curl -L  https://github.com/coreos/etcd/releases/download/v2.0.0-rc.1/etcd-v2.0.0-rc.1-linux-amd64.tar.gz -o etcd-v2.0.0-rc.1-linux-amd64.tar.gz
tar xzvf etcd-v2.0.0-rc.1-linux-amd64.tar.gz
cd etcd-v2.0.0-rc.1-linux-amd64

解壓后,可以看到文件包括

$ ls
etcd  etcdctl  etcd-migrate  README-etcdctl.md  README.md

其中 etcd 是服務(wù)主文件,etcdctl 是提供給用戶的命令客戶端,etcd-migrate 負(fù)責(zé)進(jìn)行遷移。

推薦通過(guò)下面的命令將三個(gè)文件都放到系統(tǒng)可執(zhí)行目錄 /usr/local/bin//usr/bin/。

$ sudo cp etcd* /usr/local/bin/

運(yùn)行 etcd,將默認(rèn)組建一個(gè)兩個(gè)節(jié)點(diǎn)的集群。數(shù)據(jù)庫(kù)服務(wù)端默認(rèn)監(jiān)聽(tīng)在 2379 和 4001 端口,etcd 實(shí)例監(jiān)聽(tīng)在 2380 和 7001 端口。顯示類似如下的信息:

$ ./etcd
2014/12/31 14:52:09 no data-dir provided, using default data-dir ./default.etcd
2014/12/31 14:52:09 etcd: listening for peers on http://localhost:2380
2014/12/31 14:52:09 etcd: listening for peers on http://localhost:7001
2014/12/31 14:52:09 etcd: listening for client requests on http://localhost:2379
2014/12/31 14:52:09 etcd: listening for client requests on http://localhost:4001
2014/12/31 14:52:09 etcdserver: name = default
2014/12/31 14:52:09 etcdserver: data dir = default.etcd
2014/12/31 14:52:09 etcdserver: snapshot count = 10000
2014/12/31 14:52:09 etcdserver: advertise client URLs = http://localhost:2379,http://localhost:4001
2014/12/31 14:52:09 etcdserver: initial advertise peer URLs = http://localhost:2380,http://localhost:7001
2014/12/31 14:52:09 etcdserver: initial cluster = default=http://localhost:2380,default=http://localhost:7001
2014/12/31 14:52:10 etcdserver: start member ce2a822cea30bfca in cluster 7e27652122e8b2ae
2014/12/31 14:52:10 raft: ce2a822cea30bfca became follower at term 0
2014/12/31 14:52:10 raft: newRaft ce2a822cea30bfca [peers: [], term: 0, commit: 0, lastindex: 0, lastterm: 0]
2014/12/31 14:52:10 raft: ce2a822cea30bfca became follower at term 1
2014/12/31 14:52:10 etcdserver: added local member ce2a822cea30bfca [http://localhost:2380 http://localhost:7001] to cluster 7e27652122e8b2ae
2014/12/31 14:52:11 raft: ce2a822cea30bfca is starting a new election at term 1
2014/12/31 14:52:11 raft: ce2a822cea30bfca became candidate at term 2
2014/12/31 14:52:11 raft: ce2a822cea30bfca received vote from ce2a822cea30bfca at term 2
2014/12/31 14:52:11 raft: ce2a822cea30bfca became leader at term 2
2014/12/31 14:52:11 raft.node: ce2a822cea30bfca elected leader ce2a822cea30bfca at term 2
2014/12/31 14:52:11 etcdserver: published {Name:default ClientURLs:[http://localhost:2379 http://localhost:4001]} to cluster 7e27652122e8b2ae

此時(shí),可以使用 etcdctl 命令進(jìn)行測(cè)試,設(shè)置和獲取鍵值 testkey: "hello world",檢查 etcd 服務(wù)是否啟動(dòng)成功:

$ ./etcdctl set testkey "hello world"
hello world
$ ./etcdctl get testkey
hello world

說(shuō)明 etcd 服務(wù)已經(jīng)成功啟動(dòng)了。

當(dāng)然,也可以通過(guò) HTTP 訪問(wèn)本地 2379 或 4001 端口的方式來(lái)進(jìn)行操作,例如查看 testkey 的值:

$ curl -L http://localhost:4001/v2/keys/testkey
{"action":"get","node":{"key":"/testkey","value":"hello world","modifiedIndex":3,"createdIndex":3}}

Docker 鏡像方式下載

鏡像名稱為 quay.io/coreos/etcd:v2.0.0_rc.1,可以通過(guò)下面的命令啟動(dòng) etcd 服務(wù)監(jiān)聽(tīng)到 4001 端口。

$ sudo docker run -p 4001:4001 -v /etc/ssl/certs/:/etc/ssl/certs/ quay.io/coreos/etcd:v2.0.0_rc.1
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)