K8s 開發(fā)概念簡介
1.K8s 開發(fā)概念簡介
一 運行環(huán)境
!!! Info “后期開發(fā)要求環(huán)境”
* 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 “學(xué)習需要具備的技能”
- golang基礎(chǔ)
- k8s相關(guān)知識
- cobra
- client-go
- controller相關(guān)組件及流程
- operator開發(fā)框架kubebuilder
- gin框架
二 kubernetes API 認識
Kubernetes 中最核心的就是 kube-apiserver 組件,其他組件都是和 kube-apiserver 進行通信的,本節(jié)主要就來研究下 Kubernetes API 接口的模型。
在 Kubernetes 集群中,Kubernetes 對象是我們持久化的實體,就是最終存入 etcd 中的數(shù)據(jù),集群中通過這些實體來表示整個集群的狀態(tài)。平時我們都是直接編寫 YAML 資源清單文件,然后通過 kubectl 來提交創(chuàng)建對應(yīng)的資源對象,映射到集群內(nèi)部。
這個就需要去了解下**聲明式 API**的設(shè)計,為了可擴展性,Kubernetes 在不同的 API 路徑(比如/api/v1 或者 /apis/batch)下面支持了多個 API 版本,不同的 API 版本意味著不同級別的穩(wěn)定性和支持:
Alpha 級別,例如 v1alpha1 默認情況下是被禁用的,可以隨時刪除對功能的支持,所以要慎用
Beta 級別,例如 v2beta1 默認情況下是啟用的,表示代碼已經(jīng)經(jīng)過了很好的測試,但是對象的語義可能會在隨后的版本中以不兼容的方式更改
穩(wěn)定級別,比如 v1 表示已經(jīng)是穩(wěn)定版本了,也會出現(xiàn)在后續(xù)的很多版本中。
在 Kubernetes 集群中,一個 API 對象在 Etcd 里的完整資源路徑,是由:Group(API 組)、Version(API 版本)和 Resource(API 資源類型)三個部分組成的。通過這樣的結(jié)構(gòu),整個 Kubernetes 里的所有 API 對象,實際上就可以用如下的樹形結(jié)構(gòu)表示出來:
GVK = GroupVersionKind,GVR = GroupVersionResource。
apiVersion:這個就是 GV 。
kind:這個就是 K。根據(jù) GVK K8s 就能找到你到底要創(chuàng)建什么類型的資源,根據(jù)你定義的 Spec 創(chuàng)建好資源之后就成為了 Resource,也就是 GVR。GVK/GVR 就是 K8s 資源的坐標,是我們創(chuàng)建/刪除/修改/讀取資源的基礎(chǔ)。
Group即資源組,在kubernetes對資源進行分組時,對應(yīng)的數(shù)據(jù)結(jié)構(gòu)就是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"` }
在kubernetes中有兩種資源組:
有組名資源組,例如deployment
無組名資源組(也叫核心資源組Core Groups),它們都很常見,例如單個pod
deployment有組名,pod沒有組名,咱們把它倆的OpenAPI放在一起對比就一目了然了
Version即版本,這個好理解,kubernetes的版本分為三種:
Alpha:內(nèi)部測試版本,如v1alpha1
Beta:經(jīng)歷了官方和社區(qū)測試的相對穩(wěn)定版,如v1beta1
Stable:正式發(fā)布版,如v1、v2
如下圖紅框,資源組batch之下有v1和v2alpha1兩個版本,每個版本下都有多個資源:
數(shù)據(jù)結(jié)構(gòu)源碼還是在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這些都是資源,下面是關(guān)于資源的一些小結(jié):
在kubernetes環(huán)境被實例化的資源即資源對象(ResourceObject);
資源被分為持久性(Persistent Entity)和非持久性(Ephemeral Entity),持久性如deployment,創(chuàng)建后會在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描述文件,用于創(chuàng)建3個nginx pod,對著紅框和文字就了解每個部分的作用了:
上圖并沒有status,該部分是用來反應(yīng)當前資源對象狀態(tài)的,體現(xiàn)在資源的數(shù)據(jù)結(jié)構(gòu)中,如下所示:
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是個常用的數(shù)據(jù)結(jié)構(gòu)了,可以用來描述資源,例如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,可見當前環(huán)境的所有資源,及其相關(guān)屬性:
[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
查看所有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
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應(yīng)法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。