K8s) kube-controller-manager

kube-controller-manager

API SeverInformerKCMControllerController ContextInformer FactoryindexercontrollerwatchqpushsharedProcessorhandlerqeventkeysync()listerSummery거의 모든 리소스 종류(Kind)마다 존재능동적인 상태 관리(Reconciliation)가필요한 리소스에만 존재

Creates (initializes) and runs controllers

CreateControllerContextBuildControllersInformerFactory.Start()RunControllers순서informer 생성 및 등록 (to factory)informer factory 생성핸들러 등록 (informer 구독)informer 실행- watch AP 서버Controller 실행- 리소스 상태 sync

Core Initialization (run function)

  • CreateControllerContext
    • Creates SharedInformerFactory and Client shared by all controllers
  • BuildControllers
    • Creates and registers informers
    • Controllers register handlers (subscribe) to informers
  • InformerFactory.Start
    • Starts the InformerFactory to begin cache synchronization
  • RunControllers
    • Iterates through each controller's loop

Controller

deployment.DeploymentControllerworkqueue.TypedRateLimitingInterface[string]queueappslisters.DeploymentListerdListerclientclientset.InterfaceInformerIndexer (cache)deploymentListerlisters.ResourceIndexer(Accessor)AddEventHandler(Subscribe)enqueueInformerhandlerdeployment.DeploymentControllerqueueenqueuedListerRun()indexergetprocessNextWorkItem()syncDeployment()e.g.addDeploymentController 동작

An infinite loop that aligns the desired state with the current state

Exists only for resources that require active state management (Reconciliation)

Role

  • Orchestrates multiple controllers (existing per resource)
  • Manages shared dependencies (Informers, Clients)
  • Handles High Availability (Leader Election)
Leader Election

Three kube-controller-manager processes (instances) are launched, one on each of 3 Master Nodes.

  • Only one of these is elected as the leader (election criteria: first come, first served) and creates/runs all controllers.
  • The leader renews a document called Lease every 2 seconds (for periodic liveness checks).
  • The others act as standby, taking over when the leader fails.
    • Since the original source of truth for all data is stored only in etcd, no handover is necessary.

Operation

Only the key is put into the queue, and a Worker retrieves it from the queue to execute business logic (sync).

  • Registers EventHandler to the informer
    • The EventHandler executes the logic to put the key into the queue.
  • Uses a Rate Limiting Queue
    • workqueue.TypedRateLimitingInterface[string]
    • Retries become slower as the number of retries increases.
  • Key format
    • "Namespace/Name"
  • Data is always looked up just before processing.
    • Data is looked up through the lister from the informer's local cache.

Informer

SharedInformerFactoryInformerAPI ServerListerWatcherWatchIndexersharedProcessorCachingControllerNotifyControllerupdate(Reflector)리소스 종류마다 하나씩 존재map[reflect.Type]cache.SharedIndexInformerdeploymentInformerLister()Informer()InformerIndexer (cache)InformerInformerInformer...InformerGET ?watch=trueAPI Server(Streaming)Transfer-Encoding: chunkedwatch.Eventqueueindexereventtypeobject infoprocessLoop()handlerHandleDeltas()PopprocessDeltas()ControllerupdatecallInformerRun()SharedProcessorProcessorListenerdistributeaddChnextChrunringbufferlisteners 목록 관리버퍼링(Queueing) & 핸들러 함수 실행Informerrequest(Watch)responseInformerFactory.Start()Informer 동작watch()API Serverconnectionresponse body+ DeltaTyped.reader.Read(d.buf[base:])framer.NewFrameReader(resp.Body)JSON 문법상 중괄호 { 와 } 의 짝이 맞을 때까지스트림을 읽어서 하나의 완벽한 JSON 객체가완성되는 시점을 데이터의 끝으로 인식streaming.NewDecoder

"Instead of directly querying the DB, let's subscribe to changes and keep a copy in our local memory (Cache)."

Exists for almost every resource type (Kind)

Role

  • API (Kubernetes API Server) Watch
    • Creates a connection with a single request and continuously receives the response body.
  • Cache Construction
    • Local Cache
      • cache.cacheStorage: treadSafeMap
        • lock (sync.RWMutex): Concurrency control
        • items (map[string]any): Actual data storage
        • index (storeIndex): Auxiliary index for fast searching
  • Notifies subscribed controllers of events
sharedInformerFactory

Manages informers shared by controllers in a map,
ensuring that only one informer (watch, cache) exists for the same resource.

Reference


Post
Category
Series