K8s 开发概念简介

网友投稿 813 2022-05-30

1.K8s 开发概念简介

一 运行环境

!!! Info “后期开发要求环境”

* IDE:goland * go版本:go1.16.8 * kubernetes:v1.21.1

官方源码仓库: https://github.com/kubernetes/kubernetes.git

$ git clone https://github.com/kubernetes/kubernetes.git

!!! info “学习需要具备的技能”

- golang基础

- k8s相关知识

- cobra

- client-go

- controller相关组件及流程

- operator开发框架kubebuilder

- gin框架

二 kubernetes API 认识

Kubernetes 中最核心的就是 kube-apiserver 组件,其他组件都是和 kube-apiserver 进行通信的,本节主要就来研究下 Kubernetes API 接口的模型。

在 Kubernetes 集群中,Kubernetes 对象是我们持久化的实体,就是最终存入 etcd 中的数据,集群中通过这些实体来表示整个集群的状态。平时我们都是直接编写 YAML 资源清单文件,然后通过 kubectl 来提交创建对应的资源对象,映射到集群内部。

这个就需要去了解下**声明式 API**的设计,为了可扩展性,Kubernetes 在不同的 API 路径(比如/api/v1 或者 /apis/batch)下面支持了多个 API 版本,不同的 API 版本意味着不同级别的稳定性和支持:

Alpha 级别,例如 v1alpha1 默认情况下是被禁用的,可以随时删除对功能的支持,所以要慎用

Beta 级别,例如 v2beta1 默认情况下是启用的,表示代码已经经过了很好的测试,但是对象的语义可能会在随后的版本中以不兼容的方式更改

稳定级别,比如 v1 表示已经是稳定版本了,也会出现在后续的很多版本中。

在 Kubernetes 集群中,一个 API 对象在 Etcd 里的完整资源路径,是由:Group(API 组)、Version(API 版本)和 Resource(API 资源类型)三个部分组成的。通过这样的结构,整个 Kubernetes 里的所有 API 对象,实际上就可以用如下的树形结构表示出来:

GVK = GroupVersionKind,GVR = GroupVersionResource。

apiVersion:这个就是 GV 。

kind:这个就是 K。根据 GVK K8s 就能找到你到底要创建什么类型的资源,根据你定义的 Spec 创建好资源之后就成为了 Resource,也就是 GVR。GVK/GVR 就是 K8s 资源的坐标,是我们创建/删除/修改/读取资源的基础。

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"` }

K8s 开发概念简介

在kubernetes中有两种资源组:

有组名资源组,例如deployment

无组名资源组(也叫核心资源组Core Groups),它们都很常见,例如单个pod

deployment有组名,pod没有组名,咱们把它俩的OpenAPI放在一起对比就一目了然了

Version即版本,这个好理解,kubernetes的版本分为三种:

Alpha:内部测试版本,如v1alpha1

Beta:经历了官方和社区测试的相对稳定版,如v1beta1

Stable:正式发布版,如v1、v2

如下图红框,资源组batch之下有v1和v2alpha1两个版本,每个版本下都有多个资源:

数据结构源码还是在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资源在kubernetes中的重要性是不言而喻的,常见的pod、service、deployment这些都是资源,下面是关于资源的一些小结:

在kubernetes环境被实例化的资源即资源对象(ResourceObject);

资源被分为持久性(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,对着红框和文字就了解每个部分的作用了:

上图并没有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"` }

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"}}, }, }, }

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

[root@k8s]# 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@k8s]# 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@k8s]# 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 ...

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

[root@k8s]# 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

API Kubernetes

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:怎么在excel表格中制作一份燃尽图表
下一篇:【云驻共创】【极简开发•慧享未来】低代码平台关键能力之流程编排
相关文章