k8s API Server提供了k8s各类资源对象(pod,RC,Service等)的增删改查及watch等HTTP Rest接口,是整个系统的数据总线和数据中心。功能特性:
1.是集群管理的API入口
2.是资源配额控制的入口
3.提供了完善的集群安全机制

二:概述
通常可以通过命令行工具Kubectl来与API Server交互,它们之间的接口是REST调用,同时也可以使用curl命令行工具进行快速验证。通过配置参数KUBE_MASTER="--master=http://10.116.137.196:8080" 客户获得API Server的访问路径。
1.获取 API的版本信息
curl 10.116.137.196:8080/api
点击(此处)折叠或打开
-
{
-
"kind": "APIVersions",
-
"versions": [
-
"v1"
-
],
-
"serverAddressByClientCIDRs": [
-
{
-
"clientCIDR": "0.0.0.0/0",
-
"serverAddress": "10.116.137.196:6443"
-
}
-
]
- }
curl 10.116.137.196:8080/api/v1
-
{
-
"kind": "APIResourceList",
-
"groupVersion": "v1",
-
"resources": [
-
{
-
"name": "bindings",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Binding",
-
"verbs": [
-
"create"
-
]
-
},
-
{
-
"name": "componentstatuses",
-
"singularName": "",
-
"namespaced": false,
-
"kind": "ComponentStatus",
-
"verbs": [
-
"get",
-
"list"
-
],
-
"shortNames": [
-
"cs"
-
]
-
},
-
{
-
"name": "configmaps",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "ConfigMap",
-
"verbs": [
-
"create",
-
"delete",
-
"deletecollection",
-
"get",
-
"list",
-
"patch",
-
"update",
-
"watch"
-
],
-
"shortNames": [
-
"cm"
-
]
-
},
-
{
-
"name": "endpoints",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Endpoints",
-
"verbs": [
-
"create",
-
"delete",
-
"deletecollection",
-
"get",
-
"list",
-
"patch",
-
"update",
-
"watch"
-
],
-
"shortNames": [
-
"ep"
-
]
-
},
-
{
-
"name": "events",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Event",
-
"verbs": [
-
"create",
-
"delete",
-
"deletecollection",
-
"get",
-
"list",
-
"patch",
-
"update",
-
"watch"
-
],
-
"shortNames": [
-
"ev"
-
]
-
},
-
{
-
"name": "limitranges",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "LimitRange",
-
"verbs": [
-
"create",
-
"delete",
-
"deletecollection",
-
"get",
-
"list",
-
"patch",
-
"update",
-
"watch"
-
],
-
"shortNames": [
-
"limits"
-
]
-
},
-
{
-
"name": "namespaces",
-
"singularName": "",
-
"namespaced": false,
-
"kind": "Namespace",
-
"verbs": [
-
"create",
-
"delete",
-
"get",
-
"list",
-
"patch",
-
"update",
-
"watch"
-
],
-
"shortNames": [
-
"ns"
-
]
-
},
-
{
-
"name": "namespaces/finalize",
-
"singularName": "",
-
"namespaced": false,
-
"kind": "Namespace",
-
"verbs": [
-
"update"
-
]
-
},
-
{
-
"name": "namespaces/status",
-
"singularName": "",
-
"namespaced": false,
-
"kind": "Namespace",
-
"verbs": [
-
"get",
-
"patch",
-
"update"
-
]
-
},
-
{
-
"name": "nodes",
-
"singularName": "",
-
"namespaced": false,
-
"kind": "Node",
-
"verbs": [
-
"create",
-
"delete",
-
"deletecollection",
-
"get",
-
"list",
-
"patch",
-
"proxy",
-
"update",
-
"watch"
-
],
-
"shortNames": [
-
"no"
-
]
-
},
-
{
-
"name": "nodes/proxy",
-
"singularName": "",
-
"namespaced": false,
-
"kind": "Node",
-
"verbs": []
-
},
-
{
-
"name": "nodes/status",
-
"singularName": "",
-
"namespaced": false,
-
"kind": "Node",
-
"verbs": [
-
"get",
-
"patch",
-
"update"
-
]
-
},
-
{
-
"name": "persistentvolumeclaims",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "PersistentVolumeClaim",
-
"verbs": [
-
"create",
-
"delete",
-
"deletecollection",
-
"get",
-
"list",
-
"patch",
-
"update",
-
"watch"
-
],
-
"shortNames": [
-
"pvc"
-
]
-
},
-
{
-
"name": "persistentvolumeclaims/status",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "PersistentVolumeClaim",
-
"verbs": [
-
"get",
-
"patch",
-
"update"
-
]
-
},
-
{
-
"name": "persistentvolumes",
-
"singularName": "",
-
"namespaced": false,
-
"kind": "PersistentVolume",
-
"verbs": [
-
"create",
-
"delete",
-
"deletecollection",
-
"get",
-
"list",
-
"patch",
-
"update",
-
"watch"
-
],
-
"shortNames": [
-
"pv"
-
]
-
},
-
{
-
"name": "persistentvolumes/status",
-
"singularName": "",
-
"namespaced": false,
-
"kind": "PersistentVolume",
-
"verbs": [
-
"get",
-
"patch",
-
"update"
-
]
-
},
-
{
-
"name": "pods",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Pod",
-
"verbs": [
-
"create",
-
"delete",
-
"deletecollection",
-
"get",
-
"list",
-
"patch",
-
"proxy",
-
"update",
-
"watch"
-
],
-
"shortNames": [
-
"po"
-
],
-
"categories": [
-
"all"
-
]
-
},
-
{
-
"name": "pods/attach",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Pod",
-
"verbs": []
-
},
-
{
-
"name": "pods/binding",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Binding",
-
"verbs": [
-
"create"
-
]
-
},
-
{
-
"name": "pods/eviction",
-
"singularName": "",
-
"namespaced": true,
-
"group": "policy",
-
"version": "v1beta1",
-
"kind": "Eviction",
-
"verbs": [
-
"create"
-
]
-
},
-
{
-
"name": "pods/exec",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Pod",
-
"verbs": []
-
},
-
{
-
"name": "pods/log",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Pod",
-
"verbs": [
-
"get"
-
]
-
},
-
{
-
"name": "pods/portforward",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Pod",
-
"verbs": []
-
},
-
{
-
"name": "pods/proxy",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Pod",
-
"verbs": []
-
},
-
{
-
"name": "pods/status",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Pod",
-
"verbs": [
-
"get",
-
"patch",
-
"update"
-
]
-
},
-
{
-
"name": "podtemplates",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "PodTemplate",
-
"verbs": [
-
"create",
-
"delete",
-
"deletecollection",
-
"get",
-
"list",
-
"patch",
-
"update",
-
"watch"
-
]
-
},
-
{
-
"name": "replicationcontrollers",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "ReplicationController",
-
"verbs": [
-
"create",
-
"delete",
-
"deletecollection",
-
"get",
-
"list",
-
"patch",
-
"update",
-
"watch"
-
],
-
"shortNames": [
-
"rc"
-
],
-
"categories": [
-
"all"
-
]
-
},
-
{
-
"name": "replicationcontrollers/scale",
-
"singularName": "",
-
"namespaced": true,
-
"group": "autoscaling",
-
"version": "v1",
-
"kind": "Scale",
-
"verbs": [
-
"get",
-
"patch",
-
"update"
-
]
-
},
-
{
-
"name": "replicationcontrollers/status",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "ReplicationController",
-
"verbs": [
-
"get",
-
"patch",
-
"update"
-
]
-
},
-
{
-
"name": "resourcequotas",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "ResourceQuota",
-
"verbs": [
-
"create",
-
"delete",
-
"deletecollection",
-
"get",
-
"list",
-
"patch",
-
"update",
-
"watch"
-
],
-
"shortNames": [
-
"quota"
-
]
-
},
-
{
-
"name": "resourcequotas/status",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "ResourceQuota",
-
"verbs": [
-
"get",
-
"patch",
-
"update"
-
]
-
},
-
{
-
"name": "secrets",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Secret",
-
"verbs": [
-
"create",
-
"delete",
-
"deletecollection",
-
"get",
-
"list",
-
"patch",
-
"update",
-
"watch"
-
]
-
},
-
{
-
"name": "serviceaccounts",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "ServiceAccount",
-
"verbs": [
-
"create",
-
"delete",
-
"deletecollection",
-
"get",
-
"list",
-
"patch",
-
"update",
-
"watch"
-
],
-
"shortNames": [
-
"sa"
-
]
-
},
-
{
-
"name": "services",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Service",
-
"verbs": [
-
"create",
-
"delete",
-
"get",
-
"list",
-
"patch",
-
"proxy",
-
"update",
-
"watch"
-
],
-
"shortNames": [
-
"svc"
-
],
-
"categories": [
-
"all"
-
]
-
},
-
{
-
"name": "services/proxy",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Service",
-
"verbs": []
-
},
-
{
-
"name": "services/status",
-
"singularName": "",
-
"namespaced": true,
-
"kind": "Service",
-
"verbs": [
-
"get",
-
"patch",
-
"update"
-
]
-
}
-
]
- }
kubectl proxy --port=8001
三:Kubernetes Proxy API接口
API Server最主要的REST接口是资源对象的增删改查,另外还有一类特殊的REST接口--Kubernetes Proxy API接口,这类接口的作用是代理REST请求,即kubernetes API Server把收到的REST请求转发到某个Node上的kubelet守护进程的REST端口上,由该kubelet进程负责响应。
1.Node 相关的接口
点击(此处)折叠或打开
-
/api/v1/proxy/nodes/{name}/pods/ #列出指定节点内所有Pod的信息
-
-
/api/v1/proxy/nodes/{name}/stats/ #列出指定节点内物理资源的统计信息
-
- /api/v1/prxoy/nodes/{name}/spec/ #列出指定节点的概要信息
2.Pod 相关的接口
点击(此处)折叠或打开
-
/api/v1/proxy/namespaces/{namespace}/pods/{name}/{path:*} #访问pod的某个服务接口
-
-
/api/v1/proxy/namespaces/{namespace}/pods/{name} #访问Pod
-
-
#以下写法不同,功能一样
-
-
/api/v1/namespaces/{namespace}/pods/{name}/proxy/{path:*} #访问pod的某个服务接口
-
- /api/v1/namespaces/{namespace}/pods/{name}/proxy #访问Pod
3.Service 相关的接口
点击(此处)折叠或打开
- /api/v1/proxy/namespaces/{namespace}/services/{name}
四:集群功能模块之间的通信
kubernetes API Server作为集群的核心,负责集群各功能模块之间的通信,集群内各个功能模块通过API Server将信息存入etcd,当需要获取和操作这些数据时,通过API Server提供的REST接口(GET\LIST\WATCH方法)来实现,从而实现各模块之间的信息交互。

1. kubelet与API SERVER交互
每个Node节点上的kubelet定期就会调用API Server的REST接口报告自身状态,API Server接收这些信息后,将节点状态信息更新到etcd中。kubelet也通过API Server的Watch接口监听Pod信息,从而对Node机器上的POD进行管理。
2. kube-controller-manager与API SERVER交互
kube-controller-manager中的Node Controller模块通过API Server提供的Watch接口,实时监控Node的信息,并做相应处理。
3.kube-scheduler与API SERVER交互
Scheduler通过API Server的Watch接口监听到新建Pod副本的信息后,它会检索所有符合该Pod要求的Node列表,开始执行Pod调度逻辑。调度成功后将Pod绑定到目标节点上。
五: 说明
为了缓解各模块对API Server的访问压力,各功能模块都采用缓存机制来缓存数据,各功能模块定时从API Server获取指定的资源对象信息(LIST/WATCH方法),然后将信息保存到本地缓存,功能模块在某些情况下不直接访问API Server,而是通过访问缓存数据来间接访问API Server。