1. 程式人生 > 實用技巧 >k8s中Controller-Manager和Scheduler的選主邏輯

k8s中Controller-Manager和Scheduler的選主邏輯

K8s中的control-plane包括了apiserver、controller-manager、scheduler、etcd,當搭建高可用叢集時就會涉及到部分節點的選主問題。etcd是整個叢集所有狀態資訊的儲存,涉及資料的讀寫和多個etcd之間資料的同步,對資料的一致性要求嚴格,所以使用較複雜的raft演算法來選擇用於提交資料的主節點。而apiserver作為叢集入口,本身是無狀態的web伺服器,多個apiserver服務之間直接負載請求並不需要做選主。Controller-Manager和Scheduler作為任務型別的元件,比如controller-manager內建的k8s各種資源物件的控制器實時的watch apiserver獲取物件最新的變化事件做期望狀態和實際狀態調整,顯然多個controller-manager同時工作是完全沒有必要的,所以controller-manager和scheduler也是需要選主的,但是選主邏輯和etcd不一樣的,這裡只需要保證從多個controller-manager和scheduler之間選出一個進入工作狀態即可,而無需考慮它們之間的資料一致和同步。

/ # kube-controller-manager -h 2>&1 | grep -i leader
      --leader-elect                                                      Start a leader election client and gain leadership before executing the main loop. Enable this when running replicated components for high availability. (default true)
      --leader-elect-lease-duration duration                              The duration that non-leader candidates will wait after observing a leadership renewal until attempting to acquire leadership of a led but unrenewed leader slot. This is effectively the maximum duration that a leader can be stopped before it is replaced by another candidate. This is only applicable if leader election is enabled. (default 15s)
      --leader-elect-renew-deadline duration                              The interval between attempts by the acting master to renew a leadership slot before it stops leading. This must be less than or equal to the lease duration. This is only applicable if leader election is enabled. (default 10s)
      --leader-elect-resource-lock endpoints                              The type of resource object that is used for locking during leader election. Supported options are endpoints (default) and `configmaps`. (default "endpoints")
      --leader-elect-retry-period duration                                The duration the clients should wait between attempting acquisition and renewal of a leadership. This is only applicable if leader election is enabled. (default 2s)