kubernetes之store(一)
来源:互联网 发布:淘宝卖面膜名字店面 编辑:程序博客网 时间:2024/06/06 23:41
kubernetes的存储
用过kubernetes(以下简称k8s)的都知道,k8s的存储是用etcd,这个是肯定的。我现在要说的是k8s的内存的存储(用golang的map实现的),本来是想继上一篇接着讲list-wather的,但是,发现要讲的list-watcher都涉及到了内存存储,所以就打算先把k8s的内存存储讲完之后再接着讲list-wather,这样才能更好的理解之后的list-wather是如何实现的。
在来做个简短的说明下k8s是如何通过list-wather机制去实现对etcd中各种k8s的resource进行同步同步到内存存储,然后将各种resource的改变(事件)广播个各个wather的。首先是 etcd本身的list-wather 实现对etcd中的各种resource的list和watcher,然后将list和wather到的各种event(就是各种resource的事件,其中包括各种resource对象本身)存入到内存(也就是本文要说的store),然后各个组件就在启动后去内存中进行相应的resource的list和wather。得到相应的event(就是各种resource的事件,其中包括各种resource对象本身),然后进行各自的广播,让各个事件的监听者能收到相对的event(比如:podController在wather到对应的事件后,将event的runtime.Object转成对应的pod对象,然后进行相关的操作)。所以总的来说,数据的流向是:etcd—>store—>apiserver list-wather–>broadcast–>各个wather。
store详解
之前版本的k8s源码中,storage这个包就在源码下的pkg下,应该是1.6之后统一放到了k8s.io/client-go这个项目下去了,包路径:k8s.io/client-go/tools/cache
store.go
首先看下store接口,以下说的对象实质就是event对象(实质就是runtime.Object对象)
type Store interface { //添加一个对象到内存中 Add(obj interface{}) error //更新内存中的对象 Update(obj interface{}) error //删除内存中的对象 Delete(obj interface{}) error //获取全部内存中的对象 List() []interface{} //获取全部对象的key ListKeys() []string //获取一个对象 Get(obj interface{}) (item interface{}, exists bool, err error) //通过key获取对象 GetByKey(key string) (item interface{}, exists bool, err error) //删除内存中的所有对象,添加新的一组对象 Replace([]interface{}, string) error //从新同步etcd数据到内存 Resync() error}
接下来要说的就是store接口的实现
type cache struct { // 线程安全的存储ThreadSafeStore的实现类 cacheStorage ThreadSafeStore // keyFunc KeyFunc}
ThreadSafeStore的定义以及实现的接口:
type threadSafeMap struct { lock sync.RWMutex //内存存储的map,key就是event的key,val就是对应的event items map[string]interface{} // indexers maps a name to an IndexFunc indexers Indexers // indices maps a name to an Index indices Indices}type ThreadSafeStore interface { Add(key string, obj interface{}) Update(key string, obj interface{}) Delete(key string) Get(key string) (item interface{}, exists bool) List() []interface{} ListKeys() []string Replace(map[string]interface{}, string) Index(indexName string, obj interface{}) ([]interface{}, error) ListIndexFuncValues(name string) []string ByIndex(indexName, indexKey string) ([]interface{}, error) GetIndexers() Indexers // AddIndexers adds more indexers to this store. If you call this after you already have data // in the store, the results are undefined. AddIndexers(newIndexers Indexers) error Resync() error}
ThreadSafeStore涉及到indexer,这个下一次再讲这个。
接下 就是 cache 的各种方法的实现,主要列出与index无关的方法,
//添加一个对象到cacheStorage的items的map中func (c *cache) Add(obj interface{}) error { key, err := c.keyFunc(obj) if err != nil { return KeyError{obj, err} } c.cacheStorage.Add(key, obj) return nil}//更新cacheStorage的items的map中一个对象func (c *cache) Update(obj interface{}) error { key, err := c.keyFunc(obj) if err != nil { return KeyError{obj, err} } c.cacheStorage.Update(key, obj) return nil}//从cacheStorage的items的map中删除一个对象func (c *cache) Delete(obj interface{}) error { key, err := c.keyFunc(obj) if err != nil { return KeyError{obj, err} } c.cacheStorage.Delete(key) return nil}//获取cacheStorage的items的map的全部对象func (c *cache) List() []interface{} { return c.cacheStorage.List()}//获取cacheStorage的items的map的全部对象的keyfunc (c *cache) ListKeys() []string { return c.cacheStorage.ListKeys()}//获取cacheStorage的items的map的一个对象func (c *cache) Get(obj interface{}) (item interface{}, exists bool, err error) { key, err := c.keyFunc(obj) if err != nil { return nil, false, KeyError{obj, err} } return c.GetByKey(key)}//通过key获取cacheStorage的items的map的全一个对象func (c *cache) GetByKey(key string) (item interface{}, exists bool, err error) { item, exists = c.cacheStorage.Get(key) return item, exists, nil}//替换cacheStorage的items的mapfunc (c *cache) Replace(list []interface{}, resourceVersion string) error { items := map[string]interface{}{} for _, item := range list { key, err := c.keyFunc(item) if err != nil { return KeyError{item, err} } items[key] = item } c.cacheStorage.Replace(items, resourceVersion) return nil}//从新同步数据func (c *cache) Resync() error { return c.cacheStorage.Resync()}
以上只是最底层的内存存储的实现,还有各种对内存存储的封装,之后,会一一讲解到
- kubernetes之store(一)
- kubernetes 源码分析之kubeadm(一)
- kubernetes 源码分析之ingress(一)
- Kubernetes实战(一)
- Kubernetes核心原理(一)之API Server
- Kubernetes核心原理(一)之API Server
- Kubernetes学习笔记(一)
- Kubernetes(一)
- Windows store app(一)
- Tiled Backing Store(一)
- 在CoreOS上运行Kubernetes(一)
- 在CoreOS上运行Kubernetes(一)
- Kubernetes安装部署学习笔记(一)
- Kubernetes权威指南学习笔记(一)
- Kubernetes应用健康检查(一)
- kubernetes理论之kubernetes组成
- kubernetes源码之watch包watch.go阅读理解一
- kubernetes 源码分析之kubeadm(二)
- IOS之C语言--6函数
- IOS之C语言--7内存分析
- lua多种方式计算字符长度
- Android Call分析(一) ---- Call对象详解
- 图像增强知识总结
- kubernetes之store(一)
- 跨站请求伪造CSRF防护方法
- 120.黑名单的添加
- 彩色图像处理总结
- 基于mini2440嵌入式linux上整合一套Domoticz智能家居系统(七)交叉编译paho.mqtt.embedded-c库和嵌入式linux样例程序
- redhat 7.0 64位安装rabbitmq问题说明
- 121.黑名单拦截的实现
- 基于mini2440嵌入式linux上整合一套Domoticz智能家居系统(八)使用domoticz+mosquitto+Android客户端实现控制mini2440上的LED(一)
- 8.Django中views.py详解