【K8s源码品读】007:Phase 1 - kube-apiserver - Pod数据的保存

简介: 理解Pod发送到kube-apiserver后是怎么保存的

聚焦目标

理解Pod发送到kube-apiserver后是怎么保存的

目录

  1. RESTCreateStrategy创建的预处理
  2. REST Pod数据的存储
  3. 存储的底层实现
  4. kube-apiserver第一阶段源码阅读总结

RESTCreateStrategy

// podStrategy 是封装了 Pod 的各类动作,这里我们先关注create这个操作
type podStrategy struct {
   
    runtime.ObjectTyper
    names.NameGenerator
}

// podStrategy 的接口
type RESTCreateStrategy interface {
   
    runtime.ObjectTyper
    names.NameGenerator
  // 是否属于当前的 namespace
    NamespaceScoped() bool
  // 准备创建前的检查
    PrepareForCreate(ctx context.Context, obj runtime.Object)
  // 验证资源对象
    Validate(ctx context.Context, obj runtime.Object) field.ErrorList
  // 规范化
    Canonicalize(obj runtime.Object)
}

// 完成了检查,我们就要保存数据了

Storage

// PodStorage 是 Pod 存储的实现,里面包含了多个存储的定义
type PodStorage struct {
   
  // REST implements a RESTStorage for pods
    Pod                 *REST
  // BindingREST implements the REST endpoint for binding pods to nodes when etcd is in use.
    Binding             *BindingREST
  // LegacyBindingREST implements the REST endpoint for binding pods to nodes when etcd is in use.
    LegacyBinding       *LegacyBindingREST
    Eviction            *EvictionREST
  // StatusREST implements the REST endpoint for changing the status of a pod.
    Status              *StatusREST
  // EphemeralContainersREST implements the REST endpoint for adding EphemeralContainers
    EphemeralContainers *EphemeralContainersREST
    Log                 *podrest.LogREST
    Proxy               *podrest.ProxyREST
    Exec                *podrest.ExecREST
    Attach              *podrest.AttachREST
    PortForward         *podrest.PortForwardREST
}

/*
从上一节的map关系中,保存在REST中
restStorageMap := map[string]rest.Storage{
        "pods":             podStorage.Pod,
}
*/
type REST struct {
   
    *genericregistry.Store
    proxyTransport http.RoundTripper
}

// Store是一个通用的数据结构
type Store struct {
   
    // Storage定义
    Storage DryRunnableStorage
}

// DryRunnableStorage中的Storage是一个Interface
type DryRunnableStorage struct {
   
    Storage storage.Interface
    Codec   runtime.Codec
}

func (s *DryRunnableStorage) Create(ctx context.Context, key string, obj, out runtime.Object, ttl uint64, dryRun bool) error {
   
    if dryRun {
   
        if err := s.Storage.Get(ctx, key, storage.GetOptions{
   }, out); err == nil {
   
            return storage.NewKeyExistsError(key, 0)
        }
        return s.copyInto(obj, out)
    }
  // 这里,就是Create的真正调用
    return s.Storage.Create(ctx, key, obj, out, ttl)
}

Storage Implement

// Storage Interface 的定义,包括基本的增删改查,以及watch等等进阶操作
type Interface interface {
   
    Versioner() Versioner
    Create(ctx context.Context, key string, obj, out runtime.Object, ttl uint64) error
    Delete(ctx context.Context, key string, out runtime.Object, preconditions *Preconditions, validateDeletion ValidateObjectFunc) error
    Watch(ctx context.Context, key string, opts ListOptions) (watch.Interface, error)
    WatchList(ctx context.Context, key string, opts ListOptions) (watch.Interface, error)
    Get(ctx context.Context, key string, opts GetOptions, objPtr runtime.Object) error
    GetToList(ctx context.Context, key string, opts ListOptions, listObj runtime.Object) error
    List(ctx context.Context, key string, opts ListOptions, listObj runtime.Object) error
    GuaranteedUpdate(
        ctx context.Context, key string, ptrToType runtime.Object, ignoreNotFound bool,
        precondtions *Preconditions, tryUpdate UpdateFunc, suggestion ...runtime.Object) error
    Count(key string) (int64, error)
}

func NewRawStorage(config *storagebackend.Config) (storage.Interface, factory.DestroyFunc, error) {
   
    return factory.Create(*config)
}

func Create(c storagebackend.Config) (storage.Interface, DestroyFunc, error) {
   
    switch c.Type {
   
  // 已经不支持etcd2
    case "etcd2":
        return nil, nil, fmt.Errorf("%v is no longer a supported storage backend", c.Type)
  // 默认为etcd3版本
    case storagebackend.StorageTypeUnset, storagebackend.StorageTypeETCD3:
        return newETCD3Storage(c)
    default:
        return nil, nil, fmt.Errorf("unknown storage type: %s", c.Type)
    }
}

Summary

我们对第一阶段学习kube-apiserver的知识点进行总结:

  1. kube-apiserver 包含三个apiserverAPIExtensionsServerKubeAPIServerAggregatorServer
    1. 三个APIServer底层均依赖通用的GenericServer,使用go-restful对外提供RESTful风格的API服务
  2. kube-apiserver 对请求进行 AuthenticationAuthorizationAdmission三层验证
  3. 完成验证后,请求会根据路由规则,触发到对应资源的handler,主要包括数据的预处理保存
  4. kube-apiserver 的底层存储为etcd v3,它被抽象为一种RESTStorage,使请求和存储操作一一对应
相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
目录
相关文章
|
27天前
|
前端开发 编解码 数据格式
浅谈响应式编程在企业级前端应用 UI 开发中的实践
浅谈响应式编程在企业级前端应用 UI 开发中的实践
22 0
浅谈响应式编程在企业级前端应用 UI 开发中的实践
|
29天前
|
Kubernetes 应用服务中间件 nginx
提升K8S故障排除效率:详解Pod内抓包的高效策略!
提升K8S故障排除效率:详解Pod内抓包的高效策略!
34 0
|
1月前
|
存储 Kubernetes 容器
K8s中Pod常见问题排查
K8s中Pod常见问题排查
20 6
|
2天前
|
Kubernetes 网络协议 调度
kubernetes最小调度单元pod详解(二)
kubernetes最小调度单元pod详解(二)
|
2天前
|
Kubernetes 应用服务中间件 调度
kubernetes最小调度单元pod详解(一)
kubernetes最小调度单元pod详解(一)
|
18天前
|
Kubernetes 固态存储 调度
Kubernetes节点亲和性分配Pod
Kubernetes节点亲和性分配Pod
30 0
Kubernetes节点亲和性分配Pod
|
18天前
|
存储 Kubernetes 调度
Kubernetes Pod生命周期
Kubernetes Pod生命周期
29 0
Kubernetes Pod生命周期
|
18天前
|
存储 Kubernetes 应用服务中间件
Kubernetes Pod
Kubernetes Pod
57 0
Kubernetes Pod
|
29天前
|
存储 Kubernetes 调度
kubernetes核心技术之Pod知识总结
【4月更文挑战第2天】kubernetes核心技术之Pod知识总结
49 0
|
1月前
|
存储 Kubernetes 调度
K8s Pod亲和性、污点、容忍度、生命周期与健康探测详解(下)
本文全面探讨了Kubernetes集群中Pod的四种关键机制——Pod亲和性、污点(Taints)、容忍度(Tolerations)、生命周期以及健康探测,为读者提供了深入理解并有效应用这些特性的指南。

推荐镜像

更多