docker etcd

etcd是CoreOS團隊於2013年6月發起的開源項目,它的目標是構建一個高可用的分佈式鍵值(key-value)數據庫用於配置共享和服務發現算法

etcd內部採用raft協議做爲一致性算法,etcd基於Go語言實現。數據庫

 

etcd做爲服務發現系統,有如下的特色:安全

  • 簡單:安裝配置簡單,HTTP+JSON的 API進行交互,使用curl命令能夠輕鬆使用
  • 安全:支持SSL證書驗證機制
  • 快速:根據官方提供的benchmark數據,單實例支持每秒1k+寫操做
  • 可靠:採用raft算法,實現分佈式系統數據的可用性和一致性
    • 分佈式系統中數據分爲控制數據和應用數據,etcd處理的數據爲控制數據

 

同一個分佈式集羣中的進程或服務,互相感知並創建連接,這就是服務發現。解決服務發現問題的三大支柱:架構

  • 一個強一致性、高可用的服務存儲目錄
    • 基於Raft算法
  • 一種註冊服務和監控服務健康狀態的機制
  • 一種查找和連接服務的機制
  • 微服務協同工做架構中,服務動態添加 

 

 

etcd基本命令curl

集羣搭建參考k8s集羣搭建裏的etcdtcp

 

更新一個節點分佈式

#首先查看全部節點
[root@slave2 ~]# etcdctl member list
646ec025a72fc8a: name=etcd3 peerURLs=http://192.168.132.133:2380 clientURLs=http://192.168.132.133:2379 isLeader=true
dde447f371b55f50: name=etcd1 peerURLs=http://192.168.132.131:2380 clientURLs=http://192.168.132.131:2379 isLeader=false
e96057c7f8ba5f4b: name=etcd2 peerURLs=http://192.168.132.132:2380 clientURLs=http://192.168.132.132:2379 isLeader=false

#更新一個節點
[root@slave2 ~]# etcdctl member update 646ec025a72fc8a http://192.168.132.133:2380
Updated member with ID 646ec025a72fc8a in cluster

 

刪除一個節點微服務

#查看全部節點
[root@slave2 ~]# etcdctl member list 646ec025a72fc8a: name=etcd3 peerURLs=http://192.168.132.133:2380 clientURLs=http://192.168.132.133:2379 isLeader=false dde447f371b55f50: name=etcd1 peerURLs=http://192.168.132.131:2380 clientURLs=http://192.168.132.131:2379 isLeader=true e96057c7f8ba5f4b: name=etcd2 peerURLs=http://192.168.132.132:2380 clientURLs=http://192.168.132.132:2379 isLeader=false
#刪除一個節點
[root@slave2
~]# etcdctl member remove 646ec025a72fc8a Removed member 646ec025a72fc8a from cluster
#再次查看節點,使用當前命令報錯
[root@slave2
~]# etcdctl member list Error: client: etcd cluster is unavailable or misconfigured; error #0: dial tcp 127.0.0.1:2379: getsockopt: connection refused ; error #1: dial tcp 127.0.0.1:4001: getsockopt: connection refused error #0: dial tcp 127.0.0.1:2379: getsockopt: connection refused error #1: dial tcp 127.0.0.1:4001: getsockopt: connection refused
#換一個命令
[root@slave2 ~]# etcdctl --endpoints "http://192.168.132.131:2379" member list dde447f371b55f50: name=etcd1 peerURLs=http://192.168.132.131:2380 clientURLs=http://192.168.132.131:2379 isLeader=true e96057c7f8ba5f4b: name=etcd2 peerURLs=http://192.168.132.132:2380 clientURLs=http://192.168.132.132:2379 isLeader=false

 

添加節點url

#將刪除的節點添加回來
[root@slave2 ~]# etcdctl --endpoints "http://192.168.132.132:2379" member add etcd3 http://192.168.132.133:2380 Added member named etcd3 with ID 7a6809311b4a3579 to cluster ETCD_NAME="etcd3" ETCD_INITIAL_CLUSTER="etcd3=http://192.168.132.133:2380,etcd1=http://192.168.132.131:2380,etcd2=http://192.168.132.132:2380" ETCD_INITIAL_CLUSTER_STATE="existing"
#再次查看,狀態爲unstarted [root@slave2
~]# etcdctl --endpoints "http://192.168.132.132:2379" member list 7a6809311b4a3579[unstarted]: peerURLs=http://192.168.132.133:2380 dde447f371b55f50: name=etcd1 peerURLs=http://192.168.132.131:2380 clientURLs=http://192.168.132.131:2379 isLeader=true e96057c7f8ba5f4b: name=etcd2 peerURLs=http://192.168.132.132:2380 clientURLs=http://192.168.132.132:2379 isLeader=false

清空目標節點數據spa

目標節點從集羣中刪除後,成員信息會更新。新節點是做爲一個全新的節點加入集羣,若是data-dir有數據,etcd啓動時會讀取己經存在的數據,仍然用舊的memberID會形成沒法加入集羣,因此必定要清空新節點的data-dir。

#刪除的節點的主機
$ rm -rf /var/lib/etcd/etcd3

 

在目標節點上啓動新增長的成員

修改配置文件中ETCD_INITIAL_CLUSTER_STATE標記爲existing,若是爲new,則會自動生成一個新的memberID,這和前面添加節點時生成的ID不一致,故日誌中會報節點ID不匹配的錯。

[root@slave2 ~]# cat /etc/etcd/etcd.conf |grep -v "^#"
[Member]
ETCD_DATA_DIR="/var/lib/etcd/etcd3"
ETCD_LISTEN_PEER_URLS="http://192.168.132.133:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.132.133:2379,http://127.0.0.1:2379"
ETCD_NAME="etcd3"
[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.132.133:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.132.133:2379"
ETCD_INITIAL_CLUSTER_STATE="existing"
ETCD_INITIAL_CLUSTER="etcd1=http://192.168.132.131:2380,etcd2=http://192.168.132.132:2380,etcd3=http://192.168.132.133:2380"

 

重啓服務,再次查看

[root@slave2 ~]# systemctl restart etcd
[root@slave2 ~]# etcdctl --endpoints "http://192.168.132.132:2379" member list
7a6809311b4a3579: name=etcd3 peerURLs=http://192.168.132.133:2380 clientURLs=http://192.168.132.133:2379 isLeader=false
dde447f371b55f50: name=etcd1 peerURLs=http://192.168.132.131:2380 clientURLs=http://192.168.132.131:2379 isLeader=true
e96057c7f8ba5f4b: name=etcd2 peerURLs=http://192.168.132.132:2380 clientURLs=http://192.168.132.132:2379 isLeader=false
[root@slave2 ~]# etcdctl member list
7a6809311b4a3579: name=etcd3 peerURLs=http://192.168.132.133:2380 clientURLs=http://192.168.132.133:2379 isLeader=false
dde447f371b55f50: name=etcd1 peerURLs=http://192.168.132.131:2380 clientURLs=http://192.168.132.131:2379 isLeader=true
e96057c7f8ba5f4b: name=etcd2 peerURLs=http://192.168.132.132:2380 clientURLs=http://192.168.132.132:2379 isLeader=false
相關文章
相關標籤/搜索