Kubernetes Source Code Overview: kubelet

Photo by James Harrison on Unsplash

kubelet

The kubelet is the primary “node agent” that runs on each node in the cluster. It makes sure that containers are running in a Pod. The kubelet works in terms of a PodSpec. A PodSpec is a YAML or JSON object that describes a pod. The kubelet takes a set of PodSpecs and ensures that the containers described in those PodSpecs are running and healthy.

1. Introduction

This article provides an overview of kubelet source code in the kubernetes/cmd/kubelet directory, including parameter parsing and dependencies (in the kubeDeps structure). For the code in kubernetes/pkg/kublete module, a further overview will be provided later.

The code structure of kubelet is as follows:

Code structure of kubelet

The following code snippets are based on the Kubernetes v1.24 version.

The source code is at https://github.com/kubernetes/kubernetes/tree/release-1.24/cmd/kubelet

2. Main Function

The main function creates a NewKubeletCommand and runs the kubelet.

main

3. NewKubeletCommand

NewKubeletCommand creates a *cobra.Command object with default parameters.

NewKubeletCommand

3.1 KubeletServer

KubeletServer encapsulates all of the parameters necessary for starting up a kubelet.

Construct a KubeletServer from kubeletFlags and kubeletConfig:

kubeletServer := &options.KubeletServer{
KubeletFlags: *kubeletFlags,
KubeletConfiguration: *kubeletConfig,
}

3.2 kubeletDeps

KubeletDeps is a bin for things that are necessary for running the Kubelet.

Use kubeletServer to construct the default KubeletDeps:

kubeletDeps, err := UnsecuredDependencies(kubeletServer, utilfeature.DefaultFeatureGate)
if err != nil {
return fmt.Errorf("failed to construct kubelet dependencies: %w", err)
}

4. Run

Run runs the specified KubeletServer with the given Dependencies. This should never exit.

Run

4.1 Initialize kubeDeps.KubeClient

Initialize kubeDeps.KubeClient, and make other two separate clients, kubeDeps.EventClient for events, and kubeDeps.HeartbeatClient for heartbeat.

kubeDeps.KubeClient

4.2 Start healthz server

If the healthzPort is specified, start a healthz server. The healthzPort is the port of the localhost healthz endpoint (set to 0 to disable).

Start healthz server

5. RunKubelet

RunKubelet is responsible for setting up and running a kubelet.

RunKubelet

5.1 startKubelet

startKubelet start the kubelet and the kubelet HTTP server.

startKubelet

5.2 k.Run

go k.Run(podCfg.Updates())

k.Run starts the kubelet reacting to config updates. It runs the code in kubernetes/pkg/kublete module.

A further overview of the code will be provided later.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Able Lv

Able Lv

Cloud Infrastructure Engineer @Airwallex: Kubernetes, DevOps, Terraform, Istio, Go, and Cloud-Native stuff