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