Kubernetes核心原理(二)之Controller Manager
版權聲明:本文為博主原創文章,未經博主允許不得轉載。如需轉載請聯係本人,並標明出處和作者。
本文CSDN博客地址:https://blog.csdn.net/huwh_/article/details/75675761
1. Controller Manager簡介
Controller Manager作為集群內部的管理控製中心,負責集群內的Node、Pod副本、服務端點(Endpoint)、命名空間(Namespace)、服務賬號(ServiceAccount)、資源定額(ResourceQuota)的管理,當某個Node意外宕機時,Controller Manager會及時發現並執行自動化修複流程,確保集群始終處於預期的工作狀態。
每個Controller通過API Server提供的接口實時監控整個集群的每個資源對象的當前狀態,當發生各種故障導致係統狀態發生變化時,會嚐試將係統狀態修複到“期望狀態”。
2. Replication Controller
為了區分,資源對象Replication Controller簡稱RC,而本文是指Controller Manager中的Replication Controller,稱為副本控製器。副本控製器的作用即保證集群中一個RC所關聯的Pod副本數始終保持預設值。
- 隻有當Pod的重啟策略是Always的時候(RestartPolicy=Always),副本控製器才會管理該Pod的操作(創建、銷毀、重啟等)。
- RC中的Pod模板就像一個模具,模具製造出來的東西一旦離開模具,它們之間就再沒關係了。一旦Pod被創建,無論模板如何變化,也不會影響到已經創建的Pod。
- Pod可以通過修改label來脫離RC的管控,該方法可以用於將Pod從集群中遷移,數據修複等調試。
- 刪除一個RC不會影響它所創建的Pod,如果要刪除Pod需要將RC的副本數屬性設置為0。
- 不要越過RC創建Pod,因為RC可以實現自動化控製Pod,提高容災能力。
2.1. Replication Controller的職責
- 確保集群中有且僅有N個Pod實例,N是RC中定義的Pod副本數量。
- 通過調整RC中的spec.replicas屬性值來實現係統擴容或縮容。
- 通過改變RC中的Pod模板來實現係統的滾動升級。
2.2. Replication Controller使用場景
使用場景
|
說明
|
使用命令
|
---|---|---|
重新調度 | 當發生節點故障或Pod被意外終止運行時,可以重新調度保證集群中仍然運行指定的副本數。 | |
彈性伸縮 | 通過手動或自動擴容代理修複副本控製器的spec.replicas屬性,可以實現彈性伸縮。 | kubectl scale |
滾動更新 | 創建一個新的RC文件,通過kubectl 命令或API執行,則會新增一個新的副本同時刪除舊的副本,當舊副本為0時,刪除舊的RC。 | kubectl rolling-update |
滾動升級,具體可參考kubectl rolling-update --help,官方文檔:https://kubernetes.io/docs/tasks/run-application/rolling-update-replication-controller/
[root@node5 ~] # kubectl rolling-update --help
Perform a rolling update of the given ReplicationController. Replaces the specified replication controller with a new replication controller by updating one pod at a time to use the
new PodTemplate. The new-controller.json must specify the same namespace as the existing replication controller and overwrite at least one (common) label in its replicaSelector.
Usage: kubectl rolling-update OLD_CONTROLLER_NAME ([NEW_CONTROLLER_NAME] --image=NEW_CONTAINER_IMAGE | -f NEW_CONTROLLER_SPEC) [flags]
Aliases: rolling-update, rollingupdate
Examples: # Update pods of frontend-v1 using new replication controller data in frontend-v2.json. $ kubectl rolling-update frontend-v1 -f frontend-v2.json # Update pods of frontend-v1 using JSON data passed into stdin. $ cat frontend-v2.json | kubectl rolling-update frontend-v1 -f -
# Update the pods of frontend-v1 to frontend-v2 by just changing the image, and switching the # name of the replication controller. $ kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2 # Update the pods of frontend by just changing the image, and keeping the old name $ kubectl rolling-update frontend --image=image:v2 |
3. Node Controller
kubelet在啟動時會通過API Server注冊自身的節點信息,並定時向API Server匯報狀態信息,API Server接收到信息後將信息更新到etcd中。
Node Controller通過API Server實時獲取Node的相關信息,實現管理和監控集群中的各個Node節點的相關控製功能。流程如下
1、Controller Manager在啟動時如果設置了--cluster-cidr參數,那麼為每個沒有設置Spec.PodCIDR的Node節點生成一個CIDR地址,並用該CIDR地址設置節點的Spec.PodCIDR屬性,防止不同的節點的CIDR地址發生衝突。
2、具體流程見以上流程圖。
3、逐個讀取節點信息,如果節點狀態變成非“就緒”狀態,則將節點加入待刪除隊列,否則將節點從該隊列刪除。
4. ResourceQuota Controller
資源配額管理確保指定的資源對象在任何時候都不會超量占用係統物理資源。
支持三個層次的資源配置管理:
1)容器級別:對CPU和Memory進行限製
2)Pod級別:對一個Pod內所有容器的可用資源進行限製
3)Namespace級別:包括
- Pod數量
- Replication Controller數量
- Service數量
- ResourceQuota數量
- Secret數量
- 可持有的PV(Persistent Volume)數量
說明:
- k8s配額管理是通過Admission Control(準入控製)來控製的;
- Admission Control提供兩種配額約束方式:LimitRanger和ResourceQuota;
- LimitRanger作用於Pod和Container;
- ResourceQuota作用於Namespace上,限定一個Namespace裏的各類資源的使用總額。
ResourceQuota Controller流程圖:
5. Namespace Controller
用戶通過API Server可以創建新的Namespace並保存在etcd中,Namespace Controller定時通過API Server讀取這些Namespace信息。
如果Namespace被API標記為優雅刪除(即設置刪除期限,DeletionTimestamp),則將該Namespace狀態設置為“Terminating”,並保存到etcd中。同時Namespace Controller刪除該Namespace下的ServiceAccount、RC、Pod等資源對象。
6. Endpoint Controller
Service、Endpoint、Pod的關係:
Endpoints表示了一個Service對應的所有Pod副本的訪問地址,而Endpoints Controller負責生成和維護所有Endpoints對象的控製器。它負責監聽Service和對應的Pod副本的變化。
- 如果監測到Service被刪除,則刪除和該Service同名的Endpoints對象;
- 如果監測到新的Service被創建或修改,則根據該Service信息獲得相關的Pod列表,然後創建或更新Service對應的Endpoints對象。
- 如果監測到Pod的事件,則更新它對應的Service的Endpoints對象。
kube-proxy進程獲取每個Service的Endpoints,實現Service的負載均衡功能。
7. Service Controller
Service Controller是屬於kubernetes集群與外部的雲平台之間的一個接口控製器。Service Controller監聽Service變化,如果是一個LoadBalancer類型的Service,則確保外部的雲平台上對該Service對應的LoadBalancer實例被相應地創建、刪除及更新路由轉發表。
參考《Kubernetes權威指南》
最後更新:2017-08-13 22:53:12