Kubernetes的Group、Version、Resource学习小记

Stella981
• 阅读 615

欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos

关于Group、Version、Resource

  • 开篇先提重点:Group、Version、Resource十分重要,它们会贯穿《client-go实战》、《kubebuilder实战》、《Kubernetes官方java客户端》等系列文章的始终;
  • Kubernetes是以资源为中心的系统,在学习client-go时,Group、Version、Resource等资源相关的数据结构就显得格外重要了,下图来自郑老师《Kubernetes源码剖析》一文,将三者关系做了简洁清晰的介绍:
    Kubernetes的Group、Version、Resource学习小记

环境信息

  • 开始学习前先交待一下我这边的环境信息,以避免因为版本引起的误会:
  1. kubernetes源码:1.20.3-rc.0
  2. 实战环境的操作系统:CentOS Linux release 7.9.2009
  3. 实战环境的kubernetes:1.20.0
  • 接下来开始学习Group、Version、Resource吧

Group

  1. Group即资源组,在kubernetes对资源进行分组时,对应的数据结构就是Group,源码路径:k8s.io/apimachinery/pkg/apis/meta/v1/types.go ,如下,可见Group有自己的名称和版本:

    type APIGroup struct {

    TypeMeta `json:",inline"`
    Name string `json:"name" protobuf:"bytes,1,opt,name=name"`
    Versions []GroupVersionForDiscovery `json:"versions" protobuf:"bytes,2,rep,name=versions"`
    PreferredVersion GroupVersionForDiscovery `json:"preferredVersion,omitempty" protobuf:"bytes,3,opt,name=preferredVersion"`
    ServerAddressByClientCIDRs []ServerAddressByClientCIDR `json:"serverAddressByClientCIDRs,omitempty" protobuf:"bytes,4,rep,name=serverAddressByClientCIDRs"`
    

    }

  2. 在kubernetes中有两种资源组:有组名资源组和无组名资源组(也叫核心资源组Core Groups),它们都很常见;

  3. deployment有组名,pod没有组名,咱们把它俩的OpenAPI放在一起对比就一目了然了:
    Kubernetes的Group、Version、Resource学习小记

Version

  • Version即版本,这个好理解,kubernetes的版本分为三种:
  1. Alpha:内部测试版本,如v1alpha1
  2. Beta:经历了官方和社区测试的相对稳定版,如v1beta1
  3. Stable:正式发布版,如v1、v2
  • 如下图红框,资源组batch之下有v1和v2alpha1两个版本,每个版本下都有多个资源:
    Kubernetes的Group、Version、Resource学习小记

  • 数据结构源码还是在types.go文件中,如下:

    type APIVersions struct {

    TypeMeta `json:",inline"`
    Versions []string `json:"versions" protobuf:"bytes,1,rep,name=versions"`
    ServerAddressByClientCIDRs []ServerAddressByClientCIDR `json:"serverAddressByClientCIDRs" protobuf:"bytes,2,rep,name=serverAddressByClientCIDRs"`
    

    }

Resource

  • Resource资源在kubernetes中的重要性是不言而喻的,常见的pod、service、deployment这些都是资源,下面是关于资源的一些小结:
  1. 在kubernetes环境被实例化的资源即资源对象(ResourceObject);
  2. 资源被分为持久性(Persistent Entity)和非持久性(Ephemeral Entity),持久性如deployment,创建后会在etcd保存,非持久性如pod;
  • 资源的源码:

    type APIResource struct {

    Name string `json:"name" protobuf:"bytes,1,opt,name=name"`
    SingularName string `json:"singularName" protobuf:"bytes,6,opt,name=singularName"`
    Namespaced bool `json:"namespaced" protobuf:"varint,2,opt,name=namespaced"`
    Group string `json:"group,omitempty" protobuf:"bytes,8,opt,name=group"`
    Version string `json:"version,omitempty" protobuf:"bytes,9,opt,name=version"`
    Kind string `json:"kind" protobuf:"bytes,3,opt,name=kind"`
    Verbs Verbs `json:"verbs" protobuf:"bytes,4,opt,name=verbs"`
    ShortNames []string `json:"shortNames,omitempty" protobuf:"bytes,5,rep,name=shortNames"`
    Categories []string `json:"categories,omitempty" protobuf:"bytes,7,rep,name=categories"`
    StorageVersionHash string `json:"storageVersionHash,omitempty" protobuf:"bytes,10,opt,name=storageVersionHash"`
    

    }

  • kubernetes为资源准备了8种操作:create、delete、deletecollection、get、list、patch、update、watch,每一种资源都支持其中的一部分,这在每个资源的API文档中可以看到;

  • 资源支持以命名空间(namespace)进行隔离;

  • 资源对象描述文件在日常操作中频繁用到,一共由五部分组成:apiVersion、kind、metadata、spec、status,下图是官方的deployment描述文件,用于创建3个nginx pod,对着红框和文字就了解每个部分的作用了:
    Kubernetes的Group、Version、Resource学习小记

  • 上图并没有status,该部分是用来反应当前资源对象状态的,体现在资源的数据结构中,如下所示:

    type Deployment struct {

    metav1.TypeMeta `json:",inline"`
    metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
    Spec DeploymentSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
    Status DeploymentStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
    

    }

官方文档速查

APIResources数据结构

  • APIResource是个常用的数据结构了,可以用来描述资源,例如resource_quota_controller_test.go中有对其的使用:

    func TestDiscoverySync(t *testing.T) {

    serverResources := []*metav1.APIResourceList{
    
    
    
        {
    
    
    
            GroupVersion: "v1",
            APIResources: []metav1.APIResource{
    
    
    
                {
    

    Name: "pods", Namespaced: true, Kind: "Pod", Verbs: metav1.Verbs{

    "create", "delete", "list", "watch"}}, }, }, } unsyncableServerResources := []*metav1.APIResourceList{ { GroupVersion: "v1", APIResources: []metav1.APIResource{ { Name: "pods", Namespaced: true, Kind: "Pod", Verbs: metav1.Verbs{

    "create", "delete", "list", "watch"}}, { Name: "secrets", Namespaced: true, Kind: "Secret", Verbs: metav1.Verbs{

    "create", "delete", "list", "watch"}}, }, }, }

实际操作

  • 聊了这么久理论,接下来咱们在kubernetes环境执行一些和资源有关的操作;

  • 查看所有资源kubectl api-resources -o wide,可见当前环境的所有资源,及其相关属性:

    [root@kubebuilder 07]# kubectl api-resources -o wide NAME SHORTNAMES APIVERSION NAMESPACED KIND VERBS bindings v1 true Binding [create] componentstatuses cs v1 false ComponentStatus [get list] configmaps cm v1 true ConfigMap [create delete deletecollection get list patch update watch] endpoints ep v1 true Endpoints [create delete deletecollection get list patch update watch] events ev v1 true Event [create delete deletecollection get list patch update watch] limitranges limits v1 true LimitRange [create delete deletecollection get list patch update watch] namespaces ns v1 false Namespace [create delete get list patch update watch] ...

  • 只看apps这个group下面的资源kubectl api-resources --api-group apps -o wide:

    [root@kubebuilder 07]# kubectl api-resources --api-group apps -o wide NAME SHORTNAMES APIVERSION NAMESPACED KIND VERBS controllerrevisions apps/v1 true ControllerRevision [create delete deletecollection get list patch update watch] daemonsets ds apps/v1 true DaemonSet [create delete deletecollection get list patch update watch] deployments deploy apps/v1 true Deployment [create delete deletecollection get list patch update watch] replicasets rs apps/v1 true ReplicaSet [create delete deletecollection get list patch update watch] statefulsets sts apps/v1 true StatefulSet [create delete deletecollection get list patch update watch]

  • 查看指定资源的详情kubectl explain configmap:

    [root@kubebuilder 07]# kubectl explain configmap KIND: ConfigMap VERSION: v1

    DESCRIPTION: ConfigMap holds configuration data for pods to consume. FIELDS: apiVersion APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources binaryData <map[string]string> ...

  • 查看所有Group和Version的命令kubectl api-versions

    [root@kubebuilder 07]# kubectl api-versions admissionregistration.k8s.io/v1 admissionregistration.k8s.io/v1beta1 apiextensions.k8s.io/v1 apiextensions.k8s.io/v1beta1 apiregistration.k8s.io/v1 apiregistration.k8s.io/v1beta1 apps/v1 authentication.k8s.io/v1 authentication.k8s.io/v1beta1 authorization.k8s.io/v1 authorization.k8s.io/v1beta1 autoscaling/v1 autoscaling/v2beta1 autoscaling/v2beta2 batch/v1 batch/v1beta1 certificates.k8s.io/v1 certificates.k8s.io/v1beta1 coordination.k8s.io/v1 coordination.k8s.io/v1beta1 discovery.k8s.io/v1beta1 events.k8s.io/v1 events.k8s.io/v1beta1 extensions/v1beta1 flowcontrol.apiserver.k8s.io/v1beta1 networking.k8s.io/v1 networking.k8s.io/v1beta1 node.k8s.io/v1 node.k8s.io/v1beta1 policy/v1beta1 rbac.authorization.k8s.io/v1 rbac.authorization.k8s.io/v1beta1 scheduling.k8s.io/v1 scheduling.k8s.io/v1beta1 storage.k8s.io/v1 storage.k8s.io/v1beta1 v1 webapp.com.bolingcavalry/v1

  • 以上就是个人对Group、Version、Resource的理解和整理,在后面的client-go和kubebuilder学习过程中也会频繁用到这些基础知识,希望能给您带来一些参考;

你不孤单,欣宸原创一路相伴

  1. Java系列
  2. Spring系列
  3. Docker系列
  4. kubernetes系列
  5. 数据库+中间件系列
  6. DevOps系列

欢迎关注公众号:程序员欣宸

微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界…

本文同步分享在 博客“程序员欣宸”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
jackson学习之九:springboot整合(配置文件)
欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog\_demos(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fzq2599%2Fblog_demos)
Wesley13 Wesley13
2年前
jackson学习之二:jackson
欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog\_demos(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fzq2599%2Fblog_demos)
Wesley13 Wesley13
2年前
gRPC学习之四:实战四类服务方法
欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog\_demos(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fzq2599%2Fblog_demos)
Wesley13 Wesley13
2年前
gRPC学习之三:初试GO版gRPC开发
欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog\_demos(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fzq2599%2Fblog_demos)
Stella981 Stella981
2年前
Kurento实战之一:KMS部署和体验
欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog\_demos(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fzq2599%2Fblog_demos)
Stella981 Stella981
2年前
Kurento实战之二:快速部署和体验
欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog\_demos(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fzq2599%2Fblog_demos)
Wesley13 Wesley13
2年前
gRPC学习之五:gRPC
欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog\_demos(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fzq2599%2Fblog_demos)
Stella981 Stella981
2年前
OpenFaaS实战之四:模板操作(template)
欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog\_demos(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fzq2599%2Fblog_demos)
Wesley13 Wesley13
2年前
gRPC学习之二:GO的gRPC开发环境准备
欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog\_demos(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fzq2599%2Fblog_demos)
Stella981 Stella981
2年前
JUnit5学习之四:按条件执行
欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog\_demos(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fzq2599%2Fblog_demos)