etcd是CoreOS團隊於2013年6月發起的開源專案,它的目標是構建一個高可用的分散式鍵值(key-value)資料庫。etcd內部採用raft協定作為一致性演算法,etcd基於Go語言實現。
etcd作為服務發現系統,有以下的特點
• 簡單:安裝配置簡單,而且提供了HTTP API進行交互,使用也很簡單
• 安全:支持SSL證書驗證
• 快速:根據官方提供的benchmark資料,單實例支援每秒2k+讀操作
• 可靠:採用raft演算法,實現分散式系統資料的可用性和一致性
• etcd項目位址:https://github.com/coreos/etcd/
etcd單機佈署安裝
curl -L https://github.com/coreos/etcd/releases/download/v3.2.1/etcd-v3.2.1-linux-amd64.tar.gz -o etcd-v3.2.1-linux-amd64.tar.gz tar xzvf etcd-v3.2.1-linux-amd64.tar.gz mv etcd-v3.2.1-linux-amd64 etcd
vi /etc/profile
加入以下內容
# etcd export PATH=/root/etcd:$PATH export ETCDCTL_API=3
執行source /etc/profile
測試
$ etcdctl version $ etcdctl version $ ./etcdctl put aaa 1 OK $ ./etcdctl get aaa aaa 1
etcd集群配置
192.168.0.204 node1
192.168.0.205 node2
192.168.0.206 node3
每台都要裝的部份
wget https://github.com/etcd-io/etcd/releases/download/v3.3.17/etcd-v3.3.17-linux-amd64.tar.gz tar -zxvf etcd-v3.3.17-linux-amd64.tar.gz -C /opt/ cd /opt mv etcd-v3.3.17-linux-amd64 etcd-v3.3.17
設定環境變數
vi /etc/profile
# etcd
export PATH=/opt/etcd-v3.3.17:$PATH
export ETCDCTL_API=3
執行source /etc/profile
mkdir /etc/etcd # 建立etcd配置檔案目錄
cd /etc/etcd
在每個節點上編輯設定檔
vi /etc/etcd/conf.yml
node1
name: etcd-1 data-dir: /opt/etcd-v3.3.17/data listen-client-urls: http://192.168.0.204:2379,http://127.0.0.1:2379 advertise-client-urls: http://192.168.0.204:2379,http://127.0.0.1:2379 listen-peer-urls: http://192.168.0.204:2380 initial-advertise-peer-urls: http://192.168.0.204:2380 initial-cluster: etcd-1=http://192.168.0.204:2380,etcd-2=http://192.168.0.205:2380,etcd-3=http://192.168.0.206:2380 initial-cluster-token: etcd-cluster-token initial-cluster-state: new
node2
name: etcd-2 data-dir: /opt/etcd-v3.3.17/data listen-client-urls: http://192.168.0.205:2379,http://127.0.0.1:2379 advertise-client-urls: http://192.168.0.205:2379,http://127.0.0.1:2379 listen-peer-urls: http://192.168.0.205:2380 initial-advertise-peer-urls: http://192.168.0.205:2380 initial-cluster: etcd-1=http://192.168.0.204:2380,etcd-2=http://192.168.0.205:2380,etcd-3=http://192.168.0.206:2380 initial-cluster-token: etcd-cluster-token initial-cluster-state: new
node3
name: etcd-3 data-dir: /opt/etcd-v3.3.17/data listen-client-urls: http://192.168.0.206:2379,http://127.0.0.1:2379 advertise-client-urls: http://192.168.0.206:2379,http://127.0.0.1:2379 listen-peer-urls: http://192.168.0.206:2380 initial-advertise-peer-urls: http://192.168.0.206:2380 initial-cluster: etcd-1=http://192.168.0.204:2380,etcd-2=http://192.168.0.205:2380,etcd-3=http://192.168.0.206:2380 initial-cluster-token: etcd-cluster-token initial-cluster-state: new
檢視叢集成員資訊
etcdctl member list
message key設定Hello值示例
etcdctl set /message Hello curl -X PUT http://127.0.0.1:2379/v2/keys/message -d value="Hello"
讀取message的值
etcdctl get /message curl http://127.0.0.1:2379/v2/keys/message
刪除message key
etcdctl del /message curl -X DELETE http://127.0.0.1:2379/v2/keys/message
設定啟動服務
/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=/opt/etcd-v3.3.17/ # User=etcd ExecStart=/opt/etcd-v3.3.17/etcd --config-file=/etc/etcd/conf.yml Restart=on-failure LimitNOFILE=65536 [Install] WantedBy=multi-user.target
-------------------------------------------------------
systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
systemctl restart etcd
systemctl status etcd.service -l
-------------------------------------------------------
安裝etcd webui
環境要安裝node.js、git
git clone https://github.com/henszey/etcd-browser.git cd etcd-browser/ vim server.js
編輯server.js,修改內容如下:
var etcdHost = process.env.ETCD_HOST || '192.168.0.204'; var etcdPort = process.env.ETCD_PORT || 2379; var serverPort = process.env.SERVER_PORT || 8000;
然後啟動
node server.js
訪問ui http://192.168.0.204:8000