The name "etcd" originated from two ideas, the unix "/etc" folder and "d"istributed systems. The "/etc" folder is a place to store configuration data for a single system whereas etcd stores configuration information for large scale distributed systems. Hence, a "d"istributed "/etc" is "etcd".html
Overview算法
etcd is a distributed key value store that provides a reliable way to store data across a cluster of machines. It's open-source and avaliable on GitHub.etcd gracefully handles leader elections during network partitions and will tolerate machine failure, including the leader.安全
your applications can read and write data into etcd. A simple use-case is to store database connection details or feature flags in etcd as key value pairs. these values can be watched, allowing your app to reconfigure itself when they change. advanced uses take advantage of the consistency guarantees to implement database leader elections or do distributed locking across a cluster of workers.架構
Use casesapp
Kubernets stores configuration data into etcd for service discovery and cluster management; etcd's consistency is crucial for correctly scheduling and operating services. The Kubernetes API server persists cluster state into etcd. It uses etcd's watch API to monitor the cluster and roll out critical configuration changes. curl
Features異步
架構分佈式
從etcd的架構圖中咱們能夠看到,etcd主要分爲四個部分:ide
一般,一個用戶的請求發送過來,會經由HTTP Server轉發給Store進行具體的事務處理,若是涉及到節點的修改,則交給Raft模塊進行狀態的變動、日誌的記錄,而後再同步給別的etcd節點以確認數據提交,最後進行數據的提交,再次同步。微服務
場景1. Service Discovery
Service Discovery 要解決的是分佈式系統中最建立的問題之一,即在同一個分佈式集羣中的進程或服務如何才能找到對方並創建鏈接。從本質上說,服務發現就是想要了解集羣中是否有進程在監聽TCP或UDP端口,而且經過名字就能夠進行查找和鏈接,要解決Service Discovery的問題須要有下面三大支柱,缺一不可:
微服務協同工做架構中,服務動態添加。隨着Docker容器的流行,多種微服務共同協做,構成一個相對功能強大的架構的案例愈來愈多。透明化的動態添加這些服務的需求也日益強烈。經過服務發現機制,在etcd中註冊某個服務名字的目錄,在該目錄下存儲可用的服務節點的IP。在使用服務的過程當中,只要從服務目錄下查找可用的服務節點去使用便可。
在分佈式系統中,最適用的一種組件間通訊方式就是消息發佈與訂閱。即構建一個配置共享中心,數據提供者在這個配置中心發佈消息,而消息使用者則訂閱他們關心的主題,一旦主題有消息發佈,就會實時通知訂閱者。經過這種方式能夠作到分佈式系統配置的集中式管理與動態更新。
這裏說到的分佈式通知與協調,與消息發佈和訂閱有些類似。都用到了etcd中的Watcher機制,經過註冊與異步通知機制,實現分佈式環境下不一樣系統之間的通知與協調,從而對數據變動作到實時處理。實現方式一般是這樣:不一樣系統都在etcd上對同一個目錄進行註冊,同時設置Watcher觀測該目錄的變化(若是對子目錄的變化也有須要,能夠設置遞歸模式),當某個系統更新了etcd的目錄,那麼設置了Watcher的系統就會收到通知,並做出相應處理。
由於etcd使用Raft算法保持了數據的強一致性,某次操做存儲到集羣中的值必然是全局一致的,因此很容易實現分佈式鎖。鎖服務有兩種使用方式,一是保持獨佔,二是控制時序。
參考資料:
http://www.infoq.com/cn/articles/etcd-interpretation-application-scenario-implement-principle