etcd是CoreOS團隊于2013年6月發起的開源項目,它的目標是構建一個高可用的分布式鍵值(key-value)數據庫,用于配置共享和服務發現
etcd內部采用raft
協議作為一致性算法,etcd基于Go語言實現。
?
etcd作為服務發現系統,有以下的特點:
- 簡單:安裝配置簡單,HTTP+JSON的 API進行交互,使用curl命令可以輕松使用
- 安全:支持SSL證書驗證機制
- 快速:根據官方提供的benchmark數據,單實例支持每秒1k+寫操作
- 可靠:采用raft算法,實現分布式系統數據的可用性和一致性
- 分布式系統中數據分為控制數據和應用數據,etcd處理的數據為控制數據
?
同一個分布式集群中的進程或服務,互相感知并建立鏈接,這就是服務發現。解決服務發現問題的三大支柱:
- 一個強一致性、高可用的服務存儲目錄
- 基于Raft算法
- 一種注冊服務和監控服務健康狀態的機制
- 一種查找和鏈接服務的機制
- 微服務協同工作架構中,服務動態添加
?
?
etcd基本命令
集群搭建參考k8s集群搭建里的etcd
?
更新一個節點
#首先查看所有節點 [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
?
添加節點
#將刪除的節點添加回來
[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 clusterETCD_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
清空目標節點數據
目標節點從集群中刪除后,成員信息會更新。新節點是作為一個全新的節點加入集群,如果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
?