Kubernetes核心原理(一)之API Server


1. API Server簡介

k8s API Server提供了k8s各類資源對象(pod,RC,Service等)的增刪改查及watch等HTTP Rest接口,是整個系統的數據總線和數據中心。

kubernetes API Server的功能:

  1. 提供了集群管理的REST API接口(包括認證授權、數據校驗以及集群狀態變更);
  2. 提供其他模塊之間的數據交互和通信的樞紐(其他模塊通過API Server查詢或修改數據,只有API Server才直接操作etcd);
  3. 是資源配額控制的入口;
  4. 擁有完備的集群安全機制.

kube-apiserver工作原理圖

kube-apiserver

2. 如何訪問kubernetes API

k8s通過kube-apiserver這個進程提供服務,該進程運行在單個k8s-master節點上。默認有兩個端口。

2.1. 本地端口

  1. 該端口用於接收HTTP請求;
  2. 該端口默認值為8080,可以通過API Server的啟動參數“–insecure-port”的值來修改默認值;
  3. 默認的IP地址為“localhost”,可以通過啟動參數“–insecure-bind-address”的值來修改該IP地址;
  4. 非認證或授權的HTTP請求通過該端口訪問API Server。

2.2. 安全端口

  1. 該端口默認值為6443,可通過啟動參數“–secure-port”的值來修改默認值;
  2. 默認IP地址為非本地(Non-Localhost)網絡端口,通過啟動參數“–bind-address”設置該值;
  3. 該端口用於接收HTTPS請求;
  4. 用於基於Tocken文件或客戶端證書及HTTP Base的認證;
  5. 用於基於策略的授權;
  6. 默認不啟動HTTPS安全訪問控制。

2.3. 訪問方式

Kubernetes REST API可參考https://kubernetes.io/docs/api-reference/v1.6/

2.3.1. curl

1
2
3
4
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節點的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客戶端,通過命令行參數轉換為對API Server的REST API調用,並將調用結果輸出。

命令格式:kubectl [command] [options]

具體可參考k8s常用命令

2.3.4. 編程方式調用

使用場景:

1、運行在Pod里的用戶進程調用kubernetes API,通常用來實現分布式集群搭建的目標。

2、開發基於kubernetes的管理平台,比如調用kubernetes API來完成Pod、Service、RC等資源對象的圖形化創建和管理界面。可以使用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請求轉發到某個Node上的kubelet守護進程的REST端口上,由該kubelet進程負責響應。

3.1. Node相關接口

關於Node相關的接口的REST路徑為:/api/v1/proxy/nodes/{name},其中{name}為節點的名稱或IP地址。

1
2
3
/api/v1/proxy/nodes/{name}/pods/    #列出指定節點內所有Pod的信息
/api/v1/proxy/nodes/{name}/stats/ #列出指定節點內物理資源的統計信息
/api/v1/prxoy/nodes/{name}/spec/ #列出指定節點的概要信息

這里獲取的Pod信息來自Node而非etcd數據庫,兩者時間點可能存在偏差。如果在kubelet進程啟動時加–enable-debugging-handles=true參數,那么kubernetes Proxy API還會增加以下接口:

1
2
3
4
5
6
7
8
/api/v1/proxy/nodes/{name}/run      #在節點上運行某個容器
/api/v1/proxy/nodes/{name}/exec #在節點上的某個容器中運行某條命令
/api/v1/proxy/nodes/{name}/attach #在節點上attach某個容器
/api/v1/proxy/nodes/{name}/portForward #實現節點上的Pod端口轉發
/api/v1/proxy/nodes/{name}/logs #列出節點的各類日志信息
/api/v1/proxy/nodes/{name}/metrics #列出和該節點相關的Metrics信息
/api/v1/proxy/nodes/{name}/runningpods #列出節點內運行中的Pod信息
/api/v1/proxy/nodes/{name}/debug/pprof #列出節點內當前web服務的狀態,包括CPU和內存的使用情況

3.2. Pod相關接口

1
2
3
4
5
/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.3. Service相關接口

1
/api/v1/proxy/namespaces/{namespace}/services/{name}

Pod的proxy接口的作用:在kubernetes集群之外訪問某個pod容器的服務(HTTP服務),可以用Proxy API實現,這種場景多用於管理目的,比如逐一排查Service的Pod副本,檢查哪些Pod的服務存在異常問題。

4. 集群功能模塊之間的通信

kubernetes API Server作為集群的核心,負責集群各功能模塊之間的通信,集群內各個功能模塊通過API Server將信息存入etcd,當需要獲取和操作這些數據時,通過API Server提供的REST接口(GET/LIST/WATCH方法)來實現,從而實現各模塊之間的信息交互。

4.1. kubelet與API Server交互

每個Node節點上的kubelet定期就會調用API Server的REST接口報告自身狀態,API Server接收這些信息后,將節點狀態信息更新到etcd中。kubelet也通過API Server的Watch接口監聽Pod信息,從而對Node機器上的POD進行管理。

監聽信息 kubelet動作 備注
新的POD副本被調度綁定到本節點 執行POD對應的容器的創建和啟動邏輯 -
POD對象被刪除 刪除本節點上相應的POD容器 -
修改POD信息 修改本節點的POD容器 -

4.2. kube-controller-manager與API Server交互

kube-controller-manager中的Node Controller模塊通過API Server提供的Watch接口,實時監控Node的信息,並做相應處理。

4.3. kube-scheduler與API Server交互

Scheduler通過API Server的Watch接口監聽到新建Pod副本的信息后,它會檢索所有符合該Pod要求的Node列表,開始執行Pod調度邏輯。調度成功后將Pod綁定到目標節點上。

4.4. 特別說明

為了緩解各模塊對API Server的訪問壓力,各功能模塊都采用緩存機制來緩存數據,各功能模塊定時從API Server獲取指定的資源對象信息(LIST/WATCH方法),然后將信息保存到本地緩存,功能模塊在某些情況下不直接訪問API Server,而是通過訪問緩存數據來間接訪問API Server。

參考《kubernetes權威指南》

https://www.huweihuang.com/article/kubernetes/core-principle/kubernetes-core-principle-api-server/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM