etcd 小结
Contents
简介
取名有意思。Linux下的/etc目录放的是配置文件。etcd,etc代表配置,d代表distributed,代表分布式配置。
特点:
- designed to reliably store infrequently updated data and provide reliable watch queries https://etcd.io/docs/v3.4.0/learning/data_model/
- KV 核心用户接口
- MVCC Multi-version Concurrency Control 也确实能读历史版本
- Raft consensus algorithms 共识算法
- Watch 配置更新能及时"通知"应用
- RBAC 用户、角色、权限
基于 etcd 可以做哪些事情:
- 配置中心。元数据存储。应用的配置集中存储在配置中心。
- 服务发现。配置中心的一个特例。相比起来,consul的服务发现是开箱即用的。
- 分布式锁。分布式系统协调。选主。像是Hadoop使用Zookeeper做Namenode的选主。
vs. Consul。Consul 官方(https://www.consul.io/)定义的usecase是 service discovery和 service mesh。
etcd and Consul solve different problems. If looking for a distributed consistent key value store, etcd is a better choice over Consul. If looking for end-to-end cluster service discovery, etcd will not have enough features; choose Kubernetes, Consul, or SmartStack. https://github.com/etcd-io/etcd/blob/master/Documentation/learning/why.md#consul
如果是分布式配置中心,etcd是更好的选择。
参考
- etcd versus other key-value stores https://github.com/etcd-io/etcd/blob/master/Documentation/learning/why.md
etcd集群安装
https://github.com/XUJiahua/linux_scripts/tree/master/etcd
常用操作
etcdctl的命令主要是kv操作,以及集群管理,和RBAC用户权限管理。
参考 https://etcd.io/docs/v3.4.0/demo/
KV相关
put foo "Hello World"KV写get fooKV读,还可以读历史版本的值get web --prefixKV前缀读,适用于服务发现del keyKV删除del k --prefixKV前缀删除txn --interactiveKV事务watch stock1KV关注value变化,适用于服务发现,服务发生变动watch stock --prefixwatch也支持前缀匹配
Lease
lease grant 300创建300s的leaseput sample value --lease=2be7547fbc6a5afa有效期300s的key。不设置 lease的put操作,值是永久存储的。- keep-alive 刷新TTL, list, revoke。
分布式锁
lock mutex1获取锁后,其他请求被block
集群管理
member listlist etcd member,member命令还可以增加新node,node也包含不投票的。endpoint status集群状态,能看到leader serverendpoint health健康检查snapshot save my.db数据快照存储到本地
用户权限(role based access control)
role add root创建rolerole grant-permission root readwrite foorole上赋予权限,这里赋予foo的读写权限user add root创建用户,并指定密码user grant-role root root赋予用户roleauth enable开启认证,disable 即关闭auth。--user=root:root put foo bar操作时指定用户密码