Kubernetes的Group、Version、Resource学习小记

2022-05-09 09:29:58 浏览数 (1)

欢迎访问我的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源码剖析》一文,将三者关系做了简洁清晰的介绍:

环境信息

  • 开始学习前先交待一下我这边的环境信息,以避免因为版本引起的误会:
  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有自己的名称和版本:
代码语言:javascript复制
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"`
}
  1. 在kubernetes中有两种资源组:有组名资源组和无组名资源组(也叫核心资源组Core Groups),它们都很常见;
  2. deployment有组名,pod没有组名,咱们把它俩的OpenAPI放在一起对比就一目了然了:

Version

  • Version即版本,这个好理解,kubernetes的版本分为三种:
  1. Alpha:内部测试版本,如v1alpha1
  2. Beta:经历了官方和社区测试的相对稳定版,如v1beta1
  3. Stable:正式发布版,如v1、v2
  • 如下图红框,资源组batch之下有v1和v2alpha1两个版本,每个版本下都有多个资源:
  • 数据结构源码还是在types.go文件中,如下:
代码语言:javascript复制
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;
  • 资源的源码:
代码语言:javascript复制
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,对着红框和文字就了解每个部分的作用了:
  • 上图并没有status,该部分是用来反应当前资源对象状态的,体现在资源的数据结构中,如下所示:
代码语言:javascript复制
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"`
}

官方文档速查

  • 在实际学习和开发中,对指定资源做增删改查操作时,官方文档是我们最可靠的依赖,地址:https://kubernetes.io/docs/reference/kubernetes-api/
  • 打开deployment的文档,如下图:
  • 另外还有API文档也是必不可少的,遗憾的是目前没有找到1.20的API在线文档,最新的是1.19版本,地址:https://v1-19.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/
  • 下图是deployment的api接口文档,可见示例、path、请求响应参数都有详细的说明,对咱们的学习和开发提供了强有力的支持:

APIResources数据结构

  • APIResource是个常用的数据结构了,可以用来描述资源,例如resource_quota_controller_test.go中有对其的使用:
代码语言:javascript复制
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,可见当前环境的所有资源,及其相关属性:
代码语言:javascript复制
[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:
代码语言:javascript复制
[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:
代码语言:javascript复制
[root@kubebuilder 07]# kubectl explain configmap
KIND:     ConfigMap
VERSION:  v1

DESCRIPTION:
     ConfigMap holds configuration data for pods to consume.

FIELDS:
   apiVersion	<string>
     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
代码语言:javascript复制
[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学习过程中也会频繁用到这些基础知识,希望能给您带来一些参考;

0 人点赞