1、kubnertes邏輯運作環境
如下面的架構圖
2、kubnertes的設計理念—分層架構
3、kubernetes-API設計原則
對于雲計算系統,系統API實際上處于系統設計的統領地位,kubernetes叢集系統每支援一項新功能,引入一項新技術,一定會新引入對應的API對象,支援對該功能的管理操作,了解掌握的API,就好比抓住了kubernetes系統的牛鼻子。kubernetes系統API的設計有以下幾條原則:
- 所有的API應該是聲明式的。正如前文所說,聲明式的操作,相對于指令式操作,對于重複操作的效果是穩定的,這對于容易出現資料丢失或重複的分布式環境來說是很重要的。另外,聲明式操作更容易被使用者使用,可以使系統向使用者隐藏實作的細節,隐藏實作的細節的同時,也就保留了系統未來持續優化的可能性。此外,聲明式的API,同時隐含了所有的API對象都是名詞性質的,例如Service、Volume這些API都是名詞,這些名詞描述了使用者所期望得到的一個目标分布式對象。
- API對象是彼此互補而且可組合的。這裡面實際是鼓勵API對象盡量實作面向對象設計時的要求,即“高内聚,松耦合”,對業務相關的概念有一個合适的分解,提高分解出來的對象的可重用性。事實上,K8s這種分布式系統管理平台,也是一種業務系統,隻不過它的業務就是排程和管理容器服務。
- 高層API以操作意圖為基礎設計。如何能夠設計好API,跟如何能用面向對象的方法設計好應用系統有相通的地方,高層設計一定是從業務出發,而不是過早的從技術實作出發。是以,針對K8s的高層API設計,一定是以K8s的業務為基礎出發,也就是以系統排程管理容器的操作意圖為基礎設計。
- 低層API根據高層API的控制需要設計。設計實作低層API的目的,是為了被高層API使用,考慮減少備援、提高重用性的目的,低層API的設計也要以需求為基礎,要盡量抵抗受技術實作影響的誘惑。
- 盡量避免簡單封裝,不要有在外部API無法顯式知道的内部隐藏的機制。簡單的封裝,實際沒有提供新的功能,反而增加了對所封裝API的依賴性。内部隐藏的機制也是非常不利于系統維護的設計方式,例如PetSet和ReplicaSet,本來就是兩種Pod集合,那麼K8s就用不同API對象來定義它們,而不會說隻用同一個ReplicaSet,内部通過特殊的算法再來區分這個ReplicaSet是有狀态的還是無狀态。
- API操作複雜度與對象數量成正比。這一條主要是從系統性能角度考慮,要保證整個系統随着系統規模的擴大,性能不會迅速變慢到無法使用,那麼最低的限定就是API的操作複雜度不能超過O(N),N是對象的數量,否則系統就不具備水準伸縮性了。
- API對象狀态不能依賴于網絡連接配接狀态。由于衆所周知,在分布式環境下,網絡連接配接斷開是經常發生的事情,是以要保證API對象狀态能應對網絡的不穩定,API對象的狀态就不能依賴于網絡連接配接狀态。
- 盡量避免讓操作機制依賴于全局狀态,因為在分布式系統中要保證全局狀态的同步是非常困難的。
檢視kubernetes API的方法
#查詢token的方法
curl --cacert /etc/kubernetes/ssl/ca.pem -H "Authorization: Bearer TOKEN" https://127.0.0.1:6443
#查詢API接口
root@k8s-master:~# curl --cacert /etc/kubernetes/ssl/ca.pem -H "Authorization: Bearer 上面所得的TOKEN" https://127.0.0.1:6443
{
"paths": [
"/.well-known/openid-configuration",
"/api",
"/api/v1",
"/apis",
"/apis/",
"/apis/admissionregistration.k8s.io",
"/apis/admissionregistration.k8s.io/v1",
"/apis/apiextensions.k8s.io",
"/apis/apiextensions.k8s.io/v1",
"/apis/apiregistration.k8s.io",
"/apis/apiregistration.k8s.io/v1",
"/apis/apps",
"/apis/apps/v1",
"/apis/authentication.k8s.io",
"/apis/authentication.k8s.io/v1",
"/apis/authorization.k8s.io",
"/apis/authorization.k8s.io/v1",
"/apis/autoscaling",
"/apis/autoscaling/v1",
"/apis/autoscaling/v2",
"/apis/autoscaling/v2beta1",
"/apis/autoscaling/v2beta2",
"/apis/batch",
"/apis/batch/v1",
"/apis/batch/v1beta1",
"/apis/certificates.k8s.io",
"/apis/certificates.k8s.io/v1",
"/apis/coordination.k8s.io",
"/apis/coordination.k8s.io/v1",
"/apis/discovery.k8s.io",
"/apis/discovery.k8s.io/v1",
"/apis/discovery.k8s.io/v1beta1",
"/apis/events.k8s.io",
"/apis/events.k8s.io/v1",
"/apis/events.k8s.io/v1beta1",
"/apis/flowcontrol.apiserver.k8s.io",
"/apis/flowcontrol.apiserver.k8s.io/v1beta1",
"/apis/flowcontrol.apiserver.k8s.io/v1beta2",
"/apis/networking.k8s.io",
"/apis/networking.k8s.io/v1",
"/apis/node.k8s.io",
"/apis/node.k8s.io/v1",
"/apis/node.k8s.io/v1beta1",
"/apis/policy",
"/apis/policy/v1",
"/apis/policy/v1beta1",
"/apis/rbac.authorization.k8s.io",
"/apis/rbac.authorization.k8s.io/v1",
"/apis/scheduling.k8s.io",
"/apis/scheduling.k8s.io/v1",
"/apis/storage.k8s.io",
"/apis/storage.k8s.io/v1",
"/apis/storage.k8s.io/v1beta1",
"/apis/velero.io",
"/apis/velero.io/v1",
"/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/priority-and-fairness-config-consumer",
"/healthz/poststarthook/priority-and-fairness-config-producer",
"/healthz/poststarthook/priority-and-fairness-filter",
"/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/priority-and-fairness-config-consumer",
"/livez/poststarthook/priority-and-fairness-config-producer",
"/livez/poststarthook/priority-and-fairness-filter",
"/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",
"/openid/v1/jwks",
"/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/priority-and-fairness-config-consumer",
"/readyz/poststarthook/priority-and-fairness-config-producer",
"/readyz/poststarthook/priority-and-fairness-filter",
"/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"
]
kubernetes中的API大緻分為如下幾類,請看下圖
4、kubernetes内置資源對象簡介
資源類型 | 資源名稱 |
資源對象 | Pod、ReplicaSet、ReplicationController、Deployment、StatefulSet、DaemonSet、Job、CronJob、HorizontalPodAutoscaling、Node、Namespace、Service、Ingress、Label、CustomResourceDefinition |
存儲對象 | Volume、PersistentVolume、PersistentVolumeClaim、Secret、ConfigMap |
政策對象 | SecurityContext、ResourceQuota、LimitRange |
身份對象 | ServiceAccount、Role、ClusterRole |
5、kubernetes 資源對象操作指令
下表整理了一些常用的指令,更多請kubernetes官方文檔查詢
指令集 | 指令 | 用途 |
基礎指令 | create/delete/edit/get/describe/logs/exec/scale | 增删改查 |
explain | 指令說明 | |
配置指令 | Label:給node标記label,實作親pod與node親和性 | 标簽管理 |
apply | 動态配置 | |
叢集管理指令 | cluster-info/top | 叢集狀态 |
cordon:警戒線,标記node不被排程 uncordon:取消警戒标記為cordon的node drain:驅逐node上的pod,用于node下線等場景 taint:給node标記污點,實作反親pod與node反親和性 | node節點管理 | |
api-resources/api-versions/version | api資源 | |
config | 用戶端kube-config配置 |