k8s :kube-apiserver 訪問 etcd 後端儲存

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

前言

本文介紹 kube-apiserver 是如何訪問 etcd 後端儲存

相關原始碼主要在 kubernetes/staging/src/k8s.io/apiserver/pkg/storage

通用介面

Interface offers a common interface for object marshaling/unmarshaling operations and hides all the storage-related operations behind it(原文注釋)

// kubernetes/vendor/k8s.io/apiserver/storage/interfaces.gotype Interface interface {    Versioner() Versioner    Create(...)    Delete(...)    Watch(...)    WatchList(...)    Get(...)    GetToList(...)    List(...)    GuaranteedUpdate(...)}

Interface 定義了後端儲存的通用介面,主要是一些"增刪改查"方法,這種面向介面編程,將實現和設計分離的設計提高了軟體的可擴充性,降低了模組間的耦合度,比如只要我們提供 Interface 的具體實現,那麼除了使用 etcd 作為後端儲存之外,是不是也可以使用 consul/zookeeper 等分布式 kv 儲存?

具體實現

Interface 介面目前有兩個具體實作類別,分別對應 etcd v2 和 etcd v3 兩個不同的 etcd api 版本

// kubernetes/vendor/k8s.io/apiserver/pkg/storage/etcd/etcd_helper.go type etcdHelper struct {    ...}// kubernetes/vender/k8s.io/apiserver/pkg/storage/etcd3/store.gotype store struct {    ...}

這兩個實作類別的名字差的也太大了。。。go 語言使用非侵入式的介面,所以從 類(結構)名上是看不出他們兩和 storage.Interface 有半毛線關係的,但是只要它們實現了 storate.Interface 介面裡面聲明的方法他們就 "is-a" storage.Interface

建立

factory 包裡的《Factory 方法》Create 會根據配置 storagebackend.Config 建立相應的 storage.Interface

func Create(c storagebackend.Config) (storage.Interface, DestroyFunc, error) {    swtich c.Type {        case storagebackend.StorageTypeETCD2:            return newETCD2Storage(c)        case storagebackend.StorageTypeUnset, storageback.StorageTypeETCD3:            return newETCD3Storage(c)        default:            return nil, nil, fmt.Errorf("unknown storage type: %s", c.Type)    }}

使用

storage.Interface 被 Store 類(這又是什麼東東?)用來實現 rest.StandardStorage(RESTful 增刪改查) 介面

// kubernetes/vendor/k8s.io/apiserver/pkg/registry/generic/registry/store.gotype Store struct {    ...    Storage storage.Interface}// Create inserts a new item according to the unique key from the objectfunc (e *Store) Create(ctx genericapirequest.Context, obj runtime.Object,    includeUninitialized bool)(runtime.Object, error) {    ...    key, err := e.KeyFunc(ctx, name)    ...    out := e.NewFunc()    if err := e.Storage.Create(ctx, key, obj, out, ttl); err != nil {        ...    }    ...}

總結

本介紹了 kube-apiserver 訪問 etcd 後端儲存相關的類(結構)和方法,用到的設計模式以及架構上的套路

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.