參考:《kubernetes權(quán)威指南》
1. API Server簡介
k8s API Server提供了k8s各類資源對象(pod,RC,Service等)的增刪改查及watch等HTTP Rest接口,是整個(gè)系統(tǒng)的數(shù)據(jù)總線和數(shù)據(jù)中心。
kubernetes API Server的功能:
- 提供了集群管理的REST API接口(包括認(rèn)證授權(quán)、數(shù)據(jù)校驗(yàn)以及集群狀態(tài)變更);
- 提供其他模塊之間的數(shù)據(jù)交互和通信的樞紐(其他模塊通過API Server查詢或修改數(shù)據(jù),只有API Server才直接操作etcd);
- 是資源配額控制的入口;
- 擁有完備的集群安全機(jī)制.
kube-apiserver工作原理圖
2. 如何訪問kubernetes API
k8s通過kube-apiserver這個(gè)進(jìn)程提供服務(wù),該進(jìn)程運(yùn)行在單個(gè)k8s-master節(jié)點(diǎn)上。默認(rèn)有兩個(gè)端口。
2.1. 本地端口
- 該端口用于接收HTTP請求;
- 該端口默認(rèn)值為8080,可以通過API Server的啟動參數(shù)“–insecure-port”的值來修改默認(rèn)值;
- 默認(rèn)的IP地址為“l(fā)ocalhost”,可以通過啟動參數(shù)“–insecure-bind-address”的值來修改該IP地址;
- 非認(rèn)證或授權(quán)的HTTP請求通過該端口訪問API Server。
2.2. 安全端口
- 該端口默認(rèn)值為6443,可通過啟動參數(shù)“–secure-port”的值來修改默認(rèn)值;
- 默認(rèn)IP地址為非本地(Non-Localhost)網(wǎng)絡(luò)端口,通過啟動參數(shù)“–bind-address”設(shè)置該值;
- 該端口用于接收HTTPS請求;
- 用于基于Tocken文件或客戶端證書及HTTP Base的認(rèn)證;
- 用于基于策略的授權(quán);
- 默認(rèn)不啟動HTTPS安全訪問控制。
2.3. 訪問方式
Kubernetes REST API可參考https://kubernetes.io/docs/api-reference/v1.6/
2.3.1. curl
curl localhost:8080/api
curl localhost:8080/api/v1/pods
curl localhost:8080/api/v1/services
curl localhost:8080/api/v1/replicationcontrollers
2.3.2. Kubectl Proxy
Kubectl Proxy代理程序既能作為API Server的反向代理,也能作為普通客戶端訪問API Server的代理。通過master節(jié)點(diǎn)的8080端口來啟動該代理程序。
kubectl proxy --port=8080 &
具體見kubectl proxy --help
[root@node5 ~]# kubectl proxy --help
To proxy all of the kubernetes api and nothing else, use:
kubectl proxy --api-prefix=/
To proxy only part of the kubernetes api and also some static files:
kubectl proxy --www=/my/files --www-prefix=/static/ --api-prefix=/api/
The above lets you 'curl localhost:8001/api/v1/pods'.
To proxy the entire kubernetes api at a different root, use:
kubectl proxy --api-prefix=/custom/
The above lets you 'curl localhost:8001/custom/api/v1/pods'
Usage:
kubectl proxy [--port=PORT] [--www=static-dir] [--www-prefix=prefix] [--api-prefix=prefix] [flags]
Examples:
# Run a proxy to kubernetes apiserver on port 8011, serving static content from ./local/www/
$ kubectl proxy --port=8011 --www=./local/www/
# Run a proxy to kubernetes apiserver on an arbitrary local port.
# The chosen port for the server will be output to stdout.
$ kubectl proxy --port=0
# Run a proxy to kubernetes apiserver, changing the api prefix to k8s-api
# This makes e.g. the pods api available at localhost:8011/k8s-api/v1/pods/
$ kubectl proxy --api-prefix=/k8s-api
Flags:
--accept-hosts="^localhost$,^127//.0//.0//.1$,^//[::1//]$": Regular expression for hosts that the proxy should accept.
--accept-paths="^/.*": Regular expression for paths that the proxy should accept.
--api-prefix="/": Prefix to serve the proxied API under.
--disable-filter[=false]: If true, disable request filtering in the proxy. This is dangerous, and can leave you vulnerable to XSRF attacks, when used with an accessible port.
-p, --port=8001: The port on which to run the proxy. Set to 0 to pick a random port.
--reject-methods="POST,PUT,PATCH": Regular expression for HTTP methods that the proxy should reject.
--reject-paths="^/api/.*/exec,^/api/.*/run": Regular expression for paths that the proxy should reject.
-u, --unix-socket="": Unix socket on which to run the proxy.
-w, --www="": Also serve static files from the given directory under the specified prefix.
-P, --www-prefix="/static/": Prefix to serve static files under, if static file directory is specified.
Global Flags:
--alsologtostderr[=false]: log to standard error as well as files
--api-version="": The API version to use when talking to the server
--certificate-authority="": Path to a cert. file for the certificate authority.
--client-certificate="": Path to a client key file for TLS.
--client-key="": Path to a client key file for TLS.
--cluster="": The name of the kubeconfig cluster to use
--context="": The name of the kubeconfig context to use
--insecure-skip-tls-verify[=false]: If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure.
--kubeconfig="": Path to the kubeconfig file to use for CLI requests.
--log-backtrace-at=:0: when logging hits line file:N, emit a stack trace
--log-dir="": If non-empty, write log files in this directory
--log-flush-frequency=5s: Maximum number of seconds between log flushes
--logtostderr[=true]: log to standard error instead of files
--match-server-version[=false]: Require server version to match client version
--namespace="": If present, the namespace scope for this CLI request.
--password="": Password for basic authentication to the API server.
-s, --server="": The address and port of the Kubernetes API server
--stderrthreshold=2: logs at or above this threshold go to stderr
--token="": Bearer token for authentication to the API server.
--user="": The name of the kubeconfig user to use
--username="": Username for basic authentication to the API server.
--v=0: log level for V logs
--vmodule=: comma-separated list of pattern=N settings for file-filtered logging
2.3.3. kubectl客戶端
命令行工具kubectl客戶端,通過命令行參數(shù)轉(zhuǎn)換為對API Server的REST API調(diào)用,并將調(diào)用結(jié)果輸出。
命令格式:kubectl [command] [options]
具體可參考k8s常用命令
2.3.4. 編程方式調(diào)用
使用場景:
1、運(yùn)行在Pod里的用戶進(jìn)程調(diào)用kubernetes API,通常用來實(shí)現(xiàn)分布式集群搭建的目標(biāo)。
2、開發(fā)基于kubernetes的管理平臺,比如調(diào)用kubernetes API來完成Pod、Service、RC等資源對象的圖形化創(chuàng)建和管理界面。可以使用kubernetes提供的Client Library。
具體可參考https://github.com/kubernetes/client-go。
3. 通過API Server訪問Node、Pod和Service
k8s API Server最主要的REST接口是資源對象的增刪改查,另外還有一類特殊的REST接口—k8s Proxy API接口,這類接口的作用是代理REST請求,即kubernetes API Server把收到的REST請求轉(zhuǎn)發(fā)到某個(gè)Node上的kubelet守護(hù)進(jìn)程的REST端口上,由該kubelet進(jìn)程負(fù)責(zé)響應(yīng)。
3.1. Node相關(guān)接口
關(guān)于Node相關(guān)的接口的REST路徑為:/api/v1/proxy/nodes/{name},其中{name}為節(jié)點(diǎn)的名稱或IP地址。
/api/v1/proxy/nodes/{name}/pods/ #列出指定節(jié)點(diǎn)內(nèi)所有Pod的信息
/api/v1/proxy/nodes/{name}/stats/ #列出指定節(jié)點(diǎn)內(nèi)物理資源的統(tǒng)計(jì)信息
/api/v1/prxoy/nodes/{name}/spec/ #列出指定節(jié)點(diǎn)的概要信息
這里獲取的Pod信息來自Node而非etcd數(shù)據(jù)庫,兩者時(shí)間點(diǎn)可能存在偏差。如果在kubelet進(jìn)程啟動時(shí)加–enable-debugging-handles=true參數(shù),那么kubernetes Proxy API還會增加以下接口:
/api/v1/proxy/nodes/{name}/run #在節(jié)點(diǎn)上運(yùn)行某個(gè)容器
/api/v1/proxy/nodes/{name}/exec #在節(jié)點(diǎn)上的某個(gè)容器中運(yùn)行某條命令
/api/v1/proxy/nodes/{name}/attach #在節(jié)點(diǎn)上attach某個(gè)容器
/api/v1/proxy/nodes/{name}/portForward #實(shí)現(xiàn)節(jié)點(diǎn)上的Pod端口轉(zhuǎn)發(fā)
/api/v1/proxy/nodes/{name}/logs #列出節(jié)點(diǎn)的各類日志信息
/api/v1/proxy/nodes/{name}/metrics #列出和該節(jié)點(diǎn)相關(guān)的Metrics信息
/api/v1/proxy/nodes/{name}/runningpods #列出節(jié)點(diǎn)內(nèi)運(yùn)行中的Pod信息
/api/v1/proxy/nodes/{name}/debug/pprof #列出節(jié)點(diǎn)內(nèi)當(dāng)前web服務(wù)的狀態(tài),包括CPU和內(nèi)存的使用情況
3.2. Pod相關(guān)接口
/api/v1/proxy/namespaces/{namespace}/pods/{name}/{path:*} #訪問pod的某個(gè)服務(wù)接口
/api/v1/proxy/namespaces/{namespace}/pods/{name} #訪問Pod
#以下寫法不同,功能一樣
/api/v1/namespaces/{namespace}/pods/{name}/proxy/{path:*} #訪問pod的某個(gè)服務(wù)接口
/api/v1/namespaces/{namespace}/pods/{name}/proxy #訪問Pod
3.3. Service相關(guān)接口
/api/v1/proxy/namespaces/{namespace}/services/{name}
Pod的proxy接口的作用:在kubernetes集群之外訪問某個(gè)pod容器的服務(wù)(HTTP服務(wù)),可以用Proxy API實(shí)現(xiàn),這種場景多用于管理目的,比如逐一排查Service的Pod副本,檢查哪些Pod的服務(wù)存在異常問題。
4. 集群功能模塊之間的通信
kubernetes API Server作為集群的核心,負(fù)責(zé)集群各功能模塊之間的通信,集群內(nèi)各個(gè)功能模塊通過API Server將信息存入etcd,當(dāng)需要獲取和操作這些數(shù)據(jù)時(shí),通過API Server提供的REST接口(GET/LIST/WATCH方法)來實(shí)現(xiàn),從而實(shí)現(xiàn)各模塊之間的信息交互。
4.1. kubelet與API Server交互
每個(gè)Node節(jié)點(diǎn)上的kubelet定期就會調(diào)用API Server的REST接口報(bào)告自身狀態(tài),API Server接收這些信息后,將節(jié)點(diǎn)狀態(tài)信息更新到etcd中。kubelet也通過API Server的Watch接口監(jiān)聽Pod信息,從而對Node機(jī)器上的POD進(jìn)行管理。
監(jiān)聽信息 | kubelet動作 | 備注 |
---|---|---|
新的POD副本被調(diào)度綁定到本節(jié)點(diǎn) | 執(zhí)行POD對應(yīng)的容器的創(chuàng)建和啟動邏輯 | - |
POD對象被刪除 | 刪除本節(jié)點(diǎn)上相應(yīng)的POD容器 | - |
修改POD信息 | 修改本節(jié)點(diǎn)的POD容器 | - |
4.2. kube-controller-manager與API Server交互
kube-controller-manager中的Node Controller模塊通過API Server提供的Watch接口,實(shí)時(shí)監(jiān)控Node的信息,并做相應(yīng)處理。
4.3. kube-scheduler與API Server交互
Scheduler通過API Server的Watch接口監(jiān)聽到新建Pod副本的信息后,它會檢索所有符合該P(yáng)od要求的Node列表,開始執(zhí)行Pod調(diào)度邏輯。調(diào)度成功后將Pod綁定到目標(biāo)節(jié)點(diǎn)上。
4.4. 特別說明
為了緩解各模塊對API Server的訪問壓力,各功能模塊都采用緩存機(jī)制來緩存數(shù)據(jù),各功能模塊定時(shí)從API Server獲取指定的資源對象信息(LIST/WATCH方法),然后將信息保存到本地緩存,功能模塊在某些情況下不直接訪問API Server,而是通過訪問緩存數(shù)據(jù)來間接訪問API Server。