etcd 是一個高可用的 Key/Value 存儲系統,主要用於分享配置和服務發現。etcd 的靈感來自於 ZooKeeper 和 Doozer,側重於:
簡單:支持 curl 方式的用戶 API (HTTP+JSON)
安全:可選 SSL 客戶端證書認證
快速:單實例可達每秒 10000 次寫操作
可靠:使用 Raft 實現分佈式
ETCD下載
https://github.com/etcd-io/etcd/releases 選擇最新版本:etcd-v3.3.13-darwin-amd64.zip上傳到服務器
etcd部署安裝
部署架構
192.168.10.12 節點1
192.168.10.13 節點2
192.168.10.14 節點3
安裝解壓
$tar -zxvf etcd-v3.3.13-linux-amd64.tar.gz -C /workspace |
創建etcd配置文件
$cd /workspace/etcd-v3.3.13/ $vim conf.yml |
節點1,添加如下內容:
name: etcd-1 data-dir: /data/etcd listen-client-urls: http://192.168.10.12:2379,http://127.0.0.1:2379 advertise-client-urls: http://192.168.10.12:2379,http://127.0.0.1:2379 listen-peer-urls: http://192.168.10.12:2380 initial-advertise-peer-urls: http://192.168.10.12:2380 initial-cluster: etcd-1=http://192.168.10.12:2380,etcd-2=http://192.168.10.13:2380,etcd-3=http://192.168.10.14:2380 initial-cluster-token: etcd-cluster-token initial-cluster-state: new |
節點2,添加如下內容:
name: etcd-2 data-dir: /data/etcd listen-client-urls: http://192.168.10.13:2379,http://127.0.0.1:2379 advertise-client-urls: http://192.168.10.13:2379,http://127.0.0.1:2379 listen-peer-urls: http://192.168.10.13:2380 initial-advertise-peer-urls: http://192.168.10.13:2380 initial-cluster: etcd-1=http://192.168.10.12:2380,etcd-2=http://192.168.10.13:2380,etcd-3=http://192.168.10.14:2380 initial-cluster-token: etcd-cluster-token initial-cluster-state: new |
節點3,添加如下內容:
name: etcd-3 data-dir: /data/etcd listen-client-urls: http://192.168.10.14:2379,http://127.0.0.1:2379 advertise-client-urls: http://192.168.10.14:2379,http://127.0.0.1:2379 listen-peer-urls: http://192.168.10.14:2380 initial-advertise-peer-urls: http://192.168.10.14:2380 initial-cluster: etcd-1=http://192.168.10.12:2380,etcd-2=http://192.168.10.13:2380,etcd-3=http://192.168.10.14:2380 initial-cluster-token: etcd-cluster-token initial-cluster-state: new |
更新etcd系統默認配置
當前使用的是etcd v3版本,系統默認的是v2,通過下面命令修改配置。
$vim /etc/profile |
在文件末尾追加:
export ETCDCTL_API=3 |
使文件生效
$ source /etc/profile |
ETCD命令
查看版本信息:
$ ./etcdctl version |
啓動命令
$nohup ./etcd --config-file=/workspace/etcd-v3.3.13/conf.yml & |
查看集羣成員信息
$ ./etcdctl member list |
ECTD讀寫操作
基於HTTP協議的API使用起來比較簡單,這裏主要通過etcdctl和curl兩種方式來做簡單介紹
下面通過給message key設置Hello值示例
./etcdctl put /message howareyou |
$ curl -X PUT http://127.0.0.1:2379/v2/keys/message -d value="youyou" |
讀取message的值
$ ./etcdctl get /message |
$ curl http://127.0.0.1:2379/v2/keys/message |
刪除message key
$ ./etcdctl del /message |
再查看消息
注意:因爲是集羣,所以message在其中一個節點創建後,在集羣中的任何節點都可以查詢到