一、K8S的api概览:
在Kubernetes集群中,一个API对象在Etcd里完整资源路径,是由:Group(API组)、Version(API版本)和Resource(API资源类型)三个部分组成的。通过这样的结构,整个
Kubernetes里的所有API对象,实际上就可以用如下的树形结构表示出来
我们也可以用命令来查看API组织形式
~ kubectl get --raw / { "paths": [ "/api", "/api/v1", "/apis", "/apis/", "/apis/admissionregistration.k8s.io", "/apis/admissionregistration.k8s.io/v1", "/apis/admissionregistration.k8s.io/v1beta1", "/apis/apiextensions.k8s.io", "/apis/apiextensions.k8s.io/v1", "/apis/apiextensions.k8s.io/v1beta1", "/apis/apiregistration.k8s.io", "/apis/apiregistration.k8s.io/v1", "/apis/apiregistration.k8s.io/v1beta1", "/apis/apps", "/apis/apps/v1", "/apis/authentication.k8s.io", "/apis/authentication.k8s.io/v1", "/apis/authentication.k8s.io/v1beta1", "/apis/authorization.k8s.io", "/apis/authorization.k8s.io/v1", "/apis/authorization.k8s.io/v1beta1", "/apis/autoscaling", "/apis/autoscaling/v1", "/apis/autoscaling/v2beta1", "/apis/autoscaling/v2beta2", "/apis/batch", "/apis/batch/v1", "/apis/batch/v1beta1", "/apis/certificates.k8s.io", "/apis/certificates.k8s.io/v1", "/apis/certificates.k8s.io/v1beta1", "/apis/coordination.k8s.io", "/apis/coordination.k8s.io/v1", "/apis/coordination.k8s.io/v1beta1", "/apis/crd.projectcalico.org", "/apis/crd.projectcalico.org/v1", "/apis/discovery.k8s.io", "/apis/discovery.k8s.io/v1beta1", "/apis/events.k8s.io", "/apis/events.k8s.io/v1", "/apis/events.k8s.io/v1beta1", "/apis/extensions", "/apis/extensions/v1beta1", "/apis/networking.k8s.io", "/apis/networking.k8s.io/v1", "/apis/networking.k8s.io/v1beta1", "/apis/node.k8s.io", "/apis/node.k8s.io/v1beta1", "/apis/policy", "/apis/policy/v1beta1", "/apis/rbac.authorization.k8s.io", "/apis/rbac.authorization.k8s.io/v1", "/apis/rbac.authorization.k8s.io/v1beta1", "/apis/scheduling.k8s.io", "/apis/scheduling.k8s.io/v1", "/apis/scheduling.k8s.io/v1beta1", "/apis/storage.k8s.io", "/apis/storage.k8s.io/v1", "/apis/storage.k8s.io/v1beta1", "/healthz", "/healthz/autoregister-completion", "/healthz/etcd", "/healthz/log", "/healthz/ping", "/healthz/poststarthook/aggregator-reload-proxy-client-cert", "/healthz/poststarthook/apiservice-openapi-controller", "/healthz/poststarthook/apiservice-registration-controller", "/healthz/poststarthook/apiservice-status-available-controller", "/healthz/poststarthook/bootstrap-controller", "/healthz/poststarthook/crd-informer-synced", "/healthz/poststarthook/generic-apiserver-start-informers", "/healthz/poststarthook/kube-apiserver-autoregistration", "/healthz/poststarthook/rbac/bootstrap-roles", "/healthz/poststarthook/scheduling/bootstrap-system-priority-classes", "/healthz/poststarthook/start-apiextensions-controllers", "/healthz/poststarthook/start-apiextensions-informers", "/healthz/poststarthook/start-cluster-authentication-info-controller", "/healthz/poststarthook/start-kube-aggregator-informers", "/healthz/poststarthook/start-kube-apiserver-admission-initializer", "/livez", "/livez/autoregister-completion", "/livez/etcd", "/livez/log", "/livez/ping", "/livez/poststarthook/aggregator-reload-proxy-client-cert", "/livez/poststarthook/apiservice-openapi-controller", "/livez/poststarthook/apiservice-registration-controller", "/livez/poststarthook/apiservice-status-available-controller", "/livez/poststarthook/bootstrap-controller", "/livez/poststarthook/crd-informer-synced", "/livez/poststarthook/generic-apiserver-start-informers", "/livez/poststarthook/kube-apiserver-autoregistration", "/livez/poststarthook/rbac/bootstrap-roles", "/livez/poststarthook/scheduling/bootstrap-system-priority-classes", "/livez/poststarthook/start-apiextensions-controllers", "/livez/poststarthook/start-apiextensions-informers", "/livez/poststarthook/start-cluster-authentication-info-controller", "/livez/poststarthook/start-kube-aggregator-informers", "/livez/poststarthook/start-kube-apiserver-admission-initializer", "/logs", "/metrics", "/openapi/v2", "/readyz", "/readyz/autoregister-completion", "/readyz/etcd", "/readyz/informer-sync", "/readyz/log", "/readyz/ping", "/readyz/poststarthook/aggregator-reload-proxy-client-cert", "/readyz/poststarthook/apiservice-openapi-controller", "/readyz/poststarthook/apiservice-registration-controller", "/readyz/poststarthook/apiservice-status-available-controller", "/readyz/poststarthook/bootstrap-controller", "/readyz/poststarthook/crd-informer-synced", "/readyz/poststarthook/generic-apiserver-start-informers", "/readyz/poststarthook/kube-apiserver-autoregistration", "/readyz/poststarthook/rbac/bootstrap-roles", "/readyz/poststarthook/scheduling/bootstrap-system-priority-classes", "/readyz/poststarthook/start-apiextensions-controllers", "/readyz/poststarthook/start-apiextensions-informers", "/readyz/poststarthook/start-cluster-authentication-info-controller", "/readyz/poststarthook/start-kube-aggregator-informers", "/readyz/poststarthook/start-kube-apiserver-admission-initializer", "/readyz/shutdown", "/version" ] }%
如:查看批处理操作
[root@master1 ~]# kubectl get --raw /apis/batch/v1 | python -m json.tool { "apiVersion": "v1", "groupVersion": "batch/v1", "kind": "APIResourceList", "resources": [ { "categories": [ "all" ], "kind": "CronJob", "name": "cronjobs", "namespaced": true, "shortNames": [ "cj" ], "singularName": "", "storageVersionHash": "h/JlFAZkyyY=", "verbs": [ "create", "delete", "deletecollection", "get", "list", "patch", "update", "watch" ] }, { "kind": "CronJob", "name": "cronjobs/status", "namespaced": true, "singularName": "", "verbs": [ "get", "patch", "update" ] }, { "categories": [ "all" ], "kind": "Job", "name": "jobs", "namespaced": true, "singularName": "", "storageVersionHash": "mudhfqk/qZY=", "verbs": [ "create", "delete", "deletecollection", "get", "list", "patch",
二、YAML书写
通常,kubernetes API支持函通过标准HTTP POST、PUT、DELETE和GET在指定PATH路径上创建、更新 、删除和检索操作,并使用JSON作为默认的数据交互格式。
比如现在我们要创建一个Job对象,那么我们的YAML文件的声明就需要怎么写
apiVersion: batch/v1 kind: Job metadata: name: demo namespace: default
其中Job就是这个API对象的资源类型Kind,而资源类型Resource通常为Kind的小写复数词,比如这里就是jobs,batch就是它的组(Group),v1就是它的版本(Version),APIGroup、Version和资源就唯一定义了一个HTTP路径,
然后kube-apiserver端对这个URL进行了监听,然后把对应的请求传递给了对应的控制器进行处理而已
Resources和Kind的区别是什么?需要注意的是Resources指的是HTTP Restful API请求路径中的资源(理解Restful API的资源),而kind对应的是系统中真正的实体,这两个是有本质区别的。
每个kind都存在于一个Group和Version中,并通过GroupVersionKind(GVK)来标识,GVR和GVK是相关联的,GVK通过GVR标识的HTTP路径来提供服务,将GVK映射到GVR的过程就叫做Rest mapping
三、api请求流程
1、HTTP请求先由DefaultBuildHandlerChain()注册的一系列过滤器处理,这个函数位于k8s.io/apiserver/pkg/server/config.go文件中,它对请求进行一系列过滤操作,经过验证的就返回相应的HTTP返回码
2、接下来根据请求的路径,通过handler路由到各种处理程序中k8s.io/apiserver/pkg/server/handler.go
3、每个API Group都注册了一个handler,详情参见k8s.io/apiserver/pkg/endpoints/groupversion.go和k8s.io/apiserver/pkg/endpoints/installer.go它接受HTTP请求和上下文,并从e处链接及本声明。
原文链接:https://blog.csdn.net/qq_21127151/article/details/124854845